Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 5 additions & 0 deletions tests/EventListener/TracingConsoleListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -73,6 +76,7 @@ public function handleConsoleCommandEventStartsTransactionIfNoSpanIsSetOnHubData
$transactionContext->setName('<unnamed command>');
$transactionContext->setOrigin('auto.console');
$transactionContext->setSource(TransactionSource::task());
$transactionContext->getMetadata()->setSampleRand(0.1337);

yield [
new Command(),
Expand All @@ -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'),
Expand Down
17 changes: 17 additions & 0 deletions tests/EventListener/TracingRequestListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -216,6 +223,7 @@ public function handleKernelRequestEventDataProvider(): \Generator
'route' => '<unknown>',
'net.host.name' => 'www.example.com',
]);
$transactionContext->getMetadata()->setSampleRand(0.1337);

$request = Request::create('http://www.example.com');
$request->server->remove('REQUEST_TIME_FLOAT');
Expand All @@ -240,6 +248,7 @@ public function handleKernelRequestEventDataProvider(): \Generator
'route' => '<unknown>',
'net.host.ip' => '127.0.0.1',
]);
$transactionContext->getMetadata()->setSampleRand(0.1337);

yield 'request.server.HOST IS IPV4' => [
new Options(),
Expand Down Expand Up @@ -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(),
Expand All @@ -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(),
Expand All @@ -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(),
Expand All @@ -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(),
Expand All @@ -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(),
Expand All @@ -397,6 +411,7 @@ public function handleKernelRequestEventDataProvider(): \Generator
'route' => '<unknown>',
'net.host.name' => 'www.example.com',
]);
$transactionContext->getMetadata()->setSampleRand(0.1337);

yield 'request.attributes._controller IS ARRAY and NOT VALID CALLABLE' => [
new Options(),
Expand All @@ -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]),
Expand All @@ -446,6 +462,7 @@ public function handleKernelRequestEventDataProvider(): \Generator
'route' => '<unknown>',
'net.host.name' => '',
]);
$transactionContext->getMetadata()->setSampleRand(0.1337);

yield 'request.server.SERVER_PROTOCOL NOT EXISTS' => [
new Options(),
Expand Down
12 changes: 6 additions & 6 deletions tests/Tracing/HttpClient/TraceableHttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ public function testRequest(): void
$this->assertSame(200, $response->getStatusCode());
$this->assertSame('GET', $response->getInfo('http_method'));
$this->assertSame('https://username:[email protected]/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();
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tests/Twig/SentryExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public function testBaggageMetaFunctionWithNoActiveSpan(): void

SentrySdk::setCurrentHub($hub);

$this->assertSame('<meta name="baggage" content="sentry-trace_id=566e3688a61d4bc888951642d6f14a19,sentry-sample_rate=1,sentry-release=1.0.0,sentry-environment=development" />', $environment->render('foo.twig'));
$this->assertSame(\sprintf('<meta name="baggage" content="%s" />', $propagationContext->toBaggage()), $environment->render('foo.twig'));
}

public function testBaggageMetaFunctionWithActiveSpan(): void
Expand All @@ -164,7 +164,7 @@ public function testBaggageMetaFunctionWithActiveSpan(): void

$hub->setSpan($transaction);

$this->assertSame('<meta name="baggage" content="sentry-trace_id=a3c01c41d7b94b90aee23edac90f4319,sentry-transaction=%3Cunlabeled%20transaction%3E,sentry-release=1.0.0,sentry-environment=development" />', $environment->render('foo.twig'));
$this->assertSame(\sprintf('<meta name="baggage" content="%s" />', $transaction->toBaggage()), $environment->render('foo.twig'));
}

private static function isTwigBundlePackageInstalled(): bool
Expand Down
Loading