sentry-module
sentry-module copied to clipboard
Cannot see how to attach custom data to events
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?
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'],
]);
});
}