Skip to content

Commit

Permalink
Use the current timestamp for otlp gauges
Browse files Browse the repository at this point in the history
  • Loading branch information
lenin-jaganathan committed May 6, 2024
1 parent c727c3f commit 9b7e7da
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ private void writeGauge(Gauge gauge) {
}
metricBuilder.getGaugeBuilder()
.addDataPoints(NumberDataPoint.newBuilder()
.setTimeUnixNano(getTimeUnixNano())
// For gauge, always use the instant when the sample was taken
.setTimeUnixNano(TimeUnit.MILLISECONDS.toNanos(clock.wallTime()))
.setAsDouble(gauge.value())
.addAllAttributes(getKeyValuesForId(gauge.getId()))
.build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ void gauge() {
Metric metric = writeToMetric(gauge);
assertThat(metric.getGauge()).isNotNull();
assertThat(metric.getGauge().getDataPoints(0).getAsDouble()).isEqualTo(5);
assertThat(metric.getGauge().getDataPoints(0).getTimeUnixNano()).isEqualTo(TimeUnit.MINUTES.toNanos(1));
assertThat(metric.getGauge().getDataPoints(0).getTimeUnixNano())
.describedAs("Gauges should have timestamp of the instant when data is sampled")
.isEqualTo(otlpConfig().step().plus(Duration.ofMillis(1)).toNanos());
}

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

@Test
Expand Down

0 comments on commit 9b7e7da

Please sign in to comment.