Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test span sampled status before creating child spans #898

Merged
merged 1 commit into from
May 24, 2024
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
4 changes: 2 additions & 2 deletions src/Sentry/Laravel/Features/CacheIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ public function handleRedisCommand(RedisEvents\CommandExecuted $event): void
{
$parentSpan = SentrySdk::getCurrentHub()->getSpan();

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

Expand Down
2 changes: 1 addition & 1 deletion src/Sentry/Laravel/Features/FolioPackageIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function handleViewMatched(ViewMatched $matched): void

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

if ($transaction === null) {
if ($transaction === null || !$transaction->getSampled()) {
return;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Sentry/Laravel/Features/HttpClientIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ public function handleRequestSendingHandlerForTracing(RequestSending $event): vo
{
$parentSpan = SentrySdk::getCurrentHub()->getSpan();

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

Expand Down
3 changes: 2 additions & 1 deletion src/Sentry/Laravel/Features/LivewirePackageIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ public function handleComponentBoot(Component $component, ?string $method = null

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

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

Expand Down
3 changes: 2 additions & 1 deletion src/Sentry/Laravel/Features/NotificationsIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public function handleNotificationSending(NotificationSending $event): void
{
$parentSpan = SentrySdk::getCurrentHub()->getSpan();

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

Expand Down
2 changes: 1 addition & 1 deletion src/Sentry/Laravel/Features/QueueIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function onBoot(Dispatcher $events): void
Queue::createPayloadUsing(function (?string $connection, ?string $queue, ?array $payload): ?array {
$parentSpan = SentrySdk::getCurrentHub()->getSpan();

if ($parentSpan !== null) {
if ($parentSpan !== null && $parentSpan->getSampled()) {
$context = (new SpanContext)
->setOp(self::QUEUE_SPAN_OP_QUEUE_PUBLISH)
->setData([
Expand Down
12 changes: 6 additions & 6 deletions src/Sentry/Laravel/Tracing/EventHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,8 @@ protected function queryExecutedHandler(DatabaseEvents\QueryExecuted $query): vo

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

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

Expand Down Expand Up @@ -219,8 +219,8 @@ protected function responsePreparingHandler(RoutingEvents\PreparingResponse $eve

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

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

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ public function handleStartRequest(StartRequest $startRequest): void
{
$this->previousSpan = SentrySdk::getCurrentHub()->getSpan();

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

Expand Down
9 changes: 7 additions & 2 deletions src/Sentry/Laravel/Tracing/Middleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,11 @@ public function setBootedTimestamp(?float $timestamp = null): void

private function startTransaction(Request $request, HubInterface $sentry): void
{
// Prevent starting a new transaction if we are already in a transaction
if ($sentry->getTransaction() !== null) {
return;
}

// Reset our internal state in case we are handling multiple requests (e.g. in Octane)
$this->didRouteMatch = false;

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

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

SentrySdk::getCurrentHub()->setSpan($transaction);

// If this transaction is not sampled, we can stop here to prevent doing work for nothing
if (!$transaction->getSampled()) {
return;
}

$this->transaction = $transaction;

SentrySdk::getCurrentHub()->setSpan($this->transaction);

$bootstrapSpan = $this->addAppBootstrapSpan();

$appContextStart = new SpanContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ protected function wrapRouteDispatch(callable $dispatch, Route $route)
{
$parentSpan = SentrySdk::getCurrentHub()->getSpan();

// When there is no span we can skip creating
// the span and just immediately return with the
// callable result because there is no transaction.
if ($parentSpan === null) {
// If there is no sampled span there is no need to wrap the dispatch
if ($parentSpan === null || !$parentSpan->getSampled()) {
return $dispatch();
}

Expand Down
3 changes: 2 additions & 1 deletion src/Sentry/Laravel/Tracing/ViewEngineDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ public function get($path, array $data = []): string
{
$parentSpan = SentrySdk::getCurrentHub()->getSpan();

if ($parentSpan === null) {
// If there is no sampled span there is no need to wrap the engine call
if ($parentSpan === null || !$parentSpan->getSampled()) {
return $this->engine->get($path, $data);
}

Expand Down
1 change: 1 addition & 0 deletions test/Sentry/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ protected function startTransaction(): Transaction

$transaction = $hub->startTransaction(new TransactionContext);
$transaction->initSpanRecorder();
$transaction->setSampled(true);

$this->getCurrentSentryScope()->setSpan($transaction);

Expand Down
Loading