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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
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::<Counter>()?;
    m.add_class::<Gauge>()?;
    m.add_class::<Pulse>()?;
    m.add_class::<Histogram>()?;
    m.add_class::<Logger>()?;
    m.add_class::<Dict>()?;
    Ok(())
}

#[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, min: f64, max: f64) -> Self {
        let tracer = rillrate::Gauge::create(&path, min, max).unwrap();
        Self { tracer }
    }

    fn is_active(&mut self, _py: Python) -> bool {
        self.tracer.is_active()
    }

    fn set(&mut self, _py: Python, delta: f64) {
        self.tracer.set(delta);
    }
}

#[pyclass]
pub struct Histogram {
    tracer: rillrate::Histogram,
}

#[pymethods]
impl Histogram {
    #[new]
    fn new(path: String, levels: Vec<f64>) -> Self {
        let tracer = rillrate::Histogram::create(&path, &levels).unwrap();
        Self { tracer }
    }

    fn is_active(&mut self, _py: Python) -> bool {
        self.tracer.is_active()
    }

    fn add(&mut self, _py: Python, value: f64) {
        self.tracer.add(value);
    }
}

#[pyclass]
pub struct Pulse {
    tracer: rillrate::Pulse,
}

#[pymethods]
impl Pulse {
    #[new]
    fn new(path: String) -> Self {
        let tracer = rillrate::Pulse::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);
    }
}

#[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 Dict {
    tracer: rillrate::Dict,
}

#[pymethods]
impl Dict {
    #[new]
    fn new(path: String) -> Self {
        let tracer = rillrate::Dict::create(&path).unwrap();
        Self { tracer }
    }

    fn is_active(&mut self, _py: Python) -> bool {
        self.tracer.is_active()
    }

    fn set(&mut self, _py: Python, key: String, value: String) {
        self.tracer.set(key, value);
    }
}