Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document how to create new subsegments inside a ThreadPoolExecutor thread #341

Open
tibbe opened this issue Jun 17, 2022 · 1 comment
Open

Comments

@tibbe
Copy link

tibbe commented Jun 17, 2022

The current documentation shows how to set the parent thread's X-ray entity in the worker thread:

def load_url(url, trace_entity):
    # Set the parent X-Ray entity for the worker thread.
    xray_recorder.set_trace_entity(trace_entity)
    # Subsegment captured from the following HTTP GET will be
    # a child of parent entity passed from the main thread.
    resp = requests.get(url)
    # prevent thread pollution
    xray_recorder.clear_trace_entities()
    return resp

However it's not clear how this interacts with capture/in_segment. For example, I assume this isn't correct, as the capture starts before we call set_trace_entity:

@xray_recorder.capture('subsegment_name')
def load_url(url, trace_entity):
    # Set the parent X-Ray entity for the worker thread.
    xray_recorder.set_trace_entity(trace_entity)
    # Subsegment captured from the following HTTP GET will be
    # a child of parent entity passed from the main thread.
    resp = requests.get(url)
    # prevent thread pollution
    xray_recorder.clear_trace_entities()
    return resp

However, are these two OK?

def load_url(url, trace_entity):
    # Set the parent X-Ray entity for the worker thread.
    xray_recorder.set_trace_entity(trace_entity)
    with xray_recorder.capture('subsegment_name'):
        # Subsegment captured from the following HTTP GET will be
        # a child of parent entity passed from the main thread.
        resp = requests.get(url)
    # prevent thread pollution
    xray_recorder.clear_trace_entities()
    return resp
def load_url(url, trace_entity):
    # Set the parent X-Ray entity for the worker thread.
    xray_recorder.set_trace_entity(trace_entity)
    with xray_recorder.in_segment('segment_name') as segment:
        segment.put_metadata('key', dict, 'namespace')
        # Subsegment captured from the following HTTP GET will be
        # a child of parent entity passed from the main thread.
        resp = requests.get(url)
    # prevent thread pollution
    xray_recorder.clear_trace_entities()
    return resp

It would be great if the docs on threading had a more complete example.

@NathanielRN
Copy link
Contributor

Hey there!

I'm not the most familiar with this X-Ray Python SDK, but I'll try to answer your question.

Based on what I see, both your solutions make sense and to as far as I can tell they should work. Your example of something that doesn't work also makes sense to me. Have you tried it in the X-Ray console and do you notice it doesn't work? If that's the case please include screenshots of what you see versus what you expect!

Otherwise, we also recommend you use the OpenTelemetry Python SDK. It has tons of features and I found an example with ThreadPoolExecutor to ensure spans are parented correctly. You'll find lots of support there and AWS announced GA support for its traces 1 year ago.

We're happy to review PRs for improvements to our documentation! If you open one someone from our team will check out as soon as we are able to 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants