You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Include X-Ray Trace ID in Logger when detected #137
Is your feature request related to a problem? Please describe.
When using CloudWatch ServiceLens, logs are aggregated if the lines contain the trace ID you're looking at. This is handled by the default root logger, but it lost when using the Powertools logger.
The Powertools logger would be more powerful is it worked better together with both X-Ray and ServiceLens. Structured JSON logging is must easier to search and view in ServiceLens results. The whole thing becomes a better out-of-the-box solution.
Describe the solution you'd like
If present, I would like the X-Ray trace ID appended automatically to the log structure.
Describe alternatives you've considered
I've worked around this in one Lambda Function I've been using to try out Powertools.
We'll only add the key xray_trace_id if your Lambda function enabled Tracing. You can also change the order of the key in your final log record if you fancy too.
I took a look at the implementation you linked. The trace ID changes for each invocation. Setting in the init of the formatter would mean that the logger would have to be initialized in the handler and not the root, right?
That is absolutely true, and I completely forgot that! @brysontyrrell - This means we'll only guarantee this if customers use the inject_lambda_context decorator since we can update the logger on every invocation.
@heitorlessa That would still work. I'd like to know more about how the Python Lambda environment configures the root logger to handle this as it sets the trace ID in the logger for each invoke even if you've initialized in the global context.
This is likely at the bootstrap.py who imports your code and updates the root logger handler. Lemme see how much of a performance impact it has if I were to update that when formatting the log keys - This would be fetched at every log statement
Activity
heitorlessa commentedon Aug 27, 2020
Thanks for raising the issue @brysontyrrell :)
We definitely want to do this. I have one question for you, though: At what point should this key be added?
logger = Logger()inject_lambda_contextdecoratorfeat: add xray_trace_id key when tracing is active #137
heitorlessa commentedon Aug 30, 2020
@brysontyrrell could you double check the implementation meets your expectation?
We'll only add the key
xray_trace_idif your Lambda function enabled Tracing. You can also change the order of the key in your final log record if you fancy too.brysontyrrell commentedon Aug 30, 2020
@heitorlessa Sorry for the delay in replying!
I took a look at the implementation you linked. The trace ID changes for each invocation. Setting in the init of the formatter would mean that the logger would have to be initialized in the handler and not the root, right?
heitorlessa commentedon Aug 30, 2020
That is absolutely true, and I completely forgot that! @brysontyrrell - This means we'll only guarantee this if customers use the
inject_lambda_contextdecorator since we can update the logger on every invocation.Does that still work for you?
brysontyrrell commentedon Aug 30, 2020
@heitorlessa That would still work. I'd like to know more about how the Python Lambda environment configures the root logger to handle this as it sets the trace ID in the logger for each invoke even if you've initialized in the global context.
Example output from some testing I just did:
The logger parsed this from the env var (not the segment ID though - unsure where's that set so the logger can discover it):
heitorlessa commentedon Aug 30, 2020
This is likely at the
bootstrap.pywho imports your code and updates the root logger handler. Lemme see how much of a performance impact it has if I were to update that when formatting the log keys - This would be fetched at every log statementheitorlessa commentedon Aug 30, 2020
Perf impact is negligible, this change won't impact customers - Difference in fetching it while formatting log records is ~0.10 nanoseconds.
I'll change this now, and merge the PR after updating tests :) -- It'll be in the next release during this coming week
Thanks again for the heads up @brysontyrrell
heitorlessa commentedon Sep 1, 2020
I've just tested it and works @brysontyrrell, also worked with the Service Lens team to ensure this will work consistently :)
Merging soon, and will update here once it's released along with other new shiny features in 1.5.0 this week
Merge pull request #150 from heitorlessa/feat/logger-add-xray-trace-id
heitorlessa commentedon Sep 11, 2020
Closing as this was included in 1.5.0 :)