Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the OTLP base time unit configurable #3883

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,11 @@
import java.util.Arrays;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import static io.micrometer.core.instrument.config.MeterRegistryConfigValidator.*;
import static io.micrometer.core.instrument.config.validate.PropertyValidator.getString;
import static io.micrometer.core.instrument.config.validate.PropertyValidator.getUrlString;
import static io.micrometer.core.instrument.config.validate.PropertyValidator.getEnum;
import static io.micrometer.core.instrument.config.validate.PropertyValidator.*;

/**
* Config for {@link OtlpMeterRegistry}.
Expand Down Expand Up @@ -146,7 +145,12 @@ default Map<String, String> headers() {
default Validated<?> validate() {
return checkAll(this, c -> PushRegistryConfig.validate(c), checkRequired("url", OtlpConfig::url),
check("resourceAttributes", OtlpConfig::resourceAttributes),
check("baseTimeUnit", OtlpConfig::baseTimeUnit),
check("aggregationTemporality", OtlpConfig::aggregationTemporality));
}

default TimeUnit baseTimeUnit() {
return getTimeUnit(this, "baseTimeUnit").orElse(TimeUnit.MILLISECONDS);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public class OtlpMeterRegistry extends PushMeterRegistry {
private final Resource resource;

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

private long deltaAggregationTimeUnixNano = 0L;

Expand All @@ -100,6 +101,7 @@ public OtlpMeterRegistry(OtlpConfig config, Clock clock) {
private OtlpMeterRegistry(OtlpConfig config, Clock clock, HttpSender httpSender) {
super(config, clock);
this.config = config;
this.baseTimeUnit = config.baseTimeUnit();
this.httpSender = httpSender;
this.resource = Resource.newBuilder().addAllAttributes(getResourceAttributes()).build();
this.otlpAggregationTemporality = AggregationTemporality
Expand Down Expand Up @@ -222,7 +224,7 @@ protected LongTaskTimer newLongTaskTimer(Meter.Id id, DistributionStatisticConfi

@Override
protected TimeUnit getBaseTimeUnit() {
return TimeUnit.MILLISECONDS;
return baseTimeUnit;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;

import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -141,4 +142,35 @@ void invalidAggregationTemporalityShouldBeCaptured() {
assertThat(otlpConfig.validate().isValid()).isFalse();
}

@Test
void baseTimeUnitDefault() {
Map<String, String> properties = new HashMap<>();

OtlpConfig otlpConfig = properties::get;
assertThat(otlpConfig.validate().isValid()).isTrue();
assertThat(otlpConfig.baseTimeUnit()).isSameAs(TimeUnit.MILLISECONDS);

properties.put("otlp.baseTimeUnit", TimeUnit.MILLISECONDS.name());
assertThat(otlpConfig.baseTimeUnit()).isSameAs(TimeUnit.MILLISECONDS);
}

@Test
void baseTimeUnitSeconds() {
Map<String, String> properties = new HashMap<>();
properties.put("otlp.baseTimeUnit", TimeUnit.SECONDS.name());

OtlpConfig otlpConfig = properties::get;
assertThat(otlpConfig.validate().isValid()).isTrue();
assertThat(otlpConfig.baseTimeUnit()).isSameAs(TimeUnit.SECONDS);
}

@Test
void invalidBaseTimeUnitShouldBeCaptured() {
Map<String, String> properties = new HashMap<>();
properties.put("otlp.baseTimeUnit", "some_random_thing");

OtlpConfig otlpConfig = properties::get;
assertThat(otlpConfig.validate().isValid()).isFalse();
}

}