Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Event processors are not executed on cloned Scopes #5575

Closed
3 tasks done
Loskir opened this issue Aug 12, 2022 · 5 comments
Closed
3 tasks done

Event processors are not executed on cloned Scopes #5575

Loskir opened this issue Aug 12, 2022 · 5 comments

Comments

@Loskir
Copy link

Loskir commented Aug 12, 2022

Is there an existing issue for this?

How do you use Sentry?

Sentry Saas (sentry.io)

Which package are you using?

@sentry/node

SDK Version

7.10.0

Framework Version

No response

Link to Sentry event

No response

Steps to Reproduce

Run this code

const Sentry = require('@sentry/node')
const {Scope} = require('@sentry/node')

Sentry.init({
  dsn: '*insert some dsn*',
})

const scope = Scope.clone(Sentry.getCurrentHub().getScope())
scope.addEventProcessor(function(event, hint) {
  console.log(event, hint)
  return event
})
Sentry.captureException(new Error('test'), scope)

Expected Result

EventProcessors are executed

Actual Result

EventProcessors are not executed. No console output

@Loskir
Copy link
Author

Loskir commented Aug 12, 2022

const Sentry = require('@sentry/node')
const {Scope} = require('@sentry/node')

Sentry.init({
  dsn: '*insert dsn*',
  beforeSend(event) {
    console.log(event.tags)
    return event
  },
})

const scope = Scope.clone(Sentry.getCurrentHub().getScope())
scope.setTag('scope', 1)
scope.addEventProcessor(function(event, hint) {
  console.log('event processor 1')
  event.tags.eventProcessor1 = 1
  return event
})
Sentry.withScope((scope2) => {
  scope2.addEventProcessor(function(event, hint) {
    console.log('event processor 2')
    event.tags.eventProcessor2 = 2
    return event
  })
  scope2.setTag('scope2', 2)
  Sentry.captureException(new Error('test'), scope)
})

This code yields:

~/Projects/sentry-test $ node index.js                                                                                                                                                                                                                                  
event processor 2
{ scope2: 2, scope: 1, eventProcessor2: 2 }

Tags from scope2 are added, tags from scope are added, event processor from scope2 is executed, but event processor from scope isn't

@Lms24
Copy link
Member

Lms24 commented Aug 16, 2022

Hi @Loskir, thanks for writing in!

This doesn't work because when you call Sentry.captureException, you can't pass a scope. The reason is that the public facing API capturing an exception (which is Sentry.captureException) calls the captureException method of the Hub class. This method doesn't accept a scope but just a hint.

If you take a look at the link, you'll see that this method internally calls the client's captureException which in fact accepts a scope. However, we only use this functionality internally.

Would you mind telling me what you would like to achieve with adding this event processor? Also, is the example you provided below (with calling Sentry.withScope) a working alternative solution for you?

@Loskir
Copy link
Author

Loskir commented Aug 16, 2022

I want to create a new scope each time a request arrives at the server and then add some request-specific info to it. I thought that by configuring the scope and then passing it to captureException it would work the same way as when using Sentry.withScope or similar, but with added isolation.

The main concern here is to prevent data leaks from one request to the other. I don't use Sentry.pushScope because of that, for example. Yes, the second piece of code that uses Sentry.withScope along with tags from the cloned scope works fine and I hope it's not prone to leaks (because withScope is synchronous)

I'd like to have a good way of creating some container where I can safely store some request-specific data and then adding that data on captureException. I've asked this question on Discussions and got advice to use the domain module for that.

@AbhiPrasad
Copy link
Member

@Lms24
Copy link
Member

Lms24 commented Aug 17, 2022

@Loskir thanks for the context. Yes, in this case, definitely check out domains for that. I hope this helps you.
I'll close this issue for the time being because the initial question seems answered to me. Please feel free to reach out if you have additional questions.

@Lms24 Lms24 closed this as completed Aug 17, 2022
@Lms24 Lms24 closed this as not planned Won't fix, can't repro, duplicate, stale Aug 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants