X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=docker%2FPrometheus%2Fsrc%2Fapp.py;fp=docker%2FPrometheus%2Fsrc%2Fapp.py;h=157413dc8aa734aeb0b0993e587af06d58226fe3;hb=09eaa9263cfd3f0eaf3c10d756a781ee241cb5e6;hp=43cca0854fdb68420937aa1e145922ced3d9090d;hpb=d2772ad7d595c59fc9d06e83cbc11b8172fc3495;p=osm%2Fdevops.git diff --git a/docker/Prometheus/src/app.py b/docker/Prometheus/src/app.py index 43cca085..157413dc 100755 --- a/docker/Prometheus/src/app.py +++ b/docker/Prometheus/src/app.py @@ -37,6 +37,7 @@ from bson import ObjectId mongodb_url = os.environ["MONGODB_URL"] target_database = os.environ["TARGET_DATABASE"] prometheus_config_file = os.environ["PROMETHEUS_CONFIG_FILE"] +prometheus_base_config_file = os.environ["PROMETHEUS_BASE_CONFIG_FILE"] prometheus_url = os.environ["PROMETHEUS_URL"] @@ -63,19 +64,12 @@ def clean_up_job(prometheus_job): def generate_prometheus_config(prometheus_jobs, config_file_path): - config_file = open(config_file_path, encoding="utf-8", mode="r") - config_file_contents = config_file.read() - config_file.close() - - config_file_yaml = yaml.load(config_file_contents, yaml.FullLoader) + with open(config_file_path, encoding="utf-8", mode="r") as config_file: + config_file_yaml = yaml.safe_load(config_file) if config_file_yaml is None: config_file_yaml = {} - - if len(prometheus_jobs) == 0: + if "scrape_configs" not in config_file_yaml: config_file_yaml["scrape_configs"] = [] - return config_file_yaml - - config_file_yaml["scrape_configs"] = [] for prometheus_job in prometheus_jobs: cleaned_up_job = clean_up_job(prometheus_job) @@ -129,12 +123,12 @@ async def main_task(client): stored_jobs = get_jobs(client) print(f"Jobs detected : {len(stored_jobs):d}") generated_prometheus_config = generate_prometheus_config( - stored_jobs, prometheus_config_file + stored_jobs, prometheus_base_config_file ) print(f"Writing new config file to {prometheus_config_file}") config_file = open(prometheus_config_file, "w") config_file.truncate(0) - config_file.write(yaml.dump(generated_prometheus_config)) + config_file.write(yaml.safe_dump(generated_prometheus_config)) config_file.close() print("New config written, updating prometheus") update_resp = await reload_prometheus_config(prometheus_url)