Skip to content

Commit 63a318a

Browse files
committed
map default enabled
1 parent d4fd64a commit 63a318a

File tree

5 files changed

+91
-27
lines changed

5 files changed

+91
-27
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ dependencies {
8585
testLibrary("org.springframework.boot:spring-boot-starter-test:$springBootVersion") {
8686
exclude("org.junit.vintage", "junit-vintage-engine")
8787
}
88+
8889
testImplementation("javax.servlet:javax.servlet-api:3.1.0")
8990
testImplementation("jakarta.servlet:jakarta.servlet-api:5.0.0")
9091
testRuntimeOnly("com.h2database:h2:1.4.197")
@@ -186,6 +187,7 @@ testing {
186187
implementation("org.springframework.boot:spring-boot-starter-test:$springBootVersion") {
187188
exclude("org.junit.vintage", "junit-vintage-engine")
188189
}
190+
implementation("org.springframework.boot:spring-boot-starter-web:$springBootVersion")
189191
}
190192
}
191193
}

instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/EarlyConfig.java

Lines changed: 53 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55

66
package io.opentelemetry.instrumentation.spring.autoconfigure.internal;
77

8+
import io.opentelemetry.sdk.autoconfigure.spi.ConfigurationException;
89
import java.util.HashMap;
910
import java.util.Map;
11+
import javax.annotation.Nullable;
1012
import org.springframework.core.env.Environment;
1113

1214
/**
@@ -22,55 +24,83 @@ private EarlyConfig() {}
2224
DECLARATIVE_CONFIG_PROPERTY_MAPPING.put("otel.sdk.disabled", "otel.disabled");
2325
DECLARATIVE_CONFIG_PROPERTY_MAPPING.put(
2426
"otel.instrumentation.%s.enabled", "otel.instrumentation/development.java.%s.enabled");
25-
DECLARATIVE_CONFIG_PROPERTY_MAPPING.put(
26-
"otel.instrumentation.common.default-enabled",
27-
"otel.instrumentation/development.java.common.default.enabled");
2827
}
2928

3029
public static boolean otelEnabled(Environment environment) {
3130
return !environment.getProperty(
32-
translatePropertyName(environment, "otel.sdk.disabled"), Boolean.class, false);
31+
translatePropertyName(environment, "otel.sdk.disabled", null), Boolean.class, false);
3332
}
3433

35-
public static Boolean isDeclarativeConfig(Environment environment) {
34+
public static boolean isDeclarativeConfig(Environment environment) {
3635
return environment.getProperty("otel.file_format", String.class) != null;
3736
}
3837

39-
public static String translatePropertyName(Environment environment, String name) {
38+
public static boolean isDefaultEnabled(Environment environment) {
4039
if (isDeclarativeConfig(environment)) {
41-
String value = DECLARATIVE_CONFIG_PROPERTY_MAPPING.get(name);
42-
if (value != null) {
43-
return value;
44-
}
45-
if (name.startsWith("otel.instrumentation.")) {
46-
return String.format(
47-
"otel.instrumentation/development.java.%s",
48-
name.substring("otel.instrumentation.".length()));
40+
String mode =
41+
environment.getProperty(
42+
"otel.instrumentation/development.java.spring_starter.instrumentation_mode",
43+
String.class,
44+
"default");
45+
46+
switch (mode) {
47+
case "none":
48+
return false;
49+
case "default":
50+
return true;
51+
default:
52+
throw new ConfigurationException("Unknown instrumentation mode: " + mode);
4953
}
54+
} else {
55+
return environment.getProperty(
56+
"otel.instrumentation.common.default-enabled", Boolean.class, true);
57+
}
58+
}
5059

51-
throw new IllegalStateException(
52-
"No mapping found for property name: " + name + ". Please report this bug.");
60+
public static String translatePropertyName(
61+
Environment environment, String name, @Nullable String arg) {
62+
if (isDeclarativeConfig(environment)) {
63+
String key = declarativeConfigKey(name);
64+
if (arg != null) {
65+
key = String.format(key, arg);
66+
}
67+
return key.replace('-', '_');
5368
} else {
54-
return name;
69+
String key = name;
70+
if (arg != null) {
71+
key = String.format(key, arg);
72+
}
73+
return key;
5574
}
5675
}
5776

77+
private static String declarativeConfigKey(String name) {
78+
String value = DECLARATIVE_CONFIG_PROPERTY_MAPPING.get(name);
79+
if (value != null) {
80+
return value;
81+
}
82+
if (name.startsWith("otel.instrumentation.")) {
83+
return String.format(
84+
"otel.instrumentation/development.java.%s",
85+
name.substring("otel.instrumentation.".length()));
86+
}
87+
88+
throw new IllegalStateException(
89+
"No mapping found for property name: " + name + ". Please report this bug.");
90+
}
91+
5892
public static boolean isInstrumentationEnabled(
5993
Environment environment, String name, boolean defaultValue) {
6094
Boolean explicit =
6195
environment.getProperty(
62-
String.format(
63-
translatePropertyName(environment, "otel.instrumentation.%s.enabled"), name),
96+
translatePropertyName(environment, "otel.instrumentation.%s.enabled", name),
6497
Boolean.class);
6598
if (explicit != null) {
6699
return explicit;
67100
}
68101
if (!defaultValue) {
69102
return false;
70103
}
71-
return environment.getProperty(
72-
translatePropertyName(environment, "otel.instrumentation.common.default-enabled"),
73-
Boolean.class,
74-
true);
104+
return isDefaultEnabled(environment);
75105
}
76106
}

instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/instrumentation/logging/LogbackAppenderInstaller.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ private static Boolean evaluateBooleanProperty(
221221
ApplicationEnvironmentPreparedEvent applicationEnvironmentPreparedEvent, String property) {
222222
ConfigurableEnvironment environment = applicationEnvironmentPreparedEvent.getEnvironment();
223223
return environment.getProperty(
224-
EarlyConfig.translatePropertyName(environment, property), Boolean.class);
224+
EarlyConfig.translatePropertyName(environment, property, null), Boolean.class);
225225
}
226226

227227
private static <T> Optional<T> findAppender(Class<T> appenderClass) {

instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/OpenTelemetryAutoConfigurationTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
/** Spring Boot auto configuration test for {@link OpenTelemetryAutoConfiguration}. */
2727
class OpenTelemetryAutoConfigurationTest {
2828
@TestConfiguration
29-
static class CustomTracerConfiguration {
29+
static class CustomOtelConfiguration {
3030
@Bean
3131
public OpenTelemetry customOpenTelemetry() {
3232
return OpenTelemetry.noop();
@@ -48,7 +48,7 @@ public OpenTelemetry customOpenTelemetry() {
4848
"when Application Context contains OpenTelemetry bean should NOT initialize openTelemetry")
4949
void customOpenTelemetry() {
5050
this.contextRunner
51-
.withUserConfiguration(CustomTracerConfiguration.class)
51+
.withUserConfiguration(CustomOtelConfiguration.class)
5252
.withConfiguration(AutoConfigurations.of(OpenTelemetryAutoConfiguration.class))
5353
.run(
5454
context ->

instrumentation/spring/spring-boot-autoconfigure/src/testDeclarativeConfig/java/io/opentelemetry/instrumentation/spring/autoconfigure/DeclarativeConfigTest.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import io.opentelemetry.api.OpenTelemetry;
1111
import io.opentelemetry.instrumentation.api.incubator.config.internal.InstrumentationConfig;
12+
import io.opentelemetry.instrumentation.spring.autoconfigure.internal.instrumentation.web.SpringWebInstrumentationAutoConfiguration;
1213
import io.opentelemetry.sdk.OpenTelemetrySdk;
1314
import org.junit.jupiter.api.DisplayName;
1415
import org.junit.jupiter.api.Test;
@@ -40,6 +41,7 @@ public OpenTelemetry customOpenTelemetry() {
4041
void customOpenTelemetry() {
4142
this.contextRunner
4243
.withUserConfiguration(CustomTracerConfiguration.class)
44+
.withPropertyValues("otel.file_format=1.0-rc.1")
4345
.run(
4446
context ->
4547
assertThat(context)
@@ -72,7 +74,7 @@ void initializeProvidersAndOpenTelemetry() {
7274
@Test
7375
void shouldInitializeSdkWhenNotDisabled() {
7476
this.contextRunner
75-
.withPropertyValues("otel.disabled=false")
77+
.withPropertyValues("otel.file_format=1.0-rc.1", "otel.disabled=false")
7678
.run(
7779
context ->
7880
assertThat(context).getBean("openTelemetry").isInstanceOf(OpenTelemetrySdk.class));
@@ -82,10 +84,40 @@ void shouldInitializeSdkWhenNotDisabled() {
8284
void shouldInitializeNoopOpenTelemetryWhenSdkIsDisabled() {
8385
this.contextRunner
8486
.withPropertyValues(
87+
"otel.file_format=1.0-rc.1",
8588
"otel.disabled=true",
8689
"otel.resource.attributes=service.name=workflow-backend-dev,service.version=3c8f9ce9")
8790
.run(
8891
context ->
8992
assertThat(context).getBean("openTelemetry").isEqualTo(OpenTelemetry.noop()));
9093
}
94+
95+
@Test
96+
void shouldLoadInstrumentation() {
97+
this.contextRunner
98+
.withConfiguration(AutoConfigurations.of(SpringWebInstrumentationAutoConfiguration.class))
99+
.withPropertyValues("otel.file_format=1.0-rc.1")
100+
.run(context -> assertThat(context).hasBean("otelRestTemplateBeanPostProcessor"));
101+
}
102+
103+
@Test
104+
void shouldNotLoadInstrumentationWhenDefaultIsDisabled() {
105+
this.contextRunner
106+
.withConfiguration(AutoConfigurations.of(SpringWebInstrumentationAutoConfiguration.class))
107+
.withPropertyValues(
108+
"otel.file_format=1.0-rc.1",
109+
"otel.instrumentation/development.java.spring_starter.instrumentation_mode=none")
110+
.run(context -> assertThat(context).doesNotHaveBean("otelRestTemplateBeanPostProcessor"));
111+
}
112+
113+
@Test
114+
void shouldLoadInstrumentationWhenExplicitlyEnabled() {
115+
this.contextRunner
116+
.withConfiguration(AutoConfigurations.of(SpringWebInstrumentationAutoConfiguration.class))
117+
.withPropertyValues(
118+
"otel.file_format=1.0-rc.1",
119+
"otel.instrumentation/development.java.spring_starter.instrumentation_mode=none",
120+
"otel.instrumentation/development.java.spring_web.enabled=true")
121+
.run(context -> assertThat(context).hasBean("otelRestTemplateBeanPostProcessor"));
122+
}
91123
}

0 commit comments

Comments
 (0)