mirror of
https://github.com/spring-projects/spring-boot.git
synced 2026-09-17 12:09:16 +00:00
Polish "Allow configuring Micrometer Tracing MDC keys"
Derive logging.pattern.correlation from the configured MDC keys so that log correlation keeps working when the keys are customized, instead of silently rendering a blank correlation field. Reject empty MDC keys and only clear Brave's default correlation fields when the keys have been customized, so that applications using the defaults are unaffected if Brave adds a default field. Replace the tests that asserted on bean wiring with integration tests covering the MDC contents, plus a smoke test for the log output. See gh-50595
This commit is contained in:
@@ -26,6 +26,7 @@ dependencies {
|
||||
implementation(project(":starter:spring-boot-starter-opentelemetry"))
|
||||
|
||||
testImplementation(project(":starter:spring-boot-starter-test"))
|
||||
testImplementation(project(":test-support:spring-boot-test-support"))
|
||||
testImplementation("io.micrometer:micrometer-registry-otlp")
|
||||
|
||||
dockerTestImplementation(project(":core:spring-boot-testcontainers"))
|
||||
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* Copyright 2012-present the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package smoketest.opentelemetry;
|
||||
|
||||
import io.micrometer.tracing.Span;
|
||||
import io.micrometer.tracing.Tracer;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.system.CapturedOutput;
|
||||
import org.springframework.boot.test.system.OutputCaptureExtension;
|
||||
import org.springframework.boot.testsupport.classpath.ForkedClassPath;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests that the correlation ID in the log output follows the MDC keys configured through
|
||||
* {@code management.tracing.mdc.*}.
|
||||
* <p>
|
||||
* Runs with a forked class path because {@code LOG_CORRELATION_PATTERN} is a JVM-wide
|
||||
* system property that is only ever written once, so a context started by another test
|
||||
* class would pin it to the default pattern.
|
||||
*
|
||||
* @author Moritz Halbritter
|
||||
*/
|
||||
@SpringBootTest(properties = { "management.tracing.mdc.trace-id-key=customTraceId",
|
||||
"management.tracing.mdc.span-id-key=customSpanId" })
|
||||
@ExtendWith(OutputCaptureExtension.class)
|
||||
@ForkedClassPath
|
||||
class LogCorrelationTests {
|
||||
|
||||
private static final Log logger = LogFactory.getLog(LogCorrelationTests.class);
|
||||
|
||||
@Autowired
|
||||
private Tracer tracer;
|
||||
|
||||
@Test
|
||||
void shouldUseCustomMdcKeysForCorrelationId(CapturedOutput output) {
|
||||
Span span = this.tracer.nextSpan().name("test");
|
||||
try (Tracer.SpanInScope scope = this.tracer.withSpan(span.start())) {
|
||||
logger.info("Hello from a span");
|
||||
}
|
||||
finally {
|
||||
span.end();
|
||||
}
|
||||
assertThat(output).containsPattern(
|
||||
"\\[%s-%s\\].*Hello from a span".formatted(span.context().traceId(), span.context().spanId()));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user