Skip to content

Commit f12e9ee

Browse files
committed
otlp: Add DeltaTemporalitySelector
1 parent 36c2a8e commit f12e9ee

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

opentelemetry-otlp/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ pub use crate::span::{
226226

227227
#[cfg(feature = "metrics")]
228228
pub use crate::metric::{
229-
MetricsExporter, MetricsExporterBuilder, OtlpMetricPipeline,
229+
DeltaTemporalitySelector, MetricsExporter, MetricsExporterBuilder, OtlpMetricPipeline,
230230
OTEL_EXPORTER_OTLP_METRICS_COMPRESSION, OTEL_EXPORTER_OTLP_METRICS_ENDPOINT,
231231
OTEL_EXPORTER_OTLP_METRICS_HEADERS, OTEL_EXPORTER_OTLP_METRICS_TIMEOUT,
232232
};

opentelemetry-otlp/src/metric.rs

+40
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,16 @@ where
169169
}
170170
}
171171

172+
/// Build with [`DeltaTemporalitySelector`] as the temporality selector.
173+
///
174+
/// This temporality selector is equivalent to OTLP Metrics Exporter's
175+
/// `Delta` temporality preference (see [its documentation][exporter-docs]).
176+
///
177+
/// [exporter-docs]: https://github.com/open-telemetry/opentelemetry-specification/blob/a1c13d59bb7d0fb086df2b3e1eaec9df9efef6cc/specification/metrics/sdk_exporters/otlp.md#additional-configuration
178+
pub fn with_delta_temporality(self) -> Self {
179+
self.with_temporality_selector(DeltaTemporalitySelector)
180+
}
181+
172182
/// Build with the given aggregation selector
173183
pub fn with_aggregation_selector<T: AggregationSelector + 'static>(self, selector: T) -> Self {
174184
OtlpMetricPipeline {
@@ -248,6 +258,36 @@ impl<RT, EB: Debug> Debug for OtlpMetricPipeline<RT, EB> {
248258
}
249259
}
250260

261+
/// A temporality selector that returns [`Delta`][Temporality::Delta] for all
262+
/// instruments except `UpDownCounter` and `ObservableUpDownCounter`.
263+
///
264+
/// This temporality selector is equivalent to OTLP Metrics Exporter's
265+
/// `Delta` temporality preference (see [its documentation][exporter-docs]).
266+
///
267+
/// [exporter-docs]: https://github.com/open-telemetry/opentelemetry-specification/blob/a1c13d59bb7d0fb086df2b3e1eaec9df9efef6cc/specification/metrics/sdk_exporters/otlp.md#additional-configuration
268+
#[non_exhaustive]
269+
#[derive(Debug)]
270+
pub struct DeltaTemporalitySelector;
271+
272+
impl TemporalitySelector for DeltaTemporalitySelector {
273+
#[rustfmt::skip]
274+
fn temporality(&self, kind: InstrumentKind) -> Temporality {
275+
match kind {
276+
InstrumentKind::Counter
277+
| InstrumentKind::Histogram
278+
| InstrumentKind::ObservableCounter
279+
| InstrumentKind::Gauge
280+
| InstrumentKind::ObservableGauge => {
281+
Temporality::Delta
282+
}
283+
InstrumentKind::UpDownCounter
284+
| InstrumentKind::ObservableUpDownCounter => {
285+
Temporality::Cumulative
286+
}
287+
}
288+
}
289+
}
290+
251291
/// An interface for OTLP metrics clients
252292
#[async_trait]
253293
pub trait MetricsClient: fmt::Debug + Send + Sync + 'static {

0 commit comments

Comments
 (0)