Crate pyo3_opentelemetry

Source
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 pypropagate for additional requirements and limitations.
  • pyo3-opentelemetry-macros - a crate defining the pypropagate macro.
  • 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 OpenTelemetry context 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_python to the function or method body, effectively ensuring that the wrapped function or method is executed in the current OpenTelemetry context from the Python side.