How to enable OpenTelemetry tracing#
This guide shows you how to turn on distributed tracing and send spans to an OTLP collector.
Install the extra#
pip install "plone.observability[opentelemetry]"
Point it at a collector#
Tracing activates when an OTLP endpoint is configured. Set the standard OpenTelemetry environment variables:
export OTEL_EXPORTER_OTLP_ENDPOINT=http://otel-collector:4317
export OTEL_SERVICE_NAME=plone-backend
The exporter speaks OTLP over gRPC by default, usually on port 4317.
If your collector accepts only OTLP over HTTP, usually on port 4318, select that transport:
export OTEL_EXPORTER_OTLP_ENDPOINT=http://otel-collector:4318
export OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
For the full set of variables and the package-specific overrides, see Configuration.
Add the WSGI filter#
Add the opentelemetry filter to your pipeline.
The wiring is the same as for the other filters.
See How to install plone.observability.
The filter is required. It sets up the exporter, installs the instrumentation, and opens the root span of each request. Without it, no spans are exported.
Exclude noisy paths#
The package’s own @@metrics scrape is excluded from tracing by default.
The periodic scrape does not flood your traces.
To exclude additional paths, set the standard exclusion variable to a comma-separated list of regexes, matched as a substring against the path:
export OTEL_PYTHON_WSGI_EXCLUDED_URLS="/@@health,/static"
Exclusion applies at both layers, the WSGI root span and the event-driven inner spans. An excluded path produces no spans at all.
To trace the @@metrics scrape after all, turn the defaults off:
export PLONE_OBSERVABILITY_OTEL_EXCLUDE_DEFAULTS=0
External I/O spans (S3, HTTP, SQL)#
By default only this package’s own spans are emitted. To also trace external I/O, install the instrumentor packages and switch them on. External I/O covers S3 blob access (botocore/boto3) and outbound HTTP (requests/urllib3/httpx):
pip install "plone.observability[opentelemetry,opentelemetry-io]"
export PLONE_OBSERVABILITY_OTEL_INSTRUMENTORS=1
The switch enables every supported instrumentor whose package is installed, together with the library it instruments.
Those calls then appear as child spans nested under the active request/publish/render span.
The requests library uses urllib3 internally.
With both instrumentors enabled, a single requests call produces a requests span and a nested urllib3 span.
Install only the instrumentor you want if that is noisy.
SQL spans (psycopg)#
Use the opentelemetry-db extra when the ZODB sits on PostgreSQL via zodb-pgjsonb.
Use it too when anything else in the process talks to PostgreSQL through psycopg 3.
The extra adds per-statement SQL spans under the same switch:
pip install "plone.observability[opentelemetry,opentelemetry-db]"
export PLONE_OBSERVABILITY_OTEL_INSTRUMENTORS=1
Each statement becomes a child span of the current request span, carrying the query as db.statement.
A busy request can emit dozens of these spans.
Budget for the span volume in your collector.
Connections opened before the WSGI pipeline is assembled (a storage’s initial pool) are not instrumented.
Connections created afterwards (pool growth, reconnects) are instrumented.
See also
Tracing for the emitted spans and their attributes.
How to add custom spans in your code to trace your own code.
About tracing for why the spans come from Zope events and how tracing relates to metrics.