This library provides automatic and manual instrumentation of FastAPI web frameworks, instrumenting http requests served by applications utilizing the framework.
auto-instrumentation using the opentelemetry-instrumentation package is also supported.
Installationpip install opentelemetry-instrumentation-fastapiReferences API Usage
import fastapi from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor app = fastapi.FastAPI() @app.get("/foobar") async def foobar(): return {"message": "hello world"} FastAPIInstrumentor.instrument_app(app)Configuration Exclude lists
To exclude certain URLs from tracking, set the environment variable OTEL_PYTHON_FASTAPI_EXCLUDED_URLS
(or OTEL_PYTHON_EXCLUDED_URLS
to cover all instrumentations) to a string of comma delimited regexes that match the URLs.
For example,
export OTEL_PYTHON_FASTAPI_EXCLUDED_URLS="client/.*/info,healthcheck"
will exclude requests such as https://site/client/123/info
and https://site/xyz/healthcheck
.
You can also pass comma delimited regexes directly to the instrument_app
method:
FastAPIInstrumentor.instrument_app(app, excluded_urls="client/.*/info,healthcheck")Request/Response hooks
This instrumentation supports request and response hooks. These are functions that get called right after a span is created for a request and right before the span is finished for the response.
The server request hook is passed a server span and ASGI scope object for every incoming request.
The client request hook is called with the internal span, and ASGI scope and event when the method receive
is called.
The client response hook is called with the internal span, and ASGI scope and event when the method send
is called.
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor from opentelemetry.trace import Span from typing import Any def server_request_hook(span: Span, scope: dict[str, Any]): if span and span.is_recording(): span.set_attribute("custom_user_attribute_from_request_hook", "some-value") def client_request_hook(span: Span, scope: dict[str, Any], message: dict[str, Any]): if span and span.is_recording(): span.set_attribute("custom_user_attribute_from_client_request_hook", "some-value") def client_response_hook(span: Span, scope: dict[str, Any], message: dict[str, Any]): if span and span.is_recording(): span.set_attribute("custom_user_attribute_from_response_hook", "some-value") FastAPIInstrumentor().instrument(server_request_hook=server_request_hook, client_request_hook=client_request_hook, client_response_hook=client_response_hook)Capture HTTP request and response headers
You can configure the agent to capture specified HTTP headers as span attributes, according to the semantic convention.
APIBases: BaseInstrumentor
An instrumentor for FastAPI
See BaseInstrumentor
Instrument an uninstrumented FastAPI application.
app (FastAPI
) – The fastapi ASGI application callable to forward requests to.
server_request_hook (Optional
[Callable
[[Span
, Dict
[str
, Any
]], None
]]) – Optional callback which is called with the server span and ASGI scope object for every incoming request.
client_request_hook (Optional
[Callable
[[Span
, Dict
[str
, Any
], Dict
[str
, Any
]], None
]]) – Optional callback which is called with the internal span, and ASGI scope and event which are sent as dictionaries for when the method receive is called.
client_response_hook (Optional
[Callable
[[Span
, Dict
[str
, Any
], Dict
[str
, Any
]], None
]]) – Optional callback which is called with the internal span, and ASGI scope and event which are sent as dictionaries for when the method send is called.
tracer_provider (Optional
[TracerProvider
]) – The optional tracer provider to use. If omitted the current globally configured one is used.
meter_provider (Optional
[MeterProvider
]) – The optional meter provider to use. If omitted the current globally configured one is used.
excluded_urls (Optional
[str
]) – Optional comma delimited string of regexes to match URLs that should not be traced.
http_capture_headers_server_request (Optional
[list
[str
]]) – Optional list of HTTP headers to capture from the request.
http_capture_headers_server_response (Optional
[list
[str
]]) – Optional list of HTTP headers to capture from the response.
http_capture_headers_sanitize_fields (Optional
[list
[str
]]) – Optional list of HTTP headers to sanitize.
exclude_spans (Optional
[list
[Literal
['receive'
, 'send'
]]]) – Optionally exclude HTTP send and/or receive spans from the trace.
Return a list of python packages with versions that the will be instrumented.
The format should be the same as used in requirements.txt or pyproject.toml.
For example, if an instrumentation instruments requests 1.x, this method should look like: :rtype: Collection
[str
]
- def instrumentation_dependencies(self) -> Collection[str]:
return [‘requests ~= 1.0’]
This will ensure that the instrumentation will only be used when the specified library is present in the environment.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4