Skip to content

Commit cfa03db

Browse files
authored
Merge pull request #37 from keepsuit/app-bootstrap
added app boostrap span
2 parents bff7a9f + 5e71a48 commit cfa03db

File tree

1 file changed

+35
-5
lines changed

1 file changed

+35
-5
lines changed

src/Support/HttpServer/TraceRequestMiddleware.php

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,20 @@ public function handle(Request $request, Closure $next): mixed
2121
return $next($request);
2222
}
2323

24-
$span = $this->startTracing($request);
24+
$bootedTimestamp = $this->bootedTimestamp($request);
25+
26+
$span = $this->startTracing($request, $bootedTimestamp);
2527
$scope = $span->activate();
2628

2729
Tracer::updateLogContext();
2830

31+
if ($bootedTimestamp) {
32+
Tracer::newSpan('app boostrap')
33+
->setStartTimestamp($bootedTimestamp)
34+
->start()
35+
->end();
36+
}
37+
2938
try {
3039
$response = $next($request);
3140

@@ -45,15 +54,15 @@ public function handle(Request $request, Closure $next): mixed
4554
}
4655
}
4756

48-
protected function startTracing(Request $request): SpanInterface
57+
protected function startTracing(Request $request, ?int $startTimestamp = null): SpanInterface
4958
{
5059
$context = Tracer::extractContextFromPropagationHeaders($request->headers->all());
5160

5261
/** @var non-empty-string $route */
5362
$route = rescue(fn () => Route::getRoutes()->match($request)->uri(), $request->path(), false);
5463
$route = str_starts_with($route, '/') ? $route : '/'.$route;
5564

56-
$span = Tracer::newSpan($route)
65+
$builder = Tracer::newSpan($route)
5766
->setSpanKind(SpanKind::KIND_SERVER)
5867
->setParent($context)
5968
->setAttribute(TraceAttributes::URL_FULL, $request->fullUrl())
@@ -67,8 +76,13 @@ protected function startTracing(Request $request): SpanInterface
6776
->setAttribute(TraceAttributes::SERVER_PORT, $request->getPort())
6877
->setAttribute(TraceAttributes::USER_AGENT_ORIGINAL, $request->userAgent())
6978
->setAttribute(TraceAttributes::NETWORK_PROTOCOL_VERSION, $request->getProtocolVersion())
70-
->setAttribute(TraceAttributes::NETWORK_PEER_ADDRESS, $request->ip())
71-
->start();
79+
->setAttribute(TraceAttributes::NETWORK_PEER_ADDRESS, $request->ip());
80+
81+
if ($startTimestamp) {
82+
$builder->setStartTimestamp($startTimestamp);
83+
}
84+
85+
$span = $builder->start();
7286

7387
$this->recordHeaders($span, $request);
7488

@@ -115,4 +129,20 @@ protected function recordHeaders(SpanInterface $span, Request|Response $http): S
115129

116130
return $span;
117131
}
132+
133+
/**
134+
* @return int|null Timestamp in nanoseconds when the application booted
135+
*/
136+
protected function bootedTimestamp(Request $request): ?int
137+
{
138+
if ($request->server->has('REQUEST_TIME_FLOAT')) {
139+
return (int) (floatval($request->server('REQUEST_TIME_FLOAT')) * 1_000_000_000); // Convert seconds to nanoseconds
140+
}
141+
142+
if (defined('LARAVEL_START')) {
143+
return (int) (LARAVEL_START * 1_000_000_000); // Convert seconds to nanoseconds
144+
}
145+
146+
return null;
147+
}
118148
}

0 commit comments

Comments
 (0)