|
| 1 | +from typing import Tuple |
| 2 | + |
| 3 | +import time |
| 4 | +import os |
| 5 | + |
| 6 | +from opentelemetry import trace |
| 7 | +from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter |
| 8 | +from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor |
| 9 | +from opentelemetry.instrumentation.logging import LoggingInstrumentor |
| 10 | +from opentelemetry.sdk.resources import Resource |
| 11 | +from opentelemetry.sdk.trace import TracerProvider |
| 12 | +from opentelemetry.sdk.trace.export import BatchSpanProcessor |
| 13 | +from prometheus_client import REGISTRY, Counter, Gauge, Histogram |
| 14 | +from prometheus_client.openmetrics.exposition import ( |
| 15 | + CONTENT_TYPE_LATEST, |
| 16 | + generate_latest, |
| 17 | +) |
| 18 | +from starlette.middleware.base import BaseHTTPMiddleware, RequestResponseEndpoint |
| 19 | +from starlette.requests import Request |
| 20 | +from starlette.responses import Response |
| 21 | +from starlette.routing import Match |
| 22 | +from starlette.status import HTTP_500_INTERNAL_SERVER_ERROR |
| 23 | +from starlette.types import ASGIApp |
| 24 | + |
| 25 | +ENABLE_TELEMETRY = os.getenv("ENABLE_TELEMETRY", "false") == "true" |
| 26 | + |
| 27 | +INFO = Gauge("fastapi_app_info", "FastAPI application information.", ["app_name"]) |
| 28 | +REQUESTS = Counter( |
| 29 | + "fastapi_requests_total", |
| 30 | + "Total count of requests by method and path.", |
| 31 | + ["method", "path", "app_name"], |
| 32 | +) |
| 33 | +RESPONSES = Counter( |
| 34 | + "fastapi_responses_total", |
| 35 | + "Total count of responses by method, path and status codes.", |
| 36 | + ["method", "path", "status_code", "app_name"], |
| 37 | +) |
| 38 | +REQUESTS_PROCESSING_TIME = Histogram( |
| 39 | + "fastapi_requests_duration_seconds", |
| 40 | + "Histogram of requests processing time by path (in seconds)", |
| 41 | + ["method", "path", "app_name"], |
| 42 | +) |
| 43 | +EXCEPTIONS = Counter( |
| 44 | + "fastapi_exceptions_total", |
| 45 | + "Total count of exceptions raised by path and exception type", |
| 46 | + ["method", "path", "exception_type", "app_name"], |
| 47 | +) |
| 48 | +REQUESTS_IN_PROGRESS = Gauge( |
| 49 | + "fastapi_requests_in_progress", |
| 50 | + "Gauge of requests by method and path currently being processed", |
| 51 | + ["method", "path", "app_name"], |
| 52 | +) |
| 53 | + |
| 54 | + |
| 55 | +class PrometheusMiddleware(BaseHTTPMiddleware): |
| 56 | + def __init__(self, app: ASGIApp, app_name: str = "fastapi-app") -> None: |
| 57 | + super().__init__(app) |
| 58 | + self.app_name = app_name |
| 59 | + INFO.labels(app_name=self.app_name).inc() |
| 60 | + |
| 61 | + async def dispatch( |
| 62 | + self, request: Request, call_next: RequestResponseEndpoint |
| 63 | + ) -> Response: |
| 64 | + method = request.method |
| 65 | + path, is_handled_path = self.get_path(request) |
| 66 | + |
| 67 | + if not is_handled_path: |
| 68 | + return await call_next(request) |
| 69 | + |
| 70 | + REQUESTS_IN_PROGRESS.labels( |
| 71 | + method=method, path=path, app_name=self.app_name |
| 72 | + ).inc() |
| 73 | + REQUESTS.labels(method=method, path=path, app_name=self.app_name).inc() |
| 74 | + before_time = time.perf_counter() |
| 75 | + try: |
| 76 | + response = await call_next(request) |
| 77 | + except BaseException as e: |
| 78 | + status_code = HTTP_500_INTERNAL_SERVER_ERROR |
| 79 | + EXCEPTIONS.labels( |
| 80 | + method=method, |
| 81 | + path=path, |
| 82 | + exception_type=type(e).__name__, |
| 83 | + app_name=self.app_name, |
| 84 | + ).inc() |
| 85 | + raise e from None |
| 86 | + else: |
| 87 | + status_code = response.status_code |
| 88 | + after_time = time.perf_counter() |
| 89 | + # retrieve trace id for exemplar |
| 90 | + span = trace.get_current_span() |
| 91 | + trace_id = trace.format_trace_id(span.get_span_context().trace_id) |
| 92 | + |
| 93 | + REQUESTS_PROCESSING_TIME.labels( |
| 94 | + method=method, path=path, app_name=self.app_name |
| 95 | + ).observe(after_time - before_time, exemplar={"TraceID": trace_id}) |
| 96 | + finally: |
| 97 | + RESPONSES.labels( |
| 98 | + method=method, |
| 99 | + path=path, |
| 100 | + status_code=status_code, |
| 101 | + app_name=self.app_name, |
| 102 | + ).inc() |
| 103 | + REQUESTS_IN_PROGRESS.labels( |
| 104 | + method=method, path=path, app_name=self.app_name |
| 105 | + ).dec() |
| 106 | + |
| 107 | + return response |
| 108 | + |
| 109 | + @staticmethod |
| 110 | + def get_path(request: Request) -> Tuple[str, bool]: |
| 111 | + for route in request.app.routes: |
| 112 | + match, child_scope = route.matches(request.scope) |
| 113 | + if match == Match.FULL: |
| 114 | + return route.path, True |
| 115 | + |
| 116 | + return request.url.path, False |
| 117 | + |
| 118 | + |
| 119 | +def metrics(request: Request) -> Response: |
| 120 | + return Response( |
| 121 | + generate_latest(REGISTRY), headers={"Content-Type": CONTENT_TYPE_LATEST} |
| 122 | + ) |
| 123 | + |
| 124 | + |
| 125 | +def setting_otlp( |
| 126 | + app: ASGIApp, app_name: str, endpoint: str, log_correlation: bool = True |
| 127 | +) -> None: |
| 128 | + # Setting OpenTelemetry |
| 129 | + # set the service name to show in traces |
| 130 | + resource = Resource.create(attributes={"service.name": app_name}) |
| 131 | + |
| 132 | + # set the tracer provider |
| 133 | + tracer = TracerProvider(resource=resource) |
| 134 | + trace.set_tracer_provider(tracer) |
| 135 | + |
| 136 | + tracer.add_span_processor( |
| 137 | + BatchSpanProcessor(OTLPSpanExporter(endpoint=endpoint, insecure=True)) |
| 138 | + ) |
| 139 | + |
| 140 | + if log_correlation: |
| 141 | + LoggingInstrumentor().instrument(set_logging_format=True) |
| 142 | + |
| 143 | + FastAPIInstrumentor.instrument_app(app, tracer_provider=tracer) |
0 commit comments