Skip to content

Commit

Permalink
Laravel: refactor the Request facade usage. (#220)
Browse files Browse the repository at this point in the history
* Laravel: refactor the Request facade usage.

* Laravel: use route resolver from request to access current Route object.

* Laravel: linting.

* Laravel: added RequestWatcher, updating current span name when a RouteMatched event is received.
  • Loading branch information
ChrisLightfootWild authored Jan 29, 2024
1 parent 11a7322 commit d377253
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
11 changes: 0 additions & 11 deletions src/HttpInstrumentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Illuminate\Contracts\Http\Kernel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use OpenTelemetry\API\Globals;
use OpenTelemetry\API\Instrumentation\CachedInstrumentation;
use OpenTelemetry\API\Trace\Span;
Expand Down Expand Up @@ -80,16 +79,6 @@ public static function register(CachedInstrumentation $instrumentation): void
$span->setAttribute(TraceAttributes::NETWORK_PROTOCOL_VERSION, $response->getProtocolVersion());
$span->setAttribute(TraceAttributes::HTTP_RESPONSE_BODY_SIZE, $response->headers->get('Content-Length'));
}
if(($route = Route::getCurrentRoute()?->uri()) !== null) {
$request = ($params[0] instanceof Request) ? $params[0] : null;

if (! str_starts_with($route, '/')) {
$route = '/' . $route;
}

/** @psalm-suppress ArgumentTypeCoercion */
$span->updateName(sprintf('%s %s', $request?->method() ?? 'unknown', $route));
}

$span->end();
}
Expand Down
2 changes: 2 additions & 0 deletions src/LaravelInstrumentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use OpenTelemetry\Contrib\Instrumentation\Laravel\Watchers\ExceptionWatcher;
use OpenTelemetry\Contrib\Instrumentation\Laravel\Watchers\LogWatcher;
use OpenTelemetry\Contrib\Instrumentation\Laravel\Watchers\QueryWatcher;
use OpenTelemetry\Contrib\Instrumentation\Laravel\Watchers\RequestWatcher;
use OpenTelemetry\Contrib\Instrumentation\Laravel\Watchers\Watcher;
use function OpenTelemetry\Instrumentation\hook;
use Throwable;
Expand All @@ -38,6 +39,7 @@ public static function register(): void
self::registerWatchers($application, new ExceptionWatcher());
self::registerWatchers($application, new LogWatcher());
self::registerWatchers($application, new QueryWatcher($instrumentation));
self::registerWatchers($application, new RequestWatcher());
},
);

Expand Down
27 changes: 27 additions & 0 deletions src/Watchers/RequestWatcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace OpenTelemetry\Contrib\Instrumentation\Laravel\Watchers;

use Illuminate\Contracts\Foundation\Application;
use Illuminate\Routing\Events\RouteMatched;
use OpenTelemetry\API\Trace\SpanInterface;
use OpenTelemetry\SemConv\TraceAttributes;

class RequestWatcher extends Watcher
{
/** @psalm-suppress UndefinedInterfaceMethod */
public function register(Application $app): void
{
$app['events']->listen(RouteMatched::class, static function (RouteMatched $event): void {
/** @var SpanInterface|null $span */
$span = $event->request->attributes->get(SpanInterface::class);

if ($span) {
$span->updateName("{$event->request->getMethod()} /" . ltrim($event->route->uri, '/'));
$span->setAttribute(TraceAttributes::HTTP_ROUTE, $event->route->uri);
}
});
}
}

0 comments on commit d377253

Please sign in to comment.