add opentelemetry

This commit is contained in:
Viktor Barzin 2025-08-02 17:25:56 +00:00
parent d1cef99c5a
commit e5c68f6bb7
No known key found for this signature in database
GPG key ID: 4056458DBDBF8863
3 changed files with 333 additions and 1 deletions

17
crawler/api/metrics.py Normal file
View file

@ -0,0 +1,17 @@
# metrics.py
from opentelemetry.metrics import set_meter_provider
from opentelemetry.sdk.metrics import MeterProvider
from opentelemetry.sdk.resources import SERVICE_NAME, Resource
from opentelemetry.exporter.prometheus import PrometheusMetricReader
from prometheus_client import make_asgi_app
# Set up Prometheus reader and meter provider
reader = PrometheusMetricReader()
provider = MeterProvider(
metric_readers=[reader],
resource=Resource.create({SERVICE_NAME: "fastapi-metrics-app"}),
)
set_meter_provider(provider)
# Expose the Prometheus metrics endpoint
metrics_app = make_asgi_app() # Exposes /metrics