Skip to content

Commit

Permalink
Add AggregationTemporality Enum
Browse files Browse the repository at this point in the history
  • Loading branch information
lenin-jaganathan committed Feb 22, 2023
1 parent 7854a88 commit 487af47
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2023 VMware, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micrometer.registry.otlp;

public enum AggregationTemporality {

AGGREGATION_TEMPORALITY_DELTA, AGGREGATION_TEMPORALITY_CUMULATIVE;

public static io.opentelemetry.proto.metrics.v1.AggregationTemporality mapToOtlp(
AggregationTemporality aggregationTemporality) {
switch (aggregationTemporality) {
case AGGREGATION_TEMPORALITY_DELTA:
return io.opentelemetry.proto.metrics.v1.AggregationTemporality.AGGREGATION_TEMPORALITY_DELTA;
case AGGREGATION_TEMPORALITY_CUMULATIVE:
return io.opentelemetry.proto.metrics.v1.AggregationTemporality.AGGREGATION_TEMPORALITY_CUMULATIVE;
default:
return io.opentelemetry.proto.metrics.v1.AggregationTemporality.UNRECOGNIZED;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import io.micrometer.core.instrument.config.validate.Validated;
import io.micrometer.core.instrument.push.PushRegistryConfig;
import io.opentelemetry.proto.metrics.v1.AggregationTemporality;

import java.util.Arrays;
import java.util.Map;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ public class OtlpMeterRegistry extends PushMeterRegistry {

private final boolean isDeltaAggregationTemporality;

private final io.opentelemetry.proto.metrics.v1.AggregationTemporality otlpAggregationTemporality;

public OtlpMeterRegistry() {
this(OtlpConfig.DEFAULT, Clock.SYSTEM);
}
Expand All @@ -88,6 +90,7 @@ private OtlpMeterRegistry(OtlpConfig config, Clock clock, HttpSender httpSender)
this.resource = Resource.newBuilder().addAllAttributes(getResourceAttributes()).build();
this.isDeltaAggregationTemporality = config
.aggregationTemporality() == AggregationTemporality.AGGREGATION_TEMPORALITY_DELTA;
this.otlpAggregationTemporality = AggregationTemporality.mapToOtlp(config.aggregationTemporality());
config().namingConvention(NamingConvention.dot);
start(DEFAULT_THREAD_FACTORY);
}
Expand Down Expand Up @@ -222,7 +225,7 @@ private Metric writeSum(Meter meter, DoubleSupplier count) {
.addDataPoints(NumberDataPoint.newBuilder().setStartTimeUnixNano(getStartTimeNanos(meter))
.setTimeUnixNano(getEndTimeNanos()).setAsDouble(count.getAsDouble())
.addAllAttributes(getTagsForId(meter.getId())).build())
.setIsMonotonic(true).setAggregationTemporality(config.aggregationTemporality()).build()).build();
.setIsMonotonic(true).setAggregationTemporality(otlpAggregationTemporality).build()).build();
}

// VisibleForTesting
Expand Down Expand Up @@ -263,13 +266,13 @@ Metric writeHistogramSupport(HistogramSupport histogramSupport) {
isTimeBased ? countAtBucket.bucket(getBaseTimeUnit()) : countAtBucket.bucket());
histogramDataPoint.addBucketCounts((long) countAtBucket.count());
}
metricBuilder.setHistogram(Histogram.newBuilder().setAggregationTemporality(config.aggregationTemporality())
metricBuilder.setHistogram(Histogram.newBuilder().setAggregationTemporality(otlpAggregationTemporality)
.addDataPoints(histogramDataPoint));
return metricBuilder.build();
}

return metricBuilder.setHistogram(Histogram.newBuilder()
.setAggregationTemporality(config.aggregationTemporality()).addDataPoints(histogramDataPoint)).build();
return metricBuilder.setHistogram(Histogram.newBuilder().setAggregationTemporality(otlpAggregationTemporality)
.addDataPoints(histogramDataPoint)).build();
}

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

private long getStartTimeNanos(Meter meter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;

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

class DeltaOtlpMeterRegistryTest {
Expand All @@ -38,7 +39,7 @@ class DeltaOtlpMeterRegistryTest {
OtlpConfig otlpConfig = new OtlpConfig() {
@Override
public AggregationTemporality aggregationTemporality() {
return AggregationTemporality.AGGREGATION_TEMPORALITY_DELTA;
return AGGREGATION_TEMPORALITY_DELTA;
}

@Override
Expand Down Expand Up @@ -186,7 +187,7 @@ private void assertHistogram(Metric metric, long startTime, long endTime, String
assertThat(histogram.getAttributes(0).getKey()).hasToString(meterTag.getKey());
assertThat(histogram.getAttributes(0).getValue().getStringValue()).hasToString(meterTag.getValue());
assertThat(metric.getHistogram().getAggregationTemporality())
.isEqualTo(AggregationTemporality.AGGREGATION_TEMPORALITY_DELTA);
.isEqualTo(AggregationTemporality.mapToOtlp(AGGREGATION_TEMPORALITY_DELTA));
}

private void assertSum(Metric metric, long startTime, long endTime, double expectedValue) {
Expand All @@ -200,7 +201,7 @@ private void assertSum(Metric metric, long startTime, long endTime, double expec
assertThat(sumDataPoint.getAttributes(0).getKey()).hasToString(meterTag.getKey());
assertThat(sumDataPoint.getAttributes(0).getValue().getStringValue()).hasToString(meterTag.getValue());
assertThat(metric.getSum().getAggregationTemporality())
.isEqualTo(AggregationTemporality.AGGREGATION_TEMPORALITY_DELTA);
.isEqualTo(AggregationTemporality.mapToOtlp(AGGREGATION_TEMPORALITY_DELTA));
}

private void stepOverNStep(int numStepsToSkip) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package io.micrometer.registry.otlp;

import io.opentelemetry.proto.metrics.v1.AggregationTemporality;
import org.junit.jupiter.api.Test;

import java.util.Collections;
Expand Down

0 comments on commit 487af47

Please sign in to comment.