How do I instrument additional functions in X-Ray with AWS Lambda function in Python?

306 views Asked by At

I am trying to instrument an AWS Lambda function using X Ray. According to the official documentation of the aws_xray_sdk, I can't instrument anything outside the handler function. If I have the following sample code:

from aws_xray_sdk.core import xray_recorder


@xray_recorder.capture("handler")
def my_handler(event, context):
  # some code here
  xray_recorder.begin_subsegment("my_function")
  my_function(params)
  xray_recorder.end_subsegment("my_function")
  return {"message": "done"}

@xray_recorder.capture("my_function")
def my_function(params):
  # do work

nothing gets instrumented in X-Ray traces other than handler. I have tried with different combinations of begin_subsegment and not having @xray_recorder.capture() on my_function. Nothing seems to generate any traces for my_function. How do I work around this?

1

There are 1 answers

1
Lei Wang On BEST ANSWER

Please try to change

xray_recorder.end_subsegment("my_function")

to

xray_recorder.end_subsegment()