pyo3_opentelemetry provides a macro to simply and easily instrument your PyO3 bindings so that OpenTelemetry contexts can be easily passed from a Python caller into a Rust library. The #[pypropagate]
macro instruments your Rust functions for you so that the global Python OpenTelemetry context is shared across the FFI boundary.
#[pypropagate]
attribute, Rust code is unaffected and will not attach the Python OpenTelemetry context.This repo contains utilities for automatically passing OpenTelemetry contexts from Python into Rust.
From Rust:
```rs use pyo3_opentelemetry::prelude::; use pyo3::prelude::; use tracing::instrument;
fn myfunction() { println!("span \"myfunction\" is active and will share the Python OpenTelemetry context"); }
fn mymodule(py: Python, m: &PyModule) -> PyResult<()> { m.addfunction(wrappyfunction!(my_function, m)?)?; Ok(()) } ```
These features require no Python code changes, however, opentelemetry-api must be installed.
| Directory | Purpose | |-----------|---------| | crates/macros | Rust macro definitions | | crates/lib | Supporting Rust functions that get OTel context from Python. | | examples/pyo3-opentelemetry-lib | maturin PyO3 project with Python test assertions on Rust OTel spans |
It should be sufficient to install the Rust toolchain and cargo-make. Then:
sh
cargo make check-all
Install:
examples/pyo3-opentelemetry-lib contains a full end-to-end pyo3 project with pytests. To build and run them:
sh
cd examples/pyo3-opentelemetry-lib
cargo make python-check-all