diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 01:13:27 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 01:13:27 +0000 |
commit | 40a355a42d4a9444dc753c04c6608dade2f06a23 (patch) | |
tree | 871fc667d2de662f171103ce5ec067014ef85e61 /toolkit/components/glean/api/src/private/object.rs | |
parent | Adding upstream version 124.0.1. (diff) | |
download | firefox-40a355a42d4a9444dc753c04c6608dade2f06a23.tar.xz firefox-40a355a42d4a9444dc753c04c6608dade2f06a23.zip |
Adding upstream version 125.0.1.upstream/125.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'toolkit/components/glean/api/src/private/object.rs')
-rw-r--r-- | toolkit/components/glean/api/src/private/object.rs | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/toolkit/components/glean/api/src/private/object.rs b/toolkit/components/glean/api/src/private/object.rs new file mode 100644 index 0000000000..5199cfad31 --- /dev/null +++ b/toolkit/components/glean/api/src/private/object.rs @@ -0,0 +1,83 @@ +// 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 https://mozilla.org/MPL/2.0/. + +use super::{CommonMetricData, MetricId}; + +use crate::ipc::need_ipc; + +use glean::traits::ObjectSerialize; + +/// An object metric. +pub enum ObjectMetric<K> { + Parent { + id: MetricId, + inner: glean::private::ObjectMetric<K>, + }, + Child, +} + +impl<K: ObjectSerialize> ObjectMetric<K> { + /// Create a new object metric. + pub fn new(id: MetricId, meta: CommonMetricData) -> Self { + if need_ipc() { + ObjectMetric::Child + } else { + let inner = glean::private::ObjectMetric::new(meta); + ObjectMetric::Parent { id, inner } + } + } + + pub fn set(&self, value: K) { + match self { + ObjectMetric::Parent { inner, .. } => { + inner.set(value); + } + ObjectMetric::Child => { + log::error!("Unable to set object metric in non-main process. This operation will be ignored."); + // TODO: Record an error. + } + }; + } + + pub fn set_string(&self, value: String) { + match self { + ObjectMetric::Parent { inner, .. } => { + inner.set_string(value); + } + ObjectMetric::Child => { + log::error!("Unable to set object metric in non-main process. This operation will be ignored."); + // TODO: Record an error. + } + }; + } + + pub fn test_get_value<'a, S: Into<Option<&'a str>>>( + &self, + ping_name: S, + ) -> Option<serde_json::Value> { + match self { + ObjectMetric::Parent { inner, .. } => inner.test_get_value(ping_name), + ObjectMetric::Child => { + panic!("Cannot get test value for object metric in non-parent process!",) + } + } + } + + pub fn test_get_value_as_str<'a, S: Into<Option<&'a str>>>( + &self, + ping_name: S, + ) -> Option<String> { + self.test_get_value(ping_name) + .map(|val| serde_json::to_string(&val).unwrap()) + } + + pub fn test_get_num_recorded_errors(&self, error: glean::ErrorType) -> i32 { + match self { + ObjectMetric::Parent { inner, .. } => inner.test_get_num_recorded_errors(error), + ObjectMetric::Child => { + panic!("Cannot get the number of recorded errors in non-parent process!") + } + } + } +} |