Connehito / cake-sentry

CakePHP plugin integration for Sentry
MIT License
34 stars 26 forks source link

Adding scope does not work #73

Closed GrantAnt closed 2 years ago

GrantAnt commented 2 years ago

I have tried multiple ways to add scope after Sentry::init(). The new tags are not showing up.

e.g.: I am calling this from in my AppController->before

\Sentry\configureScope(function (\Sentry\State\Scope $scope): void {
    $scope->setUser(['email' => $this->user->email]);
    $scope->setTag('email', $this->user->email);
});

or I have been implementing certain EventListenerInterface and set the tags there. They never show up.

So I got curios and tried to add a new tag in my app.php and the test tag was shown immediately!!

'Sentry' => [
    'tags' => ['test' => 'test'],
    'dsn' => env('SENTRY_DSN'),
    'environment' => env('ENVIRONMENT', 'localhost'),
],

Versions:

GrantAnt commented 2 years ago

Small update. I try to set the user like this:

    \Sentry\configureScope(function (\Sentry\State\Scope $scope) {
        $scope->setUser(['email' => 'jane.doe@example.com']);
    });

When I set to user in 'CakeSentry.Client.afterSetup' Event, it's succesfully shown. When I set to user in 'CakeSentry.Client.beforeCapture' Event, it's not shown at all.

But I need to read the user from the request, which is not available in the ''afterSetup"

GrantAnt commented 2 years ago

I found the solution. It just needs an update in the readme file. Tags and the user can be set like this:

class SentryOptionsContext implements EventListenerInterface
{
    public function implementedEvents(): array
    {
        return [
            'CakeSentry.Client.beforeCapture' => 'setContext',
        ];
    }

    public function setContext(Event $event): void
    {
        $event->getSubject()->getHub()->configureScope(function (\Sentry\State\Scope $scope) {
            $scope->setUser(['email' => 'jane.doe@example.com']);
        });
    }
}