Skip to content

Commit c4e6a43

Browse files
committed
ObservableGauge collect data points since previous collection
1 parent 0e751b4 commit c4e6a43

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

opentelemetry-sdk/src/metrics/internal/aggregate.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,12 @@ impl<T: Number> AggregateBuilder<T> {
146146
}
147147

148148
/// Builds a last-value aggregate function input and output.
149-
pub(crate) fn last_value(&self) -> AggregateFns<T> {
150-
LastValue::new(self.temporality, self.filter.clone()).into()
149+
pub(crate) fn last_value(&self, overwrite_temporality: Option<Temporality>) -> AggregateFns<T> {
150+
LastValue::new(
151+
overwrite_temporality.unwrap_or(self.temporality),
152+
self.filter.clone(),
153+
)
154+
.into()
151155
}
152156

153157
/// Builds a precomputed sum aggregate function input and output.
@@ -210,7 +214,7 @@ mod tests {
210214
#[test]
211215
fn last_value_aggregation() {
212216
let AggregateFns { measure, collect } =
213-
AggregateBuilder::<u64>::new(Temporality::Cumulative, None).last_value();
217+
AggregateBuilder::<u64>::new(Temporality::Cumulative, None).last_value(None);
214218
let mut a = Gauge {
215219
data_points: vec![GaugeDataPoint {
216220
attributes: vec![KeyValue::new("a", 1)],

opentelemetry-sdk/src/metrics/pipeline.rs

+16-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use crate::{
2323

2424
use self::internal::AggregateFns;
2525

26-
use super::Aggregation;
26+
use super::{Aggregation, Temporality};
2727

2828
/// Connects all of the instruments created by a meter provider to a [MetricReader].
2929
///
@@ -488,9 +488,20 @@ fn aggregate_fn<T: Number>(
488488
match agg {
489489
Aggregation::Default => aggregate_fn(b, &default_aggregation_selector(kind), kind),
490490
Aggregation::Drop => Ok(None),
491-
Aggregation::LastValue => Ok(Some(b.last_value())),
491+
Aggregation::LastValue => {
492+
match kind {
493+
InstrumentKind::Gauge => Ok(Some(b.last_value(None))),
494+
// temporality for LastValue only affects how data points are reported, so we can always use
495+
// delta temporality, because observable instruments should report data points only since previous collection
496+
InstrumentKind::ObservableGauge => Ok(Some(b.last_value(Some(Temporality::Delta)))),
497+
_ => Err(MetricError::Other(format!("LastValue aggregation is only available for Gauge or ObservableGauge, but not for {kind:?}")))
498+
}
499+
}
492500
Aggregation::Sum => {
493501
let fns = match kind {
502+
// TODO implement: observable instruments should not report data points on every collect
503+
// from SDK: For asynchronous instruments with Delta or Cumulative aggregation temporality,
504+
// MetricReader.Collect MUST only receive data points with measurements recorded since the previous collection
494505
InstrumentKind::ObservableCounter => b.precomputed_sum(true),
495506
InstrumentKind::ObservableUpDownCounter => b.precomputed_sum(false),
496507
InstrumentKind::Counter | InstrumentKind::Histogram => b.sum(true),
@@ -508,6 +519,9 @@ fn aggregate_fn<T: Number>(
508519
| InstrumentKind::ObservableUpDownCounter
509520
| InstrumentKind::ObservableGauge
510521
);
522+
// TODO implement: observable instruments should not report data points on every collect
523+
// from SDK: For asynchronous instruments with Delta or Cumulative aggregation temporality,
524+
// MetricReader.Collect MUST only receive data points with measurements recorded since the previous collection
511525
Ok(Some(b.explicit_bucket_histogram(
512526
boundaries.to_vec(),
513527
*record_min_max,

0 commit comments

Comments
 (0)