Skip to content

Commit 7919690

Browse files
authored
Always start performance tracing (#600)
1 parent 20a6d07 commit 7919690

File tree

8 files changed

+57
-41
lines changed

8 files changed

+57
-41
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unreleased
44

5+
- Remove incorrect checks if performance tracing should be enabled and rely on the transaction sampling decision instead (#600)
6+
57
## 3.0.0
68

79
**New features**

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
"require": {
2424
"php": "^7.2 | ^8.0",
2525
"illuminate/support": "^6.0 | ^7.0 | ^8.0 | ^9.0",
26-
"sentry/sentry": "^3.9",
27-
"sentry/sdk": "^3.1",
26+
"sentry/sentry": "^3.10",
27+
"sentry/sdk": "^3.3",
2828
"symfony/psr-http-message-bridge": "^1.0 | ^2.0",
2929
"nyholm/psr7": "^1.0"
3030
},

src/Sentry/Laravel/EventHandler.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use Illuminate\Http\Request;
1515
use Illuminate\Log\Events as LogEvents;
1616
use Illuminate\Queue\Events as QueueEvents;
17-
use Illuminate\Queue\QueueManager;
1817
use Illuminate\Routing\Events as RoutingEvents;
1918
use Laravel\Octane\Events as Octane;
2019
use Laravel\Sanctum\Events as Sanctum;

src/Sentry/Laravel/ServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ private function resolveIntegrationsFromUserConfig(): array
238238

239239
$enableDefaultTracingIntegrations = $userConfig['tracing']['default_integrations'] ?? true;
240240

241-
if ($enableDefaultTracingIntegrations && $this->couldHavePerformanceTracingEnabled()) {
241+
if ($enableDefaultTracingIntegrations) {
242242
$integrationsToResolve = array_merge($integrationsToResolve, TracingServiceProvider::DEFAULT_INTEGRATIONS);
243243
}
244244

src/Sentry/Laravel/Tracing/EventHandler.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ protected function transactionBeginningHandler(DatabaseEvents\TransactionBeginni
250250
{
251251
$parentSpan = SentrySdk::getCurrentHub()->getSpan();
252252

253+
// If there is no tracing span active there is no need to handle the event
253254
if ($parentSpan === null) {
254255
return;
255256
}
@@ -284,6 +285,7 @@ protected function httpClientRequestSendingHandler(HttpClientEvents\RequestSendi
284285
{
285286
$parentSpan = SentrySdk::getCurrentHub()->getSpan();
286287

288+
// If there is no tracing span active there is no need to handle the event
287289
if ($parentSpan === null) {
288290
return;
289291
}
@@ -387,12 +389,10 @@ private function afterQueuedJob(?SpanStatus $status = null): void
387389
{
388390
$span = $this->popSpan();
389391

390-
if ($span === null) {
391-
return;
392+
if ($span !== null) {
393+
$span->finish();
394+
$span->setStatus($status);
392395
}
393-
394-
$span->setStatus($status);
395-
$span->finish();
396396
}
397397

398398
private function pushSpan(Span $span): void

src/Sentry/Laravel/Tracing/Middleware.php

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -62,26 +62,29 @@ public function handle(Request $request, Closure $next)
6262
*/
6363
public function terminate(Request $request, $response): void
6464
{
65-
if ($this->transaction !== null && app()->bound(HubInterface::class)) {
66-
// We stop here if a route has not been matched unless we are configured to trace missing routes
67-
if (config('sentry.tracing.missing_routes', false) === false && $request->route() === null) {
68-
return;
69-
}
65+
// If there is no transaction or the HubInterface is not bound in the container there is nothing for us to do
66+
if ($this->transaction === null || !app()->bound(HubInterface::class)) {
67+
return;
68+
}
7069

71-
if ($this->appSpan !== null) {
72-
$this->appSpan->finish();
73-
}
70+
// We stop here if a route has not been matched unless we are configured to trace missing routes
71+
if (config('sentry.tracing.missing_routes', false) === false && $request->route() === null) {
72+
return;
73+
}
7474

75-
// Make sure we set the transaction and not have a child span in the Sentry SDK
76-
// If the transaction is not on the scope during finish, the trace.context is wrong
77-
SentrySdk::getCurrentHub()->setSpan($this->transaction);
75+
if ($this->appSpan !== null) {
76+
$this->appSpan->finish();
77+
}
7878

79-
if ($response instanceof SymfonyResponse) {
80-
$this->hydrateResponseData($response);
81-
}
79+
// Make sure we set the transaction and not have a child span in the Sentry SDK
80+
// If the transaction is not on the scope during finish, the trace.context is wrong
81+
SentrySdk::getCurrentHub()->setSpan($this->transaction);
8282

83-
$this->transaction->finish();
83+
if ($response instanceof SymfonyResponse) {
84+
$this->hydrateResponseData($response);
8485
}
86+
87+
$this->transaction->finish();
8588
}
8689

8790
/**
@@ -119,7 +122,14 @@ private function startTransaction(Request $request, HubInterface $sentry): void
119122
'method' => strtoupper($request->method()),
120123
]);
121124

122-
$this->transaction = $sentry->startTransaction($context);
125+
$transaction = $sentry->startTransaction($context);
126+
127+
// If this transaction is not sampled, don't set it either and stop doing work from this point on
128+
if (!$transaction->getSampled()) {
129+
return;
130+
}
131+
132+
$this->transaction = $transaction;
123133

124134
// Setting the Transaction on the Hub
125135
SentrySdk::getCurrentHub()->setSpan($this->transaction);

src/Sentry/Laravel/Tracing/Routing/TracingRoutingDispatcher.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ protected function wrapRouteDispatch(callable $dispatch, Route $route)
1212
{
1313
$parentSpan = SentrySdk::getCurrentHub()->getSpan();
1414

15-
// When there is no active transaction we can skip doing anything and just immediately return the callable
15+
// When there is no span we can skip creating
16+
// the span and just immediately return with the
17+
// callable result because there is no transaction.
1618
if ($parentSpan === null) {
1719
return $dispatch();
1820
}

src/Sentry/Laravel/Tracing/ServiceProvider.php

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,29 @@ class ServiceProvider extends BaseServiceProvider
2626

2727
public function boot(): void
2828
{
29-
if ($this->hasDsnSet() && $this->couldHavePerformanceTracingEnabled()) {
30-
$tracingConfig = $this->getUserConfig()['tracing'] ?? [];
29+
// If there is no DSN set we register nothing since it's impossible for us to send traces without a DSN set
30+
if (!$this->hasDsnSet()) {
31+
return;
32+
}
3133

32-
$this->bindEvents($tracingConfig);
34+
$this->app->booted(function () {
35+
$this->app->make(Middleware::class)->setBootedTimestamp();
36+
});
3337

34-
$this->bindViewEngine($tracingConfig);
38+
$tracingConfig = $this->getUserConfig()['tracing'] ?? [];
3539

36-
$this->decorateRoutingDispatchers();
40+
$this->bindEvents($tracingConfig);
3741

38-
if ($this->app->bound(HttpKernelInterface::class)) {
39-
/** @var \Illuminate\Foundation\Http\Kernel $httpKernel */
40-
$httpKernel = $this->app->make(HttpKernelInterface::class);
42+
$this->bindViewEngine($tracingConfig);
4143

42-
if ($httpKernel instanceof HttpKernel) {
43-
$httpKernel->prependMiddleware(Middleware::class);
44-
}
44+
$this->decorateRoutingDispatchers();
45+
46+
if ($this->app->bound(HttpKernelInterface::class)) {
47+
/** @var \Illuminate\Foundation\Http\Kernel $httpKernel */
48+
$httpKernel = $this->app->make(HttpKernelInterface::class);
49+
50+
if ($httpKernel instanceof HttpKernel) {
51+
$httpKernel->prependMiddleware(Middleware::class);
4552
}
4653
}
4754
}
@@ -58,10 +65,6 @@ public function register(): void
5865

5966
return new BacktraceHelper($options, new RepresentationSerializer($options));
6067
});
61-
62-
$this->app->booted(function () {
63-
$this->app->make(Middleware::class)->setBootedTimestamp();
64-
});
6568
}
6669

6770
private function bindEvents(array $tracingConfig): void

0 commit comments

Comments
 (0)