Skip to content

Commit 487af47

Browse files
Add AggregationTemporality Enum
1 parent 7854a88 commit 487af47

File tree

5 files changed

+46
-10
lines changed

5 files changed

+46
-10
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2023 VMware, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.micrometer.registry.otlp;
17+
18+
public enum AggregationTemporality {
19+
20+
AGGREGATION_TEMPORALITY_DELTA, AGGREGATION_TEMPORALITY_CUMULATIVE;
21+
22+
public static io.opentelemetry.proto.metrics.v1.AggregationTemporality mapToOtlp(
23+
AggregationTemporality aggregationTemporality) {
24+
switch (aggregationTemporality) {
25+
case AGGREGATION_TEMPORALITY_DELTA:
26+
return io.opentelemetry.proto.metrics.v1.AggregationTemporality.AGGREGATION_TEMPORALITY_DELTA;
27+
case AGGREGATION_TEMPORALITY_CUMULATIVE:
28+
return io.opentelemetry.proto.metrics.v1.AggregationTemporality.AGGREGATION_TEMPORALITY_CUMULATIVE;
29+
default:
30+
return io.opentelemetry.proto.metrics.v1.AggregationTemporality.UNRECOGNIZED;
31+
}
32+
}
33+
34+
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import io.micrometer.core.instrument.config.validate.Validated;
1919
import io.micrometer.core.instrument.push.PushRegistryConfig;
20-
import io.opentelemetry.proto.metrics.v1.AggregationTemporality;
2120

2221
import java.util.Arrays;
2322
import java.util.Map;

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ public class OtlpMeterRegistry extends PushMeterRegistry {
7171

7272
private final boolean isDeltaAggregationTemporality;
7373

74+
private final io.opentelemetry.proto.metrics.v1.AggregationTemporality otlpAggregationTemporality;
75+
7476
public OtlpMeterRegistry() {
7577
this(OtlpConfig.DEFAULT, Clock.SYSTEM);
7678
}
@@ -88,6 +90,7 @@ private OtlpMeterRegistry(OtlpConfig config, Clock clock, HttpSender httpSender)
8890
this.resource = Resource.newBuilder().addAllAttributes(getResourceAttributes()).build();
8991
this.isDeltaAggregationTemporality = config
9092
.aggregationTemporality() == AggregationTemporality.AGGREGATION_TEMPORALITY_DELTA;
93+
this.otlpAggregationTemporality = AggregationTemporality.mapToOtlp(config.aggregationTemporality());
9194
config().namingConvention(NamingConvention.dot);
9295
start(DEFAULT_THREAD_FACTORY);
9396
}
@@ -222,7 +225,7 @@ private Metric writeSum(Meter meter, DoubleSupplier count) {
222225
.addDataPoints(NumberDataPoint.newBuilder().setStartTimeUnixNano(getStartTimeNanos(meter))
223226
.setTimeUnixNano(getEndTimeNanos()).setAsDouble(count.getAsDouble())
224227
.addAllAttributes(getTagsForId(meter.getId())).build())
225-
.setIsMonotonic(true).setAggregationTemporality(config.aggregationTemporality()).build()).build();
228+
.setIsMonotonic(true).setAggregationTemporality(otlpAggregationTemporality).build()).build();
226229
}
227230

228231
// VisibleForTesting
@@ -263,13 +266,13 @@ Metric writeHistogramSupport(HistogramSupport histogramSupport) {
263266
isTimeBased ? countAtBucket.bucket(getBaseTimeUnit()) : countAtBucket.bucket());
264267
histogramDataPoint.addBucketCounts((long) countAtBucket.count());
265268
}
266-
metricBuilder.setHistogram(Histogram.newBuilder().setAggregationTemporality(config.aggregationTemporality())
269+
metricBuilder.setHistogram(Histogram.newBuilder().setAggregationTemporality(otlpAggregationTemporality)
267270
.addDataPoints(histogramDataPoint));
268271
return metricBuilder.build();
269272
}
270273

271-
return metricBuilder.setHistogram(Histogram.newBuilder()
272-
.setAggregationTemporality(config.aggregationTemporality()).addDataPoints(histogramDataPoint)).build();
274+
return metricBuilder.setHistogram(Histogram.newBuilder().setAggregationTemporality(otlpAggregationTemporality)
275+
.addDataPoints(histogramDataPoint)).build();
273276
}
274277

275278
// VisibleForTesting
@@ -278,7 +281,7 @@ Metric writeFunctionTimer(FunctionTimer functionTimer) {
278281
.addDataPoints(HistogramDataPoint.newBuilder().addAllAttributes(getTagsForId(functionTimer.getId()))
279282
.setStartTimeUnixNano(getStartTimeNanos((functionTimer))).setTimeUnixNano(getEndTimeNanos())
280283
.setSum(functionTimer.totalTime(getBaseTimeUnit())).setCount((long) functionTimer.count()))
281-
.setAggregationTemporality(config.aggregationTemporality())).build();
284+
.setAggregationTemporality(otlpAggregationTemporality)).build();
282285
}
283286

284287
private long getStartTimeNanos(Meter meter) {

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.concurrent.TimeUnit;
2424
import java.util.concurrent.atomic.AtomicLong;
2525

26+
import static io.micrometer.registry.otlp.AggregationTemporality.AGGREGATION_TEMPORALITY_DELTA;
2627
import static org.assertj.core.api.Assertions.assertThat;
2728

2829
class DeltaOtlpMeterRegistryTest {
@@ -38,7 +39,7 @@ class DeltaOtlpMeterRegistryTest {
3839
OtlpConfig otlpConfig = new OtlpConfig() {
3940
@Override
4041
public AggregationTemporality aggregationTemporality() {
41-
return AggregationTemporality.AGGREGATION_TEMPORALITY_DELTA;
42+
return AGGREGATION_TEMPORALITY_DELTA;
4243
}
4344

4445
@Override
@@ -186,7 +187,7 @@ private void assertHistogram(Metric metric, long startTime, long endTime, String
186187
assertThat(histogram.getAttributes(0).getKey()).hasToString(meterTag.getKey());
187188
assertThat(histogram.getAttributes(0).getValue().getStringValue()).hasToString(meterTag.getValue());
188189
assertThat(metric.getHistogram().getAggregationTemporality())
189-
.isEqualTo(AggregationTemporality.AGGREGATION_TEMPORALITY_DELTA);
190+
.isEqualTo(AggregationTemporality.mapToOtlp(AGGREGATION_TEMPORALITY_DELTA));
190191
}
191192

192193
private void assertSum(Metric metric, long startTime, long endTime, double expectedValue) {
@@ -200,7 +201,7 @@ private void assertSum(Metric metric, long startTime, long endTime, double expec
200201
assertThat(sumDataPoint.getAttributes(0).getKey()).hasToString(meterTag.getKey());
201202
assertThat(sumDataPoint.getAttributes(0).getValue().getStringValue()).hasToString(meterTag.getValue());
202203
assertThat(metric.getSum().getAggregationTemporality())
203-
.isEqualTo(AggregationTemporality.AGGREGATION_TEMPORALITY_DELTA);
204+
.isEqualTo(AggregationTemporality.mapToOtlp(AGGREGATION_TEMPORALITY_DELTA));
204205
}
205206

206207
private void stepOverNStep(int numStepsToSkip) {

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package io.micrometer.registry.otlp;
1717

18-
import io.opentelemetry.proto.metrics.v1.AggregationTemporality;
1918
import org.junit.jupiter.api.Test;
2019

2120
import java.util.Collections;

0 commit comments

Comments
 (0)