Add prometheus metrics to alloy

This commit is contained in:
Tobias Reisinger 2025-05-06 15:14:49 +02:00
parent 616788c5ea
commit 5ad3e9bfe2
Signed by: serguzim
GPG key ID: 13AD60C237A28DFE
8 changed files with 174 additions and 19 deletions

View file

@ -0,0 +1,44 @@
def transfer_optional_param(source, target, name, target_name=None):
if param := source.get(name):
target[target_name or name] = param
class FilterModule(object):
def filters(self):
return {
'services_to_alloy': self.services_to_alloy,
}
def services_to_alloy(self, services):
result = []
for name, service in services.items():
if not bool(service.get("host")):
continue
if targets := service.get("metrics") or []:
job = {
"name": name,
"targets": [],
"scrape_interval": "60s",
}
for target in targets:
address = target.get("address") or service["dns"][0]['domain']
transfer_optional_param(target, job, "interval", "scrape_interval")
new_target = {
"address": address,
"path": target["path"],
"instance": name
}
transfer_optional_param(target, new_target, "instance")
transfer_optional_param(target, new_target, "job")
job["targets"].append(new_target)
result.append(job)
return result