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
Add trace.id to request trace logs #91772
Conversation
Pinging @elastic/es-core-infra (Team:Core/Infra) |
Hi @thecoop, I've created a changelog YAML for you. |
restRequest.getRequestId(), | ||
restRequest.header(Task.X_OPAQUE_ID_HTTP_HEADER), | ||
restRequest.method(), | ||
restRequest.uri(), | ||
restRequest.getHttpChannel() | ||
restRequest.getHttpChannel(), | ||
Task.extractTraceId(restRequest.header(Task.TRACE_PARENT_HTTP_HEADER)).map(t -> " trace.id: " + t).orElse("") |
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.
Can we add a test ensuring the new code produces the expected result? I know logger is hard to test, so perhaps we can extract the code into a method? I also recall we had a way to ensure logs had a certain message in some tests, but I can't seem to find it now.
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.
some of those tests are in JsonLoggerTests
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.
Unfortunately that's in a different package, and HttpTracer
is package private. I'll see what I can do
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.
I've added a separate test
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.
I think the workaround is ok.
bear in mind that this still will produce a different result in request than in response (the trace.id will be inside a message field". Therefore it will be harder to automatically parse.
I suspect that HttpTracer class was meant to be used for human debugging - hence the trace level which will be unfeasible for investigating problems happening in real clusters.
request after changes:
{ ... "message": "[24][SomeUniqueValue][GET][/test-index/_search] received request from [Netty4HttpChannel{localAddress=/172.18.0.2:9200, remoteAddress=/172.18.0.1:62278}] trace.id: 4bf92f3577b34da6a3ce929d0e0e4736",
response
"message": "[24][SomeUniqueValue][OK][application/json; charset=UTF-8][1005] sent response to [Netty4HttpChannel{localAddress=/172.18.0.2:9200, remoteAddress=/172.18.0.1:62278}] success [true]",..., "trace.id": "4bf92f3577b34da6a3ce929d0e0e4736"
restRequest.getRequestId(), | ||
restRequest.header(Task.X_OPAQUE_ID_HTTP_HEADER), | ||
restRequest.method(), | ||
restRequest.uri(), | ||
restRequest.getHttpChannel() | ||
restRequest.getHttpChannel(), | ||
Task.extractTraceId(restRequest.header(Task.TRACE_PARENT_HTTP_HEADER)).map(t -> " trace.id: " + t).orElse("") |
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.
some of those tests are in JsonLoggerTests
@@ -57,12 +57,13 @@ HttpTracer maybeLogRequest(RestRequest restRequest, @Nullable Exception e) { | |||
if (logger.isTraceEnabled() && TransportService.shouldTraceAction(restRequest.uri(), tracerLogInclude, tracerLogExclude)) { | |||
logger.trace( | |||
() -> format( | |||
"[%s][%s][%s][%s] received request from [%s]", | |||
"[%s][%s][%s][%s] received request from [%s]%s", |
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.
should the new %s be also in square brackets?
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.
I was aiming to get it looking as similar as possible to the response log format
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.
I wonder if we actually don't want to keep this the same in both request and response. so that it would be easier to parse. the same as it is done for x-opaque-id. That means that trace.id or x-opaque-id are duplicated in a response. Present in both message and its own field.
I leave that up to you, or feel free to bring it up with the team
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.
Getting the trace id in there is not completely simple, so I would prefer to leave this hack as small a footprint as possible - if people do complain about the difference, we can then spend more time on this looking at it properly
This was originally reported on 7.17, should this fix be backported at all? |
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.
LGTM, I left one comment. Feel free to apply this or merge as it is.
@@ -57,12 +57,13 @@ HttpTracer maybeLogRequest(RestRequest restRequest, @Nullable Exception e) { | |||
if (logger.isTraceEnabled() && TransportService.shouldTraceAction(restRequest.uri(), tracerLogInclude, tracerLogExclude)) { | |||
logger.trace( | |||
() -> format( | |||
"[%s][%s][%s][%s] received request from [%s]", | |||
"[%s][%s][%s][%s] received request from [%s]%s", |
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.
I wonder if we actually don't want to keep this the same in both request and response. so that it would be easier to parse. the same as it is done for x-opaque-id. That means that trace.id or x-opaque-id are duplicated in a response. Present in both message and its own field.
I leave that up to you, or feel free to bring it up with the team
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.
LGTM! Great test!
Hi @thecoop, I've updated the changelog YAML for you. |
This reverts commit 302842e.
This adds trace.id, extracted from traceparent header, so its present in the request trace log. It is already included in response trace logs via the threadcontext, but that is not set at request log time.
💔 Backport failed
You can use sqren/backport to manually backport by running |
This adds trace.id, extracted from traceparent header, so its present in the request trace log. It is already included in response trace logs via the threadcontext, but that is not set at request log time.
* Add trace.id to request trace logs (#91772) This adds trace.id, extracted from traceparent header, so its present in the request trace log. It is already included in response trace logs via the threadcontext, but that is not set at request log time.
* Add trace.id to request trace logs (#91772) This adds trace.id, extracted from traceparent header, so its present in the request trace log. It is already included in response trace logs via the threadcontext, but that is not set at request log time.
All backports applied |
Very dirty solution to #88174 - just add the trace id to the log message, rather than using thread context