OpenTelemetry FastAPI Instrumentation

pypi

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.

Installation

pip install opentelemetry-instrumentation-fastapi

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 being tracked, set the environment variable OTEL_PYTHON_FASTAPI_EXCLUDED_URLS (or OTEL_PYTHON_EXCLUDED_URLS as fallback) with comma delimited regexes representing which URLs to exclude.

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 the comma delimited regexes to the instrument_app method directly:

FastAPIInstrumentor.instrument_app(app, excluded_urls="client/.*/info,healthcheck")

Request/Response hooks

Utilize request/reponse hooks to execute custom logic to be performed before/after performing a request. The server request hook takes in a server span and ASGI scope object for every incoming request. The client request hook is called with the internal span and an ASGI scope which is sent as a dictionary for when the method recieve is called. The client response hook is called with the internal span and an ASGI event which is sent as a dictionary for when the method send is called.

 def server_request_hook(span: Span, scope: dict):
     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):
     if span and span.is_recording():
         span.set_attribute("custom_user_attribute_from_client_request_hook", "some-value")

 def client_response_hook(span: Span, message: dict):
     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)

API

class opentelemetry.instrumentation.fastapi.FastAPIInstrumentor(*args, **kwargs)[source]

Bases: opentelemetry.instrumentation.instrumentor.BaseInstrumentor

An instrumentor for FastAPI

See BaseInstrumentor

static instrument_app(app, server_request_hook=None, client_request_hook=None, client_response_hook=None, tracer_provider=None, excluded_urls=None)[source]

Instrument an uninstrumented FastAPI application.

static uninstrument_app(app)[source]
instrumentation_dependencies()[source]

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 setup.py.

For example, if an instrumentation instruments requests 1.x, this method should look like:

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.

Return type

Collection[str]