Skip to content

Commit 1a74a32

Browse files
lenin-jaganathanshakuzen
authored andcommitted
Use the current timestamp for otlp gauges
1 parent f0882f7 commit 1a74a32

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

implementations/micrometer-registry-otlp/src/main/java/io/micrometer/registry/otlp/OtlpMeterRegistry.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ Metric writeGauge(Gauge gauge) {
309309
return getMetricBuilder(gauge.getId())
310310
.setGauge(io.opentelemetry.proto.metrics.v1.Gauge.newBuilder()
311311
.addDataPoints(NumberDataPoint.newBuilder()
312-
.setTimeUnixNano(getTimeUnixNano())
312+
.setTimeUnixNano(TimeUnit.MILLISECONDS.toNanos(clock.wallTime()))
313313
.setAsDouble(gauge.value())
314314
.addAllAttributes(getTagsForId(gauge.getId()))
315315
.build()))

implementations/micrometer-registry-otlp/src/test/java/io/micrometer/registry/otlp/OtlpDeltaMeterRegistryTest.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ void gauge() {
7878
Metric metric = writeToMetric(gauge);
7979
assertThat(metric.getGauge()).isNotNull();
8080
assertThat(metric.getGauge().getDataPoints(0).getAsDouble()).isEqualTo(5);
81-
assertThat(metric.getGauge().getDataPoints(0).getTimeUnixNano()).isEqualTo(TimeUnit.MINUTES.toNanos(1));
81+
assertThat(metric.getGauge().getDataPoints(0).getTimeUnixNano())
82+
.describedAs("Gauges should have timestamp of the instant when data is sampled")
83+
.isEqualTo(otlpConfig().step().plus(Duration.ofMillis(1)).toNanos());
8284
}
8385

8486
@Test
@@ -88,7 +90,9 @@ void timeGauge() {
8890
Metric metric = writeToMetric(timeGauge);
8991
assertThat(metric.getGauge()).isNotNull();
9092
assertThat(metric.getGauge().getDataPoints(0).getAsDouble()).isEqualTo(0.024);
91-
assertThat(metric.getGauge().getDataPoints(0).getTimeUnixNano()).isEqualTo(TimeUnit.MINUTES.toNanos(1));
93+
assertThat(metric.getGauge().getDataPoints(0).getTimeUnixNano())
94+
.describedAs("Gauges should have timestamp of the instant when data is sampled")
95+
.isEqualTo(otlpConfig().step().plus(Duration.ofMillis(1)).toNanos());
9296
}
9397

9498
@Test

0 commit comments

Comments
 (0)