Crates.io | pyo3-python-tracing-subscriber |
lib.rs | pyo3-python-tracing-subscriber |
version | 0.1.0 |
source | src |
created_at | 2024-10-29 18:07:37.971043 |
updated_at | 2024-10-29 18:07:37.971043 |
description | A `tracing_subscriber` layer that forwards data to a handler written in Python. |
homepage | |
repository | |
max_upload_size | |
id | 1427441 |
size | 19,025 |
pyo3-python-tracing-subscriber
A tracing_subscriber
layer for native extensions that forwards tracing
data to a Python handler.
See the demo
folder for a working example.
Native extensions that use tracing
can expose a function to Python to initialize tracing
:
#[tracing::instrument]
#[pyfunction]
fn fibonacci(index: usize, use_memoized: bool) -> PyResult<usize> {
// ...
}
#[pyfunction]
pub fn initialize_tracing(py_impl: Bound<'_, PyAny>) {
tracing_subscriber::registry()
.with(pyo3_python_tracing_subscriber::PythonCallbackLayerBridge::new(py_impl))
.init();
}
Python code can pass an implementation of tracing_subscriber::layer::Layer
(but slightly different) into initialize_tracing
and then future calls to instrumented Rust functions will forward tracing data to the Python layer.
import rust_extension
class MyPythonLayer:
def __init__(self):
pass
# `on_new_span` can return some state
def on_new_span(self, span_attrs: str, span_id: str) -> int:
print(f"[on_new_span]: {span_attrs} | {span_id}")
return random.randint(1, 1000)
# The state from `on_new_span` is passed back into other trait methods
def on_event(self, event: str, state: int):
print(f"[on_event]: {event} | {state}")
def on_close(self, span_id: str, state: int):
print(f"[on_close]: {span_id} | {state}")
def on_record(self, span_id: str, values: str, state: int):
print(f"[on_record]: {span_id} | {values} | {state}")
def main():
rust_extension.initialize_tracing(MyPythonLayer())
print("10th fibonacci number: ", rust_extension.fibonacci(10, True))
Only a subset of Layer
trait methods are currently forwarded to Python.
Native extensions are self-contained with their own global variables and copies of dependencies. Because of this:
tracing
separately to forward its data to Pythonpyo3-python-tracing-subscriber
itself is not a native extension and can't be used from Python
pyo3
+ tracing_subscriber
support trait objects then this can changeTest with:
$ cargo test