Skip to content

Commit e15f90a

Browse files
author
Olivier Gintrand
committed
fix: uninstrument before re-instrumenting httpx in post_fork
The _is_instrumented_by_opentelemetry flag survives fork() even though the actual wrap_function_wrapper monkey-patches do not. This caused the post_fork guard to skip re-instrumentation in worker processes. Fix: call uninstrument() first to reset the flag, then instrument() to apply the patches fresh in each worker. Signed-off-by: Olivier Gintrand <olivier.gintrand@forterro.com>
1 parent bb4744a commit e15f90a

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

gunicorn.config.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,15 +128,17 @@ def post_fork(server, worker):
128128
pass
129129

130130
# Re-apply httpx instrumentation in each worker process.
131-
# With preload_app=True, wrap_function_wrapper patches made in the master
132-
# process do not survive fork() — each worker needs its own monkey-patch.
131+
# With preload_app=True, the master process sets _is_instrumented_by_opentelemetry=True
132+
# and applies wrap_function_wrapper patches. After fork(), workers inherit the flag
133+
# but NOT the monkey-patches. We must uninstrument (reset the flag) then re-instrument.
133134
try:
134135
from opentelemetry.instrumentation.httpx import HTTPXClientInstrumentor
135136

136137
instrumentor = HTTPXClientInstrumentor()
137-
if not instrumentor.is_instrumented_by_opentelemetry:
138-
instrumentor.instrument()
139-
server.log.info("Worker %s: httpx instrumentation applied", worker.pid)
138+
if instrumentor.is_instrumented_by_opentelemetry:
139+
instrumentor.uninstrument()
140+
instrumentor.instrument()
141+
server.log.info("Worker %s: httpx instrumentation applied", worker.pid)
140142
except ImportError:
141143
pass
142144
except Exception as e:

0 commit comments

Comments
 (0)