fune/toolkit/components/glean/pytest/metrics_test_output
Chris H-C 280af19463 Bug 1688281 - Add IPC support to FOG Labeled Counters r=janerik
Since the submetric deals with IPCPayload, the submetric needs to know its
label. We could add that to CounterMetric::Child, but adding an optional label
to the child variant of CounterMetric looked strange.

So instead, I introduce the Labeled*Metric types.
Well, LabeledStringMetric and LabeledBooleanMetric are just re-exports.
But LabeledCounterMetric knows its label in non-parent processes.
And in parent processes it just acts like a normal CounterMetric, thanks to the
metric traits.

To figure out how to handle all these types it encouraged me to remove lambdas
from the MLA FFI and solve it with hygienic identifier capture instead. Bonus.

Here's hoping I don't regret this additional level of abstraction later.

Differential Revision: https://phabricator.services.mozilla.com/D107667
2021-03-10 17:51:30 +00:00

566 lines
20 KiB
Rust

// -*- mode: Rust -*-
// AUTOGENERATED BY glean_parser. DO NOT EDIT.
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
pub mod test {
use crate::private::*;
use glean::CommonMetricData;
use once_cell::sync::Lazy;
#[allow(non_upper_case_globals)]
/// generated from test.boolean_metric
///
/// A multi-line
/// description
pub static boolean_metric: Lazy<BooleanMetric> = Lazy::new(|| {
BooleanMetric::new(1.into(), CommonMetricData {
name: "boolean_metric".into(),
category: "test".into(),
send_in_pings: vec!["metrics".into()],
lifetime: Lifetime::Application,
disabled: false,
..Default::default()
})
});
#[allow(non_upper_case_globals)]
/// generated from test.labeled_boolean_metric
///
/// A multi-line
/// description
pub static labeled_boolean_metric: Lazy<LabeledMetric<LabeledBooleanMetric>> = Lazy::new(|| {
LabeledMetric::new(2.into(), CommonMetricData {
name: "labeled_boolean_metric".into(),
category: "test".into(),
send_in_pings: vec!["metrics".into()],
lifetime: Lifetime::Application,
disabled: false,
..Default::default()
}, None)
});
#[allow(non_upper_case_globals)]
/// generated from test.labeled_boolean_metric_labels
///
/// A multi-line
/// description
pub static labeled_boolean_metric_labels: Lazy<LabeledMetric<LabeledBooleanMetric>> = Lazy::new(|| {
LabeledMetric::new(3.into(), CommonMetricData {
name: "labeled_boolean_metric_labels".into(),
category: "test".into(),
send_in_pings: vec!["metrics".into()],
lifetime: Lifetime::Application,
disabled: false,
..Default::default()
}, Some(vec!["one_label".into(), "two_labels".into()]))
});
#[allow(non_upper_case_globals)]
/// generated from test.counter_metric
///
/// A multi-line
/// description
pub static counter_metric: Lazy<CounterMetric> = Lazy::new(|| {
CounterMetric::new(4.into(), CommonMetricData {
name: "counter_metric".into(),
category: "test".into(),
send_in_pings: vec!["metrics".into()],
lifetime: Lifetime::Application,
disabled: false,
..Default::default()
})
});
#[allow(non_upper_case_globals)]
/// generated from test.labeled_counter_metric
///
/// A multi-line
/// description
pub static labeled_counter_metric: Lazy<LabeledMetric<LabeledCounterMetric>> = Lazy::new(|| {
LabeledMetric::new(5.into(), CommonMetricData {
name: "labeled_counter_metric".into(),
category: "test".into(),
send_in_pings: vec!["metrics".into()],
lifetime: Lifetime::Application,
disabled: false,
..Default::default()
}, None)
});
#[allow(non_upper_case_globals)]
/// generated from test.labeled_counter_metric_labels
///
/// A multi-line
/// description
pub static labeled_counter_metric_labels: Lazy<LabeledMetric<LabeledCounterMetric>> = Lazy::new(|| {
LabeledMetric::new(6.into(), CommonMetricData {
name: "labeled_counter_metric_labels".into(),
category: "test".into(),
send_in_pings: vec!["metrics".into()],
lifetime: Lifetime::Application,
disabled: false,
..Default::default()
}, Some(vec!["one_label".into(), "two_labels".into()]))
});
#[allow(non_upper_case_globals)]
/// generated from test.string_metric
///
/// A multi-line
/// description
pub static string_metric: Lazy<StringMetric> = Lazy::new(|| {
StringMetric::new(7.into(), CommonMetricData {
name: "string_metric".into(),
category: "test".into(),
send_in_pings: vec!["metrics".into()],
lifetime: Lifetime::Application,
disabled: false,
..Default::default()
})
});
#[allow(non_upper_case_globals)]
/// generated from test.labeled_string_metric
///
/// A multi-line
/// description
pub static labeled_string_metric: Lazy<LabeledMetric<LabeledStringMetric>> = Lazy::new(|| {
LabeledMetric::new(8.into(), CommonMetricData {
name: "labeled_string_metric".into(),
category: "test".into(),
send_in_pings: vec!["metrics".into()],
lifetime: Lifetime::Application,
disabled: false,
..Default::default()
}, None)
});
#[allow(non_upper_case_globals)]
/// generated from test.labeled_string_metric_labels
///
/// A multi-line
/// description
pub static labeled_string_metric_labels: Lazy<LabeledMetric<LabeledStringMetric>> = Lazy::new(|| {
LabeledMetric::new(9.into(), CommonMetricData {
name: "labeled_string_metric_labels".into(),
category: "test".into(),
send_in_pings: vec!["metrics".into()],
lifetime: Lifetime::Application,
disabled: false,
..Default::default()
}, Some(vec!["one_label".into(), "two_labels".into()]))
});
#[allow(non_upper_case_globals)]
/// generated from test.string_list_metric
///
/// A multi-line
/// description
pub static string_list_metric: Lazy<StringListMetric> = Lazy::new(|| {
StringListMetric::new(10.into(), CommonMetricData {
name: "string_list_metric".into(),
category: "test".into(),
send_in_pings: vec!["metrics".into()],
lifetime: Lifetime::Application,
disabled: false,
..Default::default()
})
});
#[allow(non_upper_case_globals)]
/// generated from test.timespan_metric
///
/// A multi-line
/// description
pub static timespan_metric: Lazy<TimespanMetric> = Lazy::new(|| {
TimespanMetric::new(11.into(), CommonMetricData {
name: "timespan_metric".into(),
category: "test".into(),
send_in_pings: vec!["metrics".into()],
lifetime: Lifetime::Application,
disabled: false,
..Default::default()
}, TimeUnit::Millisecond)
});
#[allow(non_upper_case_globals)]
/// generated from test.timing_distribution_metric
///
/// A multi-line
/// description
pub static timing_distribution_metric: Lazy<TimingDistributionMetric> = Lazy::new(|| {
TimingDistributionMetric::new(12.into(), CommonMetricData {
name: "timing_distribution_metric".into(),
category: "test".into(),
send_in_pings: vec!["metrics".into()],
lifetime: Lifetime::Application,
disabled: false,
..Default::default()
}, TimeUnit::Nanosecond)
});
#[allow(non_upper_case_globals)]
/// generated from test.memory_distribution_metric
///
/// A multi-line
/// description
pub static memory_distribution_metric: Lazy<MemoryDistributionMetric> = Lazy::new(|| {
MemoryDistributionMetric::new(13.into(), CommonMetricData {
name: "memory_distribution_metric".into(),
category: "test".into(),
send_in_pings: vec!["metrics".into()],
lifetime: Lifetime::Application,
disabled: false,
..Default::default()
}, MemoryUnit::Kilobyte)
});
}
pub mod test_nested {
use crate::private::*;
use glean::CommonMetricData;
use once_cell::sync::Lazy;
#[allow(non_upper_case_globals)]
/// generated from test.nested.uuid_metric
///
/// A multi-line
/// description
pub static uuid_metric: Lazy<UuidMetric> = Lazy::new(|| {
UuidMetric::new(14.into(), CommonMetricData {
name: "uuid_metric".into(),
category: "test.nested".into(),
send_in_pings: vec!["metrics".into()],
lifetime: Lifetime::Application,
disabled: false,
..Default::default()
})
});
#[allow(non_upper_case_globals)]
/// generated from test.nested.datetime_metric
///
/// A multi-line
/// description
pub static datetime_metric: Lazy<DatetimeMetric> = Lazy::new(|| {
DatetimeMetric::new(15.into(), CommonMetricData {
name: "datetime_metric".into(),
category: "test.nested".into(),
send_in_pings: vec!["metrics".into()],
lifetime: Lifetime::Application,
disabled: false,
..Default::default()
}, TimeUnit::Millisecond)
});
#[allow(non_upper_case_globals)]
/// generated from test.nested.event_metric
///
/// A multi-line
/// description
pub static event_metric: Lazy<EventMetric<NoExtraKeys>> = Lazy::new(|| {
EventMetric::new(16.into(), CommonMetricData {
name: "event_metric".into(),
category: "test.nested".into(),
send_in_pings: vec!["events".into()],
lifetime: Lifetime::Ping,
disabled: false,
..Default::default()
})
});
#[derive(Clone, Copy, Hash, Eq, PartialEq)]
pub enum EventMetricWithExtraKeys {
AnExtraKey,
AnotherExtraKey,
}
impl ExtraKeys for EventMetricWithExtraKeys {
const ALLOWED_KEYS: &'static [&'static str] = &["an_extra_key", "another_extra_key"];
fn index(self) -> i32 {
self as i32
}
}
/// Convert from an extra key's index to its variant.
impl std::convert::TryFrom<i32> for EventMetricWithExtraKeys {
type Error = EventRecordingError;
fn try_from(value: i32) -> Result<Self, Self::Error> {
match value {
0 => Ok(Self::AnExtraKey),
1 => Ok(Self::AnotherExtraKey),
_ => Err(EventRecordingError::InvalidExtraKey),
}
}
}
/// Convert from an extra key's string representation to its variant.
impl std::convert::TryFrom<&str> for EventMetricWithExtraKeys {
type Error = EventRecordingError;
fn try_from(value: &str) -> Result<Self, Self::Error> {
match value {
"an_extra_key" => Ok(Self::AnExtraKey),
"another_extra_key" => Ok(Self::AnotherExtraKey),
_ => Err(EventRecordingError::InvalidExtraKey),
}
}
}
#[allow(non_upper_case_globals)]
/// generated from test.nested.event_metric_with_extra
///
/// A multi-line
/// description
pub static event_metric_with_extra: Lazy<EventMetric<EventMetricWithExtraKeys>> = Lazy::new(|| {
EventMetric::new(17.into(), CommonMetricData {
name: "event_metric_with_extra".into(),
category: "test.nested".into(),
send_in_pings: vec!["events".into()],
lifetime: Lifetime::Ping,
disabled: false,
..Default::default()
})
});
}
#[allow(dead_code)]
pub(crate) mod __glean_metric_maps {
use std::collections::HashMap;
use std::convert::TryInto;
use crate::private::*;
use once_cell::sync::Lazy;
pub static BOOLEAN_MAP: Lazy<HashMap<MetricId, &Lazy<BooleanMetric>>> = Lazy::new(|| {
let mut map = HashMap::with_capacity(1);
map.insert(1.into(), &super::test::boolean_metric);
map
});
pub static LABELED_BOOLEAN_MAP: Lazy<HashMap<MetricId, &Lazy<LabeledMetric<LabeledBooleanMetric>>>> = Lazy::new(|| {
let mut map = HashMap::with_capacity(2);
map.insert(2.into(), &super::test::labeled_boolean_metric);
map.insert(3.into(), &super::test::labeled_boolean_metric_labels);
map
});
pub static COUNTER_MAP: Lazy<HashMap<MetricId, &Lazy<CounterMetric>>> = Lazy::new(|| {
let mut map = HashMap::with_capacity(1);
map.insert(4.into(), &super::test::counter_metric);
map
});
pub static LABELED_COUNTER_MAP: Lazy<HashMap<MetricId, &Lazy<LabeledMetric<LabeledCounterMetric>>>> = Lazy::new(|| {
let mut map = HashMap::with_capacity(2);
map.insert(5.into(), &super::test::labeled_counter_metric);
map.insert(6.into(), &super::test::labeled_counter_metric_labels);
map
});
pub static STRING_MAP: Lazy<HashMap<MetricId, &Lazy<StringMetric>>> = Lazy::new(|| {
let mut map = HashMap::with_capacity(1);
map.insert(7.into(), &super::test::string_metric);
map
});
pub static LABELED_STRING_MAP: Lazy<HashMap<MetricId, &Lazy<LabeledMetric<LabeledStringMetric>>>> = Lazy::new(|| {
let mut map = HashMap::with_capacity(2);
map.insert(8.into(), &super::test::labeled_string_metric);
map.insert(9.into(), &super::test::labeled_string_metric_labels);
map
});
pub static STRING_LIST_MAP: Lazy<HashMap<MetricId, &Lazy<StringListMetric>>> = Lazy::new(|| {
let mut map = HashMap::with_capacity(1);
map.insert(10.into(), &super::test::string_list_metric);
map
});
pub static TIMESPAN_MAP: Lazy<HashMap<MetricId, &Lazy<TimespanMetric>>> = Lazy::new(|| {
let mut map = HashMap::with_capacity(1);
map.insert(11.into(), &super::test::timespan_metric);
map
});
pub static TIMING_DISTRIBUTION_MAP: Lazy<HashMap<MetricId, &Lazy<TimingDistributionMetric>>> = Lazy::new(|| {
let mut map = HashMap::with_capacity(1);
map.insert(12.into(), &super::test::timing_distribution_metric);
map
});
pub static MEMORY_DISTRIBUTION_MAP: Lazy<HashMap<MetricId, &Lazy<MemoryDistributionMetric>>> = Lazy::new(|| {
let mut map = HashMap::with_capacity(1);
map.insert(13.into(), &super::test::memory_distribution_metric);
map
});
pub static UUID_MAP: Lazy<HashMap<MetricId, &Lazy<UuidMetric>>> = Lazy::new(|| {
let mut map = HashMap::with_capacity(1);
map.insert(14.into(), &super::test_nested::uuid_metric);
map
});
pub static DATETIME_MAP: Lazy<HashMap<MetricId, &Lazy<DatetimeMetric>>> = Lazy::new(|| {
let mut map = HashMap::with_capacity(1);
map.insert(15.into(), &super::test_nested::datetime_metric);
map
});
/// Helper to get the number of allowed extra keys for a given event metric.
fn extra_keys_len<K: ExtraKeys>(_event: &EventMetric<K>) -> usize {
K::ALLOWED_KEYS.len()
}
/// Wrapper to record an event based on its metric ID.
///
/// # Arguments
///
/// * `metric_id` - The metric's ID to look up
/// * `extra` - An (optional) map of (extra key id, string) pairs.
/// The map will be decoded into the appropriate `ExtraKeys` type.
/// # Returns
///
/// Returns `Ok(())` if the event was found and `record` was called with the given `extra`,
/// or an `EventRecordingError::InvalidId` if no event by that ID exists
/// or an `EventRecordingError::InvalidExtraKey` the `extra` map could not be deserialized.
pub(crate) fn event_record_wrapper(metric_id: u32, extra: HashMap<i32, String>) -> Result<(), EventRecordingError> {
match metric_id {
16 => {
assert!(
extra_keys_len(&super::test_nested::event_metric) != 0 || extra.is_empty(),
"No extra keys allowed, but some were passed"
);
// In case of `NoExtraKeys` the whole iterator is impossible, so rustc complains.
#[allow(unused_variables)]
let extra: HashMap<_, _> = extra
.into_iter()
.map(|(k, v)| k.try_into().map(|k| (k, v)))
.collect::<Result<HashMap<_, _>, _>>()?;
super::test_nested::event_metric.record(Some(extra));
Ok(())
}
17 => {
assert!(
extra_keys_len(&super::test_nested::event_metric_with_extra) != 0 || extra.is_empty(),
"No extra keys allowed, but some were passed"
);
// In case of `NoExtraKeys` the whole iterator is impossible, so rustc complains.
#[allow(unused_variables)]
let extra: HashMap<_, _> = extra
.into_iter()
.map(|(k, v)| k.try_into().map(|k| (k, v)))
.collect::<Result<HashMap<_, _>, _>>()?;
super::test_nested::event_metric_with_extra.record(Some(extra));
Ok(())
}
_ => Err(EventRecordingError::InvalidId),
}
}
/// Wrapper to record an event based on its metric ID.
///
/// # Arguments
///
/// * `metric_id` - The metric's ID to look up
/// * `extra` - An (optional) map of (string, string) pairs.
/// The map will be decoded into the appropriate `ExtraKeys` types.
/// # Returns
///
/// Returns `Ok(())` if the event was found and `record` was called with the given `extra`,
/// or an `EventRecordingError::InvalidId` if no event by that ID exists
/// or an `EventRecordingError::InvalidExtraKey` the `extra` map could not be deserialized.
pub(crate) fn event_record_wrapper_str(metric_id: u32, extra: HashMap<String, String>) -> Result<(), EventRecordingError> {
match metric_id {
16 => {
assert!(
extra_keys_len(&super::test_nested::event_metric) != 0 || extra.is_empty(),
"No extra keys allowed, but some were passed"
);
// In case of `NoExtraKeys` the whole iterator is impossible, so rustc complains.
#[allow(unused_variables)]
let extra = extra
.into_iter()
.map(|(k, v)| (&*k).try_into().map(|k| (k, v)))
.collect::<Result<HashMap<_, _>, _>>()?;
super::test_nested::event_metric.record(Some(extra));
Ok(())
}
17 => {
assert!(
extra_keys_len(&super::test_nested::event_metric_with_extra) != 0 || extra.is_empty(),
"No extra keys allowed, but some were passed"
);
// In case of `NoExtraKeys` the whole iterator is impossible, so rustc complains.
#[allow(unused_variables)]
let extra = extra
.into_iter()
.map(|(k, v)| (&*k).try_into().map(|k| (k, v)))
.collect::<Result<HashMap<_, _>, _>>()?;
super::test_nested::event_metric_with_extra.record(Some(extra));
Ok(())
}
_ => Err(EventRecordingError::InvalidId),
}
}
/// Wrapper to get the currently stored events for event metric.
///
/// # Arguments
///
/// * `metric_id` - The metric's ID to look up
/// * `storage_name` - the storage name to look into.
///
/// # Returns
///
/// Returns the recorded events or `None` if nothing stored.
///
/// # Panics
///
/// Panics if no event by the given metric ID could be found.
pub(crate) fn event_test_get_value_wrapper(metric_id: u32, storage_name: &str) -> Option<Vec<RecordedEvent>> {
match metric_id {
16 => super::test_nested::event_metric.test_get_value(storage_name),
17 => super::test_nested::event_metric_with_extra.test_get_value(storage_name),
_ => panic!("No event for metric id {}", metric_id),
}
}
pub(crate) mod submetric_maps {
use std::sync::{
atomic::AtomicU32,
RwLock,
};
use super::*;
pub(crate) const MIN_LABELED_SUBMETRIC_ID: u32 = 134217729;
pub(crate) static NEXT_LABELED_SUBMETRIC_ID: AtomicU32 = AtomicU32::new(MIN_LABELED_SUBMETRIC_ID);
pub(crate) static LABELED_METRICS_TO_IDS: Lazy<RwLock<HashMap<(u32, String), u32>>> = Lazy::new(||
RwLock::new(HashMap::new())
);
pub static BOOLEAN_MAP: Lazy<RwLock<HashMap<MetricId, LabeledBooleanMetric>>> = Lazy::new(||
RwLock::new(HashMap::new())
);
pub static COUNTER_MAP: Lazy<RwLock<HashMap<MetricId, LabeledCounterMetric>>> = Lazy::new(||
RwLock::new(HashMap::new())
);
pub static STRING_MAP: Lazy<RwLock<HashMap<MetricId, LabeledStringMetric>>> = Lazy::new(||
RwLock::new(HashMap::new())
);
}
}