Skip to content

Commit

Permalink
Otlp exporter default aggregation (open-telemetry#4557)
Browse files Browse the repository at this point in the history
* Add default aggregation selector option to OTLP metric exporters

* change compose to with
  • Loading branch information
jack-berg authored Jul 5, 2022
1 parent a68ce4e commit d2a8304
Show file tree
Hide file tree
Showing 14 changed files with 347 additions and 44 deletions.
13 changes: 12 additions & 1 deletion docs/apidiffs/current_vs_latest/opentelemetry-exporter-otlp.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
Comparing source compatibility of against
No changes.
*** MODIFIED CLASS: PUBLIC FINAL io.opentelemetry.exporter.otlp.http.metrics.OtlpHttpMetricExporter (not serializable)
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.sdk.metrics.Aggregation getDefaultAggregation(io.opentelemetry.sdk.metrics.InstrumentType)
*** MODIFIED CLASS: PUBLIC FINAL io.opentelemetry.exporter.otlp.http.metrics.OtlpHttpMetricExporterBuilder (not serializable)
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.exporter.otlp.http.metrics.OtlpHttpMetricExporterBuilder setDefaultAggregationSelector(io.opentelemetry.sdk.metrics.export.DefaultAggregationSelector)
*** MODIFIED CLASS: PUBLIC FINAL io.opentelemetry.exporter.otlp.metrics.OtlpGrpcMetricExporter (not serializable)
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.sdk.metrics.Aggregation getDefaultAggregation(io.opentelemetry.sdk.metrics.InstrumentType)
*** MODIFIED CLASS: PUBLIC FINAL io.opentelemetry.exporter.otlp.metrics.OtlpGrpcMetricExporterBuilder (not serializable)
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.exporter.otlp.metrics.OtlpGrpcMetricExporterBuilder setDefaultAggregationSelector(io.opentelemetry.sdk.metrics.export.DefaultAggregationSelector)
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Comparing source compatibility of against
+++ NEW SUPERCLASS: java.lang.Object
+++ NEW METHOD: PUBLIC(+) STATIC(+) io.opentelemetry.sdk.metrics.export.DefaultAggregationSelector getDefault()
+++ NEW METHOD: PUBLIC(+) ABSTRACT(+) io.opentelemetry.sdk.metrics.Aggregation getDefaultAggregation(io.opentelemetry.sdk.metrics.InstrumentType)
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.sdk.metrics.export.DefaultAggregationSelector with(io.opentelemetry.sdk.metrics.InstrumentType, io.opentelemetry.sdk.metrics.Aggregation)
+++ NEW ANNOTATION: java.lang.FunctionalInterface
**** MODIFIED INTERFACE: PUBLIC ABSTRACT io.opentelemetry.sdk.metrics.export.MetricExporter (not serializable)
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
import io.opentelemetry.exporter.internal.okhttp.OkHttpExporter;
import io.opentelemetry.exporter.internal.otlp.metrics.MetricsRequestMarshaler;
import io.opentelemetry.sdk.common.CompletableResultCode;
import io.opentelemetry.sdk.metrics.Aggregation;
import io.opentelemetry.sdk.metrics.InstrumentType;
import io.opentelemetry.sdk.metrics.data.AggregationTemporality;
import io.opentelemetry.sdk.metrics.data.MetricData;
import io.opentelemetry.sdk.metrics.export.AggregationTemporalitySelector;
import io.opentelemetry.sdk.metrics.export.DefaultAggregationSelector;
import io.opentelemetry.sdk.metrics.export.MetricExporter;
import java.util.Collection;
import javax.annotation.concurrent.ThreadSafe;
Expand All @@ -26,12 +28,15 @@ public final class OtlpHttpMetricExporter implements MetricExporter {

private final OkHttpExporter<MetricsRequestMarshaler> delegate;
private final AggregationTemporalitySelector aggregationTemporalitySelector;
private final DefaultAggregationSelector defaultAggregationSelector;

OtlpHttpMetricExporter(
OkHttpExporter<MetricsRequestMarshaler> delegate,
AggregationTemporalitySelector aggregationTemporalitySelector) {
AggregationTemporalitySelector aggregationTemporalitySelector,
DefaultAggregationSelector defaultAggregationSelector) {
this.delegate = delegate;
this.aggregationTemporalitySelector = aggregationTemporalitySelector;
this.defaultAggregationSelector = defaultAggregationSelector;
}

/**
Expand All @@ -57,6 +62,11 @@ public AggregationTemporality getAggregationTemporality(InstrumentType instrumen
return aggregationTemporalitySelector.getAggregationTemporality(instrumentType);
}

@Override
public Aggregation getDefaultAggregation(InstrumentType instrumentType) {
return defaultAggregationSelector.getDefaultAggregation(instrumentType);
}

/**
* Submits all the given metrics in a single batch to the OpenTelemetry collector.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import io.opentelemetry.exporter.internal.otlp.metrics.MetricsRequestMarshaler;
import io.opentelemetry.sdk.metrics.InstrumentType;
import io.opentelemetry.sdk.metrics.export.AggregationTemporalitySelector;
import io.opentelemetry.sdk.metrics.export.DefaultAggregationSelector;
import io.opentelemetry.sdk.metrics.export.MetricExporter;
import java.time.Duration;
import java.util.concurrent.TimeUnit;
Expand All @@ -32,6 +33,9 @@ public final class OtlpHttpMetricExporterBuilder {
private AggregationTemporalitySelector aggregationTemporalitySelector =
DEFAULT_AGGREGATION_TEMPORALITY_SELECTOR;

private DefaultAggregationSelector defaultAggregationSelector =
DefaultAggregationSelector.getDefault();

OtlpHttpMetricExporterBuilder() {
delegate = new OkHttpExporterBuilder<>("otlp", "metric", DEFAULT_ENDPOINT);
}
Expand Down Expand Up @@ -120,6 +124,21 @@ public OtlpHttpMetricExporterBuilder setAggregationTemporalitySelector(
return this;
}

/**
* Set the {@link DefaultAggregationSelector} used for {@link
* MetricExporter#getDefaultAggregation(InstrumentType)}.
*
* <p>If unset, defaults to {@link DefaultAggregationSelector#getDefault()}.
*
* @since 1.16.0
*/
public OtlpHttpMetricExporterBuilder setDefaultAggregationSelector(
DefaultAggregationSelector defaultAggregationSelector) {
requireNonNull(defaultAggregationSelector, "defaultAggregationSelector");
this.defaultAggregationSelector = defaultAggregationSelector;
return this;
}

OtlpHttpMetricExporterBuilder exportAsJson() {
delegate.exportAsJson();
return this;
Expand All @@ -131,6 +150,7 @@ OtlpHttpMetricExporterBuilder exportAsJson() {
* @return a new exporter's instance
*/
public OtlpHttpMetricExporter build() {
return new OtlpHttpMetricExporter(delegate.build(), aggregationTemporalitySelector);
return new OtlpHttpMetricExporter(
delegate.build(), aggregationTemporalitySelector, defaultAggregationSelector);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
import io.opentelemetry.exporter.internal.grpc.GrpcExporter;
import io.opentelemetry.exporter.internal.otlp.metrics.MetricsRequestMarshaler;
import io.opentelemetry.sdk.common.CompletableResultCode;
import io.opentelemetry.sdk.metrics.Aggregation;
import io.opentelemetry.sdk.metrics.InstrumentType;
import io.opentelemetry.sdk.metrics.data.AggregationTemporality;
import io.opentelemetry.sdk.metrics.data.MetricData;
import io.opentelemetry.sdk.metrics.export.AggregationTemporalitySelector;
import io.opentelemetry.sdk.metrics.export.DefaultAggregationSelector;
import io.opentelemetry.sdk.metrics.export.MetricExporter;
import java.util.Collection;
import javax.annotation.concurrent.ThreadSafe;
Expand All @@ -26,6 +28,7 @@ public final class OtlpGrpcMetricExporter implements MetricExporter {

private final GrpcExporter<MetricsRequestMarshaler> delegate;
private final AggregationTemporalitySelector aggregationTemporalitySelector;
private final DefaultAggregationSelector defaultAggregationSelector;

/**
* Returns a new {@link OtlpGrpcMetricExporter} reading the configuration values from the
Expand All @@ -49,16 +52,23 @@ public static OtlpGrpcMetricExporterBuilder builder() {

OtlpGrpcMetricExporter(
GrpcExporter<MetricsRequestMarshaler> delegate,
AggregationTemporalitySelector aggregationTemporalitySelector) {
AggregationTemporalitySelector aggregationTemporalitySelector,
DefaultAggregationSelector defaultAggregationSelector) {
this.delegate = delegate;
this.aggregationTemporalitySelector = aggregationTemporalitySelector;
this.defaultAggregationSelector = defaultAggregationSelector;
}

@Override
public AggregationTemporality getAggregationTemporality(InstrumentType instrumentType) {
return aggregationTemporalitySelector.getAggregationTemporality(instrumentType);
}

@Override
public Aggregation getDefaultAggregation(InstrumentType instrumentType) {
return defaultAggregationSelector.getDefaultAggregation(instrumentType);
}

/**
* Submits all the given metrics in a single batch to the OpenTelemetry collector.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import io.opentelemetry.exporter.internal.otlp.metrics.MetricsRequestMarshaler;
import io.opentelemetry.sdk.metrics.InstrumentType;
import io.opentelemetry.sdk.metrics.export.AggregationTemporalitySelector;
import io.opentelemetry.sdk.metrics.export.DefaultAggregationSelector;
import io.opentelemetry.sdk.metrics.export.MetricExporter;
import java.net.URI;
import java.time.Duration;
Expand Down Expand Up @@ -43,6 +44,9 @@ public final class OtlpGrpcMetricExporterBuilder {
private AggregationTemporalitySelector aggregationTemporalitySelector =
DEFAULT_AGGREGATION_TEMPORALITY_SELECTOR;

private DefaultAggregationSelector defaultAggregationSelector =
DefaultAggregationSelector.getDefault();

OtlpGrpcMetricExporterBuilder() {
delegate =
GrpcExporter.builder(
Expand Down Expand Up @@ -161,12 +165,28 @@ public OtlpGrpcMetricExporterBuilder setAggregationTemporalitySelector(
return this;
}

/**
* Set the {@link DefaultAggregationSelector} used for {@link
* MetricExporter#getDefaultAggregation(InstrumentType)}.
*
* <p>If unset, defaults to {@link DefaultAggregationSelector#getDefault()}.
*
* @since 1.16.0
*/
public OtlpGrpcMetricExporterBuilder setDefaultAggregationSelector(
DefaultAggregationSelector defaultAggregationSelector) {
requireNonNull(defaultAggregationSelector, "defaultAggregationSelector");
this.defaultAggregationSelector = defaultAggregationSelector;
return this;
}

/**
* Constructs a new instance of the exporter based on the builder's values.
*
* @return a new exporter's instance
*/
public OtlpGrpcMetricExporter build() {
return new OtlpGrpcMetricExporter(delegate.build(), aggregationTemporalitySelector);
return new OtlpGrpcMetricExporter(
delegate.build(), aggregationTemporalitySelector, defaultAggregationSelector);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@
import io.opentelemetry.proto.metrics.v1.ResourceMetrics;
import io.opentelemetry.sdk.common.CompletableResultCode;
import io.opentelemetry.sdk.common.InstrumentationScopeInfo;
import io.opentelemetry.sdk.metrics.Aggregation;
import io.opentelemetry.sdk.metrics.InstrumentType;
import io.opentelemetry.sdk.metrics.data.AggregationTemporality;
import io.opentelemetry.sdk.metrics.data.MetricData;
import io.opentelemetry.sdk.metrics.export.AggregationTemporalitySelector;
import io.opentelemetry.sdk.metrics.export.DefaultAggregationSelector;
import io.opentelemetry.sdk.metrics.internal.data.ImmutableLongPointData;
import io.opentelemetry.sdk.metrics.internal.data.ImmutableMetricData;
import io.opentelemetry.sdk.metrics.internal.data.ImmutableSumData;
Expand Down Expand Up @@ -167,6 +169,15 @@ void validConfig() {
.build()
.getAggregationTemporality(InstrumentType.COUNTER))
.isEqualTo(AggregationTemporality.CUMULATIVE);

assertThat(
OtlpHttpMetricExporter.builder()
.setDefaultAggregationSelector(
DefaultAggregationSelector.getDefault()
.with(InstrumentType.HISTOGRAM, Aggregation.drop()))
.build()
.getDefaultAggregation(InstrumentType.HISTOGRAM))
.isEqualTo(Aggregation.drop());
}

@Test
Expand Down Expand Up @@ -207,6 +218,10 @@ void invalidConfig() {
() -> OtlpHttpMetricExporter.builder().setAggregationTemporalitySelector(null))
.isInstanceOf(NullPointerException.class)
.hasMessage("aggregationTemporalitySelector");

assertThatThrownBy(() -> OtlpHttpMetricExporter.builder().setDefaultAggregationSelector(null))
.isInstanceOf(NullPointerException.class)
.hasMessage("defaultAggregationSelector");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import static io.opentelemetry.api.common.AttributeKey.stringKey;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.exporter.internal.grpc.OkHttpGrpcExporter;
Expand All @@ -19,8 +20,12 @@
import io.opentelemetry.exporter.otlp.testing.internal.TelemetryExporterBuilder;
import io.opentelemetry.proto.metrics.v1.ResourceMetrics;
import io.opentelemetry.sdk.common.InstrumentationScopeInfo;
import io.opentelemetry.sdk.metrics.Aggregation;
import io.opentelemetry.sdk.metrics.InstrumentType;
import io.opentelemetry.sdk.metrics.data.AggregationTemporality;
import io.opentelemetry.sdk.metrics.data.MetricData;
import io.opentelemetry.sdk.metrics.export.AggregationTemporalitySelector;
import io.opentelemetry.sdk.metrics.export.DefaultAggregationSelector;
import io.opentelemetry.sdk.metrics.internal.data.ImmutableLongPointData;
import io.opentelemetry.sdk.metrics.internal.data.ImmutableMetricData;
import io.opentelemetry.sdk.metrics.internal.data.ImmutableSumData;
Expand All @@ -47,6 +52,50 @@ void testSetRetryPolicyOnDelegate() {
.doesNotThrowAnyException();
}

/** Test configuration specific to metric exporter. */
@Test
void validMetricConfig() {
assertThatCode(
() ->
OtlpGrpcMetricExporter.builder()
.setAggregationTemporalitySelector(
AggregationTemporalitySelector.deltaPreferred()))
.doesNotThrowAnyException();
assertThat(
OtlpGrpcMetricExporter.builder()
.setAggregationTemporalitySelector(AggregationTemporalitySelector.deltaPreferred())
.build()
.getAggregationTemporality(InstrumentType.COUNTER))
.isEqualTo(AggregationTemporality.DELTA);
assertThat(
OtlpGrpcMetricExporter.builder()
.build()
.getAggregationTemporality(InstrumentType.COUNTER))
.isEqualTo(AggregationTemporality.CUMULATIVE);

assertThat(
OtlpGrpcMetricExporter.builder()
.setDefaultAggregationSelector(
DefaultAggregationSelector.getDefault()
.with(InstrumentType.HISTOGRAM, Aggregation.drop()))
.build()
.getDefaultAggregation(InstrumentType.HISTOGRAM))
.isEqualTo(Aggregation.drop());
}

/** Test configuration specific to metric exporter. */
@Test
void invalidMetricConfig() {
assertThatThrownBy(
() -> OtlpGrpcMetricExporter.builder().setAggregationTemporalitySelector(null))
.isInstanceOf(NullPointerException.class)
.hasMessage("aggregationTemporalitySelector");

assertThatThrownBy(() -> OtlpGrpcMetricExporter.builder().setDefaultAggregationSelector(null))
.isInstanceOf(NullPointerException.class)
.hasMessage("defaultAggregationSelector");
}

@Test
void usingOkHttp() throws Exception {
try (Closeable exporter = OtlpGrpcMetricExporter.builder().build()) {
Expand Down
Loading

0 comments on commit d2a8304

Please sign in to comment.