PyO3 Tracing Subscriber
Background
What this is
pyo3_tracing_subscriber provides a PyModule that can be added to upstream pyo3 extension modules in order to support the configuration and initialization of Rust tracing subscribers from Python.
What this is not
- Any initialized tracing subscriber imported from your upstream package will not collect traces from any other
pyo3extension module. In other words, anypyo3extension module will need to separately export tracing configuration and context managers, which in turn must be separately initialized in order to capture Rust traces from respectivepyo3extension modules. - Currently, only three tracing subcriber layers are supported:
tracing_subscriber::fmtwhich writes traces to file (or stdout) in a human readable format.opentelemetry-stdoutwhich writes traces to file (or stdout) in OTLP format. Available only with thelayer-otel-otlp-filefeature.opentelemetry-otlpwhich sends traces to an OpenTelemetry OTLP endpoint. Available only with thelayer-otel-otlpfeature.
- This does not propagate OpenTelemetry contexts from Python into Rust (or vice versa). Use the
pyo3-opentelemetrycrate for that feature.
Usage
For a complete functioning example, see the
examples/pyo3-opentelemetry-lib/src/lib.rsexample within this crate's repository.
Given a pyo3 extension module named "my_module" that would like to expose the tracing subscriber configuration and context manager classes from "my_module._tracing_subscriber", from Rust:
use *;
Then a user could initialize a tracing subscriber that logged to stdout from Python:
=
=
Building Python Stub Files
This crate provides a convenient method for adding stub files to your Python source code with the stubs feature.
Given a pyo3 extension module named "my_module" that uses the pyo3-tracing-subscriber crate to expose tracing subscriber configuration and context manager classes from "my_module._tracing_subscriber", in the upstream build.rs file:
use write_stub_files;