Skip to content

Commit

Permalink
#1739 log 2 separate MDC fields: "traceparent-trace-id" and "tracepar…
Browse files Browse the repository at this point in the history
…ent-span-id"

Signed-off-by: Thomas Jäckle <thomas.jaeckle@beyonnex.io>
  • Loading branch information
thjaeckle committed Sep 18, 2023
1 parent 4d60f6a commit 707c773
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 24 deletions.
4 changes: 2 additions & 2 deletions connectivity/service/src/main/resources/logback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%date{ISO8601} %-5level [%X{correlation-id}][%X{traceparent}] %logger{20} %X{pekkoSource} - %msg%n</pattern>
<pattern>%date{ISO8601} %-5level [%X{correlation-id}][%X{traceparent-trace-id}] %logger{20} %X{pekkoSource} - %msg%n</pattern>
</encoder>
</appender>

<appender name="STDERR" class="ch.qos.logback.core.ConsoleAppender">
<target>System.err</target>
<encoder>
<pattern>%date{ISO8601} %-5level [%X{correlation-id}][%X{traceparent}] %logger{20} %X{pekkoSource} - %msg%n</pattern>
<pattern>%date{ISO8601} %-5level [%X{correlation-id}][%X{traceparent-trace-id}] %logger{20} %X{pekkoSource} - %msg%n</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERROR</level>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@

import javax.annotation.concurrent.Immutable;

import org.apache.pekko.http.javadsl.model.HttpRequest;
import org.apache.pekko.http.javadsl.model.Query;
import org.apache.pekko.http.javadsl.model.Uri;
import org.apache.pekko.http.javadsl.server.RequestContext;
import org.eclipse.ditto.base.model.auth.AuthorizationContext;
import org.eclipse.ditto.base.model.auth.AuthorizationContextType;
import org.eclipse.ditto.base.model.auth.AuthorizationModelFactory;
Expand All @@ -41,11 +45,6 @@
import org.eclipse.ditto.internal.utils.pekko.logging.ThreadSafeDittoLogger;
import org.eclipse.ditto.utils.jsr305.annotations.AllValuesAreNonnullByDefault;

import org.apache.pekko.http.javadsl.model.HttpRequest;
import org.apache.pekko.http.javadsl.model.Query;
import org.apache.pekko.http.javadsl.model.Uri;
import org.apache.pekko.http.javadsl.server.RequestContext;

/**
* Handles authentication by using a defined header field {@link org.eclipse.ditto.gateway.service.security.HttpHeader#X_DITTO_PRE_AUTH} which proxies in front
* of Ditto may set to inject authenticated subjects into a HTTP request.
Expand Down Expand Up @@ -126,7 +125,10 @@ protected CompletableFuture<AuthenticationResult> tryToAuthenticate(final Reques
final var combinedHeaders = new HashMap<>(dittoHeaders);
combinedHeaders.putAll(StreamSupport.stream(requestContext.getRequest().getHeaders().spliterator(), false)
.collect(Collectors.toMap(
akka.http.javadsl.model.HttpHeader::name, akka.http.javadsl.model.HttpHeader::value)));
org.apache.pekko.http.javadsl.model.HttpHeader::name,
org.apache.pekko.http.javadsl.model.HttpHeader::value
))
);
LOGGER.withCorrelationId(combinedHeaders)
.info("Pre-authentication has been applied resulting in AuthorizationContext <{}>.", authContext);

Expand Down
4 changes: 2 additions & 2 deletions gateway/service/src/main/resources/logback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%date{ISO8601} %-5level [%X{correlation-id}][%X{traceparent}] %logger{20} %X{pekkoSource} - %msg%n</pattern>
<pattern>%date{ISO8601} %-5level [%X{correlation-id}][%X{traceparent-trace-id}] %logger{20} %X{pekkoSource} - %msg%n</pattern>
</encoder>
</appender>

<appender name="STDERR" class="ch.qos.logback.core.ConsoleAppender">
<target>System.err</target>
<encoder>
<pattern>%date{ISO8601} %-5level [%X{correlation-id}][%X{traceparent}] %logger{20} %X{pekkoSource} - %msg%n</pattern>
<pattern>%date{ISO8601} %-5level [%X{correlation-id}][%X{traceparent-trace-id}] %logger{20} %X{pekkoSource} - %msg%n</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERROR</level>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Stream;

import javax.annotation.Nullable;
Expand All @@ -34,6 +35,9 @@ public enum CommonMdcEntryKey implements CharSequence {
DITTO_LOG_TAG("ditto-log-tag"),
TRACE_PARENT(DittoHeaderDefinition.W3C_TRACEPARENT.getKey());

private static final String TRACE_ID = TRACE_PARENT.key + "-trace-id";
private static final String SPAN_ID = TRACE_PARENT.key + "-span-id";

private final String key;

CommonMdcEntryKey(final String key) {
Expand Down Expand Up @@ -90,18 +94,27 @@ public static List<MdcEntry> extractMdcEntriesFromHeaders(@Nullable final Map<St
return Collections.emptyList();
}
return Stream.of(CORRELATION_ID, TRACE_PARENT)
.flatMap(mdcEntryKey ->
extractValue(headers, mdcEntryKey)
.map(value -> MdcEntry.of(mdcEntryKey.key, value))
.stream()
).toList();
.flatMap(mdcEntryKey -> extractValue(headers, mdcEntryKey))
.toList();
}

private static Optional<String> extractValue(final Map<String, String> headers,
private static Stream<MdcEntry> extractValue(final Map<String, String> headers,
final CommonMdcEntryKey mdcEntryKey) {

// accessing a key in a HashMap is super fast
return Optional.ofNullable(headers.get(mdcEntryKey.key));
if (mdcEntryKey == TRACE_PARENT) {
return Optional.ofNullable(headers.get(mdcEntryKey.key))
.filter(traceParent -> traceParent.charAt(2) == '-' && traceParent.length() == 55)
.map(traceParent -> Stream.of(
// positions defined by https://www.w3.org/TR/trace-context/#traceparent-header-field-values to contain the "trace-id"
MdcEntry.of(TRACE_ID, traceParent.substring(3, 35)),
MdcEntry.of(SPAN_ID, traceParent.substring(36, 52))
)).stream()
.flatMap(Function.identity());
} else {
return Optional.ofNullable(headers.get(mdcEntryKey.key))
.map(value -> MdcEntry.of(mdcEntryKey.key, value))
.stream();
}
}

}
4 changes: 2 additions & 2 deletions policies/service/src/main/resources/logback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%date{ISO8601} %-5level [%X{correlation-id}][%X{traceparent}] %logger{20} %X{pekkoSource} - %msg%n</pattern>
<pattern>%date{ISO8601} %-5level [%X{correlation-id}][%X{traceparent-trace-id}] %logger{20} %X{pekkoSource} - %msg%n</pattern>
</encoder>
</appender>

<appender name="STDERR" class="ch.qos.logback.core.ConsoleAppender">
<target>System.err</target>
<encoder>
<pattern>%date{ISO8601} %-5level [%X{correlation-id}][%X{traceparent}] %logger{20} %X{pekkoSource} - %msg%n</pattern>
<pattern>%date{ISO8601} %-5level [%X{correlation-id}][%X{traceparent-trace-id}] %logger{20} %X{pekkoSource} - %msg%n</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERROR</level>
Expand Down
4 changes: 2 additions & 2 deletions things/service/src/main/resources/logback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%date{ISO8601} %-5level [%X{correlation-id}][%X{traceparent}] %logger{20} %X{pekkoSource} - %msg%n</pattern>
<pattern>%date{ISO8601} %-5level [%X{correlation-id}][%X{traceparent-trace-id}] %logger{20} %X{pekkoSource} - %msg%n</pattern>
</encoder>
</appender>

<appender name="STDERR" class="ch.qos.logback.core.ConsoleAppender">
<target>System.err</target>
<encoder>
<pattern>%date{ISO8601} %-5level [%X{correlation-id}][%X{traceparent}] %logger{20} %X{pekkoSource} - %msg%n</pattern>
<pattern>%date{ISO8601} %-5level [%X{correlation-id}][%X{traceparent-trace-id}] %logger{20} %X{pekkoSource} - %msg%n</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERROR</level>
Expand Down
4 changes: 2 additions & 2 deletions thingsearch/service/src/main/resources/logback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%date{ISO8601} %-5level [%X{correlation-id}][%X{traceparent}] %logger{20} %X{pekkoSource} - %msg%n</pattern>
<pattern>%date{ISO8601} %-5level [%X{correlation-id}][%X{traceparent-trace-id}] %logger{20} %X{pekkoSource} - %msg%n</pattern>
</encoder>
</appender>

<appender name="STDERR" class="ch.qos.logback.core.ConsoleAppender">
<target>System.err</target>
<encoder>
<pattern>%date{ISO8601} %-5level [%X{correlation-id}][%X{traceparent}] %logger{20} %X{pekkoSource} - %msg%n</pattern>
<pattern>%date{ISO8601} %-5level [%X{correlation-id}][%X{traceparent-trace-id}] %logger{20} %X{pekkoSource} - %msg%n</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERROR</level>
Expand Down

0 comments on commit 707c773

Please sign in to comment.