Skip to content

Commit 35331f7

Browse files
committed
add sentry context in middleware
1 parent 6de04ec commit 35331f7

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
This package is a Laravel extension
88

99
## Installation
10-
This package requires PHP 7.4 and Laravel 5.8 or higher.
10+
This package requires PHP 5.6 and Laravel 5.0 or higher.
1111

1212
```
1313
composer require programic/laravel-tools

src/Middleware/SentryContext.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,29 @@
22

33
namespace Programic\Tools\Middleware;
44

5+
use Sentry\State\Scope;
6+
use Programic\Tools\ToolsServiceProvider;
57
use Closure;
68

79
class SentryContext
810
{
911
public function handle($request, Closure $next)
1012
{
1113
if (app()->bound('sentry')) {
12-
app('sentry')->configureScope(function (\Sentry\State\Scope $scope) {
14+
app('sentry')->configureScope(function (Scope $scope) {
1315
if (auth()->check()) {
1416
$user = auth()->user();
1517

1618
$scope->setUser([
1719
'id' => $user->id,
1820
'email' => $user->email,
19-
'plan' => $user->plan
2021
]);
2122
}
2223

2324
$scope
24-
->setTags([
25-
'laravel' => app()->version(),
25+
->setContext('Laravel', [
26+
'version' => app()->version(),
27+
'Tools version' => ToolsServiceProvider::VERSION,
2628
]);
2729

2830
});

src/ToolsServiceProvider.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,27 @@
22

33
namespace Programic\Tools;
44

5-
use Illuminate\Contracts\Http\Kernel;
5+
use Illuminate\Routing\Router;
66
use Illuminate\Support\ServiceProvider;
77
use Programic\Tools\Middleware\SentryContext;
88

99
class ToolsServiceProvider extends ServiceProvider
1010
{
11+
const VERSION = 1.1;
12+
private $middlewareGroups = ['web', 'api'];
13+
1114
/**
1215
* Boot the service provider.
1316
*
1417
* @return void
1518
*/
16-
public function boot(Kernel $kernel)
19+
public function boot(Router $router)
1720
{
18-
$kernel->pushMiddleware(SentryContext::class);
21+
foreach ($this->middlewareGroups as $group) {
22+
if ($router->hasMiddlewareGroup($group)) {
23+
$router->pushMiddlewareToGroup($group, SentryContext::class);
24+
}
25+
}
1926
}
2027

2128

0 commit comments

Comments
 (0)