Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add http.route span #593

Merged
merged 3 commits into from Oct 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -21,6 +21,7 @@
- Add tracing span for Laravel HTTP client requests (#585)
- Simplify Sentry meta tag retrieval, by adding `Sentry\Laravel\Integration::sentryMeta()` (#586)
- Fix tracing with nested queue jobs (mostly when running jobs in the `sync` driver) (#592)
- Add a `http.route` span, this span indicates the time that is spent inside a controller method or route closure (#593)
- Add a `db.transaction` span, this span indicates the time that is spent inside a database transaction (#594)

## 2.14.2
Expand Down
@@ -0,0 +1,24 @@
<?php

namespace Sentry\Laravel\Tracing\Routing;

use Illuminate\Routing\Contracts\CallableDispatcher;
use Illuminate\Routing\Route;

class TracingCallableDispatcherTracing extends TracingRoutingDispatcher implements CallableDispatcher
{
/** @var \Illuminate\Routing\Contracts\CallableDispatcher */
private $dispatcher;

public function __construct(CallableDispatcher $dispatcher)
{
$this->dispatcher = $dispatcher;
}

public function dispatch(Route $route, $callable)
{
return $this->wrapRouteDispatch(function () use ($route, $callable) {
return $this->dispatcher->dispatch($route, $callable);
}, $route);
}
}
@@ -0,0 +1,29 @@
<?php

namespace Sentry\Laravel\Tracing\Routing;

use Illuminate\Routing\Contracts\ControllerDispatcher;
use Illuminate\Routing\Route;

class TracingControllerDispatcherTracing extends TracingRoutingDispatcher implements ControllerDispatcher
{
/** @var \Illuminate\Routing\Contracts\ControllerDispatcher */
private $dispatcher;

public function __construct(ControllerDispatcher $dispatcher)
{
$this->dispatcher = $dispatcher;
}

public function dispatch(Route $route, $controller, $method)
{
return $this->wrapRouteDispatch(function () use ($route, $controller, $method) {
return $this->dispatcher->dispatch($route, $controller, $method);
}, $route);
}

public function getMiddleware($controller, $method)
{
return $this->dispatcher->getMiddleware($controller, $method);
}
}
36 changes: 36 additions & 0 deletions src/Sentry/Laravel/Tracing/Routing/TracingRoutingDispatcher.php
@@ -0,0 +1,36 @@
<?php

namespace Sentry\Laravel\Tracing\Routing;

use Illuminate\Routing\Route;
use Sentry\SentrySdk;
use Sentry\Tracing\SpanContext;

abstract class TracingRoutingDispatcher
{
protected function wrapRouteDispatch(callable $dispatch, Route $route)
{
$parentSpan = SentrySdk::getCurrentHub()->getSpan();

// When there is no active transaction we can skip doing anything and just immediately return the callable
if ($parentSpan === null) {
return $dispatch();
}

$context = new SpanContext;
$context->setOp('http.route');
$context->setDescription($route->getActionName());

$span = $parentSpan->startChild($context);

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

try {
return $dispatch();
} finally {
$span->finish();

SentrySdk::getCurrentHub()->setSpan($parentSpan);
}
}
}
17 changes: 17 additions & 0 deletions src/Sentry/Laravel/Tracing/ServiceProvider.php
Expand Up @@ -8,10 +8,14 @@
use Illuminate\Contracts\View\Engine;
use Illuminate\Contracts\View\View;
use Illuminate\Foundation\Http\Kernel as HttpKernel;
use Illuminate\Routing\Contracts\CallableDispatcher;
use Illuminate\Routing\Contracts\ControllerDispatcher;
use Illuminate\View\Engines\EngineResolver;
use Illuminate\View\Factory as ViewFactory;
use InvalidArgumentException;
use Sentry\Laravel\BaseServiceProvider;
use Sentry\Laravel\Tracing\Routing\TracingCallableDispatcherTracing;
use Sentry\Laravel\Tracing\Routing\TracingControllerDispatcherTracing;
use Sentry\Serializer\RepresentationSerializer;

class ServiceProvider extends BaseServiceProvider
Expand All @@ -29,6 +33,8 @@ public function boot(): void

$this->bindViewEngine($tracingConfig);

$this->decorateRoutingDispatchers();

if ($this->app->bound(HttpKernelInterface::class)) {
/** @var \Illuminate\Foundation\Http\Kernel $httpKernel */
$httpKernel = $this->app->make(HttpKernelInterface::class);
Expand Down Expand Up @@ -119,4 +125,15 @@ private function wrapViewEngine(Engine $realEngine): Engine

return new ViewEngineDecorator($realEngine, $viewFactory);
}

private function decorateRoutingDispatchers(): void
{
$this->app->extend(CallableDispatcher::class, static function (CallableDispatcher $dispatcher) {
return new TracingCallableDispatcherTracing($dispatcher);
});

$this->app->extend(ControllerDispatcher::class, static function (ControllerDispatcher $dispatcher) {
return new TracingControllerDispatcherTracing($dispatcher);
});
}
}