use lazy_static::lazy_static;
use std::convert::TryFrom;
use glean_core::{metrics::*, CommonMetricData, Lifetime};
use crate::boolean::BOOLEAN_METRICS;
use crate::counter::COUNTER_METRICS;
use crate::string::STRING_METRICS;
use crate::*;
macro_rules! impl_labeled_metric {
($metric:ty, $global:ident, $metric_global:ident, $new_name:ident, $destroy_name:ident, $get_name:ident, $test_get_num_recorded_errors:ident) => {
lazy_static! {
static ref $global: ConcurrentHandleMap<LabeledMetric<$metric>> =
ConcurrentHandleMap::new();
}
$crate::define_infallible_handle_map_deleter!($global, $destroy_name);
#[no_mangle]
pub extern "C" fn $new_name(
category: FfiStr,
name: FfiStr,
send_in_pings: RawStringArray,
send_in_pings_len: i32,
lifetime: i32,
disabled: u8,
labels: RawStringArray,
label_count: i32,
) -> u64 {
$global.insert_with_log(|| {
let name = name.to_string_fallible()?;
let category = category.to_string_fallible()?;
let send_in_pings = from_raw_string_array(send_in_pings, send_in_pings_len)?;
let labels = from_raw_string_array(labels, label_count)?;
let labels = if labels.is_empty() {
None
} else {
Some(labels)
};
let lifetime = Lifetime::try_from(lifetime)?;
Ok(LabeledMetric::new(
<$metric>::new(CommonMetricData {
name,
category,
send_in_pings,
lifetime,
disabled: disabled != 0,
..Default::default()
}),
labels,
))
})
}
#[no_mangle]
pub extern "C" fn $get_name(handle: u64, label: FfiStr) -> u64 {
$global.call_infallible_mut(handle, |labeled| {
let metric = labeled.get(label.as_str());
$metric_global.insert_with_log(|| Ok(metric))
})
}
#[no_mangle]
pub extern "C" fn $test_get_num_recorded_errors(
glean_handle: u64,
metric_id: u64,
error_type: i32,
storage_name: FfiStr,
) -> i32 {
crate::HandleMapExtension::call_infallible(&*crate::GLEAN, glean_handle, |glean| {
crate::HandleMapExtension::call_infallible(&*$global, metric_id, |metric| {
let error_type = std::convert::TryFrom::try_from(error_type).unwrap();
let storage_name =
crate::FallibleToString::to_string_fallible(&storage_name).unwrap();
glean_core::test_get_num_recorded_errors(
glean,
&metric.get_submetric().meta(),
error_type,
Some(&storage_name),
)
.unwrap_or(0)
})
})
}
};
}
impl_labeled_metric!(
CounterMetric,
LABELED_COUNTER,
COUNTER_METRICS,
glean_new_labeled_counter_metric,
glean_destroy_labeled_counter_metric,
glean_labeled_counter_metric_get,
glean_labeled_counter_test_get_num_recorded_errors
);
impl_labeled_metric!(
BooleanMetric,
LABELED_BOOLEAN,
BOOLEAN_METRICS,
glean_new_labeled_boolean_metric,
glean_destroy_labeled_boolean_metric,
glean_labeled_boolean_metric_get,
glean_labeled_boolean_test_get_num_recorded_errors
);
impl_labeled_metric!(
StringMetric,
LABELED_STRING,
STRING_METRICS,
glean_new_labeled_string_metric,
glean_destroy_labeled_string_metric,
glean_labeled_string_metric_get,
glean_labeled_string_test_get_num_recorded_errors
);