use std::collections::HashMap;
use crate::common_metric_data::CommonMetricDataInternal;
use crate::error_recording::{record_error, test_get_num_recorded_errors, ErrorType};
use crate::event_database::RecordedEvent;
use crate::metrics::MetricType;
use crate::util::truncate_string_at_boundary_with_error;
use crate::CommonMetricData;
use crate::Glean;
const MAX_LENGTH_EXTRA_KEY_VALUE: usize = 500;
#[derive(Clone, Debug)]
pub struct EventMetric {
meta: CommonMetricDataInternal,
allowed_extra_keys: Vec<String>,
}
impl MetricType for EventMetric {
fn meta(&self) -> &CommonMetricDataInternal {
&self.meta
}
}
impl EventMetric {
pub fn new(meta: CommonMetricData, allowed_extra_keys: Vec<String>) -> Self {
Self {
meta: meta.into(),
allowed_extra_keys,
}
}
pub fn record(&self, extra: HashMap<String, String>) {
let timestamp = crate::get_timestamp_ms();
self.record_with_time(timestamp, extra);
}
pub fn record_with_time(&self, timestamp: u64, extra: HashMap<String, String>) {
let metric = self.clone();
crate::launch_with_glean(move |glean| {
let sent = metric.record_sync(glean, timestamp, extra);
if sent {
let state = crate::global_state().lock().unwrap();
if let Err(e) = state.callbacks.trigger_upload() {
log::error!("Triggering upload failed. Error: {}", e);
}
}
});
}
fn validate_extra(
&self,
glean: &Glean,
extra: HashMap<String, String>,
) -> Result<Option<HashMap<String, String>>, ()> {
if extra.is_empty() {
return Ok(None);
}
let mut extra_strings = HashMap::new();
for (k, v) in extra.into_iter() {
if !self.allowed_extra_keys.contains(&k) {
let msg = format!("Invalid key index {}", k);
record_error(glean, &self.meta, ErrorType::InvalidValue, msg, None);
return Err(());
}
let value = truncate_string_at_boundary_with_error(
glean,
&self.meta,
v,
MAX_LENGTH_EXTRA_KEY_VALUE,
);
extra_strings.insert(k, value);
}
Ok(Some(extra_strings))
}
#[doc(hidden)]
pub fn record_sync(
&self,
glean: &Glean,
timestamp: u64,
extra: HashMap<String, String>,
) -> bool {
if !self.should_record(glean) {
return false;
}
let extra_strings = match self.validate_extra(glean, extra) {
Ok(extra) => extra,
Err(()) => return false,
};
glean
.event_storage()
.record(glean, &self.meta, timestamp, extra_strings)
}
#[doc(hidden)]
pub fn get_value<'a, S: Into<Option<&'a str>>>(
&self,
glean: &Glean,
ping_name: S,
) -> Option<Vec<RecordedEvent>> {
let queried_ping_name = ping_name
.into()
.unwrap_or_else(|| &self.meta().inner.send_in_pings[0]);
glean
.event_storage()
.test_get_value(&self.meta, queried_ping_name)
}
pub fn test_get_value(&self, ping_name: Option<String>) -> Option<Vec<RecordedEvent>> {
crate::block_on_dispatcher();
crate::core::with_glean(|glean| self.get_value(glean, ping_name.as_deref()))
}
pub fn test_get_num_recorded_errors(&self, error: ErrorType) -> i32 {
crate::block_on_dispatcher();
crate::core::with_glean(|glean| {
test_get_num_recorded_errors(glean, self.meta(), error).unwrap_or(0)
})
}
}