diff --git a/CHANGELOG.md b/CHANGELOG.md index 09fe0325..5ba0013b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## Unreleased - Add `TracingDriverConnectionInterface::getNativeConnection()` method to get the original driver connection (#597) +- Add support for tracing of http client requests (#606) ## 4.2.6 (2022-01-10) diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 15f9be12..7f0503ea 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -91,4 +91,23 @@ PostResponseEvent + + + $container->getParameter('sentry.tracing.cache.enabled') + + + + + $container->getParameter('sentry.tracing.enabled') + $container->getParameter('sentry.tracing.http_client.enabled') + + + + + $container->getParameter('sentry.tracing.enabled') + $container->getParameter('sentry.tracing.dbal.enabled') + $container->getParameter('sentry.tracing.dbal.connections') + $container->getParameter('doctrine.connections') + + diff --git a/tests/DependencyInjection/Compiler/HttpClientTracingPassTest.php b/tests/DependencyInjection/Compiler/HttpClientTracingPassTest.php new file mode 100644 index 00000000..16861636 --- /dev/null +++ b/tests/DependencyInjection/Compiler/HttpClientTracingPassTest.php @@ -0,0 +1,73 @@ + $params + * + * @dataProvider provideDisableContainerParameters + */ + public function testShouldNotDecorateHttpClientServicesIfDisabled(array $params): void + { + $container = new ContainerBuilder(new ParameterBag($params)); + $container->register('http.client', HttpClient::class) + ->setPublic(true) + ->addTag('http_client.client'); + + $container->addCompilerPass(new HttpClientTracingPass()); + $container->compile(); + + self::assertEquals(HttpClient::class, $container->getDefinition('http.client')->getClass()); + } + + /** + * @return iterable + */ + public function provideDisableContainerParameters(): iterable + { + yield [['sentry.tracing.enabled' => true, 'sentry.tracing.http_client.enabled' => false]]; + yield [['sentry.tracing.enabled' => false, 'sentry.tracing.http_client.enabled' => false]]; + yield [['sentry.tracing.enabled' => false, 'sentry.tracing.http_client.enabled' => true]]; + } + + public function testShouldDecorateHttpClients(): void + { + $container = new ContainerBuilder(new ParameterBag(['sentry.tracing.enabled' => true, 'sentry.tracing.http_client.enabled' => true])); + $container->register(HubInterface::class) + ->setFactory([HubAdapter::class, 'getInstance']); + $container->register('http.client', HttpClient::class) + ->setPublic(true) + ->addTag('http_client.client'); + + $container->addCompilerPass(new HttpClientTracingPass()); + $container->compile(); + + self::assertEquals(TraceableHttpClient::class, $container->findDefinition('http.client')->getClass()); + } + + private static function isHttpClientPackageInstalled(): bool + { + return interface_exists(HttpClientInterface::class); + } +} diff --git a/tests/Tracing/HttpClient/TraceableHttpClientTest.php b/tests/Tracing/HttpClient/TraceableHttpClientTest.php index 62e7f985..5ae72757 100644 --- a/tests/Tracing/HttpClient/TraceableHttpClientTest.php +++ b/tests/Tracing/HttpClient/TraceableHttpClientTest.php @@ -7,6 +7,8 @@ use PHPUnit\Framework\Constraint\Callback; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; +use Psr\Log\LoggerAwareInterface; +use Psr\Log\NullLogger; use Sentry\SentryBundle\Tracing\HttpClient\AbstractTraceableResponse; use Sentry\SentryBundle\Tracing\HttpClient\TraceableHttpClient; use Sentry\State\HubInterface; @@ -14,6 +16,7 @@ use Sentry\Tracing\TransactionContext; use Symfony\Contracts\HttpClient\HttpClientInterface; use Symfony\Contracts\HttpClient\ResponseInterface; +use Symfony\Contracts\Service\ResetInterface; final class TraceableHttpClientTest extends TestCase { @@ -23,7 +26,7 @@ final class TraceableHttpClientTest extends TestCase private $hub; /** - * @var MockObject&HttpClientInterface + * @var MockObject&HttpClientInterface&LoggerAwareInterface&ResetInterface */ private $client; @@ -42,7 +45,7 @@ public static function setUpBeforeClass(): void protected function setUp(): void { $this->hub = $this->createMock(HubInterface::class); - $this->client = $this->createMock(HttpClientInterface::class); + $this->client = $this->createMock(TestableHttpClientInterface::class); $this->httpClient = new TraceableHttpClient($this->client, $this->hub); } @@ -91,8 +94,30 @@ public function testRequest(): void ], $spans[1]->getTags()); } + public function testSetLoggerShouldBeForwardedToDecoratedInstance(): void + { + $logger = new NullLogger(); + $this->client->expects($this->once()) + ->method('setLogger') + ->with($logger); + + $this->httpClient->setLogger($logger); + } + + public function testResetCallShouldBeForwardedToDecoratedInstance(): void + { + $this->client->expects($this->once()) + ->method('reset'); + + $this->httpClient->reset(); + } + private static function isHttpClientPackageInstalled(): bool { return interface_exists(HttpClientInterface::class); } } + +interface TestableHttpClientInterface extends HttpClientInterface, LoggerAwareInterface, ResetInterface +{ +}