molinari135 commited on
Commit
ada9485
·
verified ·
1 Parent(s): 0f16b6f

Upload monitoring.py

Browse files
product_return_prediction/monitoring.py ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from typing import Callable
3
+
4
+ from prometheus_client import Histogram
5
+ from prometheus_fastapi_instrumentator import Instrumentator, metrics
6
+ from prometheus_fastapi_instrumentator.metrics import Info
7
+
8
+ # Let's use environment variables METRICS_NAMESPACE and METRICS_SUBSYSTEM for logical grouping of metrics
9
+ NAMESPACE = os.environ.get("METRICS_NAMESPACE", "fastapi")
10
+ SUBSYSTEM = os.environ.get("METRICS_SUBSYSTEM", "model")
11
+
12
+ instrumentator = Instrumentator(
13
+ should_group_status_codes=True,
14
+ should_ignore_untemplated=True,
15
+ should_instrument_requests_inprogress=True,
16
+ excluded_handlers=["/metrics"],
17
+ inprogress_name="fastapi_inprogress",
18
+ inprogress_labels=True,
19
+ )
20
+
21
+ # ----- add metrics -----
22
+
23
+ instrumentator.add(
24
+ metrics.request_size(
25
+ should_include_handler=True,
26
+ should_include_method=True,
27
+ should_include_status=True,
28
+ metric_namespace=NAMESPACE,
29
+ metric_subsystem=SUBSYSTEM,
30
+ )
31
+ )
32
+ instrumentator.add(
33
+ metrics.response_size(
34
+ should_include_handler=True,
35
+ should_include_method=True,
36
+ should_include_status=True,
37
+ metric_namespace=NAMESPACE,
38
+ metric_subsystem=SUBSYSTEM,
39
+ )
40
+ )
41
+ instrumentator.add(
42
+ metrics.latency(
43
+ should_include_handler=True,
44
+ should_include_method=True,
45
+ should_include_status=True,
46
+ metric_namespace=NAMESPACE,
47
+ metric_subsystem=SUBSYSTEM,
48
+ )
49
+ )
50
+ instrumentator.add(
51
+ metrics.requests(
52
+ should_include_handler=True,
53
+ should_include_method=True,
54
+ should_include_status=True,
55
+ metric_namespace=NAMESPACE,
56
+ metric_subsystem=SUBSYSTEM,
57
+ )
58
+ )
59
+
60
+
61
+ # ----- custom metrics -----
62
+ def model_output(
63
+ metric_name: str = "model_output",
64
+ metric_doc: str = "Output value of model",
65
+ metric_namespace: str = "",
66
+ metric_subsystem: str = "",
67
+ buckets=(0, 1, 2),
68
+ ) -> Callable[[Info], None]:
69
+ METRIC = Histogram(
70
+ metric_name,
71
+ metric_doc,
72
+ labelnames=["model_type"],
73
+ buckets=buckets,
74
+ namespace=metric_namespace,
75
+ subsystem=metric_subsystem,
76
+ )
77
+ METRIC.labels("SVC")
78
+ METRIC.labels("LogisticRegression")
79
+
80
+ def instrumentation(info: Info) -> None:
81
+ if info.modified_handler == "/models/{type}":
82
+ predicted_flower_type = info.response.headers.get("X-model-prediction") # type: ignore
83
+ model_type = info.response.headers.get("X-model-type") # type: ignore
84
+ if predicted_flower_type:
85
+ METRIC.labels(model_type).observe(float(predicted_flower_type))
86
+
87
+ return instrumentation
88
+
89
+
90
+ instrumentator.add(model_output(metric_namespace=NAMESPACE, metric_subsystem=SUBSYSTEM))