Skip to content

Commit 78817c5

Browse files
authored
Remove extracting route name or controller for transaction names (#583)
* Remove extracting route name or controller for transaction names * CS * Add changelog entry
1 parent 8614807 commit 78817c5

File tree

5 files changed

+11
-137
lines changed

5 files changed

+11
-137
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- Remove `Sentry\Integration::extractNameForRoute()`, it's alternative `Sentry\Integration::extractNameAndSourceForRoute()` is marked as `@internal` (#580)
88
- Drop support for Laravel 5.x (#581)
99
- Fix not setting the correct SDK ID and version when running the `sentry:test` command (#582)
10+
- Transaction names now only show the parameterized URL (`/some/{route}`) instead of the route name or controller class (#583)
1011

1112
## 2.14.0
1213

config/sentry.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,4 @@
5555

5656
'traces_sample_rate' => (float)(env('SENTRY_TRACES_SAMPLE_RATE', 0.0)),
5757

58-
'controllers_base_namespace' => env('SENTRY_CONTROLLERS_BASE_NAMESPACE', 'App\\Http\\Controllers'),
59-
6058
];

src/Sentry/Laravel/Integration.php

Lines changed: 6 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,6 @@ class Integration implements IntegrationInterface
2222
*/
2323
private static $transaction;
2424

25-
/**
26-
* @var null|string
27-
*/
28-
private static $baseControllerNamespace;
29-
3025
/**
3126
* {@inheritdoc}
3227
*/
@@ -95,14 +90,6 @@ public static function setTransaction(?string $transaction): void
9590
self::$transaction = $transaction;
9691
}
9792

98-
/**
99-
* @param null|string $namespace
100-
*/
101-
public static function setControllersBaseNamespace(?string $namespace): void
102-
{
103-
self::$baseControllerNamespace = $namespace !== null ? trim($namespace, '\\') : null;
104-
}
105-
10693
/**
10794
* Block until all async events are processed for the HTTP transport.
10895
*
@@ -129,75 +116,10 @@ public static function flushEvents(): void
129116
*/
130117
public static function extractNameAndSourceForRoute(Route $route): array
131118
{
132-
$source = null;
133-
$routeName = null;
134-
135-
// some.action (route name/alias)
136-
if ($route->getName()) {
137-
$source = TransactionSource::component();
138-
$routeName = self::extractNameForNamedRoute($route->getName());
139-
}
140-
141-
// Some\Controller@someAction (controller action)
142-
if (empty($routeName) && $route->getActionName()) {
143-
$source = TransactionSource::component();
144-
$routeName = self::extractNameForActionRoute($route->getActionName());
145-
}
146-
147-
// /some/{action} // Fallback to the route uri (with parameter placeholders)
148-
if (empty($routeName) || $routeName === 'Closure') {
149-
$source = TransactionSource::route();
150-
$routeName = '/' . ltrim($route->uri(), '/');
151-
}
152-
153-
return [$routeName, $source];
154-
}
155-
156-
/**
157-
* Take a route name and return it only if it's a usable route name.
158-
*
159-
* @param string $name
160-
*
161-
* @return string|null
162-
*/
163-
private static function extractNameForNamedRoute(string $name): ?string
164-
{
165-
// Laravel 7 route caching generates a route names if the user didn't specify one
166-
// theirselfs to optimize route matching. These route names are useless to the
167-
// developer so if we encounter a generated route name we discard the value
168-
if (Str::contains($name, 'generated::')) {
169-
return null;
170-
}
171-
172-
// If the route name ends with a `.` we assume an incomplete group name prefix
173-
// we discard this value since it will most likely not mean anything to the
174-
// developer and will be duplicated by other unnamed routes in the group
175-
if (Str::endsWith($name, '.')) {
176-
return null;
177-
}
178-
179-
return $name;
180-
}
181-
182-
/**
183-
* Take a controller action and strip away the base namespace if needed.
184-
*
185-
* @param string $action
186-
*
187-
* @return string
188-
*/
189-
private static function extractNameForActionRoute(string $action): string
190-
{
191-
$routeName = ltrim($action, '\\');
192-
193-
$baseNamespace = self::$baseControllerNamespace ?? '';
194-
195-
if (empty($baseNamespace)) {
196-
return $routeName;
197-
}
198-
199-
// Strip away the base namespace from the action name
200-
return ltrim(Str::after($routeName, $baseNamespace), '\\');
119+
return [
120+
'/' . ltrim($route->uri(), '/'),
121+
TransactionSource::route()
122+
];
201123
}
202124

203125
/**
@@ -213,9 +135,7 @@ public static function sentryTracingMeta(): string
213135
return '';
214136
}
215137

216-
$content = sprintf('<meta name="sentry-trace" content="%s"/>', $span->toTraceparent());
217-
218-
return $content;
138+
return sprintf('<meta name="sentry-trace" content="%s"/>', $span->toTraceparent());
219139
}
220140

221141
/**
@@ -232,9 +152,7 @@ public static function sentryBaggageMeta(): string
232152
return '';
233153
}
234154

235-
$content = sprintf('<meta name="baggage" content="%s"/>', $span->toBaggage());
236-
237-
return $content;
155+
return sprintf('<meta name="baggage" content="%s"/>', $span->toBaggage());
238156
}
239157

240158
/**

src/Sentry/Laravel/ServiceProvider.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ class ServiceProvider extends BaseServiceProvider
3333
'integrations',
3434
// This is kept for backwards compatibility and can be dropped in a future breaking release
3535
'breadcrumbs.sql_bindings',
36-
// The base namespace for controllers to strip of the beginning of controller class names
36+
37+
// This config option is no longer in use but to prevent errors when upgrading we leave it here to be discarded
3738
'controllers_base_namespace',
3839
];
3940

@@ -125,12 +126,6 @@ protected function registerArtisanCommands(): void
125126
*/
126127
protected function configureAndRegisterClient(): void
127128
{
128-
$userConfig = $this->getUserConfig();
129-
130-
if (isset($userConfig['controllers_base_namespace'])) {
131-
Integration::setControllersBaseNamespace($userConfig['controllers_base_namespace']);
132-
}
133-
134129
$this->app->bind(ClientBuilderInterface::class, function () {
135130
$basePath = base_path();
136131
$userConfig = $this->getUserConfig();
@@ -161,15 +156,15 @@ protected function configureAndRegisterClient(): void
161156
return $clientBuilder;
162157
});
163158

164-
$this->app->singleton(HubInterface::class, function ($app) {
159+
$this->app->singleton(HubInterface::class, function () {
165160
/** @var \Sentry\ClientBuilderInterface $clientBuilder */
166161
$clientBuilder = $this->app->make(ClientBuilderInterface::class);
167162

168163
$options = $clientBuilder->getOptions();
169164

170165
$userIntegrations = $this->resolveIntegrationsFromUserConfig();
171166

172-
$options->setIntegrations(function (array $integrations) use ($options, $userIntegrations, $app) {
167+
$options->setIntegrations(function (array $integrations) use ($options, $userIntegrations) {
173168
if ($options->hasDefaultIntegrations()) {
174169
// Remove the default error and fatal exception listeners to let Laravel handle those
175170
// itself. These event are still bubbling up through the documented changes in the users

test/Sentry/IntegrationTest.php

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -89,38 +89,13 @@ public function testTransactionIsNotAppliedToEventWhenTransactionIsAlreadySet():
8989
});
9090
}
9191

92-
public function testExtractingNameForRouteWithName(): void
93-
{
94-
$route = (new Route('GET', '/foo', []))->name($routeName = 'foo-bar');
95-
96-
$this->assetRouteNameAndSource($route, $routeName, TransactionSource::component());
97-
}
98-
99-
public function testExtractingNameForRouteWithAction(): void
100-
{
101-
$route = (new Route('GET', '/foo', [
102-
'controller' => $controller = 'SomeController@someAction',
103-
]));
104-
105-
$this->assetRouteNameAndSource($route, $controller, TransactionSource::component());
106-
}
107-
10892
public function testExtractingNameForRouteWithoutName(): void
10993
{
11094
$route = new Route('GET', $url = '/foo', []);
11195

11296
$this->assetRouteNameAndSource($route, $url, TransactionSource::route());
11397
}
11498

115-
public function testExtractingNameForRouteWithActionAndName(): void
116-
{
117-
$route = (new Route('GET', '/foo', [
118-
'controller' => 'SomeController@someAction',
119-
]))->name($routeName = 'foo-bar');
120-
121-
$this->assetRouteNameAndSource($route, $routeName, TransactionSource::component());
122-
}
123-
12499
public function testExtractingNameForRouteWithAutoGeneratedName(): void
125100
{
126101
// We fake a generated name here, Laravel generates them each starting with `generated::`
@@ -136,19 +111,6 @@ public function testExtractingNameForRouteWithIncompleteGroupName(): void
136111
$this->assetRouteNameAndSource($route, $url, TransactionSource::route());
137112
}
138113

139-
public function testExtractingNameForRouteWithStrippedBaseNamespaceFromAction(): void
140-
{
141-
Integration::setControllersBaseNamespace('BaseNamespace');
142-
143-
$route = (new Route('GET', '/foo', [
144-
'controller' => 'BaseNamespace\\SomeController@someAction',
145-
]));
146-
147-
$this->assetRouteNameAndSource($route, 'SomeController@someAction', TransactionSource::component());
148-
149-
Integration::setControllersBaseNamespace(null);
150-
}
151-
152114
private function assetRouteNameAndSource(Route $route, string $expectedName, TransactionSource $expectedSource): void
153115
{
154116
[$actualName, $actualSource] = Integration::extractNameAndSourceForRoute($route);

0 commit comments

Comments
 (0)