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

Support two instances of sentry client

Open alexandresoler opened this issue 5 years ago • 8 comments

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

alexandresoler avatar Oct 16 '20 10:10 alexandresoler

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.

marandaneto avatar Oct 16 '20 10:10 marandaneto

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);

arunshankar87 avatar Nov 05 '20 21:11 arunshankar87

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 avatar Nov 13 '20 07:11 rrodrigues

@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.

marandaneto avatar Nov 13 '20 13:11 marandaneto

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.

rrodrigues avatar Nov 13 '20 16:11 rrodrigues

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 avatar Dec 01 '20 14:12 alexandresoler

@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.

arunshankar87 avatar Dec 01 '20 22:12 arunshankar87

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);

marandaneto avatar Feb 02 '21 19:02 marandaneto

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.

adinauer avatar Feb 08 '23 14:02 adinauer