Feature 10961 Addition of external targets to OSM Prometheus
This commit allows the automatic regeneration of a Prometheus config
file from a sidecar by combining the information in PROMETHEUS_BASE_CONFIG_FILE
and the prometheus_jobs in mongo.
Change-Id: I0246d60ec32733a088ca8590a6445735549bf4de
Signed-off-by: garciadeblas <gerardo.garciadeblas@telefonica.com>
diff --git a/docker/Prometheus/Dockerfile b/docker/Prometheus/Dockerfile
index 7c84207..228f597 100644
--- a/docker/Prometheus/Dockerfile
+++ b/docker/Prometheus/Dockerfile
@@ -21,6 +21,7 @@
ENV PROMETHEUS_URL http://prometheus:9090
ENV MONGODB_URL mongodb://mongo:27017
ENV PROMETHEUS_CONFIG_FILE /etc/prometheus/prometheus.yml
+ENV PROMETHEUS_BASE_CONFIG_FILE /etc/prometheus_base/prometheus.yml
ENV TARGET_DATABASE osm
WORKDIR /code
@@ -31,4 +32,4 @@
COPY src/app.py .
-CMD [ "python", "-u", "./app.py" ]
\ No newline at end of file
+CMD [ "python", "-u", "./app.py" ]
diff --git a/docker/Prometheus/src/app.py b/docker/Prometheus/src/app.py
index 43cca08..157413d 100755
--- a/docker/Prometheus/src/app.py
+++ b/docker/Prometheus/src/app.py
@@ -37,6 +37,7 @@
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 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 @@
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)