relay_event_schema/protocol/
metrics_summary.rs

1use relay_protocol::{Annotated, Array, Empty, FromValue, IntoValue, Object};
2
3use crate::processor::ProcessValue;
4
5pub type MetricSummaryMapping = Object<Array<MetricSummary>>;
6
7/// A collection of [`MetricSummary`] items keyed by the metric.
8#[derive(Clone, Debug, Default, PartialEq, Empty, FromValue, IntoValue, ProcessValue)]
9pub struct MetricsSummary(pub MetricSummaryMapping);
10
11impl MetricsSummary {
12    /// Combinator to modify the contained metric summaries.
13    pub fn update_value<F: FnOnce(MetricSummaryMapping) -> MetricSummaryMapping>(&mut self, f: F) {
14        self.0 = f(std::mem::take(&mut self.0));
15    }
16}
17
18/// The metric summary of a single metric that is emitted by the SDK.
19///
20/// The summary contains specific aggregate values that the metric had during the span's lifetime. A single span can
21/// have the same metric emitted multiple times, which is the reason for aggregates being computed in each summary.
22#[derive(Clone, Debug, Default, PartialEq, Empty, FromValue, IntoValue, ProcessValue)]
23pub struct MetricSummary {
24    /// Minimum value of the metric.
25    pub min: Annotated<f64>,
26
27    /// Maximum value of the metric.
28    pub max: Annotated<f64>,
29
30    /// Sum of all metric values.
31    pub sum: Annotated<f64>,
32
33    /// Count of all metric values.
34    pub count: Annotated<u64>,
35
36    /// Tags of the metric.
37    pub tags: Annotated<Object<String>>,
38}
39
40impl MetricSummary {
41    /// Merges another [`MetricSummary`] in this [`MetricsSummary`].
42    pub fn merge(&mut self, other: MetricSummary) {
43        self.min.merge(other.min, |l, r| *l = l.min(r));
44        self.max.merge(other.max, |l, r| *l = l.max(r));
45        self.sum.merge(other.sum, |l, r| *l += r);
46        self.count.merge(other.count, |l, r| *l += r);
47    }
48}