OpenTelemetry Tornado Instrumentation

0.62b0 · active · verified Fri Apr 10

This library provides instrumentation for the Tornado web framework within OpenTelemetry, capturing request flows and performance metrics for observability. It builds upon OpenTelemetry's WSGI middleware to track web requests. Currently, the library is in beta and designed to integrate with Tornado's asynchronous, high-performance architecture.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to instrument a basic Tornado application. It sets up the OpenTelemetry SDK with a console exporter to print traces, then applies the Tornado instrumentation before the application starts. Requests to the server will generate and export traces.

import tornado.ioloop
import tornado.web
from opentelemetry import trace
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import ConsoleSpanExporter, BatchSpanProcessor
from opentelemetry.instrumentation.tornado import TornadoInstrumentor

# 1. Configure OpenTelemetry SDK
resource = Resource.create({"service.name": "tornado-app"})
provider = TracerProvider(resource=resource)
processor = BatchSpanProcessor(ConsoleSpanExporter())
provider.add_span_processor(processor)
trace.set_tracer_provider(provider)

# 2. Instrument Tornado
TornadoInstrumentor().instrument()

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write("Hello, Tornado OpenTelemetry!")

def make_app():
    return tornado.web.Application([
        (r"/", MainHandler),
    ])

if __name__ == "__main__":
    app = make_app()
    app.listen(8888)
    print("Tornado server listening on http://localhost:8888")
    print("Visit http://localhost:8888 to see traces in console.")
    tornado.ioloop.IOLoop.current().start()

view raw JSON →