sentry-module icon indicating copy to clipboard operation
sentry-module copied to clipboard

Cannot see how to attach custom data to events

Open mattfletcher opened this issue 1 year ago • 1 comments

Perhaps I've missed something in the docs, but I cannot see how to add custom data (such as the user that's logged in) to an event that's being recorded. Can you advise?

mattfletcher avatar Jun 23 '23 10:06 mattfletcher

Worked it out, thought I'd leave the answer here for anyone else struggling, or myself when I need to do it again!

use Laminas\Authentication\AuthenticationService;
use Sentry\State\Scope;
use function Sentry\configureScope;

            /** @var ErrorHandlerListener $errorHandlerListener */
            $errorHandlerListener = $serviceManager->get(ErrorHandlerListener::class);
            $errorHandlerListener->attach($eventManager);

            $identity = $serviceManager->get(AuthenticationService::class)->getIdentity();

            if ($identity) {
                configureScope(function (Scope $scope) use ($identity): void {
                    $scope->setUser([
                        'username' => $identity['firstName'] . ' ' . $identity['lastName'],
                        'email'    => $identity['email'],
                        'segment'  => $identity['roleType'],
                    ]);
                });
            }

mattfletcher avatar Jul 21 '23 08:07 mattfletcher