PyO3 OpenTelemetry

Background

What this is

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.

What this is not

What this is

This repo contains utilities for automatically passing OpenTelemetry contexts from Python into Rust.

What this is not

Usage

From Rust:

```rs use pyo3_opentelemetry::prelude::; use pyo3::prelude::; use tracing::instrument;

[pypropagate]

[pyfunction]

[instrument]

fn myfunction() { println!("span \"myfunction\" is active and will share the Python OpenTelemetry context"); }

[pymodule]

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.

Development

| 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 |

Rust

It should be sufficient to install the Rust toolchain and cargo-make. Then:

sh cargo make check-all

Python

Install:

Python Example

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