Skip to content

Commit 039fd4e

Browse files
authored
Check if transaction is sampled before creating child spans (#898)
1 parent 490555d commit 039fd4e

12 files changed

+30
-22
lines changed

src/Sentry/Laravel/Features/CacheIntegration.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ public function handleRedisCommand(RedisEvents\CommandExecuted $event): void
7676
{
7777
$parentSpan = SentrySdk::getCurrentHub()->getSpan();
7878

79-
// If there is no tracing span active there is no need to handle the event
80-
if ($parentSpan === null) {
79+
// If there is no sampled span there is no need to handle the event
80+
if ($parentSpan === null || !$parentSpan->getSampled()) {
8181
return;
8282
}
8383

src/Sentry/Laravel/Features/FolioPackageIntegration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function handleViewMatched(ViewMatched $matched): void
4545

4646
$transaction = SentrySdk::getCurrentHub()->getTransaction();
4747

48-
if ($transaction === null) {
48+
if ($transaction === null || !$transaction->getSampled()) {
4949
return;
5050
}
5151

src/Sentry/Laravel/Features/HttpClientIntegration.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ public function handleRequestSendingHandlerForTracing(RequestSending $event): vo
6767
{
6868
$parentSpan = SentrySdk::getCurrentHub()->getSpan();
6969

70-
// If there is no tracing span active there is no need to handle the event
71-
if ($parentSpan === null) {
70+
// If there is no sampled span there is no need to handle the event
71+
if ($parentSpan === null || !$parentSpan->getSampled()) {
7272
return;
7373
}
7474

src/Sentry/Laravel/Features/LivewirePackageIntegration.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ public function handleComponentBoot(Component $component, ?string $method = null
107107

108108
$parentSpan = SentrySdk::getCurrentHub()->getSpan();
109109

110-
if ($parentSpan === null) {
110+
// If there is no sampled span there is no need to handle the event
111+
if ($parentSpan === null || !$parentSpan->getSampled()) {
111112
return;
112113
}
113114

src/Sentry/Laravel/Features/NotificationsIntegration.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ public function handleNotificationSending(NotificationSending $event): void
3838
{
3939
$parentSpan = SentrySdk::getCurrentHub()->getSpan();
4040

41-
if ($parentSpan === null) {
41+
// If there is no sampled span there is no need to handle the event
42+
if ($parentSpan === null || !$parentSpan->getSampled()) {
4243
return;
4344
}
4445

src/Sentry/Laravel/Features/QueueIntegration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function onBoot(Dispatcher $events): void
6262
Queue::createPayloadUsing(function (?string $connection, ?string $queue, ?array $payload): ?array {
6363
$parentSpan = SentrySdk::getCurrentHub()->getSpan();
6464

65-
if ($parentSpan !== null) {
65+
if ($parentSpan !== null && $parentSpan->getSampled()) {
6666
$context = (new SpanContext)
6767
->setOp(self::QUEUE_SPAN_OP_QUEUE_PUBLISH)
6868
->setData([

src/Sentry/Laravel/Tracing/EventHandler.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,8 @@ protected function queryExecutedHandler(DatabaseEvents\QueryExecuted $query): vo
165165

166166
$parentSpan = SentrySdk::getCurrentHub()->getSpan();
167167

168-
// If there is no tracing span active there is no need to handle the event
169-
if ($parentSpan === null) {
168+
// If there is no sampled span there is no need to handle the event
169+
if ($parentSpan === null || !$parentSpan->getSampled()) {
170170
return;
171171
}
172172

@@ -219,8 +219,8 @@ protected function responsePreparingHandler(RoutingEvents\PreparingResponse $eve
219219

220220
$parentSpan = SentrySdk::getCurrentHub()->getSpan();
221221

222-
// If there is no tracing span active there is no need to handle the event
223-
if ($parentSpan === null) {
222+
// If there is no sampled span there is no need to handle the event
223+
if ($parentSpan === null || !$parentSpan->getSampled()) {
224224
return;
225225
}
226226

@@ -234,8 +234,8 @@ protected function transactionBeginningHandler(DatabaseEvents\TransactionBeginni
234234
{
235235
$parentSpan = SentrySdk::getCurrentHub()->getSpan();
236236

237-
// If there is no tracing span active there is no need to handle the event
238-
if ($parentSpan === null) {
237+
// If there is no sampled span there is no need to handle the event
238+
if ($parentSpan === null || !$parentSpan->getSampled()) {
239239
return;
240240
}
241241

src/Sentry/Laravel/Tracing/Integrations/LighthouseIntegration.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ public function handleStartRequest(StartRequest $startRequest): void
101101
{
102102
$this->previousSpan = SentrySdk::getCurrentHub()->getSpan();
103103

104-
if ($this->previousSpan === null) {
104+
// If there is no sampled span there is no need to handle the event
105+
if ($this->previousSpan === null || !$this->previousSpan->getSampled()) {
105106
return;
106107
}
107108

src/Sentry/Laravel/Tracing/Middleware.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,11 @@ public function setBootedTimestamp(?float $timestamp = null): void
163163

164164
private function startTransaction(Request $request, HubInterface $sentry): void
165165
{
166+
// Prevent starting a new transaction if we are already in a transaction
167+
if ($sentry->getTransaction() !== null) {
168+
return;
169+
}
170+
166171
// Reset our internal state in case we are handling multiple requests (e.g. in Octane)
167172
$this->didRouteMatch = false;
168173

@@ -193,15 +198,15 @@ private function startTransaction(Request $request, HubInterface $sentry): void
193198

194199
$transaction = $sentry->startTransaction($context);
195200

201+
SentrySdk::getCurrentHub()->setSpan($transaction);
202+
196203
// If this transaction is not sampled, we can stop here to prevent doing work for nothing
197204
if (!$transaction->getSampled()) {
198205
return;
199206
}
200207

201208
$this->transaction = $transaction;
202209

203-
SentrySdk::getCurrentHub()->setSpan($this->transaction);
204-
205210
$bootstrapSpan = $this->addAppBootstrapSpan();
206211

207212
$appContextStart = new SpanContext;

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,8 @@ protected function wrapRouteDispatch(callable $dispatch, Route $route)
1212
{
1313
$parentSpan = SentrySdk::getCurrentHub()->getSpan();
1414

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.
18-
if ($parentSpan === null) {
15+
// If there is no sampled span there is no need to wrap the dispatch
16+
if ($parentSpan === null || !$parentSpan->getSampled()) {
1917
return $dispatch();
2018
}
2119

0 commit comments

Comments
 (0)