How to make X-Ray traces clickable from CloudWatch logs insights? #5741
-
I recently integrated powertools logging into my Python app, and love that it automatically instruments Examples -
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 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
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.
Hey @softwareengineerprogrammer! If you want that CW Logs Dashboard render the
TracerId
with a link to Tracer, then you need to log the fieldX-Amzn-Trace-Id
as part of your log line. In this way CW will detect this field and render this as@xRayTracerId
. With Powertools you have some ways to do that.1 - Adding this as an extra key. You must take care to get
_X_AMZN_TRACE_ID
from the env, it changes on every Lambda run and it doesn't matter if this is a new container or not, so put this inside the Lambda handler.