Update product_return_prediction/monitoring.py
Browse files
product_return_prediction/monitoring.py
CHANGED
@@ -1,13 +1,13 @@
|
|
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", "
|
11 |
|
12 |
instrumentator = Instrumentator(
|
13 |
should_group_status_codes=True,
|
@@ -18,7 +18,7 @@ instrumentator = Instrumentator(
|
|
18 |
inprogress_labels=True,
|
19 |
)
|
20 |
|
21 |
-
# ----- add metrics -----
|
22 |
|
23 |
instrumentator.add(
|
24 |
metrics.request_size(
|
@@ -57,14 +57,13 @@ instrumentator.add(
|
|
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,
|
68 |
) -> Callable[[Info], None]:
|
69 |
METRIC = Histogram(
|
70 |
metric_name,
|
@@ -74,17 +73,40 @@ def model_output(
|
|
74 |
namespace=metric_namespace,
|
75 |
subsystem=metric_subsystem,
|
76 |
)
|
77 |
-
METRIC.labels("
|
78 |
-
METRIC.labels("LogisticRegression")
|
79 |
|
80 |
def instrumentation(info: Info) -> None:
|
81 |
-
if info.modified_handler == "/
|
82 |
-
|
83 |
-
model_type = info.response.headers.get("X-model-type")
|
84 |
-
if
|
85 |
-
METRIC.labels(model_type).observe(float(
|
86 |
|
87 |
return instrumentation
|
88 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
89 |
|
|
|
90 |
instrumentator.add(model_output(metric_namespace=NAMESPACE, metric_subsystem=SUBSYSTEM))
|
|
|
|
1 |
import os
|
2 |
from typing import Callable
|
3 |
|
4 |
+
from prometheus_client import Histogram, Counter
|
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", "svm_model")
|
11 |
|
12 |
instrumentator = Instrumentator(
|
13 |
should_group_status_codes=True,
|
|
|
18 |
inprogress_labels=True,
|
19 |
)
|
20 |
|
21 |
+
# ----- add general metrics -----
|
22 |
|
23 |
instrumentator.add(
|
24 |
metrics.request_size(
|
|
|
57 |
)
|
58 |
)
|
59 |
|
60 |
+
# ----- custom metrics for SVM -----
|
|
|
61 |
def model_output(
|
62 |
metric_name: str = "model_output",
|
63 |
+
metric_doc: str = "Output value of SVM model",
|
64 |
metric_namespace: str = "",
|
65 |
metric_subsystem: str = "",
|
66 |
+
buckets=(0, 1), # Adjust buckets based on the SVM model output range
|
67 |
) -> Callable[[Info], None]:
|
68 |
METRIC = Histogram(
|
69 |
metric_name,
|
|
|
73 |
namespace=metric_namespace,
|
74 |
subsystem=metric_subsystem,
|
75 |
)
|
76 |
+
METRIC.labels("SVM")
|
|
|
77 |
|
78 |
def instrumentation(info: Info) -> None:
|
79 |
+
if info.modified_handler == "/predict": # Adjust based on your endpoint
|
80 |
+
predicted_class = info.response.headers.get("X-model-prediction") # type: ignore
|
81 |
+
model_type = info.response.headers.get("X-model-type") # type: ignore
|
82 |
+
if predicted_class:
|
83 |
+
METRIC.labels(model_type or "SVM").observe(float(predicted_class))
|
84 |
|
85 |
return instrumentation
|
86 |
|
87 |
+
# Counter for model inference calls
|
88 |
+
def inference_counter(
|
89 |
+
metric_name: str = "model_inferences_total",
|
90 |
+
metric_doc: str = "Total number of inferences made by the SVM model",
|
91 |
+
metric_namespace: str = "",
|
92 |
+
metric_subsystem: str = "",
|
93 |
+
) -> Callable[[Info], None]:
|
94 |
+
METRIC = Counter(
|
95 |
+
metric_name,
|
96 |
+
metric_doc,
|
97 |
+
labelnames=["model_type"],
|
98 |
+
namespace=metric_namespace,
|
99 |
+
subsystem=metric_subsystem,
|
100 |
+
)
|
101 |
+
METRIC.labels("SVM")
|
102 |
+
|
103 |
+
def instrumentation(info: Info) -> None:
|
104 |
+
if info.modified_handler == "/predict": # Adjust based on your endpoint
|
105 |
+
model_type = info.response.headers.get("X-model-type") # type: ignore
|
106 |
+
METRIC.labels(model_type or "SVM").inc()
|
107 |
+
|
108 |
+
return instrumentation
|
109 |
|
110 |
+
# Add metrics to the instrumentator
|
111 |
instrumentator.add(model_output(metric_namespace=NAMESPACE, metric_subsystem=SUBSYSTEM))
|
112 |
+
instrumentator.add(inference_counter(metric_namespace=NAMESPACE, metric_subsystem=SUBSYSTEM))
|