Skip to content

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

Closed
@brysontyrrell

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

Metadata

Metadata

Assignees

Type

No type

Projects

Status

Triage

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions