Skip to content

Commit 21639fe

Browse files
committed
Monitoring with Prometheus - Build Grafana Dashboards for Model
1 parent 39d64d5 commit 21639fe

File tree

4 files changed

+646
-22
lines changed

4 files changed

+646
-22
lines changed

packages/ml_api/api/controller.py

+44-1
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,44 @@
44

55
from flask import request, jsonify, Response, current_app
66

7+
from gradient_boosting_model import __version__ as shadow_version
8+
from regression_model import __version__ as live_version
9+
from prometheus_client import Histogram, Gauge, Info
710
from gradient_boosting_model.predict import make_prediction
811
from api.persistence.data_access import PredictionPersistence, ModelType
12+
from api.config import APP_NAME
913

1014

1115
_logger = logging.getLogger(__name__)
1216

17+
PREDICTION_TRACKER = Histogram(
18+
name='house_price_prediction_dollars',
19+
documentation='ML Model Prediction on House Price',
20+
labelnames=['app_name', 'model_name', 'model_version']
21+
)
22+
23+
PREDICTION_GAUGE = Gauge(
24+
name='house_price_gauge_dollars',
25+
documentation='ML Model Prediction on House Price for min max calcs',
26+
labelnames=['app_name', 'model_name', 'model_version']
27+
)
28+
29+
PREDICTION_GAUGE.labels(
30+
app_name=APP_NAME,
31+
model_name=ModelType.LASSO.name,
32+
model_version=live_version)
33+
34+
MODEL_VERSIONS = Info(
35+
'model_version_details',
36+
'Capture model version information',
37+
)
38+
39+
MODEL_VERSIONS.info({
40+
'live_model': ModelType.LASSO.name,
41+
'live_version': live_version,
42+
'shadow_model': ModelType.GRADIENT_BOOSTING.name,
43+
'shadow_version': shadow_version})
44+
1345

1446
def health():
1547
if request.method == "GET":
@@ -47,7 +79,18 @@ def predict():
4779
_logger.warning(f"errors during prediction: {result.errors}")
4880
return Response(json.dumps(result.errors), status=400)
4981

50-
# Step 4: Prepare prediction response
82+
# Step 4: Monitoring
83+
for _prediction in result.predictions:
84+
PREDICTION_TRACKER.labels(
85+
app_name=APP_NAME,
86+
model_name=ModelType.LASSO.name,
87+
model_version=live_version).observe(_prediction)
88+
PREDICTION_GAUGE.labels(
89+
app_name=APP_NAME,
90+
model_name=ModelType.LASSO.name,
91+
model_version=live_version).set(_prediction)
92+
93+
# Step 5: Prepare prediction response
5194
return jsonify(
5295
{
5396
"predictions": result.predictions,

packages/ml_api/docker/config/grafana/grafana_flask_basic_dashboard_ml_api.json

+5-10
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"editable": true,
1616
"gnetId": null,
1717
"graphTooltip": 0,
18-
"id": 2,
18+
"id": 3,
1919
"links": [],
2020
"panels": [
2121
{
@@ -59,7 +59,7 @@
5959
"steppedLine": false,
6060
"targets": [
6161
{
62-
"expr": "rate(http_request_count_total{job=\"ml_api\"}[1m])",
62+
"expr": "rate(http_request_count_total{job=\"ml_api\"}[5m])",
6363
"legendFormat": "{{app_name}} {{method}} {{endpoint}} {{http_status}}",
6464
"refId": "A"
6565
}
@@ -149,11 +149,6 @@
149149
"expr": "sum (rate(http_request_latency_seconds_sum{job=\"ml_api\"}[5m])) / sum (rate(http_request_latency_seconds_count{job=\"ml_api\"}[5m]))",
150150
"legendFormat": "Average (seconds)",
151151
"refId": "A"
152-
},
153-
{
154-
"expr": "rate(http_request_latency_seconds_sum{job=\"ml_api\"}[5m]) / rate(http_request_latency_seconds_count{job=\"ml_api\"}[5m])",
155-
"legendFormat": "{{endpoint}} (seconds)",
156-
"refId": "B"
157152
}
158153
],
159154
"thresholds": [],
@@ -205,7 +200,7 @@
205200
"list": []
206201
},
207202
"time": {
208-
"from": "now-5m",
203+
"from": "now-1h",
209204
"to": "now"
210205
},
211206
"timepicker": {
@@ -224,6 +219,6 @@
224219
},
225220
"timezone": "",
226221
"title": "Really Simple Flask Dashboard",
227-
"uid": "q8vgEpLZk",
228-
"version": 6
222+
"uid": "q8vgEpLZl",
223+
"version": 3
229224
}

0 commit comments

Comments
 (0)