@@ -16,8 +16,7 @@ this package, you should first install the required dependencies:
1616 pip install ' litestar[opentelemetry]'
1717
1818 Once these requirements are satisfied, you can instrument your Litestar application by creating an instance
19- of :class: `OpenTelemetryConfig <litestar.plugins.opentelemetry.OpenTelemetryConfig> ` and passing the middleware it creates to
20- the Litestar constructor:
19+ of :class: `OpenTelemetryConfig <litestar.plugins.opentelemetry.OpenTelemetryConfig> ` and passing it to the plugin:
2120
2221.. code-block :: python
2322
@@ -32,5 +31,110 @@ The above example will work out of the box if you configure a global ``tracer_pr
3231exporter to use these (see the
3332`OpenTelemetry Exporter docs <https://opentelemetry.io/docs/instrumentation/python/exporters/ >`_ for further details).
3433
35- You can also pass configuration to the ``OpenTelemetryConfig `` telling it which providers to use. Consult
36- :class: `reference docs <litestar.plugins.opentelemetry.OpenTelemetryConfig> ` regarding the configuration options you can use.
34+ You can also pass configuration to the ``OpenTelemetryConfig `` telling it which providers to use. Consult the
35+ :class: `OpenTelemetryConfig <litestar.plugins.opentelemetry.OpenTelemetryConfig> ` reference docs for all available configuration options.
36+
37+ Configuration options
38+ ---------------------
39+
40+ Provider configuration
41+ ~~~~~~~~~~~~~~~~~~~~~~
42+
43+ The following options allow you to configure custom OpenTelemetry providers:
44+
45+ - ``tracer_provider ``: Custom ``TracerProvider `` instance. If omitted, the globally configured provider is used.
46+ - ``meter_provider ``: Custom ``MeterProvider `` instance. If omitted, the globally configured provider is used.
47+ - ``meter ``: Custom ``Meter `` instance. If omitted, the meter from the provider is used.
48+
49+ Example with custom tracer provider:
50+
51+ .. code-block :: python
52+
53+ from opentelemetry.sdk.trace import TracerProvider
54+ from opentelemetry.sdk.trace.export import BatchSpanProcessor, ConsoleSpanExporter
55+ from litestar import Litestar
56+ from litestar.plugins.opentelemetry import OpenTelemetryConfig, OpenTelemetryPlugin
57+
58+ # Configure a custom tracer provider
59+ tracer_provider = TracerProvider()
60+ tracer_provider.add_span_processor(BatchSpanProcessor(ConsoleSpanExporter()))
61+
62+ config = OpenTelemetryConfig(tracer_provider = tracer_provider)
63+ app = Litestar(plugins = [OpenTelemetryPlugin(config)])
64+
65+ Hook handlers
66+ ~~~~~~~~~~~~~
67+
68+ Hook handlers allow you to customize span behavior at various points in the request lifecycle:
69+
70+ - ``server_request_hook_handler ``: Called with the server span and ASGI scope for every incoming request.
71+ - ``client_request_hook_handler ``: Called with the internal span when the ``receive `` method is invoked.
72+ - ``client_response_hook_handler ``: Called with the internal span when the ``send `` method is invoked.
73+
74+ Example adding custom attributes:
75+
76+ .. code-block :: python
77+
78+ from opentelemetry.trace import Span
79+ from litestar.plugins.opentelemetry import OpenTelemetryConfig, OpenTelemetryPlugin
80+
81+ def request_hook (span : Span, scope : dict ) -> None :
82+ span.set_attribute(" custom.user_agent" , scope.get(" headers" , {}).get(" user-agent" , " " ))
83+
84+ config = OpenTelemetryConfig(server_request_hook_handler = request_hook)
85+ app = Litestar(plugins = [OpenTelemetryPlugin(config)])
86+
87+ URL filtering
88+ ~~~~~~~~~~~~~
89+
90+ You can exclude specific URLs from instrumentation:
91+
92+ - ``exclude ``: Pattern or list of patterns to exclude from instrumentation.
93+ - ``exclude_opt_key ``: Route option key to disable instrumentation on a per-route basis.
94+ - ``exclude_urls_env_key `` (default ``"LITESTAR" ``): Environment variable prefix for excluded URLs. With the default, the environment variable ``LITESTAR_EXCLUDED_URLS `` will be checked.
95+
96+ Example excluding health check endpoints:
97+
98+ .. code-block :: python
99+
100+ from litestar.plugins.opentelemetry import OpenTelemetryConfig, OpenTelemetryPlugin
101+
102+ config = OpenTelemetryConfig(
103+ exclude = [" /health" , " /readiness" , " /metrics" ]
104+ )
105+ app = Litestar(plugins = [OpenTelemetryPlugin(config)])
106+
107+ Advanced options
108+ ~~~~~~~~~~~~~~~~
109+
110+ - ``scope_span_details_extractor ``: Callback that returns a tuple of ``(span_name, attributes) `` for customizing span details from the ASGI scope.
111+ - ``scopes ``: ASGI scope types to process (e.g., ``{"http", "websocket"} ``). If ``None ``, both HTTP and WebSocket are processed.
112+ - ``middleware_class ``: Custom middleware class. Must be a subclass of ``OpenTelemetryInstrumentationMiddleware ``.
113+
114+ Litestar-specific spans
115+ ------------------------
116+
117+ Litestar can automatically create spans for framework events. With :class: `OpenTelemetryConfig <litestar.plugins.opentelemetry.OpenTelemetryConfig> `,
118+ all instrumentation options are enabled by default:
119+
120+ - ``instrument_guards `` (default ``True ``): Create ``guard.* `` spans for each guard executed within a request.
121+ - ``instrument_events `` (default ``True ``): Create ``event.emit.* `` and ``event.listener.* `` spans for the event emitter.
122+ - ``instrument_lifecycle `` (default ``True ``): Wrap application startup/shutdown hooks with ``lifecycle.* `` spans.
123+ - ``instrument_cli `` (default ``True ``): Emit ``cli.* `` spans for Litestar CLI commands.
124+
125+ Example with selective instrumentation:
126+
127+ .. code-block :: python
128+
129+ from litestar import Litestar
130+ from litestar.plugins.opentelemetry import OpenTelemetryConfig, OpenTelemetryPlugin
131+
132+ # Enable only guard and event instrumentation
133+ config = OpenTelemetryConfig(
134+ instrument_guards = True ,
135+ instrument_events = True ,
136+ instrument_lifecycle = False ,
137+ instrument_cli = False
138+ )
139+
140+ app = Litestar(plugins = [OpenTelemetryPlugin(config)])
0 commit comments