-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Laravel: refactor the Request facade usage. (#220)
* 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
1 parent
11a7322
commit d377253
Showing
3 changed files
with
29 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}); | ||
} | ||
} |