Search code examples
pythonamazon-web-servicesaws-lambdaaws-xray

How to use X-Ray with SNS to get one big chain instead of many small?


I have a chain (just to keep things simpler):

  1. Lambda1 (publisher)
  2. SNS
  3. Lambda2 (subscriber)

Both lambdas have X-Ray enabled and they appear at X-Ray console as two separate chains containing "clients=>AWS::Lambda=>AWS::Lambda::Function" each. How to join them to one chain? Here I see "For Lambda subscribers with active tracing enabled, Lambda records a segment with details about the function invocation, and sends it to the publisher's trace". From what I see in X-Ray console it in not true for my setup.

I tried to use SDK for python in Lambda2 like below:

from aws_xray_sdk.core import xray_recorder
from aws_xray_sdk.core import patch_all

patch_all()
def lambda_handler(event, context):
   ... some code

I do see SUBsegments in traces of this function and I can create and see another SUBsegments but the main segment of this function is still not connected to the publisher.

What am I doing wrong? Is it possible to configure xray_recorder to use xray_trace_id I pass from Lambda1 via SNS? Or is there simpler way?


Solution

  • It is not stated explicitly in the doc, but you have to use the same

    from aws_xray_sdk.core 
    from aws_xray_sdk.core import patch_all
    patch_all()
    

    not only in subscriber but in publisher as well. This did the trick for me.