-
Notifications
You must be signed in to change notification settings - Fork 184
Tracing: symfony http client #606
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
Merged
Merged
Changes from all commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
bc0592b
http client tracing
alekitto c680429
sentry sentry-trace header in tracing http client
alekitto 166c478
add test on http client request
alekitto 5ce0a9d
use newer phpunit on php 8
alekitto 9d42874
require php 8.1 in psalm
alekitto 0ee2c44
Revert "require php 8.1 in psalm"
alekitto 0648814
fix for review
alekitto 73132c2
add a couple of tests and changelog line
alekitto 095abc3
use http.url and http.method tags, instead of url and method
alekitto d4c749f
http.request -> http.client
alekitto 12490c4
add tests for traceable response classes
alekitto 6760d1b
style fixes
alekitto 79695c3
create fixtures for destructible and streamable responses
alekitto c4687c1
use ->assert instead of self::assert in phpunit tests
alekitto 38faf38
mark test classes as final
alekitto 0e2e979
rename variable
alekitto b00333f
test http-client tracing enabled/disabled
alekitto 4a28c72
add span description
alekitto 0d63111
update CHANGELOG.md
alekitto fd9f433
Add a test for the `TraceableHttpClient::withOptions()` method
ste93cry 39eaa7f
fix withOptions test for v4
alekitto 993c494
add test for http-client stream method
alekitto f2f5adc
Mark the `TraceableHttpClientFor*` classes as `@internal`
ste93cry e732d59
Minor methods and variables names changes
ste93cry c6d5f2b
Improve the test for the `TraceableHttpClient::stream()` method
ste93cry 8a824d7
Improve the tests for the `TraceableResponse` class
ste93cry 27d9d53
fix tests for lower deps
alekitto dcea7b5
fix static analysis
alekitto 71ba653
add error conditions tests for stream methods
alekitto 0ae2524
Minor improvements to the tests
ste93cry d9a3a71
Improve the tests for the `HttpClientTracingPass` class
ste93cry 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
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
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
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
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
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
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
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
32 changes: 32 additions & 0 deletions
32
src/DependencyInjection/Compiler/HttpClientTracingPass.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,32 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Sentry\SentryBundle\DependencyInjection\Compiler; | ||
|
|
||
| use Sentry\SentryBundle\Tracing\HttpClient\TraceableHttpClient; | ||
| use Sentry\State\HubInterface; | ||
| use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
| use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
| use Symfony\Component\DependencyInjection\Reference; | ||
|
|
||
| final class HttpClientTracingPass implements CompilerPassInterface | ||
| { | ||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| public function process(ContainerBuilder $container): void | ||
| { | ||
| if (!$container->getParameter('sentry.tracing.enabled') || !$container->getParameter('sentry.tracing.http_client.enabled')) { | ||
| return; | ||
| } | ||
|
|
||
| foreach ($container->findTaggedServiceIds('http_client.client') as $id => $tags) { | ||
| $container->register('.sentry.traceable.' . $id, TraceableHttpClient::class) | ||
| ->setDecoratedService($id) | ||
| ->setArgument(0, new Reference('.sentry.traceable.' . $id . '.inner')) | ||
| ->setArgument(1, new Reference(HubInterface::class)) | ||
| ->addTag('kernel.reset', ['method' => 'reset']); | ||
| } | ||
| } | ||
| } |
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
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
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
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
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,98 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Sentry\SentryBundle\Tracing\HttpClient; | ||
|
|
||
| use Psr\Log\LoggerAwareInterface; | ||
| use Psr\Log\LoggerInterface; | ||
| use Sentry\State\HubInterface; | ||
| use Sentry\Tracing\SpanContext; | ||
| use Symfony\Component\HttpClient\Response\ResponseStream; | ||
| use Symfony\Contracts\HttpClient\HttpClientInterface; | ||
| use Symfony\Contracts\HttpClient\ResponseInterface; | ||
| use Symfony\Contracts\HttpClient\ResponseStreamInterface; | ||
| use Symfony\Contracts\Service\ResetInterface; | ||
|
|
||
| /** | ||
| * This is an implementation of the {@see HttpClientInterface} that decorates | ||
| * an existing http client to support distributed tracing capabilities. | ||
| * | ||
| * @internal | ||
| */ | ||
| abstract class AbstractTraceableHttpClient implements HttpClientInterface, ResetInterface, LoggerAwareInterface | ||
| { | ||
| /** | ||
| * @var HttpClientInterface | ||
| */ | ||
| protected $client; | ||
|
|
||
| /** | ||
| * @var HubInterface | ||
| */ | ||
| protected $hub; | ||
|
|
||
| public function __construct(HttpClientInterface $client, HubInterface $hub) | ||
| { | ||
| $this->client = $client; | ||
| $this->hub = $hub; | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| public function request(string $method, string $url, array $options = []): ResponseInterface | ||
| { | ||
| $span = null; | ||
| $parent = $this->hub->getSpan(); | ||
|
|
||
| if (null !== $parent) { | ||
| $headers = $options['headers'] ?? []; | ||
| $headers['sentry-trace'] = $parent->toTraceparent(); | ||
| $options['headers'] = $headers; | ||
|
|
||
| $context = new SpanContext(); | ||
ste93cry marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| $context->setOp('http.client'); | ||
| $context->setDescription('HTTP ' . $method); | ||
| $context->setTags([ | ||
| 'http.method' => $method, | ||
| 'http.url' => $url, | ||
| ]); | ||
|
|
||
| $span = $parent->startChild($context); | ||
| } | ||
|
|
||
| return new TraceableResponse($this->client, $this->client->request($method, $url, $options), $span); | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| public function stream($responses, float $timeout = null): ResponseStreamInterface | ||
| { | ||
| if ($responses instanceof AbstractTraceableResponse) { | ||
| $responses = [$responses]; | ||
| } elseif (!is_iterable($responses)) { | ||
| throw new \TypeError(sprintf('"%s()" expects parameter 1 to be an iterable of TraceableResponse objects, "%s" given.', __METHOD__, get_debug_type($responses))); | ||
| } | ||
|
|
||
| return new ResponseStream(AbstractTraceableResponse::stream($this->client, $responses, $timeout)); | ||
| } | ||
|
|
||
| public function reset(): void | ||
| { | ||
| if ($this->client instanceof ResetInterface) { | ||
| $this->client->reset(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| public function setLogger(LoggerInterface $logger): void | ||
| { | ||
| if ($this->client instanceof LoggerAwareInterface) { | ||
| $this->client->setLogger($logger); | ||
| } | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.