Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Decorate routing dispatchers to get a route span
- Loading branch information
1 parent
e2d3819
commit dda0978
Showing
4 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
src/Sentry/Laravel/Tracing/Routing/TracingCallableDispatcherTracing.php
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,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); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/Sentry/Laravel/Tracing/Routing/TracingControllerDispatcherTracing.php
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,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
36
src/Sentry/Laravel/Tracing/Routing/TracingRoutingDispatcher.php
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,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); | ||
} | ||
} | ||
} |
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