1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
use once_cell::sync::OnceCell; use pyo3::exceptions::PyTypeError; use pyo3::prelude::*; use pyo3::wrap_pyfunction; use rillrate::RillRate; static RILLRATE: OnceCell<RillRate> = OnceCell::new(); fn py_err(err: impl ToString) -> PyErr { PyTypeError::new_err(err.to_string()) } #[pyfunction] fn install(_py: Python) -> PyResult<()> { let rillrate = RillRate::from_env("rillratepy").map_err(py_err)?; RILLRATE .set(rillrate) .map_err(|_| py_err("can't install RillRate shared object"))?; Ok(()) } #[pymodule] fn rillrate(_py: Python<'_>, m: &PyModule) -> PyResult<()> { m.add("__version__", env!("CARGO_PKG_VERSION"))?; m.add_wrapped(wrap_pyfunction!(install))?; m.add_class::<Logger>()?; m.add_class::<Counter>()?; m.add_class::<Gauge>()?; Ok(()) } #[pyclass] pub struct Logger { tracer: rillrate::Logger, } #[pymethods] impl Logger { #[new] fn new(path: String) -> Self { let tracer = rillrate::Logger::create(&path).unwrap(); Self { tracer } } fn is_active(&mut self, _py: Python) -> bool { self.tracer.is_active() } fn log(&mut self, _py: Python, msg: String) { self.tracer.log(msg); } } #[pyclass] pub struct Counter { tracer: rillrate::Counter, } #[pymethods] impl Counter { #[new] fn new(path: String) -> Self { let tracer = rillrate::Counter::create(&path).unwrap(); Self { tracer } } fn is_active(&mut self, _py: Python) -> bool { self.tracer.is_active() } fn inc(&mut self, _py: Python, delta: f64) { self.tracer.inc(delta); } } #[pyclass] pub struct Gauge { tracer: rillrate::Gauge, } #[pymethods] impl Gauge { #[new] fn new(path: String) -> Self { let tracer = rillrate::Gauge::create(&path).unwrap(); Self { tracer } } fn is_active(&mut self, _py: Python) -> bool { self.tracer.is_active() } fn inc(&mut self, _py: Python, delta: f64) { self.tracer.inc(delta); } fn dec(&mut self, _py: Python, delta: f64) { self.tracer.dec(delta); } fn set(&mut self, _py: Python, delta: f64) { self.tracer.set(delta); } }