Support two instances of sentry client
Hey guys,
I'm developing an Android SDK and I'm using the Sentry version 1.7.22 I'm getting my personal instance of Sentry with val mySentry = AndroidSentryClientFactory.sentryClient(sentryDSN) Then, my integrator can use his Sentry in his code with the standard SentryClient with Sentry.init() and Sentry.capture() etc Then, I can receive my events on my sentry server and he can receive his events on his server.
On the new 3.1.0 version, AndroidSentryClientFactory doesn't exist anymore.
Any solution?
Thanks
you need to migrate and follow the unified API conventions:
https://docs.sentry.io/platforms/android/migration/
you could create your own Hub instance, for example.
Hub doesn't add the default integrations btw, be aware of that.
Thanks to @marandaneto, was able to achieve this. Just putting the steps if it can help others
1.7.27
Creating a client instance
SentryClient client = new AndroidSentryClientFactory(context).createSentryClient(new Dsn(dsn));
Capturing the event
EventBuilder eventBuilder = new EventBuilder()
.withMessage(message)
.withSentryInterface(new ExceptionInterface(e));
client.sendEvent(eventBuilder);
3.1.1
Creating a hub instance
SentryOptions options = new SentryOptions();
options.setDsn(dsn);
options.setSerializer(new GsonSerializer(options.getLogger(), options.getEnvelopeReader()));
Hub hub = new Hub(options);
Capturing the event
SentryEvent event;
event = new SentryEvent(e);
Message sentryMessage = new Message();
sentryMessage.setFormatted(message);
event.setMessage(sentryMessage);
hub.captureEvent(event);
Thanks @arunshankar87 for your steps. I was being able to do this because I was missing the line
options.setSerializer(new GsonSerializer(options.getLogger(), options.getEnvelopeReader()));
Also I think that could be useful to have AndroidOptionsInitializer.init public.
@rrodrigues why would you need AndroidOptionsInitializer.init public?
Default integrations are supposed to be added only once, by the final App and not a library, otherwise, they would conflict, that's why it's not public.
Because if the final app does not use sentry there are integrations that I would like to include anyway. But you're right, maybe doing it blindly is not the best way.
After the answer of @arunshankar87 , I can't find any informations in the documentation on "how to add Android Context to a hub". Actually, my events sent to the hub doesn't contains informations about my mobile device.
@alexandresoler yeah it doesn't come by default. We need to set the DefaultAndroidEventProcessor as options, and that would include all the system properties to come through. But DefaultAndroidEventProcessor is not public. So I have used reflection to achieve this.
since v4.0.0, there's a default serializer set to the options already, so this is not necessary anymore:
options.setSerializer(new GsonSerializer(options.getLogger(), options.getEnvelopeReader()));
but only:
SentryOptions options = new SentryOptions();
options.setDsn(dsn);
Hub hub = new Hub(options);
Currently our SDK is not meant to be used in Framework/Libraries. In fact, we don't officially support two instances of the SDK running in the same project pointing to different DSNs.
Of course, that could change in the future if we see more interest in this feature, but we don't have any plans at the moment.