Skip to content

[BUG]: get_log_correlation_context returns {'trace_id': '0', 'span_id': '0'} after an exception within ThreadPoolExecutor #13219

Open
@dseegerzoro

Description

@dseegerzoro

Tracer Version(s)

2.16.6

Python Version(s)

Python 3.11.7

Pip Version(s)

pip 24.2

Bug Report

I have a scenario where my logs are not being correlated to my traces. I have found that the output from tracer.get_log_correlation_context() is {'trace_id': '0', 'span_id': '0', 'service': '', 'version': '', 'env': ''}

I have a flask + gevent + gunicorn app that creates a new thread from ThreadPoolExecutor to make a network call.

I have noticed that when the network call fails from requests.exceptions.ConnectTimeout my logs within the exception handling do not have valid trace or span IDs.

I am only able to reproduce this when using ThreadPoolExecutor to create the separate thread. If I use threading.Thread the exception logging correctly gets the trace and span IDs.

Wondering why using ThreadPoolExecutor is causing this issue.

Reproduction Code

from gevent import monkey
monkey.patch_all()

from concurrent.futures.thread import ThreadPoolExecutor

import requests
from ddtrace import tracer
from flask import Flask

app = Flask(__name__)
session = requests.Session()
executor = ThreadPoolExecutor()


def to_be_run_in_thread():
    try:
        session.get('https://catfact.ninja/fact', timeout=0.01)
    except requests.RequestException:
        print('---Start Exception Context---')
        print(tracer.get_log_correlation_context())
        print('---End Exception Context---')


@app.route('/')
def hello_world():
    print(tracer.get_log_correlation_context())
    future = executor.submit(to_be_run_in_thread)
    future.result()
    return 'Completed!'

Application is run with
OBJC_DISABLE_INITIALIZE_FORK_SAFETY=YES DD_TRACE_DEBUG=true ddtrace-run gunicorn -k gevent 'fake_app:app'

Request to app with
curl http://127.0.0.1:5000

App Output

{'trace_id': '680004dd00000000be58214407178728', 'span_id': '15422056973920900502', 'service': 'flask', 'version': '', 'env': ''}
---Start Exception Context---
{'trace_id': '0', 'span_id': '0', 'service': '', 'version': '', 'env': ''}
---End Exception Context---

Error Logs

No response

Libraries in Use

My requirements.txt looks like

gevent>=23.7,<23.8
requests>=2.22.0,<3.0.0
ddtrace~=2.16.0
Flask==2.2.3
gunicorn>=20.0.4,<21.0.0
Werkzeug==2.2.3

Pip freeze output

bytecode==0.16.2
certifi==2025.1.31
charset-normalizer==3.4.1
click==8.1.8
ddtrace==2.16.6
Deprecated==1.2.18
envier==0.6.1
Flask==2.2.3
gevent==23.7.0
greenlet==3.2.0
gunicorn==20.1.0
idna==3.10
importlib_metadata==8.6.1
itsdangerous==2.2.0
Jinja2==3.1.6
MarkupSafe==3.0.2
opentelemetry-api==1.32.1
protobuf==6.30.2
requests==2.32.3
typing_extensions==4.13.2
urllib3==2.4.0
Werkzeug==2.2.3
wrapt==1.17.2
xmltodict==0.14.2
zipp==3.21.0
zope.event==5.0
zope.interface==7.2

Operating System

Darwin Kernel Version 23.4.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions