diff --git a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/internal/exemplar/HistogramExemplarReservoir.java b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/internal/exemplar/HistogramExemplarReservoir.java index f86d67fbec8..ac06e64058e 100644 --- a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/internal/exemplar/HistogramExemplarReservoir.java +++ b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/internal/exemplar/HistogramExemplarReservoir.java @@ -23,6 +23,11 @@ class HistogramExemplarReservoir extends FixedSizeExemplarReservoir reservoir = new HistogramExemplarReservoir(clock, Collections.emptyList()); @@ -27,7 +27,7 @@ public void noMeasurement_returnsEmpty() { } @Test - public void oneBucket_samplesEverything() { + void oneBucket_samplesEverything() { TestClock clock = TestClock.create(); ExemplarReservoir reservoir = new HistogramExemplarReservoir(clock, Collections.emptyList()); @@ -66,7 +66,7 @@ public void oneBucket_samplesEverything() { } @Test - public void multipleBuckets_samplesIntoCorrectBucket() { + void multipleBuckets_samplesIntoCorrectBucket() { TestClock clock = TestClock.create(); AttributeKey bucketKey = AttributeKey.longKey("bucket"); ExemplarReservoir reservoir = @@ -95,4 +95,31 @@ public void multipleBuckets_samplesIntoCorrectBucket() { assertThat(e.getFilteredAttributes()).isEqualTo(Attributes.of(bucketKey, 3L)); }); } + + @Test + void longMeasurement_CastsToDouble() { + TestClock clock = TestClock.create(); + ExemplarReservoir reservoir = + new HistogramExemplarReservoir(clock, Collections.emptyList()); + reservoir.offerLongMeasurement(1L, Attributes.empty(), Context.root()); + assertThat(reservoir.collectAndReset(Attributes.empty())) + .hasSize(1) + .satisfiesExactly( + exemplar -> { + assertThat(exemplar.getEpochNanos()).isEqualTo(clock.now()); + assertThat(exemplar.getValue()).isEqualTo(1); + assertThat(exemplar.getFilteredAttributes()).isEmpty(); + }); + // Measurement count is reset, we should sample a new measurement (and only one) + clock.advance(Duration.ofSeconds(1)); + reservoir.offerLongMeasurement(2, Attributes.empty(), Context.root()); + assertThat(reservoir.collectAndReset(Attributes.empty())) + .hasSize(1) + .satisfiesExactly( + exemplar -> { + assertThat(exemplar.getEpochNanos()).isEqualTo(clock.now()); + assertThat(exemplar.getValue()).isEqualTo(2); + assertThat(exemplar.getFilteredAttributes()).isEmpty(); + }); + } }