Skip to content

Commit 66663b8

Browse files
added an initial grafana and prometheus setup
1 parent 39eaa69 commit 66663b8

File tree

4 files changed

+72
-1
lines changed

4 files changed

+72
-1
lines changed

docker-compose.yml

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,41 @@ services:
33
web:
44
build: .
55
ports:
6-
- "8550:8550"
6+
- "8550:8550"
7+
networks:
8+
- monitoring_network
9+
10+
grafana:
11+
image: grafana/grafana:latest
12+
ports:
13+
- "3000:3000"
14+
volumes:
15+
- grafana-storage:/var/lib/grafana
16+
environment:
17+
- GF_SECURITY_ADMIN_USER=${GF_ADMIN_USER:-admin}
18+
- GF_SECURITY_ADMIN_PASSWORD=${GF_ADMIN_PASSWORD:-admin}
19+
networks:
20+
- monitoring_network
21+
22+
prometheus:
23+
image: prom/prometheus:latest
24+
volumes:
25+
- ./prometheus.yml:/etc/prometheus/prometheus.yml
26+
- prometheus_data:/prometheus
27+
command:
28+
- '--config.file=/etc/prometheus/prometheus.yml'
29+
- '--storage.tsdb.path=/prometheus'
30+
- '--web.console.libraries=/usr/share/prometheus/console_libraries'
31+
- '--web.console.templates=/usr/share/prometheus/consoles'
32+
ports:
33+
- "9090:9090"
34+
networks:
35+
- monitoring_network
36+
37+
volumes:
38+
grafana-storage:
39+
prometheus_data:
40+
41+
networks:
42+
monitoring_network:
43+
driver: bridge

prometheus.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
global:
2+
scrape_interval: 15s
3+
4+
scrape_configs:
5+
- job_name: 'python_app'
6+
static_configs:
7+
- targets: ['web:8550']

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ dash[ci,dev,testing]>=2.0
1616
dash-mantine-components>=0.12.1
1717
pyyaml>=5.0
1818
pytest<8.1.0
19+
prometheus-client
1920
wheel
2021
dash>=2.9.0
2122
gunicorn>=19.9.0

run.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
import json
44
from flask import jsonify
55
from components.appshell import create_appshell
6+
from prometheus_client import make_wsgi_app
7+
from werkzeug.middleware.dispatcher import DispatcherMiddleware
8+
from prometheus_client import Counter, Histogram
9+
import time
10+
from flask import request
611

712
_dash_renderer._set_react_version("18.2.0")
813

@@ -22,6 +27,10 @@
2227
"https://unpkg.com/hotkeys-js/dist/hotkeys.min.js",
2328
]
2429

30+
# Define metrics
31+
REQUEST_COUNT = Counter('request_count', 'App Request Count', ['app_name', 'method', 'endpoint'])
32+
REQUEST_LATENCY = Histogram('request_latency_seconds', 'Request latency', ['app_name', 'endpoint'])
33+
2534
app = Dash(
2635
__name__,
2736
suppress_callback_exceptions=True,
@@ -32,6 +41,21 @@
3241
prevent_initial_callbacks=True,
3342
index_string=open('templates/index.html').read() # Add this line
3443
)
44+
app.server.wsgi_app = DispatcherMiddleware(app.server.wsgi_app, {
45+
'/metrics': make_wsgi_app()
46+
})
47+
48+
# Use metrics in your routes
49+
@app.server.before_request
50+
def before_request():
51+
REQUEST_COUNT.labels(app_name=f'{__name__}', method=request.method, endpoint=request.endpoint).inc()
52+
request.start_time = time.time()
53+
54+
@app.server.after_request
55+
def after_request(response):
56+
request_latency = time.time() - request.start_time
57+
REQUEST_LATENCY.labels(app_name=f'{__name__}', endpoint=request.endpoint).observe(request_latency)
58+
return response
3559

3660
# Load data from JSON files
3761
data = []
@@ -53,5 +77,7 @@ def zoom_in(date):
5377
# Return the data corresponding to the date
5478
return jsonify(data_dict[date])
5579

80+
81+
5682
if __name__ == "__main__":
5783
app.run_server(debug=False, host='0.0.0.0', port='8552')

0 commit comments

Comments
 (0)