diff --git a/opentelemetry-otlp/CHANGELOG.md b/opentelemetry-otlp/CHANGELOG.md index 39c5282b8f..20a0b8c47a 100644 --- a/opentelemetry-otlp/CHANGELOG.md +++ b/opentelemetry-otlp/CHANGELOG.md @@ -2,6 +2,12 @@ ## vNext +### Added + +- Added `DeltaTemporalitySelector` ([#1568]) + +[#1568]: https://github.com/open-telemetry/opentelemetry-rust/pull/1568 + ## v0.15.0 ### Changed diff --git a/opentelemetry-otlp/src/metric.rs b/opentelemetry-otlp/src/metric.rs index b0b030b359..75e968b01b 100644 --- a/opentelemetry-otlp/src/metric.rs +++ b/opentelemetry-otlp/src/metric.rs @@ -169,6 +169,16 @@ where } } + /// Build with delta temporality selector. + /// + /// This temporality selector is equivalent to OTLP Metrics Exporter's + /// `Delta` temporality preference (see [its documentation][exporter-docs]). + /// + /// [exporter-docs]: https://github.com/open-telemetry/opentelemetry-specification/blob/a1c13d59bb7d0fb086df2b3e1eaec9df9efef6cc/specification/metrics/sdk_exporters/otlp.md#additional-configuration + pub fn with_delta_temporality(self) -> Self { + self.with_temporality_selector(DeltaTemporalitySelector) + } + /// Build with the given aggregation selector pub fn with_aggregation_selector(self, selector: T) -> Self { OtlpMetricPipeline { @@ -248,6 +258,35 @@ impl Debug for OtlpMetricPipeline { } } +/// A temporality selector that returns [`Delta`][Temporality::Delta] for all +/// instruments except `UpDownCounter` and `ObservableUpDownCounter`. +/// +/// This temporality selector is equivalent to OTLP Metrics Exporter's +/// `Delta` temporality preference (see [its documentation][exporter-docs]). +/// +/// [exporter-docs]: https://github.com/open-telemetry/opentelemetry-specification/blob/a1c13d59bb7d0fb086df2b3e1eaec9df9efef6cc/specification/metrics/sdk_exporters/otlp.md#additional-configuration +#[derive(Debug)] +struct DeltaTemporalitySelector; + +impl TemporalitySelector for DeltaTemporalitySelector { + #[rustfmt::skip] + fn temporality(&self, kind: InstrumentKind) -> Temporality { + match kind { + InstrumentKind::Counter + | InstrumentKind::Histogram + | InstrumentKind::ObservableCounter + | InstrumentKind::Gauge + | InstrumentKind::ObservableGauge => { + Temporality::Delta + } + InstrumentKind::UpDownCounter + | InstrumentKind::ObservableUpDownCounter => { + Temporality::Cumulative + } + } + } +} + /// An interface for OTLP metrics clients #[async_trait] pub trait MetricsClient: fmt::Debug + Send + Sync + 'static {