Skip to content

Commit

Permalink
Add missing fields to OTLP metric exporters (#6402)
Browse files Browse the repository at this point in the history
  • Loading branch information
jack-berg authored Apr 22, 2024
1 parent 1960606 commit a5fc312
Show file tree
Hide file tree
Showing 13 changed files with 211 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
Comparing source compatibility of against
No changes.
*** MODIFIED INTERFACE: PUBLIC ABSTRACT io.opentelemetry.sdk.metrics.export.AggregationTemporalitySelector (not serializable)
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
+++ NEW METHOD: PUBLIC(+) STATIC(+) java.lang.String asString(io.opentelemetry.sdk.metrics.export.AggregationTemporalitySelector)
*** MODIFIED INTERFACE: PUBLIC ABSTRACT io.opentelemetry.sdk.metrics.export.DefaultAggregationSelector (not serializable)
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
+++ NEW METHOD: PUBLIC(+) STATIC(+) java.lang.String asString(io.opentelemetry.sdk.metrics.export.DefaultAggregationSelector)
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import io.opentelemetry.sdk.metrics.export.DefaultAggregationSelector;
import io.opentelemetry.sdk.metrics.export.MetricExporter;
import java.util.Collection;
import java.util.StringJoiner;
import javax.annotation.concurrent.ThreadSafe;

/**
Expand Down Expand Up @@ -124,6 +125,15 @@ public CompletableResultCode shutdown() {

@Override
public String toString() {
return "OtlpHttpMetricExporter{" + builder.toString(false) + "}";
StringJoiner joiner = new StringJoiner(", ", "OtlpHttpMetricExporter{", "}");
joiner.add(builder.toString(false));
joiner.add(
"aggregationTemporalitySelector="
+ AggregationTemporalitySelector.asString(aggregationTemporalitySelector));
joiner.add(
"defaultAggregationSelector="
+ DefaultAggregationSelector.asString(defaultAggregationSelector));
joiner.add("memoryMode=" + memoryMode);
return joiner.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import io.opentelemetry.sdk.metrics.export.DefaultAggregationSelector;
import io.opentelemetry.sdk.metrics.export.MetricExporter;
import java.util.Collection;
import java.util.StringJoiner;
import javax.annotation.concurrent.ThreadSafe;

/**
Expand Down Expand Up @@ -128,6 +129,15 @@ public CompletableResultCode shutdown() {

@Override
public String toString() {
return "OtlpGrpcMetricExporter{" + builder.toString(false) + "}";
StringJoiner joiner = new StringJoiner(", ", "OtlpGrpcMetricExporter{", "}");
joiner.add(builder.toString(false));
joiner.add(
"aggregationTemporalitySelector="
+ AggregationTemporalitySelector.asString(aggregationTemporalitySelector));
joiner.add(
"defaultAggregationSelector="
+ DefaultAggregationSelector.asString(defaultAggregationSelector));
joiner.add("memoryMode=" + memoryMode);
return joiner.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
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.List;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.Test;

class OtlpHttpMetricExporterOkHttpSenderTest
Expand Down Expand Up @@ -77,6 +79,33 @@ void invalidMetricConfig() {
.hasMessage("defaultAggregationSelector");
}

/** Test configuration specific to metric exporter. */
@Test
void stringRepresentation() {
try (MetricExporter metricExporter = OtlpHttpMetricExporter.builder().build()) {
assertThat(metricExporter.toString())
.matches(
"OtlpHttpMetricExporter\\{"
+ "exporterName=otlp, "
+ "type=metric, "
+ "endpoint=http://localhost:4318/v1/metrics, "
+ "timeoutNanos="
+ TimeUnit.SECONDS.toNanos(10)
+ ", "
+ "proxyOptions=null, "
+ "compressorEncoding=null, "
+ "connectTimeoutNanos="
+ TimeUnit.SECONDS.toNanos(10)
+ ", "
+ "exportAsJson=false, "
+ "headers=Headers\\{User-Agent=OBFUSCATED\\}, "
+ "aggregationTemporalitySelector=AggregationTemporalitySelector\\{.*\\}, "
+ "defaultAggregationSelector=DefaultAggregationSelector\\{.*\\}, "
+ "memoryMode=IMMUTABLE_DATA"
+ "\\}");
}
}

@Override
protected TelemetryExporterBuilder<MetricData> exporterBuilder() {
return new HttpMetricExporterBuilderWrapper(OtlpHttpMetricExporter.builder());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@
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.io.Closeable;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.Test;

class OtlpGrpcMetricExporterTest
Expand Down Expand Up @@ -78,6 +80,32 @@ void invalidMetricConfig() {
.hasMessage("defaultAggregationSelector");
}

/** Test configuration specific to metric exporter. */
@Test
void stringRepresentation() {
try (MetricExporter metricExporter = OtlpGrpcMetricExporter.builder().build()) {
assertThat(metricExporter.toString())
.matches(
"OtlpGrpcMetricExporter\\{"
+ "exporterName=otlp, "
+ "type=metric, "
+ "endpoint=http://localhost:4317, "
+ "endpointPath=.*, "
+ "timeoutNanos="
+ TimeUnit.SECONDS.toNanos(10)
+ ", "
+ "connectTimeoutNanos="
+ TimeUnit.SECONDS.toNanos(10)
+ ", "
+ "compressorEncoding=null, "
+ "headers=Headers\\{User-Agent=OBFUSCATED\\}, "
+ "aggregationTemporalitySelector=AggregationTemporalitySelector\\{.*\\}, "
+ "defaultAggregationSelector=DefaultAggregationSelector\\{.*\\}, "
+ "memoryMode=IMMUTABLE_DATA"
+ "\\}");
}
}

@Test
void usingOkHttp() throws Exception {
try (Closeable exporter = OtlpGrpcMetricExporter.builder().build()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
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.List;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.Test;

class OtlpHttpMetricExporterJdkSenderTest
Expand Down Expand Up @@ -78,6 +80,33 @@ void invalidMetricConfig() {
.hasMessage("defaultAggregationSelector");
}

/** Test configuration specific to metric exporter. */
@Test
void stringRepresentation() {
try (MetricExporter metricExporter = OtlpHttpMetricExporter.builder().build()) {
assertThat(metricExporter.toString())
.matches(
"OtlpHttpMetricExporter\\{"
+ "exporterName=otlp, "
+ "type=metric, "
+ "endpoint=http://localhost:4318/v1/metrics, "
+ "timeoutNanos="
+ TimeUnit.SECONDS.toNanos(10)
+ ", "
+ "proxyOptions=null, "
+ "compressorEncoding=null, "
+ "connectTimeoutNanos="
+ TimeUnit.SECONDS.toNanos(10)
+ ", "
+ "exportAsJson=false, "
+ "headers=Headers\\{User-Agent=OBFUSCATED\\}, "
+ "aggregationTemporalitySelector=AggregationTemporalitySelector\\{.*\\}, "
+ "defaultAggregationSelector=DefaultAggregationSelector\\{.*\\}, "
+ "memoryMode=IMMUTABLE_DATA"
+ "\\}");
}
}

@Override
protected boolean hasAuthenticatorSupport() {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -872,7 +872,7 @@ void stringRepresentation() throws IOException, CertificateEncodingException {
+ ", "
+ "compressorEncoding=null, "
+ "headers=Headers\\{User-Agent=OBFUSCATED\\}"
+ ".*" // Maybe additional grpcChannel field
+ ".*" // Maybe additional grpcChannel field, signal specific fields
+ "\\}");
} finally {
telemetryExporter.shutdown();
Expand Down Expand Up @@ -914,7 +914,7 @@ void stringRepresentation() throws IOException, CertificateEncodingException {
+ "compressorEncoding=gzip, "
+ "headers=Headers\\{.*foo=OBFUSCATED.*\\}, "
+ "retryPolicy=RetryPolicy\\{maxAttempts=2, initialBackoff=PT0\\.05S, maxBackoff=PT3S, backoffMultiplier=1\\.3\\}"
+ ".*" // Maybe additional grpcChannel field
+ ".*" // Maybe additional grpcChannel field, signal specific fields
+ "\\}");
} finally {
telemetryExporter.shutdown();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -858,6 +858,7 @@ void stringRepresentation() throws IOException, CertificateEncodingException {
+ ", "
+ "exportAsJson=false, "
+ "headers=Headers\\{User-Agent=OBFUSCATED\\}"
+ ".*" // Maybe additional signal specific fields
+ "\\}");
} finally {
telemetryExporter.shutdown();
Expand Down Expand Up @@ -900,6 +901,7 @@ void stringRepresentation() throws IOException, CertificateEncodingException {
+ "exportAsJson=false, "
+ "headers=Headers\\{.*foo=OBFUSCATED.*\\}, "
+ "retryPolicy=RetryPolicy\\{maxAttempts=2, initialBackoff=PT0\\.05S, maxBackoff=PT3S, backoffMultiplier=1\\.3\\}"
+ ".*" // Maybe additional signal specific fields
+ "\\}");
} finally {
telemetryExporter.shutdown();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.OtlpMetric;
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.OtlpMetric.DefaultHistogramAggregation;
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.Prometheus;
import io.opentelemetry.sdk.metrics.Aggregation;
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.io.Closeable;
import java.io.IOException;
Expand Down Expand Up @@ -118,6 +122,10 @@ void create_OtlpConfigured(@TempDir Path tempDir)
.addHeader("key2", "value2")
.setTimeout(Duration.ofSeconds(15))
.setCompression("gzip")
.setAggregationTemporalitySelector(AggregationTemporalitySelector.deltaPreferred())
.setDefaultAggregationSelector(
DefaultAggregationSelector.getDefault()
.with(InstrumentType.HISTOGRAM, Aggregation.base2ExponentialBucketHistogram()))
.build();
cleanup.addCloseable(expectedExporter);

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

import io.opentelemetry.sdk.metrics.InstrumentType;
import io.opentelemetry.sdk.metrics.data.AggregationTemporality;
import java.util.StringJoiner;

/**
* A functional interface that selects {@link AggregationTemporality} based on {@link
Expand Down Expand Up @@ -76,4 +77,16 @@ static AggregationTemporalitySelector lowMemory() {

/** Return the aggregation temporality for the {@link InstrumentType}. */
AggregationTemporality getAggregationTemporality(InstrumentType instrumentType);

/**
* Returns a string representation of this selector, for using in {@link Object#toString()}
* implementations.
*/
static String asString(AggregationTemporalitySelector selector) {
StringJoiner joiner = new StringJoiner(", ", "AggregationTemporalitySelector{", "}");
for (InstrumentType type : InstrumentType.values()) {
joiner.add(type.name() + "=" + selector.getAggregationTemporality(type).name());
}
return joiner.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

import io.opentelemetry.sdk.metrics.Aggregation;
import io.opentelemetry.sdk.metrics.InstrumentType;
import io.opentelemetry.sdk.metrics.internal.aggregator.AggregationUtil;
import java.util.StringJoiner;

/**
* A functional interface that selects default {@link Aggregation} based on {@link InstrumentType}.
Expand Down Expand Up @@ -58,4 +60,19 @@ default DefaultAggregationSelector with(InstrumentType instrumentType, Aggregati
* <p>The default aggregation is used when an instrument does not match any views.
*/
Aggregation getDefaultAggregation(InstrumentType instrumentType);

/**
* Returns a string representation of this selector, for using in {@link Object#toString()}
* implementations.
*/
static String asString(DefaultAggregationSelector selector) {
StringJoiner joiner = new StringJoiner(", ", "DefaultAggregationSelector{", "}");
for (InstrumentType type : InstrumentType.values()) {
joiner.add(
type.name()
+ "="
+ AggregationUtil.aggregationName(selector.getDefaultAggregation(type)));
}
return joiner.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,32 @@ void lowMemory() {
assertThat(selector.getAggregationTemporality(InstrumentType.OBSERVABLE_UP_DOWN_COUNTER))
.isEqualTo(AggregationTemporality.CUMULATIVE);
}

@Test
void stringRepresentation() {
assertThat(
AggregationTemporalitySelector.asString(
AggregationTemporalitySelector.alwaysCumulative()))
.isEqualTo(
"AggregationTemporalitySelector{"
+ "COUNTER=CUMULATIVE, "
+ "UP_DOWN_COUNTER=CUMULATIVE, "
+ "HISTOGRAM=CUMULATIVE, "
+ "OBSERVABLE_COUNTER=CUMULATIVE, "
+ "OBSERVABLE_UP_DOWN_COUNTER=CUMULATIVE, "
+ "OBSERVABLE_GAUGE=CUMULATIVE"
+ "}");
assertThat(
AggregationTemporalitySelector.asString(
AggregationTemporalitySelector.deltaPreferred()))
.isEqualTo(
"AggregationTemporalitySelector{"
+ "COUNTER=DELTA, "
+ "UP_DOWN_COUNTER=CUMULATIVE, "
+ "HISTOGRAM=DELTA, "
+ "OBSERVABLE_COUNTER=DELTA, "
+ "OBSERVABLE_UP_DOWN_COUNTER=CUMULATIVE, "
+ "OBSERVABLE_GAUGE=DELTA"
+ "}");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,31 @@ void with() {
assertThat(selector2.getDefaultAggregation(InstrumentType.OBSERVABLE_GAUGE))
.isEqualTo(Aggregation.defaultAggregation());
}

@Test
void stringRepresentation() {
assertThat(DefaultAggregationSelector.asString(DefaultAggregationSelector.getDefault()))
.isEqualTo(
"DefaultAggregationSelector{"
+ "COUNTER=default, "
+ "UP_DOWN_COUNTER=default, "
+ "HISTOGRAM=default, "
+ "OBSERVABLE_COUNTER=default, "
+ "OBSERVABLE_UP_DOWN_COUNTER=default, "
+ "OBSERVABLE_GAUGE=default"
+ "}");
assertThat(
DefaultAggregationSelector.asString(
DefaultAggregationSelector.getDefault()
.with(InstrumentType.HISTOGRAM, Aggregation.base2ExponentialBucketHistogram())))
.isEqualTo(
"DefaultAggregationSelector{"
+ "COUNTER=default, "
+ "UP_DOWN_COUNTER=default, "
+ "HISTOGRAM=base2_exponential_bucket_histogram, "
+ "OBSERVABLE_COUNTER=default, "
+ "OBSERVABLE_UP_DOWN_COUNTER=default, "
+ "OBSERVABLE_GAUGE=default"
+ "}");
}
}

0 comments on commit a5fc312

Please sign in to comment.