55 lines
1.4 KiB
Python
55 lines
1.4 KiB
Python
from ansible.parsing.yaml.objects import AnsibleUnicode
|
|
|
|
class FilterModule:
|
|
def filters(self):
|
|
return {
|
|
"prometheus__convert_jobs": convert_jobs,
|
|
"interp": interp,
|
|
}
|
|
|
|
|
|
def interp(string):
|
|
return AnsibleUnicode(f"{{{{ {string} }}}}")
|
|
|
|
|
|
def convert_jobs(config):
|
|
|
|
for name, job in config.items():
|
|
|
|
config = {
|
|
"job_name": name,
|
|
"static_configs": [
|
|
{
|
|
"targets": job["targets"],
|
|
}
|
|
],
|
|
"params": job.get("params", {}),
|
|
}
|
|
|
|
if "path" in job:
|
|
config["metrics_path"] = job["path"]
|
|
|
|
if "address" in job:
|
|
|
|
try:
|
|
replacement = f"$1:{job['address']['port']}"
|
|
except Exception:
|
|
replacement = job["address"]
|
|
|
|
config["relabel_configs"] = [
|
|
{
|
|
"source_labels": ["__address__"],
|
|
"target_label": "__param_target",
|
|
},
|
|
{
|
|
"source_labels": ["__param_target"],
|
|
"target_label": "instance",
|
|
},
|
|
{
|
|
"source_labels": ["__param_target"],
|
|
"target_label": "__address__",
|
|
"replacement": replacement,
|
|
},
|
|
]
|
|
|
|
yield config
|