Skip to content

Commit a47e8c0

Browse files
committed
add test
1 parent 11ed14e commit a47e8c0

File tree

3 files changed

+116
-0
lines changed

3 files changed

+116
-0
lines changed

instrumentation/spring/spring-boot-autoconfigure/build.gradle.kts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,17 @@ testing {
178178
}
179179
}
180180
}
181+
182+
val testDeclarativeConfig by registering(JvmTestSuite::class) {
183+
dependencies {
184+
implementation(project())
185+
implementation("org.springframework.boot:spring-boot-starter-web:3.2.4")
186+
implementation("io.opentelemetry:opentelemetry-sdk-extension-autoconfigure")
187+
implementation("org.springframework.boot:spring-boot-starter-test:3.2.4") {
188+
exclude("org.junit.vintage", "junit-vintage-engine")
189+
}
190+
}
191+
}
181192
}
182193
}
183194

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.instrumentation.spring.autoconfigure;
7+
8+
import static org.assertj.core.api.Assertions.assertThat;
9+
10+
import io.opentelemetry.api.OpenTelemetry;
11+
import org.junit.jupiter.api.DisplayName;
12+
import org.junit.jupiter.api.Test;
13+
import org.springframework.boot.autoconfigure.AutoConfigurations;
14+
import org.springframework.boot.test.context.TestConfiguration;
15+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
16+
import org.springframework.context.annotation.Bean;
17+
18+
/** Spring Boot auto configuration test for {@link OpenTelemetryAutoConfiguration}. */
19+
class OpenTelemetryAutoConfigurationTest {
20+
@TestConfiguration
21+
static class CustomTracerConfiguration {
22+
@Bean
23+
public OpenTelemetry customOpenTelemetry() {
24+
return OpenTelemetry.noop();
25+
}
26+
}
27+
28+
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner();
29+
30+
@Test
31+
@DisplayName(
32+
"when Application Context contains OpenTelemetry bean should NOT initialize openTelemetry")
33+
void customOpenTelemetry() {
34+
this.contextRunner
35+
.withUserConfiguration(CustomTracerConfiguration.class)
36+
.withConfiguration(AutoConfigurations.of(OpenTelemetryAutoConfiguration.class))
37+
.run(
38+
context ->
39+
assertThat(context)
40+
.hasBean("customOpenTelemetry")
41+
.doesNotHaveBean("openTelemetry")
42+
.hasBean("otelProperties"));
43+
}
44+
45+
@Test
46+
@DisplayName(
47+
"when Application Context DOES NOT contain OpenTelemetry bean should initialize openTelemetry")
48+
void initializeProvidersAndOpenTelemetry() {
49+
this.contextRunner
50+
.withConfiguration(AutoConfigurations.of(OpenTelemetryAutoConfiguration.class))
51+
.run(context -> assertThat(context).hasBean("openTelemetry").hasBean("otelProperties"));
52+
}
53+
54+
@Test
55+
void shouldInitializeSdkWhenNotDisabled() {
56+
this.contextRunner
57+
.withConfiguration(AutoConfigurations.of(OpenTelemetryAutoConfiguration.class))
58+
.withPropertyValues("otel.sdk.disabled=false")
59+
.run(
60+
context -> assertThat(context).getBean("openTelemetry").isInstanceOf(OpenTelemetrySdk.class));
61+
}
62+
63+
@Test
64+
void shouldInitializeNoopOpenTelemetryWhenSdkIsDisabled() {
65+
this.contextRunner
66+
.withConfiguration(AutoConfigurations.of(OpenTelemetryAutoConfiguration.class))
67+
.withPropertyValues(
68+
"otel.sdk.disabled=true",
69+
"otel.resource.attributes=service.name=workflow-backend-dev,service.version=3c8f9ce9")
70+
.run(
71+
context ->
72+
assertThat(context).getBean("openTelemetry").isEqualTo(OpenTelemetry.noop()));
73+
}
74+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
otel:
2+
# "file_format" serves as opt-in to the new file format
3+
file_format: "0.4"
4+
tracer_provider:
5+
processors:
6+
- simple:
7+
exporter:
8+
test_bridge:
9+
- simple:
10+
exporter:
11+
console:
12+
13+
logger_provider:
14+
processors:
15+
- simple:
16+
exporter:
17+
test_bridge:
18+
19+
meter_provider:
20+
readers:
21+
- periodic:
22+
# Set really long interval. We'll call forceFlush when we need the metrics
23+
# instead of collecting them periodically.
24+
interval: 1000000
25+
exporter:
26+
test_bridge:
27+
28+
resource:
29+
attributes:
30+
- name: foo
31+
value: bar

0 commit comments

Comments
 (0)