Skip to content

Commit

Permalink
ConfigurationReader handles null values as empty (#5829)
Browse files Browse the repository at this point in the history
  • Loading branch information
jack-berg authored Sep 15, 2023
1 parent 96c8958 commit 5d50936
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 6 deletions.
2 changes: 1 addition & 1 deletion sdk-extensions/incubator/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ dependencies {
// ... proceed with normal sourcesJar, compileJava, etc

// TODO(jack-berg): update ref to be released version when available
val configurationRef = "0eb96de17c6533f668163873d95bd026bce1d8fb"
val configurationRef = "0508846f82ed54b230fa638e1e7556c52efee25e"
val configurationRepoZip = "https://github.com/open-telemetry/opentelemetry-configuration/archive/$configurationRef.zip"
val buildDirectory = layout.buildDirectory.asFile.get()

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

package io.opentelemetry.sdk.extension.incubator.fileconfig;

import com.fasterxml.jackson.annotation.JsonSetter;
import com.fasterxml.jackson.annotation.Nulls;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.OpenTelemetryConfiguration;
import java.io.InputStream;
Expand All @@ -13,7 +15,10 @@

final class ConfigurationReader {

private static final ObjectMapper MAPPER = new ObjectMapper();
private static final ObjectMapper MAPPER =
new ObjectMapper()
// Create empty object instances for keys which are present but have null values
.setDefaultSetterInfo(JsonSetter.Value.forValueNulls(Nulls.AS_EMPTY));

private ConfigurationReader() {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
import io.opentelemetry.sdk.autoconfigure.internal.SpiHelper;
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.Aggregation;
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.Base2ExponentialBucketHistogram;
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.Drop;
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.ExplicitBucketHistogram;
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.LastValue;
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.Sum;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -45,13 +48,12 @@ private static Stream<Arguments> createTestCases() {
Arguments.of(
new Aggregation(), io.opentelemetry.sdk.metrics.Aggregation.defaultAggregation()),
Arguments.of(
new Aggregation().withDrop(new Object()),
new Aggregation().withDrop(new Drop()),
io.opentelemetry.sdk.metrics.Aggregation.drop()),
Arguments.of(
new Aggregation().withSum(new Object()),
io.opentelemetry.sdk.metrics.Aggregation.sum()),
new Aggregation().withSum(new Sum()), io.opentelemetry.sdk.metrics.Aggregation.sum()),
Arguments.of(
new Aggregation().withLastValue(new Object()),
new Aggregation().withLastValue(new LastValue()),
io.opentelemetry.sdk.metrics.Aggregation.lastValue()),
Arguments.of(
new Aggregation()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.TracerProvider;
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.View;
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.Zipkin;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -276,4 +278,60 @@ void read_KitchenSinkExampleFile() throws IOException {
assertThat(config).isEqualTo(expected);
}
}

@Test
void nullValuesParsedToEmptyObjects() {
String objectPlaceholderString =
"file_format: \"0.1\"\n"
+ "tracer_provider:\n"
+ " processors:\n"
+ " - batch:\n"
+ " exporter:\n"
+ " console: {}\n"
+ "meter_provider:\n"
+ " views:\n"
+ " - selector:\n"
+ " instrument_type: histogram\n"
+ " stream:\n"
+ " aggregation:\n"
+ " drop: {}\n";
OpenTelemetryConfiguration objectPlaceholderModel =
ConfigurationReader.parse(
new ByteArrayInputStream(objectPlaceholderString.getBytes(StandardCharsets.UTF_8)));

String noOjbectPlaceholderString =
"file_format: \"0.1\"\n"
+ "tracer_provider:\n"
+ " processors:\n"
+ " - batch:\n"
+ " exporter:\n"
+ " console:\n"
+ "meter_provider:\n"
+ " views:\n"
+ " - selector:\n"
+ " instrument_type: histogram\n"
+ " stream:\n"
+ " aggregation:\n"
+ " drop:\n";
OpenTelemetryConfiguration noObjectPlaceholderModel =
ConfigurationReader.parse(
new ByteArrayInputStream(noOjbectPlaceholderString.getBytes(StandardCharsets.UTF_8)));

SpanExporter exporter =
noObjectPlaceholderModel
.getTracerProvider()
.getProcessors()
.get(0)
.getBatch()
.getExporter();
assertThat(exporter.getConsole()).isNotNull();
assertThat(exporter.getOtlp()).isNull();

Aggregation aggregation =
noObjectPlaceholderModel.getMeterProvider().getViews().get(0).getStream().getAggregation();
assertThat(aggregation.getDrop()).isNotNull();
assertThat(aggregation.getSum()).isNull();

assertThat(objectPlaceholderModel).isEqualTo(noObjectPlaceholderModel);
}
}

0 comments on commit 5d50936

Please sign in to comment.