Karan6124 commited on
Commit
ad6baed
·
1 Parent(s): 6eaeadc

feat: use a python helper script to inject Grafana key into prometheus.yml on startup

Browse files
Files changed (3) hide show
  1. Dockerfile +3 -0
  2. inject_key.py +22 -0
  3. supervisord.conf +1 -1
Dockerfile CHANGED
@@ -33,6 +33,9 @@ RUN pip install --no-cache-dir --upgrade pip && \
33
  # Copy prometheus configuration file
34
  COPY prometheus.yml /code/prometheus.yml
35
 
 
 
 
36
  # Copy supervisord config
37
  COPY supervisord.conf /etc/supervisor/supervisord.conf
38
 
 
33
  # Copy prometheus configuration file
34
  COPY prometheus.yml /code/prometheus.yml
35
 
36
+ # Copy environment variable injection script
37
+ COPY inject_key.py /code/inject_key.py
38
+
39
  # Copy supervisord config
40
  COPY supervisord.conf /etc/supervisor/supervisord.conf
41
 
inject_key.py ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+
3
+ prom_path = "/code/prometheus.yml"
4
+ grafana_key = os.environ.get("GRAFANA_API_KEY")
5
+
6
+ if os.path.exists(prom_path) and grafana_key:
7
+ try:
8
+ with open(prom_path, "r") as f:
9
+ content = f.read()
10
+
11
+ # Replace the placeholder with the actual key
12
+ if "${GRAFANA_API_KEY}" in content:
13
+ new_content = content.replace("${GRAFANA_API_KEY}", grafana_key)
14
+ with open(prom_path, "w") as f:
15
+ f.write(new_content)
16
+ print("Successfully injected GRAFANA_API_KEY into prometheus.yml")
17
+ else:
18
+ print("Placeholder ${GRAFANA_API_KEY} not found in prometheus.yml")
19
+ except Exception as e:
20
+ print(f"Error modifying prometheus.yml: {e}")
21
+ else:
22
+ print("Prometheus config file or GRAFANA_API_KEY missing!")
supervisord.conf CHANGED
@@ -22,7 +22,7 @@ stderr_logfile=/dev/stderr
22
  stderr_logfile_maxbytes=0
23
 
24
  [program:prometheus]
25
- command=bash -c "sed -i \"s|\\\$\${GRAFANA_API_KEY}|\$\$GRAFANA_API_KEY|g\" /code/prometheus.yml && prometheus --config.file=/code/prometheus.yml --storage.tsdb.path=/code/prometheus_data"
26
  autostart=true
27
  autorestart=true
28
  stdout_logfile=/dev/stdout
 
22
  stderr_logfile_maxbytes=0
23
 
24
  [program:prometheus]
25
+ command=bash -c "python /code/inject_key.py && prometheus --config.file=/code/prometheus.yml --storage.tsdb.path=/code/prometheus_data"
26
  autostart=true
27
  autorestart=true
28
  stdout_logfile=/dev/stdout