-
Notifications
You must be signed in to change notification settings - Fork 1
Distributted logging for scan-id #90
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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; | ||||||||
|
|
||||||||
|
|
@@ -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)) | ||||||||
|
||||||||
| .filter(header -> header.getName().startsWith(CTX_HEADER_PREFIX)) | |
| .filter(header -> header.getName().startsWith(CTX_HEADER_PREFIX)) | |
| .filter(header -> header.getValue() != null) |
Copilot
AI
Mar 3, 2026
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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, butRequestContextpreserves the original header name case (e.g., a caller could have putX-CTX-scan-id). This can causex-ctx-*headers to be skipped and not logged in MDC. Consider performing a case-insensitive prefix check (e.g., compare ontoLowerCase()or useregionMatches(true, ...)).