Polish 'Log correlation IDs when Micrometer tracing is being used'

See gh-36158
This commit is contained in:
Johnny Lim 2023-07-02 19:07:40 +09:00 committed by Phillip Webb
parent 77245c3bd0
commit 7c77e1bb85
7 changed files with 18 additions and 18 deletions

View File

@ -26,7 +26,7 @@ import org.springframework.util.ClassUtils;
/**
* {@link EnvironmentPostProcessor} to add a {@link PropertySource} to support log
* correlation IDs when Micrometer is present. Adds support for the
* correlation IDs when Micrometer Tracing is present. Adds support for the
* {@value LoggingSystem#EXPECT_CORRELATION_ID_PROPERTY} property by delegating to
* {@code management.tracing.enabled}.
*

View File

@ -42,7 +42,7 @@ class LogCorrelationEnvironmentPostProcessorTests {
private final LogCorrelationEnvironmentPostProcessor postProcessor = new LogCorrelationEnvironmentPostProcessor();
@Test
void getExpectCorrelationIdPropertyWhenMicrometerPresentReturnsTrue() {
void getExpectCorrelationIdPropertyWhenMicrometerTracingPresentReturnsTrue() {
this.postProcessor.postProcessEnvironment(this.environment, this.application);
assertThat(this.environment.getProperty(LoggingSystem.EXPECT_CORRELATION_ID_PROPERTY, Boolean.class, false))
.isTrue();
@ -50,7 +50,7 @@ class LogCorrelationEnvironmentPostProcessorTests {
@Test
@ClassPathExclusions("micrometer-tracing-*.jar")
void getExpectCorrelationIdPropertyWhenMicrometerMissingReturnsFalse() {
void getExpectCorrelationIdPropertyWhenMicrometerTracingMissingReturnsFalse() {
this.postProcessor.postProcessEnvironment(this.environment, this.application);
assertThat(this.environment.getProperty(LoggingSystem.EXPECT_CORRELATION_ID_PROPERTY, Boolean.class, false))
.isFalse();

View File

@ -18,7 +18,6 @@ package org.springframework.boot.logging;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
@ -36,7 +35,7 @@ import org.springframework.util.StringUtils;
/**
* Utility class that can be used to format a correlation identifier for logging based on
* <a href=
* "https://www.w3.org/TR/trace-context/#examples-of-http-traceparent-headers">w3c</a>
* "https://www.w3.org/TR/trace-context/#examples-of-http-traceparent-headers">W3C</a>
* recommendations.
* <p>
* The formatter can be configured with a comma-separated list of names and the expected
@ -46,8 +45,8 @@ import org.springframework.util.StringUtils;
* {@code 16} respectively.
* <p>
* Correlation IDs are formatted as dash separated strings surrounded in square brackets.
* Formatted output is always of a fixed width and with trailing whitespace. Dashes are
* omitted if none of the named items can be resolved.
* Formatted output is always of a fixed width and with trailing space. Dashes are omitted
* if none of the named items can be resolved.
* <p>
* The following example would return a formatted result of
* {@code "[01234567890123456789012345678901-0123456789012345] "}: <pre class="code">
@ -101,10 +100,12 @@ public final class CorrelationIdFormatter {
Predicate<Part> canResolve = (part) -> StringUtils.hasLength(resolver.apply(part.name()));
try {
if (this.parts.stream().anyMatch(canResolve)) {
appendable.append("[");
appendable.append('[');
for (Iterator<Part> iterator = this.parts.iterator(); iterator.hasNext();) {
appendable.append(iterator.next().resolve(resolver));
appendable.append((!iterator.hasNext()) ? "" : "-");
if (iterator.hasNext()) {
appendable.append('-');
}
}
appendable.append("] ");
}
@ -124,7 +125,7 @@ public final class CorrelationIdFormatter {
/**
* Create a new {@link CorrelationIdFormatter} instance from the given specification.
* @param spec a comma separated specification
* @param spec a comma-separated specification
* @return a new {@link CorrelationIdFormatter} instance
*/
public static CorrelationIdFormatter of(String spec) {
@ -142,7 +143,7 @@ public final class CorrelationIdFormatter {
* @return a new {@link CorrelationIdFormatter} instance
*/
public static CorrelationIdFormatter of(String[] spec) {
return of((spec != null) ? Arrays.asList(spec) : Collections.emptyList());
return of((spec != null) ? List.of(spec) : Collections.emptyList());
}
/**
@ -166,7 +167,7 @@ public final class CorrelationIdFormatter {
*/
record Part(String name, int length) {
private static final Pattern pattern = Pattern.compile("^(.+?)\\((\\d+)\\)?$");
private static final Pattern pattern = Pattern.compile("^(.+?)\\((\\d+)\\)$");
String resolve(Function<String, String> resolver) {
String resolved = resolver.apply(name());
@ -174,7 +175,7 @@ public final class CorrelationIdFormatter {
return blank();
}
int padding = length() - resolved.length();
return resolved + " ".repeat((padding > 0) ? padding : 0);
return (padding <= 0) ? resolved : resolved + " ".repeat(padding);
}
String blank() {

View File

@ -249,8 +249,7 @@ public class Log4J2LoggingSystem extends AbstractLoggingSystem {
@Override
protected void loadDefaults(LoggingInitializationContext initializationContext, LogFile logFile) {
String location = (logFile != null) ? getPackagedConfigFile("log4j2-file.xml")
: getPackagedConfigFile("log4j2.xml");
String location = getPackagedConfigFile((logFile != null) ? "log4j2-file.xml" : "log4j2.xml");
load(initializationContext, location, logFile);
}

View File

@ -96,7 +96,7 @@ class CorrelationIdFormatterTests {
context.put("traceId", "01234567890123456789012345678901");
context.put("spanId", "0123456789012345");
StringBuilder formatted = new StringBuilder();
CorrelationIdFormatter.of("").formatTo(context::get, formatted);
CorrelationIdFormatter.DEFAULT.formatTo(context::get, formatted);
assertThat(formatted).hasToString("[01234567890123456789012345678901-0123456789012345] ");
}

View File

@ -743,7 +743,7 @@ class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
}
@Test
void correlationLoggingToConsoleWhenUsingFileConfiguration() {
void correlationLoggingToFileWhenUsingFileConfiguration() {
this.environment.setProperty(LoggingSystem.EXPECT_CORRELATION_ID_PROPERTY, "true");
File file = new File(tmpDir(), "logback-test.log");
LogFile logFile = getLogFile(file.getPath(), null);

View File

@ -11,7 +11,7 @@ dependencies {
implementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-security"))
implementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-web"))
implementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-validation"))
implementation 'io.micrometer:micrometer-tracing-bridge-brave'
implementation("io.micrometer:micrometer-tracing-bridge-brave")
runtimeOnly("com.h2database:h2")