-
Notifications
You must be signed in to change notification settings - Fork 140
feat(laravel): inject trace context into outbound HTTP client requests
#656
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
Open
zigzagdev
wants to merge
7
commits into
open-telemetry:main
Choose a base branch
from
zigzagdev:fix/laravel-outbound-trace-propagation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2b02251
feat(laravel): add RequestPropagationSetter for outbound HTTP requests
zigzagdev 1128ec4
feat(laravel): inject trace context into outbound HTTP client requests
zigzagdev d003321
feat(laravel): register PendingRequest hook
zigzagdev 16aef86
test(laravel): verify trace context is injected into outbound requests
zigzagdev 1038204
fix(laravel): make outbound HTTP client trace propagation opt-in
zigzagdev a876c09
test(laravel): verify outbound trace propagation stays off by default
zigzagdev fb70805
fix: suppress PhanAccessMethodInternal in PendingRequest hook
zigzagdev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
src/Instrumentation/Laravel/src/Hooks/Illuminate/Http/Client/PendingRequest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\Illuminate\Http\Client; | ||
|
|
||
| use Illuminate\Http\Client\PendingRequest as IlluminatePendingRequest; | ||
| use OpenTelemetry\API\Trace\Propagation\TraceContextPropagator; | ||
| use OpenTelemetry\Context\Context; | ||
| use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\LaravelHook; | ||
| use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\LaravelHookTrait; | ||
| use OpenTelemetry\Contrib\Instrumentation\Laravel\LaravelInstrumentation; | ||
| use OpenTelemetry\Contrib\Instrumentation\Laravel\Propagators\RequestPropagationSetter; | ||
| use function OpenTelemetry\Instrumentation\hook; | ||
| use Psr\Http\Message\RequestInterface; | ||
|
|
||
| class PendingRequest implements LaravelHook | ||
| { | ||
| use LaravelHookTrait; | ||
|
|
||
| public function instrument(): void | ||
| { | ||
| $this->hookRunBeforeSendingCallbacks(); | ||
| } | ||
|
|
||
| /** | ||
| * `runBeforeSendingCallbacks()` is where Laravel finalizes the PSR-7 request just before | ||
| * handing it to Guzzle, so injecting into its first argument here reaches the request that | ||
| * is actually sent (the `RequestSending` event ClientRequestWatcher listens on can't do this | ||
| * itself, since it only receives an immutable copy that never reaches the real request). | ||
| * | ||
| * Gated behind an opt-in flag: the target of an outbound HTTP client request may be a | ||
| * third-party service outside the application's control, which should not receive internal | ||
| * trace (and, in future, baggage) data unless the developer explicitly asks for it. | ||
| * | ||
| * @psalm-suppress PossiblyUnusedReturnValue | ||
| */ | ||
| protected function hookRunBeforeSendingCallbacks(): bool | ||
| { | ||
| return hook( | ||
| IlluminatePendingRequest::class, | ||
| 'runBeforeSendingCallbacks', | ||
| pre: function (IlluminatePendingRequest $_pendingRequest, array $params) { | ||
| if (!LaravelInstrumentation::shouldPropagateHttpClientTraceContext()) { | ||
| return $params; | ||
| } | ||
|
|
||
| $request = $params[0]; | ||
| if (!$request instanceof RequestInterface) { | ||
| return $params; | ||
| } | ||
|
|
||
| /** @phan-suppress-next-line PhanAccessMethodInternal */ | ||
| TraceContextPropagator::getInstance()->inject($request, RequestPropagationSetter::instance(), Context::getCurrent()); | ||
| $params[0] = $request; | ||
|
|
||
| return $params; | ||
| }, | ||
| ); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/Instrumentation/Laravel/src/Propagators/RequestPropagationSetter.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace OpenTelemetry\Contrib\Instrumentation\Laravel\Propagators; | ||
|
|
||
| use function assert; | ||
| use OpenTelemetry\Context\Propagation\PropagationSetterInterface; | ||
| use Psr\Http\Message\RequestInterface; | ||
|
|
||
| /** | ||
| * @internal | ||
| */ | ||
| class RequestPropagationSetter implements PropagationSetterInterface | ||
| { | ||
| public static function instance(): self | ||
| { | ||
| static $instance; | ||
|
|
||
| return $instance ??= new self(); | ||
| } | ||
|
|
||
| public function set(&$carrier, string $key, string $value): void | ||
| { | ||
| assert($carrier instanceof RequestInterface); | ||
|
|
||
| $carrier = $carrier->withHeader($key, $value); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This call currently fails the Laravel quality jobs with PhanAccessMethodInternal: RequestPropagationSetter is marked @internal and is being called from the Hooks subnamespace. Please adjust the annotation/scope or use the project’s established suppression pattern so the required quality checks pass.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@PuvaanRaaj
Suppressed via the same @phan-suppress-next-line PhanAccessMethodInternal pattern used in Kernel.php, rather than removing @internal from
▎ RequestPropagationSetter (it's intentionally internal). phan passes locally now.
fix commit