Skip to content

WSGI

If the WSGI framework doesn't have a dedicated OpenTelemetry package, you can use the OpenTelemetry WSGI middleware.

Installation

You need to install the opentelemetry-instrumentation-wsgi package:

pip install opentelemetry-instrumentation-wsgi

Usage

Below we have a minimal example using the standard library wsgiref. You can run it with python main.py:

main.py
from wsgiref.simple_server import make_server

from opentelemetry.instrumentation.wsgi import OpenTelemetryMiddleware


def app(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    return [b"Hello World"]

app = OpenTelemetryMiddleware(app)

with make_server("", 8000, app) as httpd:
    print("Serving on port 8000...")

    # Serve until process is killed
    httpd.serve_forever()

You can read more about the OpenTelemetry WSGI middleware here.

Capturing request and response headers