Skip to content

Commit 56f7d9f

Browse files
committed
Prefill the transaction context with the data retrieved from the sentry-trace HTTP header
1 parent 58319f8 commit 56f7d9f

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

src/EventListener/TracingRequestListener.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,18 @@
99
use Symfony\Component\HttpFoundation\Request;
1010
use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
1111

12+
/**
13+
* This event listener acts on the master requests and starts a transaction
14+
* to report performance data to Sentry. It gathers useful data like the
15+
* HTTP status code of the response or the name of the route that handles
16+
* the request and add them as tags.
17+
*/
1218
final class TracingRequestListener extends AbstractTracingRequestListener
1319
{
1420
/**
21+
* This method is called for each subrequest handled by the framework and
22+
* starts a new {@see Transaction}.
23+
*
1524
* @param RequestListenerRequestEvent $event The event
1625
*/
1726
public function handleKernelRequestEvent(RequestListenerRequestEvent $event): void
@@ -24,7 +33,7 @@ public function handleKernelRequestEvent(RequestListenerRequestEvent $event): vo
2433
$request = $event->getRequest();
2534
$requestStartTime = $request->server->get('REQUEST_TIME_FLOAT', microtime(true));
2635

27-
$context = new TransactionContext();
36+
$context = TransactionContext::fromSentryTrace($request->headers->get('sentry-trace', ''));
2837
$context->setOp('http.server');
2938
$context->setName(sprintf('%s %s%s%s', $request->getMethod(), $request->getSchemeAndHttpHost(), $request->getBaseUrl(), $request->getPathInfo()));
3039
$context->setStartTimestamp($requestStartTime);

src/EventListener/TracingSubRequestListener.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
use Sentry\Tracing\SpanContext;
99
use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
1010

11+
/**
12+
* This event listener acts on the sub requests and starts a child span of the
13+
* current transaction to gather performance data for each of them.
14+
*/
1115
final class TracingSubRequestListener extends AbstractTracingRequestListener
1216
{
1317
/**

tests/EventListener/TracingRequestListenerTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
use Sentry\SentryBundle\EventListener\RequestListenerResponseEvent;
1313
use Sentry\SentryBundle\EventListener\TracingRequestListener;
1414
use Sentry\State\HubInterface;
15+
use Sentry\Tracing\SpanId;
1516
use Sentry\Tracing\SpanStatus;
17+
use Sentry\Tracing\TraceId;
1618
use Sentry\Tracing\Transaction;
1719
use Sentry\Tracing\TransactionContext;
1820
use Symfony\Bridge\PhpUnit\ClockMock;
@@ -82,6 +84,35 @@ public function testHandleKernelRequestEvent(Options $options, Request $request,
8284
*/
8385
public function handleKernelRequestEventDataProvider(): \Generator
8486
{
87+
$transactionContext = new TransactionContext();
88+
$transactionContext->setTraceId(new TraceId('566e3688a61d4bc888951642d6f14a19'));
89+
$transactionContext->setParentSpanId(new SpanId('566e3688a61d4bc8'));
90+
$transactionContext->setParentSampled(true);
91+
$transactionContext->setName('GET http://www.example.com/');
92+
$transactionContext->setOp('http.server');
93+
$transactionContext->setStartTimestamp(1613493597.010275);
94+
$transactionContext->setTags([
95+
'net.host.port' => '80',
96+
'http.method' => 'GET',
97+
'http.url' => 'http://www.example.com/',
98+
'http.flavor' => '1.1',
99+
'route' => '<unknown>',
100+
'net.host.name' => 'www.example.com',
101+
]);
102+
103+
yield 'request.server.sentry-trace EXISTS' => [
104+
new Options(['send_default_pii' => false]),
105+
Request::create(
106+
'http://www.example.com',
107+
'GET',
108+
[],
109+
[],
110+
[],
111+
['HTTP_sentry-trace' => '566e3688a61d4bc888951642d6f14a19-566e3688a61d4bc8-1']
112+
),
113+
$transactionContext,
114+
];
115+
85116
$transactionContext = new TransactionContext();
86117
$transactionContext->setName('GET http://www.example.com/');
87118
$transactionContext->setOp('http.server');

0 commit comments

Comments
 (0)