Expand description
This crate provides a function for attaching an OpenTelemetry context from Python within
Rust. It is intended to be used in conjunction with the pypropagate macro, which is
re-exported here.
§Requirements and Limitations
- All functionality here requires the calling Python code to have opentelemetry-api to be installed.
- See
pypropagatefor additional requirements and limitations.
§Related Crates
pyo3-opentelemetry-macros- a crate defining thepypropagatemacro.pyo3-tracing-subscriber- a crate supporting configuration and initialization of Rust tracing subscribers from Python.
§Examples
use pyo3_opentelemetry::pypropagate;
use pyo3::prelude::*;
use tracing::instrument;
#[pypropagate]
#[pyfunction]
#[instrument(skip(py))]
fn my_function(py: Python<'_>) {
println!("span \"my_function\" is active and will share the Python OpenTelemetry context");
}
#[pymodule]
fn my_module(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_function(wrap_pyfunction!(my_function, m)?)?;
Ok(())
}For a more comprehensive example, see the pyo3-opentelemetry-lib example in this repository.
Specifically, see the pyo3-opentelemetry-lib/src/lib.rs for the Rust code and pyo3-opentelemetry-lib/pyo3_opentelemetry_lib/tests/test_tracing.py for the Python code and behavioural assertions.
Functions§
- attach_
otel_ context_ from_ python - Attach the current
OpenTelemetrycontext from Python. This should be called at the beginning of a function or method to attach the context. This should not be used with async functions.
Attribute Macros§
- pypropagate
- This macro prepends
pyo3_opentelemetry::attach_otel_context_from_pythonto the function or method body, effectively ensuring that the wrapped function or method is executed in the currentOpenTelemetrycontext from the Python side.