Sentry-java: User object not modifiable through Sentry singleton

Created on 1 May 2020  路  6Comments  路  Source: getsentry/sentry-java

The User class itself is mutable, however there is no getUser to go with the setUser in the Sentry class. It seems I am able to modify it through the configureScope function. Is this the only way to do it, or are there plans to expose it through the Sentry singleton?

enhancement wontfix

Most helpful comment

hey @mgray88 thanks for raising this.

Sentry.setUser overwrites the whole object. so you can just new User().

Sentry.configureScope { scope.getUser().setEmail("[email protected]") } would allow you to mutate a single field or the whole object too, both would work in the same way, so you are doing it right.

I'll check if we can expose a Sentry.getUser() and come back to you, thanks.

cc @bruno-garcia opinions?

All 6 comments

hey @mgray88 thanks for raising this.

Sentry.setUser overwrites the whole object. so you can just new User().

Sentry.configureScope { scope.getUser().setEmail("[email protected]") } would allow you to mutate a single field or the whole object too, both would work in the same way, so you are doing it right.

I'll check if we can expose a Sentry.getUser() and come back to you, thanks.

cc @bruno-garcia opinions?

It would be great to get access to the user object. There are situations where the user id is known independent of the username and being able to update the user object as data becomes known would be very helpful instead of recreating the object every time.

There are currently no plans to expose a getter via Sentry static class.

Some background why we have Sentry.setUser:

The original design was to mutate scope data through configureScope, and now it's odd that we have two ways of doing things. This creeped in because of folks using raven-js upgrading to sentry-javascript.

In JS they also have only set and no get. In fact the scope in many SDKs have no getter at all.

I agree this API isn't ideal. IMHO we should drop this static getter from the Sentry class and reduce the API surface there.

@bruno-garcia Is there any means of accessing the user object at this time?

@tlf30 https://github.com/getsentry/sentry-java/issues/917#issuecomment-622793697

tldr: use the configureScope method.

I'll close this as won't fix as we offer this feature over configureScope

As @marandaneto said, to sumarize:

Sentry.configureScope(s -> { 
  // Modify the user in the scope
  User user = scope.getUser();
});
Was this page helpful?
0 / 5 - 0 ratings