From 81f59f62c396be09928a1e41f7f217b9bd4cc52f Mon Sep 17 00:00:00 2001 From: Cijo Thomas Date: Mon, 8 May 2023 23:20:41 -0700 Subject: [PATCH 1/2] Add OTLPMetricPipeline builder methods for delta and lowmemory --- opentelemetry-otlp/CHANGELOG.md | 7 +++ opentelemetry-otlp/src/metric.rs | 73 ++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) diff --git a/opentelemetry-otlp/CHANGELOG.md b/opentelemetry-otlp/CHANGELOG.md index b1f99d5afb..c830a3bd59 100644 --- a/opentelemetry-otlp/CHANGELOG.md +++ b/opentelemetry-otlp/CHANGELOG.md @@ -1,4 +1,11 @@ # Changelog + +## Unreleased + +### Added +- Added builder methods to configure delta, low-memory temporality for OTLP + Metric pipeline + ## v0.12.0 ### Added diff --git a/opentelemetry-otlp/src/metric.rs b/opentelemetry-otlp/src/metric.rs index c0edff03ef..00143533e3 100644 --- a/opentelemetry-otlp/src/metric.rs +++ b/opentelemetry-otlp/src/metric.rs @@ -100,6 +100,57 @@ impl From for MetricsExporterBuilder { } } +/// Configure delta temporality for all [InstrumentKind] +/// +/// [Temporality::Delta] will be used for Counter, Asynchronous Counter and Histogram +/// [Temporality::Cumulative] will be used for UpDownCounter and Asynchronous UpDownCounter +#[derive(Clone, Default, Debug)] +struct DeltaTemporalitySelector { + pub(crate) _private: (), +} + +impl DeltaTemporalitySelector { + /// Create a new default temporality selector. + fn new() -> Self { + Self::default() + } +} + +impl TemporalitySelector for DeltaTemporalitySelector { + fn temporality(&self, _kind: InstrumentKind) -> Temporality { + Temporality::Delta + } +} + +/// Configure low memory temporality for all [InstrumentKind] +/// +/// [Temporality::Delta] will be used for Counter and Histogram +/// [Temporality::Cumulative] will be used for UpDownCounter, Asynchronous Counter and Asynchronous UpDownCounter +#[derive(Clone, Default, Debug)] +struct LowMemoryTemporalitySelector { + pub(crate) _private: (), +} + +impl LowMemoryTemporalitySelector { + /// Create a new default temporality selector. + fn new() -> Self { + Self::default() + } +} + +impl TemporalitySelector for LowMemoryTemporalitySelector { + fn temporality(&self, kind: InstrumentKind) -> Temporality { + match kind { + InstrumentKind::Counter + | InstrumentKind::Histogram => Temporality::Delta, + InstrumentKind::UpDownCounter + | InstrumentKind::ObservableCounter + | InstrumentKind::ObservableGauge + | InstrumentKind::ObservableUpDownCounter => Temporality::Cumulative + } + } +} + /// Pipeline to build OTLP metrics exporter /// /// Note that currently the OTLP metrics exporter only supports tonic as it's grpc layer and tokio as @@ -158,6 +209,28 @@ where } } + /// Build with Delta temporality + /// This choses Delta for Counter, Asynchronous Counter and Histogram, + /// and choses Cumulative for UpDownCounter and Asynchronous UpDownCounter + pub fn with_delta_temporality(self) -> Self { + // This implements Delta from : https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/sdk_exporters/otlp.md#additional-configuration + OtlpMetricPipeline { + temporality_selector: Some(Box::new(DeltaTemporalitySelector::new())), + ..self + } + } + + /// Build with "LowMemory" temporality + /// This choses Delta for Synchronous Counter and Histogram, + /// and choses Cumulative for UpDownCounter, Asynchronous Counter and Asynchronous UpDownCounter + pub fn with_low_memory_temporality(self) -> Self { + // This implements LowMemory from : https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/sdk_exporters/otlp.md#additional-configuration + OtlpMetricPipeline { + temporality_selector: Some(Box::new(DeltaTemporalitySelector::new())), + ..self + } + } + /// Build with the given aggregation selector pub fn with_aggregation_selector(self, selector: T) -> Self { OtlpMetricPipeline { From efcd8995aa2323684b86d2997f17a94cb2ca6a75 Mon Sep 17 00:00:00 2001 From: Cijo Thomas Date: Mon, 8 May 2023 23:33:33 -0700 Subject: [PATCH 2/2] fix fmt --- opentelemetry-otlp/src/metric.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/opentelemetry-otlp/src/metric.rs b/opentelemetry-otlp/src/metric.rs index 00143533e3..dcddebbd94 100644 --- a/opentelemetry-otlp/src/metric.rs +++ b/opentelemetry-otlp/src/metric.rs @@ -141,12 +141,11 @@ impl LowMemoryTemporalitySelector { impl TemporalitySelector for LowMemoryTemporalitySelector { fn temporality(&self, kind: InstrumentKind) -> Temporality { match kind { - InstrumentKind::Counter - | InstrumentKind::Histogram => Temporality::Delta, + InstrumentKind::Counter | InstrumentKind::Histogram => Temporality::Delta, InstrumentKind::UpDownCounter | InstrumentKind::ObservableCounter | InstrumentKind::ObservableGauge - | InstrumentKind::ObservableUpDownCounter => Temporality::Cumulative + | InstrumentKind::ObservableUpDownCounter => Temporality::Cumulative, } } }