-
Notifications
You must be signed in to change notification settings - Fork 171
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for distributed tracing of the Symfony HTTP client reques…
…ts (#606) Co-authored-by: Alessandro Lai <alessandro.lai85@gmail.com> Co-authored-by: Stefano Arlandini <sarlandini@alice.it>
- Loading branch information
1 parent
6db5a35
commit 0c6eee6
Showing
32 changed files
with
1,020 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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 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 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 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 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(); | ||
$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.