#open-telemetry #pyo3 #python #tracing

pyo3-opentelemetry

Macro and utilities for passing OpenTelemetry context from Python to Rust

12 releases

0.3.4 Oct 7, 2024
0.3.2-rc.0 Mar 2, 2024
0.3.2-dev.1 Jun 4, 2024
0.3.1 Dec 18, 2023
0.3.0-rc.2 Nov 30, 2023

#165 in FFI

Download history 27/week @ 2024-08-17 85/week @ 2024-08-24 12/week @ 2024-08-31 11/week @ 2024-09-07 66/week @ 2024-09-14 45/week @ 2024-09-21 3/week @ 2024-09-28 240/week @ 2024-10-05 140/week @ 2024-10-12 9/week @ 2024-10-19 1/week @ 2024-10-26 2/week @ 2024-11-09 43/week @ 2024-11-16 12/week @ 2024-11-23 8/week @ 2024-11-30

65 downloads per month

Apache-2.0

14KB
136 lines

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

  • This (currently) does not support propagating an OpenTelemetry context from Rust into Python.
  • This does not "magically" instrument Rust code. Without the #[pypropagate] attribute, Rust code is unaffected and will not attach the Python OpenTelemetry context.
  • This does not facilitate the processing or collection of OpenTelemetry spans; you still need to initialize and flush tracing providers and subscribers separately in Python and Rust. For more information, please see the respective OpenTelemetry documentation for Python and Rust.

Usage

For a complete functioning example, see the examples/pyo3-opentelemetry-lib/src/lib.rs example within this crate's repository.

From Rust:

use pyo3_opentelemetry::prelude::*;
use pyo3::prelude::*;
use tracing::instrument;

#[pypropagate]
#[pyfunction]
#[instrument]
fn my_function() {
  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(())
}

These features require no Python code changes, however, opentelemetry-api must be installed.

Dependencies

~5–11MB
~127K SLoC