Skip to content

Commit d59182d

Browse files
authored
Improve span timing (#734)
1 parent 1336354 commit d59182d

File tree

2 files changed

+13
-15
lines changed

2 files changed

+13
-15
lines changed

src/Sentry/Laravel/Tracing/Middleware.php

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,13 @@ public function setBootedTimestamp(?float $timestamp = null): void
154154

155155
private function startTransaction(Request $request, HubInterface $sentry): void
156156
{
157-
$requestStartTime = $request->server('REQUEST_TIME_FLOAT', microtime(true));
157+
// Try $_SERVER['REQUEST_TIME_FLOAT'] then LARAVEL_START and fallback to microtime(true) if neither are defined
158+
$requestStartTime = $request->server(
159+
'REQUEST_TIME_FLOAT',
160+
defined('LARAVEL_START')
161+
? LARAVEL_START
162+
: microtime(true)
163+
);
158164

159165
$context = continueTrace(
160166
$request->header('sentry-trace', ''),
@@ -175,17 +181,16 @@ private function startTransaction(Request $request, HubInterface $sentry): void
175181

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

178-
// If this transaction is not sampled, don't set it either and stop doing work from this point on
184+
// If this transaction is not sampled, we can stop here to prevent doing work for nothing
179185
if (!$transaction->getSampled()) {
180186
return;
181187
}
182188

183189
$this->transaction = $transaction;
184190

185-
// Setting the Transaction on the Hub
186191
SentrySdk::getCurrentHub()->setSpan($this->transaction);
187192

188-
$bootstrapSpan = $this->addAppBootstrapSpan($request);
193+
$bootstrapSpan = $this->addAppBootstrapSpan();
189194

190195
$appContextStart = new SpanContext;
191196
$appContextStart->setOp('middleware.handle');
@@ -196,21 +201,15 @@ private function startTransaction(Request $request, HubInterface $sentry): void
196201
SentrySdk::getCurrentHub()->setSpan($this->appSpan);
197202
}
198203

199-
private function addAppBootstrapSpan(Request $request): ?Span
204+
private function addAppBootstrapSpan(): ?Span
200205
{
201206
if ($this->bootedTimestamp === null) {
202207
return null;
203208
}
204209

205-
$laravelStartTime = defined('LARAVEL_START') ? LARAVEL_START : $request->server('REQUEST_TIME_FLOAT');
206-
207-
if ($laravelStartTime === null) {
208-
return null;
209-
}
210-
211210
$spanContextStart = new SpanContext;
212211
$spanContextStart->setOp('app.bootstrap');
213-
$spanContextStart->setStartTimestamp($laravelStartTime);
212+
$spanContextStart->setStartTimestamp($this->transaction->getStartTimestamp());
214213
$spanContextStart->setEndTimestamp($this->bootedTimestamp);
215214

216215
$span = $this->transaction->startChild($spanContextStart);
@@ -232,9 +231,9 @@ private function addBootDetailTimeSpans(Span $bootstrap): void
232231
return;
233232
}
234233

235-
$autoload = new SpanContext();
234+
$autoload = new SpanContext;
236235
$autoload->setOp('app.php.autoload');
237-
$autoload->setStartTimestamp($bootstrap->getStartTimestamp());
236+
$autoload->setStartTimestamp($this->transaction->getStartTimestamp());
238237
$autoload->setEndTimestamp(SENTRY_AUTOLOAD);
239238

240239
$bootstrap->startChild($autoload);

src/Sentry/Laravel/Tracing/ServiceProvider.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Illuminate\Contracts\Container\BindingResolutionException;
66
use Illuminate\Contracts\Events\Dispatcher;
7-
use Illuminate\Contracts\Foundation\Application;
87
use Illuminate\Contracts\Http\Kernel as HttpKernelInterface;
98
use Illuminate\Contracts\View\Engine;
109
use Illuminate\Contracts\View\View;

0 commit comments

Comments
 (0)