Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@ public class RequestContextConstants {

public static final String AUTHORIZATION_HEADER = "authorization";

public static final String CTX_HEADER_PREFIX = "x-ctx-";

/** The values in this set are looked up with case insensitivity. */
public static final Set<String> HEADER_PREFIXES_TO_BE_PROPAGATED =
Set.of(
TENANT_ID_HEADER_KEY,
CONTEXT_ID_HEADER_KEY,
SUPPRESS_USER_TRACKING_HEADER_KEY,
CTX_HEADER_PREFIX,
"X-B3-",
"grpc-trace-bin",
"traceparent",
Expand Down
1 change: 1 addition & 0 deletions grpc-server-utils/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ dependencies {
api(commonLibs.grpc.api)
implementation(projects.grpcContextUtils)
implementation(commonLibs.slf4j2.api)
compileOnly("org.apache.logging.log4j:log4j-core:2.20.0")

annotationProcessor(commonLibs.lombok)
compileOnly(commonLibs.lombok)
Expand Down
2 changes: 2 additions & 0 deletions grpc-server-utils/gradle.lockfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ io.netty:netty-bom:4.1.125.Final=compileClasspath,runtimeClasspath,testCompileCl
io.perfmark:perfmark-api:0.27.0=runtimeClasspath,testRuntimeClasspath
net.bytebuddy:byte-buddy-agent:1.14.10=testCompileClasspath,testRuntimeClasspath
net.bytebuddy:byte-buddy:1.14.10=testCompileClasspath,testRuntimeClasspath
org.apache.logging.log4j:log4j-api:2.20.0=compileClasspath
org.apache.logging.log4j:log4j-core:2.20.0=compileClasspath
org.apiguardian:apiguardian-api:1.1.2=testCompileClasspath
org.checkerframework:checker-qual:3.43.0=runtimeClasspath,testRuntimeClasspath
org.codehaus.mojo:animal-sniffer-annotations:1.24=runtimeClasspath,testRuntimeClasspath
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.hypertrace.core.grpcutils.server;

import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.pattern.ConverterKeys;
import org.apache.logging.log4j.core.pattern.LogEventPatternConverter;
import org.apache.logging.log4j.core.pattern.PatternConverter;
import org.apache.logging.log4j.util.ReadOnlyStringMap;

@Plugin(name = "ctxParams", category = PatternConverter.CATEGORY)
@ConverterKeys({"ctxParams"})
public class ContextMdcPatternConverter extends LogEventPatternConverter {

private static final String CTX_PREFIX = "x-ctx-";

private ContextMdcPatternConverter() {
super("ctxParams", "ctxParams");
}

public static ContextMdcPatternConverter newInstance(String[] options) {
return new ContextMdcPatternConverter();
}

@Override
public void format(LogEvent event, StringBuilder toAppendTo) {
ReadOnlyStringMap contextData = event.getContextData();
if (contextData == null || contextData.isEmpty()) return;

StringBuilder sb = new StringBuilder();
contextData.forEach(
(key, value) -> {
if (key.startsWith(CTX_PREFIX)) {
if (sb.length() > 0) sb.append(", ");
sb.append(key).append("=").append(value);
}
});
if (sb.length() > 0) {
toAppendTo.append("[").append(sb).append("]");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.hypertrace.core.grpcutils.server;

import static org.hypertrace.core.grpcutils.context.RequestContextConstants.CONTEXT_ID_HEADER_KEY;
import static org.hypertrace.core.grpcutils.context.RequestContextConstants.CTX_HEADER_PREFIX;
import static org.hypertrace.core.grpcutils.context.RequestContextConstants.REQUEST_ID_HEADER_KEY;
import static org.hypertrace.core.grpcutils.context.RequestContextConstants.TENANT_ID_HEADER_KEY;

Expand Down Expand Up @@ -69,6 +70,9 @@ public void onMessage(ReqT message) {
MDC.put(REQUEST_ID_HEADER_KEY, requestId);
opTenantId.ifPresent(s -> MDC.put(TENANT_ID_HEADER_KEY, s));
opContextId.ifPresent(s -> MDC.put(CONTEXT_ID_HEADER_KEY, s));
currentContext.getAllHeaders().stream()
.filter(header -> header.getName().startsWith(CTX_HEADER_PREFIX))
Copy link

Copilot AI Mar 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The startsWith(CTX_HEADER_PREFIX) check is case-sensitive, but RequestContext preserves the original header name case (e.g., a caller could have put X-CTX-scan-id). This can cause x-ctx-* headers to be skipped and not logged in MDC. Consider performing a case-insensitive prefix check (e.g., compare on toLowerCase() or use regionMatches(true, ...)).

Suggested change
.filter(header -> header.getName().startsWith(CTX_HEADER_PREFIX))
.filter(
header ->
header
.getName()
.regionMatches(
true, 0, CTX_HEADER_PREFIX, 0, CTX_HEADER_PREFIX.length()))

Copilot uses AI. Check for mistakes.
Copy link

Copilot AI Mar 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RequestContext.put(...) allows null header values, but this code unconditionally calls MDC.put(header.getName(), header.getValue()). SLF4J MDC implementations commonly reject null values (and even if allowed, it can lead to confusing log output). Consider filtering out headers with null values (similar to RequestContextAsCreds.applyRequestContext) or using MDC.remove(key) when the value is null.

Suggested change
.filter(header -> header.getName().startsWith(CTX_HEADER_PREFIX))
.filter(header -> header.getName().startsWith(CTX_HEADER_PREFIX))
.filter(header -> header.getValue() != null)

Copilot uses AI. Check for mistakes.
.forEach(header -> MDC.put(header.getName(), header.getValue()));
Comment on lines +73 to +75
Copy link

Copilot AI Mar 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change introduces new behavior (propagating all x-ctx-* headers into MDC) but grpc-server-utils currently has no test coverage for RequestContextLoggingServerInterceptor. Adding a unit test that asserts x-ctx-* headers appear in MDC on onMessage() and are cleared on onComplete()/onCancel() would help prevent regressions.

Copilot uses AI. Check for mistakes.
} catch (Exception e) {
log.error("Error while setting request context details in MDC params", e);
}
Expand Down
Loading