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

Scoping issues

Open spobrien opened this issue 8 years ago • 9 comments

  • [x] Review the documentation: https://docs.sentry.io/
  • [x] Search for existing issues: https://github.com/getsentry/sentry-javascript/issues
  • [x] Use the latest release: https://github.com/getsentry/sentry-javascript/releases

Package + Version

  • [ ] @sentry/browser
  • [x] @sentry/node
  • [ ] raven-js
  • [ ] raven-node (raven for node)
  • [ ] other:

Version:

4.3.0

Description

Hello there!

I am experiencing some scoping issues in my application and unsure how to solve - I have tried a variety of different things but to little avail so far.

My application is message based and has a number of handlers - the problem is that the tags and extra are being shared amongst each other and I need to localise the scope.

Initially I was wrapping the message flow like so:

	static async wrap(name, handler) {
		try {
			await handler();
		} catch (err) {
			Sentry.captureException(err);
		}
	}

And calling this from within

	static addTag(name, value) {
		Sentry.configureScope(scope => {
			scope.setTag(name, value);
		});
	}

Both functions were in an external class.

I tried replacing with 'withScope' - that didn't work and I can kinda see why. So then I am wondering what is the way forward with this, can you do something like this (I tried this exactly and did not seem to work) ?

	await Sentry.withScope(async scope => {
		try {
			scope.setTag('eventName', name);
			await handler();
		} catch (err) {
			Sentry.captureException(err);
		}
	});

Perhaps I am not using sentry in a very natural way but the the task at hand is to add to our existing APM format so a little stumped.

spobrien avatar Nov 09 '18 13:11 spobrien

sean, this is what i got working for oneflow-ship, after quite a bit of trial and error because the docs are very confusing:

Sentry.withScope(scope => {
		scope.setExtra('context', context)
		scope.setLevel('error')
		Sentry.captureException(err, e => debug(e))
})

i am not convinced it is actually recording evrery error though.

billywhizz avatar Nov 09 '18 14:11 billywhizz

thanks @billywhizz - the issue is that this won't fit with what I currently have unfortunately and would like to avoid a restructure if possible.

spobrien avatar Nov 09 '18 14:11 spobrien

@spobrien I'm not sure what you're actually trying to achieve. Can you please describe it more generically?

try {
  await handler();
} catch (err) {
  Sentry.withScope(scope => {
    scope.setTag('eventName', name);
    Sentry.captureException(err);
  });
}

This syntax will work just fine.

kamilogorek avatar Nov 13 '18 13:11 kamilogorek

hi @kamilogorek, thanks for your response.

The issue really is that I wanted nested functions to add to the scope as I go, with the goal of submitting the entire (built) scope when capturing the exception i.e building on your syntax - the handler function might look something like this:

async handler() {
          scope.setTag('tag', 'tagName');
}

The only way I can think of doing this is to add scope as a parameter to the function and this would involve a lot of changes to my code base and not sure is a great a approach anyways.

I am not sure that is possible but I couldn't quite figure out myself how the scoping works exactly after reading the docs.

spobrien avatar Nov 13 '18 13:11 spobrien

@spobrien there are pushScope and popScope methods that can be used to manually manipulate the stack, but it can get out of sync really easily without async handlers. Unfortunately without passing some data around it's very tricky to get your scenario working as expected. There has to be a place where it's stored and passed to us in the end.

kamilogorek avatar Nov 14 '18 12:11 kamilogorek

Yes I thought so - I have experimented with pushScope and popeScope a little also thanks. Passing the scope doesn't seem ideal but I also think it is really useful to be able to add as you go.

From the docs it sounds like scoping is intelligently handled for express requests. Hows is this managed I wonder ?

spobrien avatar Nov 14 '18 14:11 spobrien

@spobrien through node domains - https://github.com/getsentry/sentry-javascript/blob/master/packages/node/src/handlers.ts#L227-L236 https://nodejs.org/api/domain.html

kamilogorek avatar Nov 14 '18 17:11 kamilogorek

Thanks @kamilogorek - that might be something to investigate then.

spobrien avatar Nov 15 '18 10:11 spobrien

Cheers! Feel free to reopen or ask more questions if needed.

kamilogorek avatar Nov 15 '18 10:11 kamilogorek