Skip to content

Commit 3694c0c

Browse files
authored
Send span origin (#939)
1 parent 72b6731 commit 3694c0c

13 files changed

+130
-99
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"require": {
2626
"php": "^7.2 | ^8.0",
2727
"illuminate/support": "^6.0 | ^7.0 | ^8.0 | ^9.0 | ^10.0 | ^11.0",
28-
"sentry/sentry": "^4.7",
28+
"sentry/sentry": "^4.9",
2929
"symfony/psr-http-message-bridge": "^1.0 | ^2.0 | ^6.0 | ^7.0",
3030
"nyholm/psr7": "^1.0"
3131
},

src/Sentry/Laravel/Console/TestCommand.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -176,18 +176,20 @@ public function log($level, $message, array $context = []): void
176176
if ($this->option('transaction')) {
177177
$this->clearErrorMessagesFromSDK();
178178

179-
$transactionContext = new TransactionContext();
180-
$transactionContext->setSampled(true);
181-
$transactionContext->setName('Sentry Test Transaction');
182-
$transactionContext->setSource(TransactionSource::custom());
183-
$transactionContext->setOp('sentry.test');
184-
185-
$transaction = $hub->startTransaction($transactionContext);
186-
187-
$spanContext = new SpanContext();
188-
$spanContext->setOp('sentry.sent');
189-
190-
$span = $transaction->startChild($spanContext);
179+
$transaction = $hub->startTransaction(
180+
TransactionContext::make()
181+
->setOp('sentry.test')
182+
->setName('Sentry Test Transaction')
183+
->setOrigin('auto.test.transaction')
184+
->setSource(TransactionSource::custom())
185+
->setSampled(true)
186+
);
187+
188+
$span = $transaction->startChild(
189+
SpanContext::make()
190+
->setOp('sentry.sent')
191+
->setOrigin('auto.test.span')
192+
);
191193

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

src/Sentry/Laravel/Features/CacheIntegration.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ public function handleCacheEventsForTracing(Events\CacheEvent $event): void
116116
->setData([
117117
'cache.key' => $keys,
118118
])
119+
->setOrigin('auto.cache')
119120
->setDescription(implode(', ', $keys))
120121
)
121122
);
@@ -136,6 +137,7 @@ public function handleCacheEventsForTracing(Events\CacheEvent $event): void
136137
'cache.key' => $keys,
137138
'cache.ttl' => $event->seconds,
138139
])
140+
->setOrigin('auto.cache')
139141
->setDescription(implode(', ', $keys))
140142
)
141143
);
@@ -149,6 +151,7 @@ public function handleCacheEventsForTracing(Events\CacheEvent $event): void
149151
->setData([
150152
'cache.key' => [$event->key],
151153
])
154+
->setOrigin('auto.cache')
152155
->setDescription($event->key)
153156
)
154157
);
@@ -165,8 +168,9 @@ public function handleRedisCommands(RedisEvents\CommandExecuted $event): void
165168
return;
166169
}
167170

168-
$context = new SpanContext();
169-
$context->setOp('db.redis');
171+
$context = SpanContext::make()
172+
->setOp('db.redis')
173+
->setOrigin('auto.cache.redis');
170174

171175
$keyForDescription = '';
172176

src/Sentry/Laravel/Features/HttpClientIntegration.php

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -72,23 +72,25 @@ public function handleRequestSendingHandlerForTracing(RequestSending $event): vo
7272
return;
7373
}
7474

75-
$context = new SpanContext;
76-
7775
$fullUri = $this->getFullUri($event->request->url());
7876
$partialUri = $this->getPartialUri($fullUri);
7977

80-
$context->setOp('http.client');
81-
$context->setDescription($event->request->method() . ' ' . $partialUri);
82-
$context->setData([
83-
'url' => $partialUri,
84-
// See: https://develop.sentry.dev/sdk/performance/span-data-conventions/#http
85-
'http.query' => $fullUri->getQuery(),
86-
'http.fragment' => $fullUri->getFragment(),
87-
'http.request.method' => $event->request->method(),
88-
'http.request.body.size' => $event->request->toPsrRequest()->getBody()->getSize(),
89-
]);
90-
91-
$this->pushSpan($parentSpan->startChild($context));
78+
$this->pushSpan(
79+
$parentSpan->startChild(
80+
SpanContext::make()
81+
->setOp('http.client')
82+
->setData([
83+
'url' => $partialUri,
84+
// See: https://develop.sentry.dev/sdk/performance/span-data-conventions/#http
85+
'http.query' => $fullUri->getQuery(),
86+
'http.fragment' => $fullUri->getFragment(),
87+
'http.request.method' => $event->request->method(),
88+
'http.request.body.size' => $event->request->toPsrRequest()->getBody()->getSize(),
89+
])
90+
->setOrigin('auto.http.client')
91+
->setDescription($event->request->method() . ' ' . $partialUri)
92+
)
93+
);
9294
}
9395

9496
public function handleResponseReceivedHandlerForTracing(ResponseReceived $event): void

src/Sentry/Laravel/Features/LivewirePackageIntegration.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,18 @@ public function handleComponentBoot(Component $component, ?string $method = null
112112
return;
113113
}
114114

115-
$context = new SpanContext;
116-
$context->setOp('ui.livewire.component');
117-
$context->setDescription(
118-
empty($method)
119-
? $component->getName()
120-
: "{$component->getName()}::{$method}"
115+
$this->pushSpan(
116+
$parentSpan->startChild(
117+
SpanContext::make()
118+
->setOp('ui.livewire.component')
119+
->setOrigin('auto.laravel.livewire')
120+
->setDescription(
121+
empty($method)
122+
? $component->getName()
123+
: "{$component->getName()}::{$method}"
124+
)
125+
)
121126
);
122-
123-
$this->pushSpan($parentSpan->startChild($context));
124127
}
125128

126129
public function handleComponentMount(Component $component, array $data): void

src/Sentry/Laravel/Features/NotificationsIntegration.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,15 @@ public function handleNotificationSending(NotificationSending $event): void
4343
return;
4444
}
4545

46-
$context = (new SpanContext)
46+
$context = SpanContext::make()
4747
->setOp('notification.send')
4848
->setData([
4949
'id' => $event->notification->id,
5050
'channel' => $event->channel,
5151
'notifiable' => $this->formatNotifiable($event->notifiable),
5252
'notification' => get_class($event->notification),
5353
])
54+
->setOrigin('auto.laravel.notifications')
5455
->setDescription($event->channel);
5556

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

src/Sentry/Laravel/Features/QueueIntegration.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ public function handleJobProcessingQueueEvent(JobProcessing $event): void
202202

203203
$context->setOp('queue.process');
204204
$context->setData($job);
205+
$context->setOrigin('auto.queue');
205206
$context->setStartTimestamp(microtime(true));
206207

207208
// When the parent span is null we start a new transaction otherwise we start a child of the current span

src/Sentry/Laravel/Features/Storage/FilesystemDecorator.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,16 @@ protected function withSentry(string $method, array $args, ?string $description,
5454
}
5555

5656
if ($this->recordSpans) {
57-
$spanContext = new SpanContext;
58-
$spanContext->setOp($op);
59-
$spanContext->setData($data);
60-
$spanContext->setDescription($description);
61-
62-
return trace(function () use ($method, $args) {
63-
return $this->filesystem->{$method}(...$args);
64-
}, $spanContext);
57+
return trace(
58+
function () use ($method, $args) {
59+
return $this->filesystem->{$method}(...$args);
60+
},
61+
SpanContext::make()
62+
->setOp($op)
63+
->setData($data)
64+
->setOrigin('auto.filesystem')
65+
->setDescription($description)
66+
);
6567
}
6668

6769
return $this->filesystem->{$method}(...$args);

src/Sentry/Laravel/Tracing/EventHandler.php

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public function subscribe(Dispatcher $dispatcher): void
126126
* Pass through the event and capture any errors.
127127
*
128128
* @param string $method
129-
* @param array $arguments
129+
* @param array $arguments
130130
*/
131131
public function __call(string $method, array $arguments)
132132
{
@@ -170,16 +170,18 @@ protected function queryExecutedHandler(DatabaseEvents\QueryExecuted $query): vo
170170
return;
171171
}
172172

173-
$context = new SpanContext();
174-
$context->setOp('db.sql.query');
175-
$context->setDescription($query->sql);
176-
$context->setData([
177-
'db.name' => $query->connection->getDatabaseName(),
178-
'db.system' => $query->connection->getDriverName(),
179-
'server.address' => $query->connection->getConfig('host'),
180-
'server.port' => $query->connection->getConfig('port'),
181-
]);
182-
$context->setStartTimestamp(microtime(true) - $query->time / 1000);
173+
$context = SpanContext::make()
174+
->setOp('db.sql.query')
175+
->setData([
176+
'db.name' => $query->connection->getDatabaseName(),
177+
'db.system' => $query->connection->getDriverName(),
178+
'server.address' => $query->connection->getConfig('host'),
179+
'server.port' => $query->connection->getConfig('port'),
180+
])
181+
->setOrigin('auto.db')
182+
->setDescription($query->sql)
183+
->setStartTimestamp(microtime(true) - $query->time / 1000);
184+
183185
$context->setEndTimestamp($context->getStartTimestamp() + $query->time / 1000);
184186

185187
if ($this->traceSqlBindings) {
@@ -224,10 +226,13 @@ protected function responsePreparingHandler(RoutingEvents\PreparingResponse $eve
224226
return;
225227
}
226228

227-
$context = new SpanContext;
228-
$context->setOp('http.route.response');
229-
230-
$this->pushSpan($parentSpan->startChild($context));
229+
$this->pushSpan(
230+
$parentSpan->startChild(
231+
SpanContext::make()
232+
->setOp('http.route.response')
233+
->setOrigin('auto.http.server')
234+
)
235+
);
231236
}
232237

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

242-
$context = new SpanContext;
243-
$context->setOp('db.transaction');
244-
245-
$this->pushSpan($parentSpan->startChild($context));
247+
$this->pushSpan(
248+
$parentSpan->startChild(
249+
SpanContext::make()
250+
->setOp('db.transaction')
251+
->setOrigin('auto.db')
252+
)
253+
);
246254
}
247255

248256
protected function transactionCommittedHandler(DatabaseEvents\TransactionCommitted $event): void

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class LighthouseIntegration implements IntegrationInterface
4545

4646
public function __construct(EventDispatcher $eventDispatcher, bool $ignoreOperationName = false)
4747
{
48-
$this->eventDispatcher = $eventDispatcher;
48+
$this->eventDispatcher = $eventDispatcher;
4949
$this->ignoreOperationName = $ignoreOperationName;
5050
}
5151

@@ -106,11 +106,12 @@ public function handleStartRequest(StartRequest $startRequest): void
106106
return;
107107
}
108108

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

112-
$this->operations = [];
113-
$this->requestSpan = $this->previousSpan->startChild($context);
113+
$this->operations = [];
114+
$this->requestSpan = $this->previousSpan->startChild($context);
114115
$this->operationSpan = null;
115116

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

137138
$this->updateTransactionName();
138139

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

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

src/Sentry/Laravel/Tracing/Middleware.php

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ private function startTransaction(Request $request, HubInterface $sentry): void
159159

160160
$context->setOp('http.server');
161161
$context->setName($requestPath);
162+
$context->setOrigin('auto.http.server');
162163
$context->setSource(TransactionSource::url());
163164
$context->setStartTimestamp($requestStartTime);
164165

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

181182
$bootstrapSpan = $this->addAppBootstrapSpan();
182183

183-
$appContextStart = new SpanContext;
184-
$appContextStart->setOp('middleware.handle');
185-
$appContextStart->setStartTimestamp($bootstrapSpan ? $bootstrapSpan->getEndTimestamp() : microtime(true));
186-
187-
$this->appSpan = $this->transaction->startChild($appContextStart);
184+
$this->appSpan = $this->transaction->startChild(
185+
SpanContext::make()
186+
->setOp('middleware.handle')
187+
->setOrigin('auto.http.server')
188+
->setStartTimestamp($bootstrapSpan ? $bootstrapSpan->getEndTimestamp() : microtime(true))
189+
);
188190

189191
SentrySdk::getCurrentHub()->setSpan($this->appSpan);
190192
}
@@ -195,12 +197,13 @@ private function addAppBootstrapSpan(): ?Span
195197
return null;
196198
}
197199

198-
$spanContextStart = new SpanContext;
199-
$spanContextStart->setOp('app.bootstrap');
200-
$spanContextStart->setStartTimestamp($this->transaction->getStartTimestamp());
201-
$spanContextStart->setEndTimestamp($this->bootedTimestamp);
202-
203-
$span = $this->transaction->startChild($spanContextStart);
200+
$span = $this->transaction->startChild(
201+
SpanContext::make()
202+
->setOp('app.bootstrap')
203+
->setOrigin('auto.http.server')
204+
->setStartTimestamp($this->transaction->getStartTimestamp())
205+
->setEndTimestamp($this->bootedTimestamp)
206+
);
204207

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

222-
$autoload = new SpanContext;
223-
$autoload->setOp('app.php.autoload');
224-
$autoload->setStartTimestamp($this->transaction->getStartTimestamp());
225-
$autoload->setEndTimestamp(SENTRY_AUTOLOAD);
226-
227-
$bootstrap->startChild($autoload);
225+
$bootstrap->startChild(
226+
SpanContext::make()
227+
->setOp('app.php.autoload')
228+
->setOrigin('auto.http.server')
229+
->setStartTimestamp($this->transaction->getStartTimestamp())
230+
->setEndTimestamp(SENTRY_AUTOLOAD)
231+
);
228232
}
229233

230234
private function hydrateResponseData(SymfonyResponse $response): void

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,12 @@ protected function wrapRouteDispatch(callable $dispatch, Route $route)
2222
// @see: https://github.com/getsentry/sentry-laravel/issues/917
2323
$action = $route->getActionName() instanceof Closure ? 'Closure' : $route->getActionName();
2424

25-
$context = new SpanContext;
26-
$context->setOp('http.route');
27-
$context->setDescription($action);
28-
29-
$span = $parentSpan->startChild($context);
25+
$span = $parentSpan->startChild(
26+
SpanContext::make()
27+
->setOp('http.route')
28+
->setOrigin('auto.http.server')
29+
->setDescription($action)
30+
);
3031

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

0 commit comments

Comments
 (0)