File size: 1,954 Bytes
05f11f2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | """Implements Prometheus metrics for FastAPI application monitoring.
This is adapted from TheClouds/api/monitoring.py for the Hugging Face Space API.
"""
import os
from prometheus_client import Counter
from prometheus_fastapi_instrumentator import Instrumentator, metrics
# Namespace and Subsystem
NAMESPACE = os.environ.get("METRICS_NAMESPACE", "fastapi")
SUBSYSTEM = os.environ.get("METRICS_SUBSYSTEM", "model")
prediction_metric = Counter(
"prediction_count",
"Count of predictions by model, language and label",
labelnames=["language", "model_type"],
namespace=NAMESPACE,
subsystem=SUBSYSTEM,
)
instrumentator = Instrumentator(
should_group_status_codes=True,
should_ignore_untemplated=True,
should_instrument_requests_inprogress=True,
excluded_handlers=["/metrics", "/docs", "/openapi.json", "/health", "/status"],
inprogress_name="fastapi_inprogress",
inprogress_labels=True,
)
# ----- Standard Metrics -----
# latency, request/response size, request count, errors
instrumentator.add(
metrics.request_size(
should_include_handler=True,
should_include_method=True,
should_include_status=True,
metric_namespace=NAMESPACE,
metric_subsystem=SUBSYSTEM,
)
)
instrumentator.add(
metrics.response_size(
should_include_handler=True,
should_include_method=True,
should_include_status=True,
metric_namespace=NAMESPACE,
metric_subsystem=SUBSYSTEM,
)
)
instrumentator.add(
metrics.latency(
should_include_handler=True,
should_include_method=True,
should_include_status=True,
metric_namespace=NAMESPACE,
metric_subsystem=SUBSYSTEM,
)
)
instrumentator.add(
metrics.requests(
should_include_handler=True,
should_include_method=True,
should_include_status=True,
metric_namespace=NAMESPACE,
metric_subsystem=SUBSYSTEM,
)
)
|