How to make X-Ray traces clickable from CloudWatch logs insights? #5741
Replies: 1 comment
-
Hey @softwareengineerprogrammer! If you want that CW Logs Dashboard render the 1 - Adding this as an extra key. You must take care to get import os
from aws_lambda_powertools import Logger
logger = Logger()
def lambda_handler(event: dict, context: LambdaContext) -> str:
mylog = {
"X-Amzn-Trace-Id": os.getenv("_X_AMZN_TRACE_ID")
}
logger.info("Message",**mylog) 2 - Creating a import os
from aws_lambda_powertools import Logger
from aws_lambda_powertools.logging.formatter import LambdaPowertoolsFormatter
from aws_lambda_powertools.logging.types import LogRecord
class CustomFormatter(LambdaPowertoolsFormatter):
def serialize(self, log: LogRecord) -> str:
"""Serialize final structured log dict to JSON str"""
log["X-Amzn-Trace-Id"] = os.getenv("_X_AMZN_TRACE_ID")
return self.json_serializer(log)
logger = Logger(service="payment", logger_formatter=CustomFormatter())
logger.info("hello") 3 - I do no advice you to use append_keys method because you need to get a new value on every execution. Please ping me if you have any additional question. |
Beta Was this translation helpful? Give feedback.
-
I recently integrated powertools logging into my Python app, and love that it automatically instruments
xray_trace_id
in my cloudwatch logs. However, it would be significantly more useful to be able to click the trace ID in order to view the full trace, as CW dashboards supports when the trace ID is instrumented as@xrayTraceId
, and is done automatically forREPORT
events.Examples -
xray_trace_id
not clickable in CW dashboard logs widget for an event logged by powertools:@xrayTraceId
clickable forREPORT
event:What do I need to do to achieve clickable xray traces for all events as shown in the second image?
Edit: In the meantime, I created a workaround in the form of a Tampermonkey userscript that adds a link for
xray_trace_id
entries. install | sourceBeta Was this translation helpful? Give feedback.
All reactions