1

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?

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
ramdesh
  • 650
  • 6
  • 22

1 Answers1

1

Please try to change

xray_recorder.end_subsegment("my_function")

to

xray_recorder.end_subsegment()
ramdesh
  • 650
  • 6
  • 22
Lei Wang
  • 217
  • 1
  • 4
  • 1
    Also note per the [AWS docs](https://docs.aws.amazon.com/xray-sdk-for-python/latest/reference/basic.html#manually-create-segment-subsegment): "The `xray_recorder` keeps one segment per thread. Therefore, in manual mode, call `xray_recorder.end_segment()` before creating a new segment, otherwise the new segment overwrites the existing one" – ryanjdillon Sep 06 '21 at 12:01