Skip to content

Commit f330e4c

Browse files
authored
Fix logback MDC declarative config keys (#18924)
1 parent cbdf37d commit f330e4c

2 files changed

Lines changed: 94 additions & 35 deletions

File tree

  • instrumentation/spring/spring-boot-autoconfigure/src

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

Lines changed: 49 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import io.opentelemetry.instrumentation.spring.autoconfigure.internal.EarlyConfig;
1414
import java.util.Iterator;
1515
import java.util.Optional;
16+
import javax.annotation.Nullable;
1617
import org.slf4j.ILoggerFactory;
1718
import org.slf4j.Logger;
1819
import org.slf4j.LoggerFactory;
@@ -168,11 +169,9 @@ private static void initializeOpenTelemetryAppenderFromProperties(
168169
}
169170

170171
String mdcAttributeProperty =
171-
applicationEnvironmentPreparedEvent
172-
.getEnvironment()
173-
.getProperty(
174-
"otel.instrumentation.logback-appender.experimental.capture-mdc-attributes",
175-
String.class);
172+
getLoggingProperty(
173+
applicationEnvironmentPreparedEvent.getEnvironment(),
174+
"otel.instrumentation.logback-appender.experimental.capture-mdc-attributes");
176175
if (mdcAttributeProperty != null) {
177176
openTelemetryAppender.setCaptureMdcAttributes(mdcAttributeProperty);
178177
}
@@ -217,48 +216,77 @@ private static void initializeMdcAppenderFromProperties(
217216
}
218217

219218
String traceIdKey =
220-
applicationEnvironmentPreparedEvent
221-
.getEnvironment()
222-
.getProperty("otel.instrumentation.common.logging.trace-id", String.class);
219+
getLoggingProperty(
220+
applicationEnvironmentPreparedEvent.getEnvironment(),
221+
"otel.instrumentation.common.logging.trace-id");
223222
if (traceIdKey != null) {
224223
openTelemetryAppender.setTraceIdKey(traceIdKey);
225224
}
226225

227226
String spanIdKey =
228-
applicationEnvironmentPreparedEvent
229-
.getEnvironment()
230-
.getProperty("otel.instrumentation.common.logging.span-id", String.class);
227+
getLoggingProperty(
228+
applicationEnvironmentPreparedEvent.getEnvironment(),
229+
"otel.instrumentation.common.logging.span-id");
231230
if (spanIdKey != null) {
232231
openTelemetryAppender.setSpanIdKey(spanIdKey);
233232
}
234233

235234
String traceFlagsKey =
236-
applicationEnvironmentPreparedEvent
237-
.getEnvironment()
238-
.getProperty("otel.instrumentation.common.logging.trace-flags", String.class);
235+
getLoggingProperty(
236+
applicationEnvironmentPreparedEvent.getEnvironment(),
237+
"otel.instrumentation.common.logging.trace-flags");
239238
if (traceFlagsKey != null) {
240239
openTelemetryAppender.setTraceFlagsKey(traceFlagsKey);
241240
}
242241
}
243242

243+
@Nullable
244+
private static String getLoggingProperty(ConfigurableEnvironment environment, String property) {
245+
return environment.getProperty(getEnvironmentPropertyName(environment, property), String.class);
246+
}
247+
244248
/** Evaluates a boolean property, taking into account whether declarative config is in use. */
249+
@Nullable
245250
private static Boolean evaluateBooleanProperty(
246251
ApplicationEnvironmentPreparedEvent applicationEnvironmentPreparedEvent, String property) {
247252
ConfigurableEnvironment environment = applicationEnvironmentPreparedEvent.getEnvironment();
248-
String key = property;
253+
return environment.getProperty(
254+
getEnvironmentPropertyName(environment, property), Boolean.class);
255+
}
256+
257+
private static String getEnvironmentPropertyName(
258+
ConfigurableEnvironment environment, String property) {
249259
if (EarlyConfig.isDeclarativeConfig(environment)) {
250260
if (property.startsWith("otel.instrumentation.")) {
251-
key =
252-
String.format(
253-
"otel.instrumentation/development.java.%s",
254-
property.substring("otel.instrumentation.".length()))
255-
.replace('-', '_');
261+
return "otel.instrumentation/development.java."
262+
+ toDeclarativeInstrumentationPropertyName(
263+
property.substring("otel.instrumentation.".length()));
256264
} else {
257265
throw new IllegalStateException(
258266
"No mapping found for property name: " + property + ". Please report this bug.");
259267
}
260268
}
261-
return environment.getProperty(key, Boolean.class);
269+
return property;
270+
}
271+
272+
private static String toDeclarativeInstrumentationPropertyName(String instrumentationProperty) {
273+
StringBuilder declarativeProperty = new StringBuilder();
274+
boolean nextSegmentIsDevelopment = false;
275+
for (String segment : instrumentationProperty.split("\\.")) {
276+
if (segment.equals("experimental")) {
277+
nextSegmentIsDevelopment = true;
278+
continue;
279+
}
280+
if (declarativeProperty.length() > 0) {
281+
declarativeProperty.append('.');
282+
}
283+
declarativeProperty.append(segment.replace('-', '_'));
284+
if (nextSegmentIsDevelopment || segment.startsWith("experimental-")) {
285+
declarativeProperty.append("/development");
286+
nextSegmentIsDevelopment = false;
287+
}
288+
}
289+
return declarativeProperty.toString();
262290
}
263291

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

instrumentation/spring/spring-boot-autoconfigure/src/testLogbackAppender/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/instrumentation/logging/LogbackAppenderTest.java

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.junit.jupiter.api.Test;
3232
import org.junit.jupiter.api.extension.RegisterExtension;
3333
import org.junit.jupiter.params.ParameterizedTest;
34+
import org.junit.jupiter.params.provider.CsvSource;
3435
import org.junit.jupiter.params.provider.ValueSource;
3536
import org.slf4j.ILoggerFactory;
3637
import org.slf4j.Logger;
@@ -69,15 +70,33 @@ OpenTelemetry openTelemetry() {
6970
}
7071

7172
@ParameterizedTest
72-
@ValueSource(strings = {"logback-test.xml", "logback-test-no-mdc.xml"})
73-
void shouldInitializeAppender(String configurationFile) {
73+
@CsvSource({
74+
"logback-test.xml, false",
75+
"logback-test-no-mdc.xml, false",
76+
"logback-test.xml, true",
77+
"logback-test-no-mdc.xml, true"
78+
})
79+
void shouldInitializeAppender(String configurationFile, boolean declarativeConfig) {
7480
Map<String, Object> properties = new HashMap<>();
7581
properties.put("logging.config", "classpath:" + configurationFile);
76-
properties.put(
77-
"otel.instrumentation.logback-appender.experimental.capture-mdc-attributes", "*");
78-
properties.put(
79-
"otel.instrumentation.logback-appender.experimental.capture-code-attributes", false);
80-
properties.put("otel.instrumentation.logback-appender.experimental.capture-template", true);
82+
if (declarativeConfig) {
83+
properties.put("otel.file_format", "1.0");
84+
properties.put(
85+
"otel.instrumentation/development.java.logback_appender.capture_mdc_attributes/development",
86+
"*");
87+
properties.put(
88+
"otel.instrumentation/development.java.logback_appender.capture_code_attributes/development",
89+
false);
90+
properties.put(
91+
"otel.instrumentation/development.java.logback_appender.capture_template/development",
92+
true);
93+
} else {
94+
properties.put(
95+
"otel.instrumentation.logback-appender.experimental.capture-mdc-attributes", "*");
96+
properties.put(
97+
"otel.instrumentation.logback-appender.experimental.capture-code-attributes", false);
98+
properties.put("otel.instrumentation.logback-appender.experimental.capture-template", true);
99+
}
81100

82101
SpringApplication app =
83102
new SpringApplication(
@@ -149,15 +168,27 @@ void shouldNotInitializeAppenderWhenDisabled() {
149168
assertThat(testing.logRecords()).isEmpty();
150169
}
151170

152-
@Test
153-
void mdcAppender() {
171+
@ParameterizedTest
172+
@ValueSource(booleans = {false, true})
173+
void mdcAppender(boolean declarativeConfig) {
154174
Map<String, Object> properties = new HashMap<>();
155175
properties.put("logging.config", "classpath:logback-test.xml");
156-
properties.put("otel.instrumentation.logback-appender.enabled", "false");
157-
properties.put("otel.instrumentation.logback-mdc.add-baggage", "true");
158-
properties.put("otel.instrumentation.common.logging.trace-id", "traceid");
159-
properties.put("otel.instrumentation.common.logging.span-id", "spanid");
160-
properties.put("otel.instrumentation.common.logging.trace-flags", "traceflags");
176+
if (declarativeConfig) {
177+
properties.put("otel.file_format", "1.0");
178+
properties.put(
179+
"otel.distribution.spring_starter.instrumentation.disabled[0]", "logback_appender");
180+
properties.put("otel.instrumentation/development.java.logback_mdc.add_baggage", "true");
181+
properties.put("otel.instrumentation/development.java.common.logging.trace_id", "traceid");
182+
properties.put("otel.instrumentation/development.java.common.logging.span_id", "spanid");
183+
properties.put(
184+
"otel.instrumentation/development.java.common.logging.trace_flags", "traceflags");
185+
} else {
186+
properties.put("otel.instrumentation.logback-appender.enabled", "false");
187+
properties.put("otel.instrumentation.logback-mdc.add-baggage", "true");
188+
properties.put("otel.instrumentation.common.logging.trace-id", "traceid");
189+
properties.put("otel.instrumentation.common.logging.span-id", "spanid");
190+
properties.put("otel.instrumentation.common.logging.trace-flags", "traceflags");
191+
}
161192

162193
SpringApplication app =
163194
new SpringApplication(

0 commit comments

Comments
 (0)