use protobuf::{EnumOrUnknown, MessageField};
use crate::proto::{
Bucket, Counter, Gauge, Histogram, LabelPair, Metric, MetricFamily, MetricType, Quantile,
Summary,
};
impl Metric {
pub fn from_label(label: Vec<LabelPair>) -> Self {
Metric {
label,
..Default::default()
}
}
pub fn from_gauge(gauge: Gauge) -> Self {
Metric {
gauge: gauge.into(),
..Default::default()
}
}
#[deprecated(since = "0.14.0", note = "Please use `.timestamp_ms()` instead")]
pub fn get_timestamp_ms(&self) -> i64 {
self.timestamp_ms()
}
pub fn get_summary(&self) -> &MessageField<Summary> {
&self.summary
}
pub fn set_summary(&mut self, summary: Summary) {
self.summary = summary.into();
}
pub fn get_counter(&self) -> &MessageField<Counter> {
&self.counter
}
pub fn set_counter(&mut self, counter: Counter) {
self.counter = counter.into();
}
pub fn get_label(&self) -> &[LabelPair] {
&self.label
}
pub fn set_label(&mut self, label: Vec<LabelPair>) {
self.label = label;
}
pub fn take_label(&mut self) -> Vec<LabelPair> {
std::mem::take(&mut self.label)
}
pub fn get_gauge(&self) -> &MessageField<Gauge> {
&self.gauge
}
pub fn set_gauge(&mut self, gauge: Gauge) {
self.gauge = gauge.into();
}
pub fn get_histogram(&self) -> &MessageField<Histogram> {
&self.histogram
}
pub fn set_histogram(&mut self, histogram: Histogram) {
self.histogram = histogram.into();
}
}
impl MetricFamily {
#[deprecated(since = "0.14.0", note = "Please use `.name()` instead")]
pub fn get_name(&self) -> &str {
self.name()
}
#[deprecated(since = "0.14.0", note = "Please use `.help()` instead")]
pub fn get_help(&self) -> &str {
self.help()
}
pub fn set_metric(&mut self, metric: Vec<Metric>) {
self.metric = metric;
}
pub fn get_field_type(&self) -> MetricType {
self.type_()
}
pub fn set_field_type(&mut self, t: MetricType) {
self.type_ = t.into();
}
pub fn get_metric(&self) -> &[Metric] {
&self.metric
}
pub fn mut_metric(&mut self) -> &mut Vec<Metric> {
&mut self.metric
}
pub fn take_metric(&mut self) -> Vec<Metric> {
std::mem::take(&mut self.metric)
}
}
impl Summary {
pub fn set_quantile(&mut self, quantiles: Vec<Quantile>) {
self.quantile = quantiles;
}
pub fn get_quantile(&self) -> &[Quantile] {
&self.quantile
}
#[deprecated(since = "0.14.0", note = "Please use `.sample_count()` instead")]
pub fn get_sample_count(&self) -> u64 {
self.sample_count()
}
#[deprecated(since = "0.14.0", note = "Please use `.sample_sum()` instead")]
pub fn get_sample_sum(&self) -> f64 {
self.sample_sum()
}
}
impl Quantile {
#[deprecated(since = "0.14.0", note = "Please use `.quantile()` instead")]
pub fn get_quantile(&self) -> f64 {
self.quantile()
}
#[deprecated(since = "0.14.0", note = "Please use `.value()` instead")]
pub fn get_value(&self) -> f64 {
self.value()
}
}
pub trait MessageFieldExt {
#[allow(dead_code)]
fn get_value(&self) -> f64;
}
impl MessageFieldExt for MessageField<Gauge> {
fn get_value(&self) -> f64 {
self.value()
}
}
impl MessageFieldExt for MessageField<Counter> {
fn get_value(&self) -> f64 {
self.value()
}
}
impl Histogram {
pub fn get_sample_count(&self) -> u64 {
self.sample_count.unwrap_or_default()
}
pub fn get_sample_sum(&self) -> f64 {
self.sample_sum.unwrap_or_default()
}
pub fn get_bucket(&self) -> &[Bucket] {
&self.bucket
}
pub fn set_bucket(&mut self, bucket: Vec<Bucket>) {
self.bucket = bucket;
}
}
impl Bucket {
#[deprecated(since = "0.14.0", note = "Please use `.cumulative_count()` instead")]
pub fn get_cumulative_count(&self) -> u64 {
self.cumulative_count()
}
#[deprecated(since = "0.14.0", note = "Please use `.upper_bound()` instead")]
pub fn get_upper_bound(&self) -> f64 {
self.upper_bound()
}
}
impl LabelPair {
#[deprecated(since = "0.14.0", note = "Please use `.value()` instead")]
pub fn get_value(&self) -> &str {
self.value()
}
#[deprecated(since = "0.14.0", note = "Please use `.name()` instead")]
pub fn get_name(&self) -> &str {
self.name()
}
}
impl From<Counter> for MessageField<Counter> {
fn from(value: Counter) -> Self {
MessageField::some(value)
}
}
impl From<Gauge> for MessageField<Gauge> {
fn from(value: Gauge) -> Self {
MessageField::some(value)
}
}
impl From<Histogram> for MessageField<Histogram> {
fn from(value: Histogram) -> Self {
MessageField::some(value)
}
}
impl From<Summary> for MessageField<Summary> {
fn from(value: Summary) -> Self {
MessageField::some(value)
}
}
impl From<MetricType> for Option<EnumOrUnknown<MetricType>> {
fn from(value: MetricType) -> Self {
Some(EnumOrUnknown::from(value))
}
}