Skip to content

Commit be92d66

Browse files
authored
Move request info tracking to the middleware (#408)
* Hydrate request data in the middleware * Trim away the leading \ in route action names * Add changelog entry
1 parent 23c14c3 commit be92d66

File tree

4 files changed

+33
-42
lines changed

4 files changed

+33
-42
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Unreleased
44

55
- Fix incorrectly stripped base controller action from transaction name (#406)
6+
- Move tracing request/response data hydration to the tracing middleware (#408)
67

78
## 2.1.1
89

src/Sentry/Laravel/Integration.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,8 @@ public static function extractNameForRoute(Route $route): ?string
148148
}
149149

150150
if (empty($routeName) && $route->getActionName()) {
151-
// SomeController@someAction (controller action)
152-
$routeName = $route->getActionName();
151+
// Some\Controller@someAction (controller action)
152+
$routeName = ltrim($route->getActionName(), '\\');
153153

154154
$baseNamespace = self::$baseControllerNamespace ?? '';
155155

src/Sentry/Laravel/Tracing/EventHandler.php

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,9 @@
55
use Exception;
66
use Illuminate\Contracts\Events\Dispatcher;
77
use Illuminate\Database\Events\QueryExecuted;
8-
use Illuminate\Routing\Events\RouteMatched;
9-
use Illuminate\Routing\Route;
108
use RuntimeException;
119
use Sentry\Laravel\Integration;
12-
use Sentry\SentrySdk;
1310
use Sentry\Tracing\SpanContext;
14-
use Sentry\Tracing\Transaction;
1511

1612
class EventHandler
1713
{
@@ -21,9 +17,6 @@ class EventHandler
2117
* @var array
2218
*/
2319
protected static $eventHandlerMap = [
24-
'router.matched' => 'routerMatched', // Until Laravel 5.1
25-
RouteMatched::class => 'routeMatched', // Since Laravel 5.2
26-
2720
'illuminate.query' => 'query', // Until Laravel 5.1
2821
QueryExecuted::class => 'queryExecuted', // Since Laravel 5.2
2922
];
@@ -83,36 +76,6 @@ public function __call($method, $arguments)
8376
}
8477
}
8578

86-
/**
87-
* Until Laravel 5.1
88-
*
89-
* @param \Illuminate\Routing\Route $route
90-
*/
91-
protected function routerMatchedHandler(Route $route): void
92-
{
93-
$transaction = SentrySdk::getCurrentHub()->getTransaction();
94-
95-
if ($transaction instanceof Transaction) {
96-
$routeName = Integration::extractNameForRoute($route) ?? '<unlabeled transaction>';
97-
98-
$transaction->setName($routeName);
99-
$transaction->setData([
100-
'action' => $route->getActionName(),
101-
'name' => $route->getName(),
102-
]);
103-
}
104-
}
105-
106-
/**
107-
* Since Laravel 5.2
108-
*
109-
* @param \Illuminate\Routing\Events\RouteMatched $match
110-
*/
111-
protected function routeMatchedHandler(RouteMatched $match): void
112-
{
113-
$this->routerMatchedHandler($match->route);
114-
}
115-
11679
/**
11780
* Until Laravel 5.1
11881
*

src/Sentry/Laravel/Tracing/Middleware.php

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
use Closure;
66
use Illuminate\Http\Request;
77
use Illuminate\Http\Response;
8+
use Illuminate\Routing\Route;
9+
use Sentry\Laravel\Integration;
810
use Sentry\SentrySdk;
9-
use Sentry\State\Hub;
11+
use Sentry\State\HubInterface;
1012
use Sentry\Tracing\SpanContext;
1113
use Sentry\Tracing\TransactionContext;
1214

@@ -62,15 +64,19 @@ public function terminate($request, $response): void
6264
// If the transaction is not on the scope during finish, the trace.context is wrong
6365
SentrySdk::getCurrentHub()->setSpan($this->transaction);
6466

67+
if ($request instanceof Request) {
68+
$this->hydrateRequestData($request);
69+
}
70+
6571
if ($response instanceof Response) {
66-
$this->transaction->setHttpStatus($response->status());
72+
$this->hydrateResponseData($response);
6773
}
6874

6975
$this->transaction->finish();
7076
}
7177
}
7278

73-
private function startTransaction(Request $request, Hub $sentry): void
79+
private function startTransaction(Request $request, HubInterface $sentry): void
7480
{
7581
$path = '/' . ltrim($request->path(), '/');
7682
$fallbackTime = microtime(true);
@@ -140,4 +146,25 @@ private function addBootTimeSpans(): bool
140146

141147
return true;
142148
}
149+
150+
private function hydrateRequestData(Request $request): void
151+
{
152+
$route = $request->route();
153+
154+
if ($route instanceof Route) {
155+
$routeName = Integration::extractNameForRoute($route) ?? '<unlabeled transaction>';
156+
157+
$this->transaction->setName($routeName);
158+
$this->transaction->setData([
159+
'name' => $route->getName(),
160+
'action' => $route->getActionName(),
161+
'method' => $request->getMethod(),
162+
]);
163+
}
164+
}
165+
166+
private function hydrateResponseData(Response $response): void
167+
{
168+
$this->transaction->setHttpStatus($response->status());
169+
}
143170
}

0 commit comments

Comments
 (0)