-
Notifications
You must be signed in to change notification settings - Fork 876
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
re-use sdk logic for configuring otlp exporters
- Loading branch information
1 parent
b6b4ce3
commit e24ba9e
Showing
4 changed files
with
96 additions
and
200 deletions.
There are no files selected for viewing
95 changes: 95 additions & 0 deletions
95
...metry/instrumentation/spring/autoconfigure/exporters/otlp/OtlpExporterPropertiesTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.spring.autoconfigure.exporters.otlp; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.entry; | ||
|
||
import io.opentelemetry.instrumentation.spring.autoconfigure.MapConverterTestAutoConfiguration; | ||
import io.opentelemetry.instrumentation.spring.autoconfigure.OpenTelemetryAutoConfiguration; | ||
import io.opentelemetry.instrumentation.spring.autoconfigure.resources.SpringResourceConfigProperties; | ||
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; | ||
import java.util.Arrays; | ||
import java.util.stream.Stream; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
import org.springframework.boot.autoconfigure.AutoConfigurations; | ||
import org.springframework.boot.test.context.assertj.AssertableApplicationContext; | ||
import org.springframework.boot.test.context.runner.ApplicationContextRunner; | ||
import org.springframework.core.env.Environment; | ||
import org.springframework.expression.spel.standard.SpelExpressionParser; | ||
|
||
class OtlpExporterPropertiesTest { | ||
|
||
private static final String[] HEADER_KEYS = { | ||
"otel.exporter.otlp.traces.headers", | ||
"otel.exporter.otlp.metrics.headers", | ||
"otel.exporter.otlp.logs.headers", | ||
"otel.exporter.otlp.headers", | ||
}; | ||
|
||
private final ApplicationContextRunner contextRunner = | ||
new ApplicationContextRunner() | ||
.withConfiguration( | ||
AutoConfigurations.of( | ||
OpenTelemetryAutoConfiguration.class, MapConverterTestAutoConfiguration.class)); | ||
|
||
public static Stream<Arguments> headerKeys() { | ||
return Arrays.stream(HEADER_KEYS).map(Arguments::of); | ||
} | ||
|
||
@Test | ||
@DisplayName("test all property types") | ||
void allTypes() { | ||
this.contextRunner | ||
.withPropertyValues( | ||
"otel.exporter.otlp.enabled=true", | ||
"otel.exporter.otlp.timeout=1s", | ||
"otel.exporter.otlp.compression=gzip") | ||
.run( | ||
context -> { | ||
ConfigProperties config = getConfig(context); | ||
assertThat(config.getString("otel.exporter.otlp.compression")).isEqualTo("gzip"); | ||
assertThat(config.getBoolean("otel.exporter.otlp.enabled")).isTrue(); | ||
assertThat(config.getDuration("otel.exporter.otlp.timeout")) | ||
.isEqualByComparingTo(java.time.Duration.ofSeconds(1)); | ||
}); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("headerKeys") | ||
@DisplayName("should map headers from spring properties") | ||
void mapFlatHeaders(String key) { | ||
this.contextRunner | ||
.withSystemProperties(key + "=a=1,b=2") | ||
.run( | ||
context -> | ||
assertThat(getConfig(context).getMap(key)) | ||
.containsExactly(entry("a", "1"), entry("b", "2"))); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("headerKeys") | ||
@DisplayName("should map headers from spring application.yaml") | ||
void mapObjectHeaders(String key) { | ||
this.contextRunner | ||
.withPropertyValues(key + ".a=1", key + ".b=2") | ||
.run( | ||
context -> | ||
assertThat(getConfig(context).getMap(key)) | ||
.containsExactly(entry("a", "1"), entry("b", "2"))); | ||
} | ||
|
||
private static ConfigProperties getConfig(AssertableApplicationContext context) { | ||
return new SpringResourceConfigProperties( | ||
context.getBean("environment", Environment.class), | ||
new SpelExpressionParser(), | ||
context.getBean(OtlpExporterProperties.class)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters