Skip to content

Commit

Permalink
Send span origin (#939)
Browse files Browse the repository at this point in the history
  • Loading branch information
stayallive authored Aug 14, 2024
1 parent 72b6731 commit 3694c0c
Show file tree
Hide file tree
Showing 13 changed files with 130 additions and 99 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"require": {
"php": "^7.2 | ^8.0",
"illuminate/support": "^6.0 | ^7.0 | ^8.0 | ^9.0 | ^10.0 | ^11.0",
"sentry/sentry": "^4.7",
"sentry/sentry": "^4.9",
"symfony/psr-http-message-bridge": "^1.0 | ^2.0 | ^6.0 | ^7.0",
"nyholm/psr7": "^1.0"
},
Expand Down
26 changes: 14 additions & 12 deletions src/Sentry/Laravel/Console/TestCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,18 +176,20 @@ public function log($level, $message, array $context = []): void
if ($this->option('transaction')) {
$this->clearErrorMessagesFromSDK();

$transactionContext = new TransactionContext();
$transactionContext->setSampled(true);
$transactionContext->setName('Sentry Test Transaction');
$transactionContext->setSource(TransactionSource::custom());
$transactionContext->setOp('sentry.test');

$transaction = $hub->startTransaction($transactionContext);

$spanContext = new SpanContext();
$spanContext->setOp('sentry.sent');

$span = $transaction->startChild($spanContext);
$transaction = $hub->startTransaction(
TransactionContext::make()
->setOp('sentry.test')
->setName('Sentry Test Transaction')
->setOrigin('auto.test.transaction')
->setSource(TransactionSource::custom())
->setSampled(true)
);

$span = $transaction->startChild(
SpanContext::make()
->setOp('sentry.sent')
->setOrigin('auto.test.span')
);

$this->info('Sending transaction...');

Expand Down
8 changes: 6 additions & 2 deletions src/Sentry/Laravel/Features/CacheIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ public function handleCacheEventsForTracing(Events\CacheEvent $event): void
->setData([
'cache.key' => $keys,
])
->setOrigin('auto.cache')
->setDescription(implode(', ', $keys))
)
);
Expand All @@ -136,6 +137,7 @@ public function handleCacheEventsForTracing(Events\CacheEvent $event): void
'cache.key' => $keys,
'cache.ttl' => $event->seconds,
])
->setOrigin('auto.cache')
->setDescription(implode(', ', $keys))
)
);
Expand All @@ -149,6 +151,7 @@ public function handleCacheEventsForTracing(Events\CacheEvent $event): void
->setData([
'cache.key' => [$event->key],
])
->setOrigin('auto.cache')
->setDescription($event->key)
)
);
Expand All @@ -165,8 +168,9 @@ public function handleRedisCommands(RedisEvents\CommandExecuted $event): void
return;
}

$context = new SpanContext();
$context->setOp('db.redis');
$context = SpanContext::make()
->setOp('db.redis')
->setOrigin('auto.cache.redis');

$keyForDescription = '';

Expand Down
30 changes: 16 additions & 14 deletions src/Sentry/Laravel/Features/HttpClientIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,23 +72,25 @@ public function handleRequestSendingHandlerForTracing(RequestSending $event): vo
return;
}

$context = new SpanContext;

$fullUri = $this->getFullUri($event->request->url());
$partialUri = $this->getPartialUri($fullUri);

$context->setOp('http.client');
$context->setDescription($event->request->method() . ' ' . $partialUri);
$context->setData([
'url' => $partialUri,
// See: https://develop.sentry.dev/sdk/performance/span-data-conventions/#http
'http.query' => $fullUri->getQuery(),
'http.fragment' => $fullUri->getFragment(),
'http.request.method' => $event->request->method(),
'http.request.body.size' => $event->request->toPsrRequest()->getBody()->getSize(),
]);

$this->pushSpan($parentSpan->startChild($context));
$this->pushSpan(
$parentSpan->startChild(
SpanContext::make()
->setOp('http.client')
->setData([
'url' => $partialUri,
// See: https://develop.sentry.dev/sdk/performance/span-data-conventions/#http
'http.query' => $fullUri->getQuery(),
'http.fragment' => $fullUri->getFragment(),
'http.request.method' => $event->request->method(),
'http.request.body.size' => $event->request->toPsrRequest()->getBody()->getSize(),
])
->setOrigin('auto.http.client')
->setDescription($event->request->method() . ' ' . $partialUri)
)
);
}

public function handleResponseReceivedHandlerForTracing(ResponseReceived $event): void
Expand Down
19 changes: 11 additions & 8 deletions src/Sentry/Laravel/Features/LivewirePackageIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,18 @@ public function handleComponentBoot(Component $component, ?string $method = null
return;
}

$context = new SpanContext;
$context->setOp('ui.livewire.component');
$context->setDescription(
empty($method)
? $component->getName()
: "{$component->getName()}::{$method}"
$this->pushSpan(
$parentSpan->startChild(
SpanContext::make()
->setOp('ui.livewire.component')
->setOrigin('auto.laravel.livewire')
->setDescription(
empty($method)
? $component->getName()
: "{$component->getName()}::{$method}"
)
)
);

$this->pushSpan($parentSpan->startChild($context));
}

public function handleComponentMount(Component $component, array $data): void
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 @@ -43,14 +43,15 @@ public function handleNotificationSending(NotificationSending $event): void
return;
}

$context = (new SpanContext)
$context = SpanContext::make()
->setOp('notification.send')
->setData([
'id' => $event->notification->id,
'channel' => $event->channel,
'notifiable' => $this->formatNotifiable($event->notifiable),
'notification' => get_class($event->notification),
])
->setOrigin('auto.laravel.notifications')
->setDescription($event->channel);

$this->pushSpan($parentSpan->startChild($context));
Expand Down
1 change: 1 addition & 0 deletions src/Sentry/Laravel/Features/QueueIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ public function handleJobProcessingQueueEvent(JobProcessing $event): void

$context->setOp('queue.process');
$context->setData($job);
$context->setOrigin('auto.queue');
$context->setStartTimestamp(microtime(true));

// When the parent span is null we start a new transaction otherwise we start a child of the current span
Expand Down
18 changes: 10 additions & 8 deletions src/Sentry/Laravel/Features/Storage/FilesystemDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,16 @@ protected function withSentry(string $method, array $args, ?string $description,
}

if ($this->recordSpans) {
$spanContext = new SpanContext;
$spanContext->setOp($op);
$spanContext->setData($data);
$spanContext->setDescription($description);

return trace(function () use ($method, $args) {
return $this->filesystem->{$method}(...$args);
}, $spanContext);
return trace(
function () use ($method, $args) {
return $this->filesystem->{$method}(...$args);
},
SpanContext::make()
->setOp($op)
->setData($data)
->setOrigin('auto.filesystem')
->setDescription($description)
);
}

return $this->filesystem->{$method}(...$args);
Expand Down
46 changes: 27 additions & 19 deletions src/Sentry/Laravel/Tracing/EventHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public function subscribe(Dispatcher $dispatcher): void
* Pass through the event and capture any errors.
*
* @param string $method
* @param array $arguments
* @param array $arguments
*/
public function __call(string $method, array $arguments)
{
Expand Down Expand Up @@ -170,16 +170,18 @@ protected function queryExecutedHandler(DatabaseEvents\QueryExecuted $query): vo
return;
}

$context = new SpanContext();
$context->setOp('db.sql.query');
$context->setDescription($query->sql);
$context->setData([
'db.name' => $query->connection->getDatabaseName(),
'db.system' => $query->connection->getDriverName(),
'server.address' => $query->connection->getConfig('host'),
'server.port' => $query->connection->getConfig('port'),
]);
$context->setStartTimestamp(microtime(true) - $query->time / 1000);
$context = SpanContext::make()
->setOp('db.sql.query')
->setData([
'db.name' => $query->connection->getDatabaseName(),
'db.system' => $query->connection->getDriverName(),
'server.address' => $query->connection->getConfig('host'),
'server.port' => $query->connection->getConfig('port'),
])
->setOrigin('auto.db')
->setDescription($query->sql)
->setStartTimestamp(microtime(true) - $query->time / 1000);

$context->setEndTimestamp($context->getStartTimestamp() + $query->time / 1000);

if ($this->traceSqlBindings) {
Expand Down Expand Up @@ -224,10 +226,13 @@ protected function responsePreparingHandler(RoutingEvents\PreparingResponse $eve
return;
}

$context = new SpanContext;
$context->setOp('http.route.response');

$this->pushSpan($parentSpan->startChild($context));
$this->pushSpan(
$parentSpan->startChild(
SpanContext::make()
->setOp('http.route.response')
->setOrigin('auto.http.server')
)
);
}

protected function transactionBeginningHandler(DatabaseEvents\TransactionBeginning $event): void
Expand All @@ -239,10 +244,13 @@ protected function transactionBeginningHandler(DatabaseEvents\TransactionBeginni
return;
}

$context = new SpanContext;
$context->setOp('db.transaction');

$this->pushSpan($parentSpan->startChild($context));
$this->pushSpan(
$parentSpan->startChild(
SpanContext::make()
->setOp('db.transaction')
->setOrigin('auto.db')
)
);
}

protected function transactionCommittedHandler(DatabaseEvents\TransactionCommitted $event): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class LighthouseIntegration implements IntegrationInterface

public function __construct(EventDispatcher $eventDispatcher, bool $ignoreOperationName = false)
{
$this->eventDispatcher = $eventDispatcher;
$this->eventDispatcher = $eventDispatcher;
$this->ignoreOperationName = $ignoreOperationName;
}

Expand Down Expand Up @@ -106,11 +106,12 @@ public function handleStartRequest(StartRequest $startRequest): void
return;
}

$context = new SpanContext;
$context->setOp('graphql.request');
$context = SpanContext::make()
->setOp('graphql.request')
->setOrigin('auto.graphql.server');

$this->operations = [];
$this->requestSpan = $this->previousSpan->startChild($context);
$this->operations = [];
$this->requestSpan = $this->previousSpan->startChild($context);
$this->operationSpan = null;

SentrySdk::getCurrentHub()->setSpan($this->requestSpan);
Expand All @@ -136,8 +137,9 @@ public function handleStartExecution(StartExecution $startExecution): void

$this->updateTransactionName();

$context = new SpanContext;
$context->setOp("graphql.{$operationDefinition->operation}");
$context = SpanContext::make()
->setOp("graphql.{$operationDefinition->operation}")
->setOrigin('auto.graphql.server');

$this->operationSpan = $this->requestSpan->startChild($context);

Expand Down
38 changes: 21 additions & 17 deletions src/Sentry/Laravel/Tracing/Middleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ private function startTransaction(Request $request, HubInterface $sentry): void

$context->setOp('http.server');
$context->setName($requestPath);
$context->setOrigin('auto.http.server');
$context->setSource(TransactionSource::url());
$context->setStartTimestamp($requestStartTime);

Expand All @@ -180,11 +181,12 @@ private function startTransaction(Request $request, HubInterface $sentry): void

$bootstrapSpan = $this->addAppBootstrapSpan();

$appContextStart = new SpanContext;
$appContextStart->setOp('middleware.handle');
$appContextStart->setStartTimestamp($bootstrapSpan ? $bootstrapSpan->getEndTimestamp() : microtime(true));

$this->appSpan = $this->transaction->startChild($appContextStart);
$this->appSpan = $this->transaction->startChild(
SpanContext::make()
->setOp('middleware.handle')
->setOrigin('auto.http.server')
->setStartTimestamp($bootstrapSpan ? $bootstrapSpan->getEndTimestamp() : microtime(true))
);

SentrySdk::getCurrentHub()->setSpan($this->appSpan);
}
Expand All @@ -195,12 +197,13 @@ private function addAppBootstrapSpan(): ?Span
return null;
}

$spanContextStart = new SpanContext;
$spanContextStart->setOp('app.bootstrap');
$spanContextStart->setStartTimestamp($this->transaction->getStartTimestamp());
$spanContextStart->setEndTimestamp($this->bootedTimestamp);

$span = $this->transaction->startChild($spanContextStart);
$span = $this->transaction->startChild(
SpanContext::make()
->setOp('app.bootstrap')
->setOrigin('auto.http.server')
->setStartTimestamp($this->transaction->getStartTimestamp())
->setEndTimestamp($this->bootedTimestamp)
);

// Add more information about the bootstrap section if possible
$this->addBootDetailTimeSpans($span);
Expand All @@ -219,12 +222,13 @@ private function addBootDetailTimeSpans(Span $bootstrap): void
return;
}

$autoload = new SpanContext;
$autoload->setOp('app.php.autoload');
$autoload->setStartTimestamp($this->transaction->getStartTimestamp());
$autoload->setEndTimestamp(SENTRY_AUTOLOAD);

$bootstrap->startChild($autoload);
$bootstrap->startChild(
SpanContext::make()
->setOp('app.php.autoload')
->setOrigin('auto.http.server')
->setStartTimestamp($this->transaction->getStartTimestamp())
->setEndTimestamp(SENTRY_AUTOLOAD)
);
}

private function hydrateResponseData(SymfonyResponse $response): void
Expand Down
11 changes: 6 additions & 5 deletions src/Sentry/Laravel/Tracing/Routing/TracingRoutingDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ protected function wrapRouteDispatch(callable $dispatch, Route $route)
// @see: https://github.com/getsentry/sentry-laravel/issues/917
$action = $route->getActionName() instanceof Closure ? 'Closure' : $route->getActionName();

$context = new SpanContext;
$context->setOp('http.route');
$context->setDescription($action);

$span = $parentSpan->startChild($context);
$span = $parentSpan->startChild(
SpanContext::make()
->setOp('http.route')
->setOrigin('auto.http.server')
->setDescription($action)
);

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

Expand Down
Loading

0 comments on commit 3694c0c

Please sign in to comment.