diff --git a/composer.json b/composer.json index 4dbee1b8..580a952c 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,7 @@ "php": "^7.2||^8.0", "guzzlehttp/psr7": "^2.1.1", "jean85/pretty-package-versions": "^1.5||^2.0", - "sentry/sentry": "^4.10.0", + "sentry/sentry": "^4.11.0", "symfony/cache-contracts": "^1.1||^2.4||^3.0", "symfony/config": "^4.4.20||^5.0.11||^6.0||^7.0", "symfony/console": "^4.4.20||^5.0.11||^6.0||^7.0", diff --git a/tests/EventListener/TracingConsoleListenerTest.php b/tests/EventListener/TracingConsoleListenerTest.php index fe87b34e..b619dfe7 100644 --- a/tests/EventListener/TracingConsoleListenerTest.php +++ b/tests/EventListener/TracingConsoleListenerTest.php @@ -44,6 +44,9 @@ public function testHandleConsoleCommandEventStartsTransactionIfNoSpanIsSetOnHub $this->hub->expects($this->once()) ->method('startTransaction') ->with($this->callback(function (TransactionContext $context) use ($expectedTransactionContext): bool { + // This value is random when the metadata is constructed, thus we set it to a fixed expected value since we don't care for the value here + $context->getMetadata()->setSampleRand(0.1337); + $this->assertEquals($expectedTransactionContext, $context); return true; @@ -73,6 +76,7 @@ public function handleConsoleCommandEventStartsTransactionIfNoSpanIsSetOnHubData $transactionContext->setName(''); $transactionContext->setOrigin('auto.console'); $transactionContext->setSource(TransactionSource::task()); + $transactionContext->getMetadata()->setSampleRand(0.1337); yield [ new Command(), @@ -84,6 +88,7 @@ public function handleConsoleCommandEventStartsTransactionIfNoSpanIsSetOnHubData $transactionContext->setName('app:command'); $transactionContext->setOrigin('auto.console'); $transactionContext->setSource(TransactionSource::task()); + $transactionContext->getMetadata()->setSampleRand(0.1337); yield [ new Command('app:command'), diff --git a/tests/EventListener/TracingRequestListenerTest.php b/tests/EventListener/TracingRequestListenerTest.php index 4430466f..1c0c3193 100644 --- a/tests/EventListener/TracingRequestListenerTest.php +++ b/tests/EventListener/TracingRequestListenerTest.php @@ -65,6 +65,9 @@ public function testHandleKernelRequestEvent(Options $options, Request $request, $this->hub->expects($this->once()) ->method('startTransaction') ->with($this->callback(function (TransactionContext $context) use ($expectedTransactionContext): bool { + // This value is random when the metadata is constructed, thus we set it to a fixed expected value since we don't care for the value here + $context->getMetadata()->setSampleRand(0.1337); + $this->assertEquals($expectedTransactionContext, $context); return true; @@ -108,6 +111,7 @@ public function handleKernelRequestEventDataProvider(): \Generator 'net.host.name' => 'www.example.com', ]); $transactionContext->getMetadata()->setDynamicSamplingContext($samplingContext); + $transactionContext->getMetadata()->setSampleRand(0.1337); yield 'request.headers.sentry-trace EXISTS' => [ new Options(), @@ -146,6 +150,7 @@ public function handleKernelRequestEventDataProvider(): \Generator 'net.host.name' => 'www.example.com', ]); $transactionContext->getMetadata()->setDynamicSamplingContext($samplingContext); + $transactionContext->getMetadata()->setSampleRand(0.1337); yield 'request.headers.traceparent EXISTS' => [ new Options(), @@ -184,6 +189,8 @@ public function handleKernelRequestEventDataProvider(): \Generator 'net.host.name' => 'www.example.com', ]); $transactionContext->getMetadata()->setDynamicSamplingContext($samplingContext); + $transactionContext->getMetadata()->setParentSamplingRate(1.0); + $transactionContext->getMetadata()->setSampleRand(0.1337); yield 'request.headers.sentry-trace and headers.baggage EXISTS' => [ new Options(), @@ -216,6 +223,7 @@ public function handleKernelRequestEventDataProvider(): \Generator 'route' => '', 'net.host.name' => 'www.example.com', ]); + $transactionContext->getMetadata()->setSampleRand(0.1337); $request = Request::create('http://www.example.com'); $request->server->remove('REQUEST_TIME_FLOAT'); @@ -240,6 +248,7 @@ public function handleKernelRequestEventDataProvider(): \Generator 'route' => '', 'net.host.ip' => '127.0.0.1', ]); + $transactionContext->getMetadata()->setSampleRand(0.1337); yield 'request.server.HOST IS IPV4' => [ new Options(), @@ -272,6 +281,7 @@ public function handleKernelRequestEventDataProvider(): \Generator 'route' => 'app_homepage', 'net.host.name' => 'www.example.com', ]); + $transactionContext->getMetadata()->setSampleRand(0.1337); yield 'request.attributes.route IS STRING' => [ new Options(), @@ -297,6 +307,7 @@ public function handleKernelRequestEventDataProvider(): \Generator 'route' => '/path', 'net.host.name' => 'www.example.com', ]); + $transactionContext->getMetadata()->setSampleRand(0.1337); yield 'request.attributes.route IS INSTANCEOF Symfony\Component\Routing\Route' => [ new Options(), @@ -322,6 +333,7 @@ public function handleKernelRequestEventDataProvider(): \Generator 'route' => 'App\\Controller::indexAction', 'net.host.name' => 'www.example.com', ]); + $transactionContext->getMetadata()->setSampleRand(0.1337); yield 'request.attributes._controller IS STRING' => [ new Options(), @@ -347,6 +359,7 @@ public function handleKernelRequestEventDataProvider(): \Generator 'route' => 'App\\Controller::indexAction', 'net.host.name' => 'www.example.com', ]); + $transactionContext->getMetadata()->setSampleRand(0.1337); yield 'request.attributes._controller IS CALLABLE (1)' => [ new Options(), @@ -372,6 +385,7 @@ public function handleKernelRequestEventDataProvider(): \Generator 'route' => 'class@anonymous::indexAction', 'net.host.name' => 'www.example.com', ]); + $transactionContext->getMetadata()->setSampleRand(0.1337); yield 'request.attributes._controller IS CALLABLE (2)' => [ new Options(), @@ -397,6 +411,7 @@ public function handleKernelRequestEventDataProvider(): \Generator 'route' => '', 'net.host.name' => 'www.example.com', ]); + $transactionContext->getMetadata()->setSampleRand(0.1337); yield 'request.attributes._controller IS ARRAY and NOT VALID CALLABLE' => [ new Options(), @@ -423,6 +438,7 @@ public function handleKernelRequestEventDataProvider(): \Generator 'net.host.name' => 'www.example.com', 'net.peer.ip' => '127.0.0.1', ]); + $transactionContext->getMetadata()->setSampleRand(0.1337); yield 'request.server.REMOTE_ADDR EXISTS and client.options.send_default_pii = TRUE' => [ new Options(['send_default_pii' => true]), @@ -446,6 +462,7 @@ public function handleKernelRequestEventDataProvider(): \Generator 'route' => '', 'net.host.name' => '', ]); + $transactionContext->getMetadata()->setSampleRand(0.1337); yield 'request.server.SERVER_PROTOCOL NOT EXISTS' => [ new Options(), diff --git a/tests/Tracing/HttpClient/TraceableHttpClientTest.php b/tests/Tracing/HttpClient/TraceableHttpClientTest.php index 31147116..4306378e 100644 --- a/tests/Tracing/HttpClient/TraceableHttpClientTest.php +++ b/tests/Tracing/HttpClient/TraceableHttpClientTest.php @@ -92,9 +92,9 @@ public function testRequest(): void $this->assertSame(200, $response->getStatusCode()); $this->assertSame('GET', $response->getInfo('http_method')); $this->assertSame('https://username:password@www.example.com/test-page?foo=bar#baz', $response->getInfo('url')); - $this->assertSame(['sentry-trace: ' . $spans[1]->toTraceparent()], $mockResponse->getRequestOptions()['normalized_headers']['sentry-trace']); - $this->assertSame(['traceparent: ' . $spans[1]->toW3CTraceparent()], $mockResponse->getRequestOptions()['normalized_headers']['traceparent']); - $this->assertSame(['baggage: ' . $transaction->toBaggage()], $mockResponse->getRequestOptions()['normalized_headers']['baggage']); + $this->assertSame([\sprintf('sentry-trace: %s', $spans[1]->toTraceparent())], $mockResponse->getRequestOptions()['normalized_headers']['sentry-trace']); + $this->assertSame([\sprintf('traceparent: %s', $spans[1]->toW3CTraceparent())], $mockResponse->getRequestOptions()['normalized_headers']['traceparent']); + $this->assertSame([\sprintf('baggage: %s', $transaction->toBaggage())], $mockResponse->getRequestOptions()['normalized_headers']['baggage']); $this->assertNotNull($transaction->getSpanRecorder()); $spans = $transaction->getSpanRecorder()->getSpans(); @@ -199,9 +199,9 @@ public function testRequestDoesContainsTracingHeadersWithoutTransaction(): void $this->assertSame(200, $response->getStatusCode()); $this->assertSame('POST', $response->getInfo('http_method')); $this->assertSame('https://www.example.com/test-page', $response->getInfo('url')); - $this->assertSame(['sentry-trace: 566e3688a61d4bc888951642d6f14a19-566e3688a61d4bc8'], $mockResponse->getRequestOptions()['normalized_headers']['sentry-trace']); - $this->assertSame(['traceparent: 00-566e3688a61d4bc888951642d6f14a19-566e3688a61d4bc8-00'], $mockResponse->getRequestOptions()['normalized_headers']['traceparent']); - $this->assertSame(['baggage: sentry-trace_id=566e3688a61d4bc888951642d6f14a19,sentry-public_key=public,sentry-release=1.0.0,sentry-environment=test'], $mockResponse->getRequestOptions()['normalized_headers']['baggage']); + $this->assertSame([\sprintf('sentry-trace: %s', $propagationContext->toTraceparent())], $mockResponse->getRequestOptions()['normalized_headers']['sentry-trace']); + $this->assertSame([\sprintf('traceparent: %s', $propagationContext->toW3CTraceparent())], $mockResponse->getRequestOptions()['normalized_headers']['traceparent']); + $this->assertSame([\sprintf('baggage: %s', $propagationContext->toBaggage())], $mockResponse->getRequestOptions()['normalized_headers']['baggage']); } public function testRequestSetsUnknownErrorAsSpanStatusIfResponseStatusCodeIsUnavailable(): void diff --git a/tests/Twig/SentryExtensionTest.php b/tests/Twig/SentryExtensionTest.php index 861ca14c..5cbfd25b 100644 --- a/tests/Twig/SentryExtensionTest.php +++ b/tests/Twig/SentryExtensionTest.php @@ -138,7 +138,7 @@ public function testBaggageMetaFunctionWithNoActiveSpan(): void SentrySdk::setCurrentHub($hub); - $this->assertSame('', $environment->render('foo.twig')); + $this->assertSame(\sprintf('', $propagationContext->toBaggage()), $environment->render('foo.twig')); } public function testBaggageMetaFunctionWithActiveSpan(): void @@ -164,7 +164,7 @@ public function testBaggageMetaFunctionWithActiveSpan(): void $hub->setSpan($transaction); - $this->assertSame('', $environment->render('foo.twig')); + $this->assertSame(\sprintf('', $transaction->toBaggage()), $environment->render('foo.twig')); } private static function isTwigBundlePackageInstalled(): bool