aws / aws/aws-xray-sdk-python

Unable to create top-level subsegment in image Lambda

Open
#300 2 comments 0 reactions 1 assignee Claimed by @wangzlei View on GitHub
stale
Dominant language
Python
Stars
338
Forks
147
PR merge metrics
No merged PRs in 30d

Description

I have a Lambda deployed as a Docker image that instantiates a class in the global context. This class is used in various other functions.

I wanted to see how much of the time during instantiation (cold start) was taken up by this class instantiation. However, I can't seem to make a (sub)segment that shows me this in the console.

Minimal reproducible code:
```python
from aws_xray_sdk.core import xray_recorder
from my_classes import BigClass

# Instantiate big class up-front.
xray_recorder.configure(sampling=False)
with xray_recorder.in_segment("instantiate class") as segment: # Have also tried without this line.
with xray_recorder.in_subsegment("instantiate class") as subsegment:
subsegment.put_annotation("key", "value")
big_class = BigClass()
```

The X-Ray decorators work in other parts of my code so it's not an issue with `requirements.txt` or my Dockerfile.

Here's the Dockerfile anyway.
```dockerfile
FROM public.ecr.aws/lambda/python:3.8

# Install dependencies.
COPY requirements.txt ./
RUN python3.8 -m pip --no-cache-dir install --upgrade pip && python3.8 -m pip --no-cache-dir install -r requirements.txt

# Copy application code and provide entrypoint.
COPY src/index.py ./
CMD ["index.handler"]
```

How should I trace the instantiation of global objects (top level of a script)?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.