Skip to content

Conversation

@PROFeNoM
Copy link
Contributor

@PROFeNoM PROFeNoM commented Dec 23, 2025

Description

Problem

Distributed tracing headers were not being injected in HyperF/Swoole environments, breaking trace continuity to downstream services.

Root Cause

  1. HyperF uses CoroutineHandler instead of curl when running in Swoole coroutines
  2. CoroutineHandler reads headers from the PSR-7 request object, not curl options
  3. Our old integration only had post-hooks (via trace_method), which run AFTER the request is sent—too late to inject headers

Old flow:

transfer() → CoroutineHandler gets request → Headers missing → Request sent

Solution

Use install_hook instead of trace_method to add a pre-hook that injects distributed tracing headers into the PSR-7 request object BEFORE the handler executes.

New flow:

transfer() → PRE-HOOK injects headers → CoroutineHandler gets request with headers → Request sent

Important: The pre-hook must always call overrideArguments(), even when no headers are injected. Per ext/hook/uhook.c:

"hooks wishing to override args must do so unconditionally, even if the args are not changed. Otherwise, if overrideArguments was not called on the first time this function was traced, the JIT will have successfully traced the function and there's no going back."

Changes

  • Convert transfer() instrumentation from trace_method to install_hook
  • Add pre-hook to inject distributed tracing headers before request execution
  • Ensure overrideArguments() is always called (required for JIT compatibility)
  • Remove outdated comment about not supporting pre+post hooks

Testing

Verified by customer in production HyperF/Swoole environment: https://datadoghq.atlassian.net/browse/APMS-17909

Reviewer checklist

  • Test coverage seems ok.
  • Appropriate labels assigned.

Use install_hook with pre-hook to inject headers into PSR-7 request
before it reaches any handler (curl or CoroutineHandler). This ensures
headers are propagated in HyperF/Swoole environments where CoroutineHandler
reads headers directly from the request object.

Fixes distributed tracing continuity for Guzzle in coroutine contexts.
@PROFeNoM PROFeNoM changed the title wip fix distributed tracing header injection for Guzzle in HyperF/Swoole environments Dec 23, 2025
@PROFeNoM PROFeNoM marked this pull request as ready for review December 23, 2025 10:31
@PROFeNoM PROFeNoM requested a review from a team as a code owner December 23, 2025 10:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants