OpenTelemetry Starlette Instrumentation


This library provides automatic and manual instrumentation of Starlette web frameworks, instrumenting http requests served by applications utilizing the framework.

auto-instrumentation using the opentelemetry-instrumentation package is also supported.


pip install opentelemetry-instrumentation-starlette


Exclude lists

To exclude certain URLs from being tracked, set the environment variable OTEL_PYTHON_STARLETTE_EXCLUDED_URLS with comma delimited regexes representing which URLs to exclude.

For example,

export OTEL_PYTHON_STARLETTE_EXCLUDED_URLS="client/.*/info,healthcheck"

will exclude requests such as https://site/client/123/info and https://site/xyz/healthcheck.


from opentelemetry.instrumentation.starlette import StarletteInstrumentor
from starlette import applications
from starlette.responses import PlainTextResponse
from starlette.routing import Route

def home(request):
    return PlainTextResponse("hi")

app = applications.Starlette(
    routes=[Route("/foobar", home)]


class opentelemetry.instrumentation.starlette.StarletteInstrumentor(*args, **kwargs)[source]

Bases: opentelemetry.instrumentation.instrumentor.BaseInstrumentor

An instrumentor for starlette

See BaseInstrumentor

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

Instrument an uninstrumented Starlette application.


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

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