OpenTelemetry Pyramid Instrumentation

0.62b0 · active · verified Sat Apr 11

The `opentelemetry-instrumentation-pyramid` library provides automatic tracing for Pyramid web applications, capturing HTTP requests, view execution, and template rendering with minimal setup. It is part of the OpenTelemetry Python Contrib project, currently at version 0.62b0, and receives monthly updates as part of the broader OpenTelemetry Python Contrib release cadence.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up OpenTelemetry for a basic Pyramid application. It initializes the OpenTelemetry SDK with a `TracerProvider` and a `ConsoleSpanExporter` (for local testing), then instruments the Pyramid application using `PyramidInstrumentor().instrument()` before the WSGI app is created. Replace `ConsoleSpanExporter` with `OTLPSpanExporter` for production use, ensuring an OTLP collector is configured.

import os
from wsgiref.simple_server import make_server
from pyramid.config import Configurator
from pyramid.response import Response

from opentelemetry import trace
from opentelemetry.sdk.resources import Resource, SERVICE_NAME
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import ConsoleSpanExporter, BatchSpanProcessor
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter

from opentelemetry.instrumentation.pyramid import PyramidInstrumentor

# Configure OpenTelemetry SDK
resource = Resource.create({
    SERVICE_NAME: os.environ.get('OTEL_SERVICE_NAME', 'my-pyramid-app'),
})

tracer_provider = TracerProvider(resource=resource)

# Use OTLP exporter for production, ConsoleSpanExporter for debugging
# For OTLP, ensure an OTLP collector is running or set OTEL_EXPORTER_OTLP_ENDPOINT
# otlp_exporter = OTLPSpanExporter(endpoint=os.environ.get('OTEL_EXPORTER_OTLP_ENDPOINT', 'localhost:4317'))
# tracer_provider.add_span_processor(BatchSpanProcessor(otlp_exporter))

tracer_provider.add_span_processor(BatchSpanProcessor(ConsoleSpanExporter())) # for stdout output
trace.set_tracer_provider(tracer_provider)

# Instrument Pyramid (before creating the app)
PyramidInstrumentor().instrument()

# Define a simple Pyramid view
def hello_world(request):
    return Response('Hello, OpenTelemetry Pyramid!')

def create_app():
    with Configurator() as config:
        config.add_route('hello', '/')
        config.add_view(hello_world, route_name='hello')
        app = config.make_wsgi_app()
    return app

# Run the application
if __name__ == '__main__':
    app = create_app()
    print("Serving Pyramid app on http://localhost:6543")
    server = make_server('0.0.0.0', 6543, app)
    server.serve_forever()

view raw JSON →