Skip to content

Include X-Ray Trace ID in Logger when detected #137

Closed

Description

@brysontyrrell

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.

@lambda_handler_decorator
def lambda_middleware(handler, event, context):
    logger.structure_logs(
        append=True,
        xray_trace_id=os.environ["_X_AMZN_TRACE_ID"].split(";")[0].strip("Root=")
        or None,
    )
    try:
        return handler(event, context)
    except Exception as error:
        logger.exception({"error": str(error)})
        raise

Activity

  1. added
    feature-request
    triage
    on Aug 26, 2020
  2. added
    good first issue
    and removed
    triage
    on Aug 27, 2020
  3. heitorlessa commented on Aug 27, 2020

    @heitorlessa
    Contributor

    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?

  4. heitorlessa commented on Aug 30, 2020

    @heitorlessa
    Contributor

    @brysontyrrell could you double check the implementation meets your expectation?

    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.

  5. self-assigned this
    on Aug 30, 2020
  6. brysontyrrell commented on Aug 30, 2020

    @brysontyrrell
    Author

    @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?

  7. heitorlessa commented on Aug 30, 2020

    @heitorlessa
    Contributor

    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.

    Does that still work for you?

  8. brysontyrrell commented on Aug 30, 2020

    @brysontyrrell
    Author

    @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:

    XRAY TraceId: 1-5f4be783-6e3816ed594eceb206b65359	SegmentId: 40d625d54b0dd18b	Sampled: true
    

    The logger parsed this from the env var (not the segment ID though - unsure where's that set so the logger can discover it):

    _X_AMZN_TRACE_ID: Root=1-5f4be783-6e3816ed594eceb206b65359;Parent=9335e1d5c7bb5c5d;Sampled=1
    
  9. heitorlessa commented on Aug 30, 2020

    @heitorlessa
    Contributor

    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

  10. heitorlessa commented on Aug 30, 2020

    @heitorlessa
    Contributor

    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

  11. added
    pending-release
    on Aug 31, 2020
  12. heitorlessa commented on Sep 1, 2020

    @heitorlessa
    Contributor

    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

  13. added a commit that references this issue on Sep 1, 2020
  14. removed
    pending-release
    on Sep 7, 2020
  15. heitorlessa commented on Sep 11, 2020

    @heitorlessa
    Contributor

    Closing as this was included in 1.5.0 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

feature-requestgood first issue

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions