Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
izeye committed Jul 2, 2023
1 parent 1bf334a commit cadab4c
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

/**
* {@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}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ 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();
}

@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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
Expand All @@ -36,7 +35,7 @@
/**
* 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
Expand All @@ -46,8 +45,8 @@
* {@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">
Expand Down Expand Up @@ -101,10 +100,12 @@ public void formatTo(Function<String, String> resolver, Appendable appendable) {
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("] ");
}
Expand All @@ -124,7 +125,7 @@ public String toString() {

/**
* 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) {
Expand All @@ -142,7 +143,7 @@ public static CorrelationIdFormatter of(String spec) {
* @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());
}

/**
Expand All @@ -166,15 +167,18 @@ public static CorrelationIdFormatter of(Collection<String> spec) {
*/
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());
if (resolved == null) {
return blank();
}
int padding = length() - resolved.length();
return resolved + " ".repeat((padding > 0) ? padding : 0);
if (padding > 0) {
return resolved + " ".repeat(padding);
}
return resolved;
}

String blank() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,7 @@ public void initialize(LoggingInitializationContext initializationContext, Strin

@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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ void formatToWithDefaultSpec() {
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] ");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -743,7 +743,7 @@ void correlationLoggingToConsoleWhenUsingXmlConfiguration(CapturedOutput output)
}

@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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down

0 comments on commit cadab4c

Please sign in to comment.