Help with ideas how to implement sending all records that coredns receives as a metric in prometheus, I had an idea through a script to first query and then send to pushgateway but such an implementation limits me to record labels for each unit that I receive from the DNS list. I also had an idea to send unique records like AAAA or A from logs coredns, but no idea how to implement this mechanism.
I tried to request all records through a script and then send them to pushgateway, but ran into a problem with labels limitation.
def resolve_dns_name(ip):
try:
dns_name = socket.gethostbyaddr(ip)[0]
logging.info(f"DNS имя для IP {ip}: {dns_name}")
return dns_name
except socket.herror:
logging.warning(f"Не удалось разрешить DNS имя для IP {ip}")
return "Неизвестно"
def get_instance_id(svc):
return "some_unique_instance_id"
def push_metric_directly(base_metric_name, labels, value, dns_name, namespace, metric_id):
metric_name = f"{base_metric_name}_by_dns"
labels['dns_name'] = dns_name
labels['namespace'] = namespace
labels['metric_id'] = metric_id
labels_str = ", ".join([f'{k}="{v}"' for k, v in labels.items()])
data = f'{metric_name}{{{labels_str}}} {value}\n'
response = requests.post(PUSHGATEWAY_URL, data=data, headers={"Content-Type": "text/plain"})
if response.ok:
logging.info(f"Метрика {metric_name} с labels {labels_str} успешно отправлена в Pushgateway.")
else:
logging.error(f"Не удалось отправить метрику {metric_name} с labels {labels_str} в Pushgateway: {response.status_code} {response.reason}")
if __name__ == '__main__':
config.load_incluster_config()
v1 = client.CoreV1Api()
logging.info("Начинаем запрос списка сервисов...")
services = v1.list_service_for_all_namespaces()
for svc in services.items:
ip = svc.spec.cluster_ip
namespace = svc.metadata.namespace
if ip and ip != "None":
dns_name = resolve_dns_name(ip)
instance_id = get_instance_id(svc)
metric_id = f"{namespace}_{svc.metadata.name}_{int(time.time())}"
labels = {
'instance_id': instance_id,
'svc_ip': ip,
'svc_name': svc.metadata.name,
}
push_metric_directly("k8s_service_dns_info_collector", labels, 1, dns_name, namespace, metric_id)
logging.info("Обработка сервисов завершена.")