Skip to content

Commit a1f5b93

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

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

opentelemetry-otlp/src/metric.rs

+39
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,35 @@ 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+
#[derive(Debug)]
269+
struct DeltaTemporalitySelector;
270+
271+
impl TemporalitySelector for DeltaTemporalitySelector {
272+
#[rustfmt::skip]
273+
fn temporality(&self, kind: InstrumentKind) -> Temporality {
274+
match kind {
275+
InstrumentKind::Counter
276+
| InstrumentKind::Histogram
277+
| InstrumentKind::ObservableCounter
278+
| InstrumentKind::Gauge
279+
| InstrumentKind::ObservableGauge => {
280+
Temporality::Delta
281+
}
282+
InstrumentKind::UpDownCounter
283+
| InstrumentKind::ObservableUpDownCounter => {
284+
Temporality::Cumulative
285+
}
286+
}
287+
}
288+
}
289+
251290
/// An interface for OTLP metrics clients
252291
#[async_trait]
253292
pub trait MetricsClient: fmt::Debug + Send + Sync + 'static {

0 commit comments

Comments
 (0)