OpenTelemetry WSGI Instrumentation

This library provides a WSGI middleware that can be used on any WSGI framework (such as Django / Flask / to track requests timing through OpenTelemetry.

Usage (Flask)

from flask import Flask
from opentelemetry.instrumentation.wsgi import OpenTelemetryMiddleware

app = Flask(__name__)
app.wsgi_app = OpenTelemetryMiddleware(app.wsgi_app)

def hello():
    return "Hello!"

if __name__ == "__main__":

Usage (Django)

Modify the application’s file as shown below.

import os
from opentelemetry.instrumentation.wsgi import OpenTelemetryMiddleware
from django.core.wsgi import get_wsgi_application

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

application = get_wsgi_application()
application = OpenTelemetryMiddleware(application)

Usage (

import web
from opentelemetry.instrumentation.wsgi import OpenTelemetryMiddleware
from cheroot import wsgi

urls = ('/', 'index')

class index:

    def GET(self):
        return "Hello, world!"

if __name__ == "__main__":
    app = web.application(urls, globals())
    func = app.wsgifunc()

    func = OpenTelemetryMiddleware(func)

    server = wsgi.WSGIServer(
        ("localhost", 5100), func, server_name="localhost"


Request/Response hooks

Utilize request/reponse hooks to execute custom logic to be performed before/after performing a request. Environ is an instance of WSGIEnvironment. Response_headers is a list of key-value (tuples) representing the response headers returned from the response.

def request_hook(span: Span, environ: WSGIEnvironment):
    if span and span.is_recording():
        span.set_attribute("custom_user_attribute_from_request_hook", "some-value")

def response_hook(span: Span, environ: WSGIEnvironment, status: str, response_headers: List):
    if span and span.is_recording():
        span.set_attribute("custom_user_attribute_from_response_hook", "some-value")

OpenTelemetryMiddleware(request_hook=request_hook, response_hook=response_hook)


class opentelemetry.instrumentation.wsgi.WSGIGetter[source]

Bases: opentelemetry.propagators.textmap.Getter

get(carrier, key)[source]
Getter implementation to retrieve a HTTP header value from the

PEP3333-conforming WSGI environ

  • carrier (dict) – WSGI environ object

  • key (str) –

    header name in environ object


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

Return type



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


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


list of keys from the carrier.

opentelemetry.instrumentation.wsgi.setifnotnone(dic, key, value)[source]

Collects HTTP request attributes from the PEP3333-conforming WSGI environ and returns a dictionary to be used as span creation attributes.

opentelemetry.instrumentation.wsgi.add_response_attributes(span, start_response_status, response_headers)[source]

Adds HTTP response attributes to span using the arguments passed to a PEP3333-conforming start_response callable.


Default implementation for name_callback, returns HTTP {METHOD_NAME}.

class opentelemetry.instrumentation.wsgi.OpenTelemetryMiddleware(wsgi, request_hook=None, response_hook=None, tracer_provider=None)[source]

Bases: object

The WSGI application middleware.

This class is a PEP 3333 conforming WSGI middleware that starts and annotates spans for any requests it is invoked with.

  • wsgi – The WSGI application callable to forward requests to.

  • request_hook – Optional callback which is called with the server span and WSGI environ object for every incoming request.

  • response_hook – Optional callback which is called with the server span, WSGI environ, status_code and response_headers for every incoming request.

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

class opentelemetry.instrumentation.wsgi.ResponsePropagationSetter[source]

Bases: object

set(carrier, key, value)[source]