@sentry/browser
@sentry/node
raven-js
raven-node
_(raven for node)_5.15.4
As per the Advance Usage docs, I am trying to manage several instances of Sentry. I am developing a widget and would like to use Sentry without it conflicting with the host page.
Creating new instances BrowserClient
and Hub
classes seems to be a great way to do that, but I can't get it to work with my integrations. I found a similar issue https://github.com/getsentry/sentry-javascript/issues/2329, however it was resolved by using hub.run(...)
. This does not work for me.
There was another thread https://github.com/getsentry/sentry-javascript/issues/1764 as well, but the provided solution involves setting the new hub to the current global hub with getCurrentHub().bindClient(client);
. This works, but it seems to defeat the point of what I'm trying to accomplish since I don't want to modify any globals that might interfere with the host page.
My code looks like this:
const { Hub, BrowserClient, defaultIntegrations } = Sentry;
const client = new BrowserClient({
dsn: process.env.SENTRY_DSN,
environment: process.env.NODE_ENV,
integrations: defaultIntegrations
});
const hub = new Hub(client);
// ...Later
hub.run((currentHub) => {
currentHub.captureException(error);
});
As an aside, I also tried triggering an exception directly from the Client
based on the Advanced Usage docs:
client.captureException(new Error('example'));
This also fails to work with integrations passed to the Client.
@NicHaley By reading the source code, I found that you need to explicitly call setupIntegrations
on the Client
object for the integrations to be added to the client. Here is the source code: https://github.com/getsentry/sentry-javascript/blob/2efbc98e86a54b59336fd9c3998ff04955aa449f/packages/core/src/baseclient.ts#L196
Alternatively you can call bindClient
instead of passing the client through the Hub
constructor. That function calls setupIntegrations
internally. The source code: https://github.com/getsentry/sentry-javascript/blob/2efbc98e86a54b59336fd9c3998ff04955aa449f/packages/hub/src/hub.ts#L91
I also found that the GlobalHandlers integration calls getCurrentHub
to get the instance of the hub to interact with. This means that it will only work with a global hub. See an example here: https://github.com/getsentry/sentry-javascript/blob/2efbc98e86a54b59336fd9c3998ff04955aa449f/packages/browser/src/integrations/globalhandlers.ts#L142
In general, I think the advance experience of creating your own hub instance it not completely polished and you are pretty much on your own in terms of integrations unfortunately.
Thanks for the feedback @mcaballeropinto. @NicHaley the comments above are correct. You need to manually bind the client to the new hub as well hub.bindClient(client)
. This looks like an oversight on our end, as it's a very rarely used feature.
I'll rename this issue to more descriptive one and mark it as a bug.
Thanks @mcaballeropinto and @kamilogorek. The above makes sense. I am using the Client
directly, and leveraging setupIntegrations
should give me most of what I need for the time being.
Thanks @kamilogorek! I see you want to use this issue to fix having to call bindClient
explicitly. What about the issue of default integrations not working on custom Hubs, as expressed at the end of my comment: https://github.com/getsentry/sentry-javascript/issues/2541#issuecomment-631135161?
I also found that the GlobalHandlers integration calls getCurrentHub to get the instance of the hub to interact with. This means that it will only work with a global hub. See an example here:
Is this something you will consider looking at?
We are experiencing a follow-up issue. It seems distinct from this one, so logged it as a new bug: https://github.com/getsentry/sentry-javascript/issues/2622