|
| 1 | +use std::{collections::HashMap, sync::Arc}; |
| 2 | + |
| 3 | +use pyo3::exceptions::PyTypeError; |
| 4 | +use pyo3::prelude::*; |
| 5 | +use temporal_sdk_core_api::telemetry::metrics; |
| 6 | + |
| 7 | +use crate::runtime; |
| 8 | + |
| 9 | +#[pyclass] |
| 10 | +pub struct MetricMeterRef { |
| 11 | + meter: metrics::TemporalMeter, |
| 12 | + #[pyo3(get)] |
| 13 | + default_attributes: MetricAttributesRef, |
| 14 | +} |
| 15 | + |
| 16 | +#[pyclass] |
| 17 | +#[derive(Clone)] |
| 18 | +pub struct MetricAttributesRef { |
| 19 | + attrs: metrics::MetricAttributes, |
| 20 | +} |
| 21 | + |
| 22 | +#[pyclass] |
| 23 | +pub struct MetricCounterRef { |
| 24 | + counter: Arc<dyn metrics::Counter>, |
| 25 | +} |
| 26 | + |
| 27 | +#[pyclass] |
| 28 | +pub struct MetricHistogramRef { |
| 29 | + histogram: Arc<dyn metrics::Histogram>, |
| 30 | +} |
| 31 | + |
| 32 | +#[pyclass] |
| 33 | +pub struct MetricGaugeRef { |
| 34 | + gauge: Arc<dyn metrics::Gauge>, |
| 35 | +} |
| 36 | + |
| 37 | +pub fn new_metric_meter(runtime_ref: &runtime::RuntimeRef) -> Option<MetricMeterRef> { |
| 38 | + runtime_ref |
| 39 | + .runtime |
| 40 | + .core |
| 41 | + .telemetry() |
| 42 | + .get_metric_meter() |
| 43 | + .map(|meter| { |
| 44 | + let default_attributes = MetricAttributesRef { |
| 45 | + attrs: meter.inner.new_attributes(meter.default_attribs.clone()), |
| 46 | + }; |
| 47 | + MetricMeterRef { |
| 48 | + meter, |
| 49 | + default_attributes, |
| 50 | + } |
| 51 | + }) |
| 52 | +} |
| 53 | + |
| 54 | +#[pymethods] |
| 55 | +impl MetricMeterRef { |
| 56 | + fn new_counter( |
| 57 | + &self, |
| 58 | + name: String, |
| 59 | + description: Option<String>, |
| 60 | + unit: Option<String>, |
| 61 | + ) -> MetricCounterRef { |
| 62 | + MetricCounterRef { |
| 63 | + counter: self |
| 64 | + .meter |
| 65 | + .inner |
| 66 | + .counter(build_metric_parameters(name, description, unit)), |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + fn new_histogram( |
| 71 | + &self, |
| 72 | + name: String, |
| 73 | + description: Option<String>, |
| 74 | + unit: Option<String>, |
| 75 | + ) -> MetricHistogramRef { |
| 76 | + MetricHistogramRef { |
| 77 | + histogram: self |
| 78 | + .meter |
| 79 | + .inner |
| 80 | + .histogram(build_metric_parameters(name, description, unit)), |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + fn new_gauge( |
| 85 | + &self, |
| 86 | + name: String, |
| 87 | + description: Option<String>, |
| 88 | + unit: Option<String>, |
| 89 | + ) -> MetricGaugeRef { |
| 90 | + MetricGaugeRef { |
| 91 | + gauge: self |
| 92 | + .meter |
| 93 | + .inner |
| 94 | + .gauge(build_metric_parameters(name, description, unit)), |
| 95 | + } |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +#[pymethods] |
| 100 | +impl MetricCounterRef { |
| 101 | + fn add(&self, value: u64, attrs_ref: &MetricAttributesRef) { |
| 102 | + self.counter.add(value, &attrs_ref.attrs); |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +#[pymethods] |
| 107 | +impl MetricHistogramRef { |
| 108 | + fn record(&self, value: u64, attrs_ref: &MetricAttributesRef) { |
| 109 | + self.histogram.record(value, &attrs_ref.attrs); |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +#[pymethods] |
| 114 | +impl MetricGaugeRef { |
| 115 | + fn set(&self, value: u64, attrs_ref: &MetricAttributesRef) { |
| 116 | + self.gauge.record(value, &attrs_ref.attrs); |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +fn build_metric_parameters( |
| 121 | + name: String, |
| 122 | + description: Option<String>, |
| 123 | + unit: Option<String>, |
| 124 | +) -> metrics::MetricParameters { |
| 125 | + let mut build = metrics::MetricParametersBuilder::default(); |
| 126 | + build.name(name); |
| 127 | + if let Some(description) = description { |
| 128 | + build.description(description); |
| 129 | + } |
| 130 | + if let Some(unit) = unit { |
| 131 | + build.unit(unit); |
| 132 | + } |
| 133 | + // Should be nothing that would fail validation here |
| 134 | + build.build().unwrap() |
| 135 | +} |
| 136 | + |
| 137 | +#[pymethods] |
| 138 | +impl MetricAttributesRef { |
| 139 | + fn with_additional_attributes<'p>( |
| 140 | + &self, |
| 141 | + py: Python<'p>, |
| 142 | + new_attrs: HashMap<String, PyObject>, |
| 143 | + ) -> PyResult<Self> { |
| 144 | + let mut attrs = self.attrs.clone(); |
| 145 | + attrs.add_new_attrs( |
| 146 | + new_attrs |
| 147 | + .into_iter() |
| 148 | + .map(|(k, obj)| metric_key_value_from_py(py, k, obj)) |
| 149 | + .collect::<PyResult<Vec<metrics::MetricKeyValue>>>()?, |
| 150 | + ); |
| 151 | + Ok(MetricAttributesRef { attrs }) |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +fn metric_key_value_from_py<'p>( |
| 156 | + py: Python<'p>, |
| 157 | + k: String, |
| 158 | + obj: PyObject, |
| 159 | +) -> PyResult<metrics::MetricKeyValue> { |
| 160 | + let val = if let Ok(v) = obj.extract::<String>(py) { |
| 161 | + metrics::MetricValue::String(v) |
| 162 | + } else if let Ok(v) = obj.extract::<bool>(py) { |
| 163 | + metrics::MetricValue::Bool(v) |
| 164 | + } else if let Ok(v) = obj.extract::<i64>(py) { |
| 165 | + metrics::MetricValue::Int(v) |
| 166 | + } else if let Ok(v) = obj.extract::<f64>(py) { |
| 167 | + metrics::MetricValue::Float(v) |
| 168 | + } else { |
| 169 | + return Err(PyTypeError::new_err(format!( |
| 170 | + "Invalid value type for key {}, must be str, int, float, or bool", |
| 171 | + k |
| 172 | + ))); |
| 173 | + }; |
| 174 | + Ok(metrics::MetricKeyValue::new(k, val)) |
| 175 | +} |
0 commit comments