aws / aws/aws-xray-sdk-python

Bug: nested subsegments don't work across threads

Open
#388 4 comments 0 reactions 1 assignee Claimed by @srprash View on GitHub
pending release
Dominant language
Python
Stars
338
Forks
147
PR merge metrics
No merged PRs in 30d

Description

We're tracking a bug where nested subsegments don't work across threads, even when using the [official suggestion in the documentation](https://github.com/aws/aws-xray-sdk-python#trace-threadpoolexecutor).

We've managed to reproduce the issue with some minimal code:

## Code snippet

```python
from concurrent.futures import ThreadPoolExecutor

from aws_xray_sdk.core import xray_recorder

@xray_recorder.capture("## lambda handler")
def lambda_handler(event, context):
with ThreadPoolExecutor() as pool:
@xray_recorder.capture("## f")
def f():
@xray_recorder.capture("## h")
def h():
pass

def g(trace_entity):
xray_recorder.set_trace_entity(trace_entity)
h()
xray_recorder.clear_trace_entities()

curr = xray_recorder.get_trace_entity()
pool.submit(g, curr)
```

This produces the following XRay traces:

## XRay trace

```
XRay Event at (2023-04-05T16:32:52.012000) with id (1-642d8694-731f0a1a6d204264677086db) and duration (0.388s)
- 0.388s - lambda-function-url-HelloWorldFunction-sRQB00cHvXOP [HTTP: 200]
- 0.021s - lambda-function-url-HelloWorldFunction-sRQB00cHvXOP
- 0.223s - Initialization
- 0.020s - Invocation
- 0.019s - ## lambda handler
- 0.018s - ## f
- 0.000s - ## h
- 0.000s - Overhead
```

The expected output would be for the "h" trace to be nested under "f":

## Expected output

```
XRay Event at (2023-04-05T16:32:52.012000) with id (1-642d8694-731f0a1a6d204264677086db) and duration (0.388s)
- 0.388s - lambda-function-url-HelloWorldFunction-sRQB00cHvXOP [HTTP: 200]
- 0.021s - lambda-function-url-HelloWorldFunction-sRQB00cHvXOP
- 0.223s - Initialization
- 0.020s - Invocation
- 0.019s - ## lambda handler
- 0.018s - ## f
- 0.000s - ## h
- 0.000s - Overhead
```

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.