|
| 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 | +} |
0 commit comments