OpenTelemetry ASGI Instrumentation


This library provides a ASGI middleware that can be used on any ASGI framework (such as Django, Starlette, FastAPI or Quart) to track requests timing through OpenTelemetry.


pip install opentelemetry-instrumentation-asgi

Usage (Quart)

from quart import Quart
from opentelemetry.instrumentation.asgi import OpenTelemetryMiddleware

app = Quart(__name__)
app.asgi_app = OpenTelemetryMiddleware(app.asgi_app)

async def hello():
    return "Hello!"

if __name__ == "__main__":

Usage (Django 3.0)

Modify the application’s file as shown below.

import os
from django.core.asgi import get_asgi_application
from opentelemetry.instrumentation.asgi import OpenTelemetryMiddleware

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'asgi_example.settings')

application = get_asgi_application()
application = OpenTelemetryMiddleware(application)

Usage (Raw ASGI)

from opentelemetry.instrumentation.asgi import OpenTelemetryMiddleware

app = ...  # An ASGI application.
app = OpenTelemetryMiddleware(app)


The opentelemetry-instrumentation-asgi package provides an ASGI middleware that can be used on any ASGI framework (such as Django-channels / Quart) to track requests timing through OpenTelemetry.

class opentelemetry.instrumentation.asgi.ASGIGetter[source]

Bases: opentelemetry.propagators.textmap.Getter

get(carrier, key)[source]

Getter implementation to retrieve a HTTP header value from the ASGI scope.

  • carrier (dict) – ASGI scope object

  • key (str) – header name in scope

Return type



A list with a single string with the header value if it exists,

else None.


Function that can retrieve all the keys in a carrier object.


carrier (dict) – An object which contains values that are used to construct a Context.

Return type



list of keys from the carrier.


Collects HTTP request attributes from the ASGI scope and returns a dictionary to be used as span creation attributes.


Returns (host, port, full_url) tuple.

opentelemetry.instrumentation.asgi.set_status_code(span, status_code)[source]

Adds HTTP response attributes to span using the status_code argument.


Default implementation for span_details_callback


scope (dict) – the asgi scope dictionary

Return type

Tuple[str, dict]


a tuple of the span, and any attributes to attach to the span.

class opentelemetry.instrumentation.asgi.OpenTelemetryMiddleware(app, excluded_urls=None, span_details_callback=None, tracer_provider=None)[source]

Bases: object

The ASGI application middleware.

This class is an ASGI middleware that starts and annotates spans for any requests it is invoked with.

  • app – The ASGI application callable to forward requests to.

  • span_details_callback – Callback which should return a string and a tuple, representing the desired span name and a dictionary with any additional span attributes to set. Optional: Defaults to get_default_span_details.

  • tracer_provider – The optional tracer provider to use. If omitted the current globally configured one is used.