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

@sentry/node does not include user data in Transactions #15044

Closed
3 tasks done
enixsoft opened this issue Jan 17, 2025 · 8 comments
Closed
3 tasks done

@sentry/node does not include user data in Transactions #15044

enixsoft opened this issue Jan 17, 2025 · 8 comments
Labels
Package: browser Issues related to the Sentry Browser SDK

Comments

@enixsoft
Copy link

enixsoft commented Jan 17, 2025

Is there an existing issue for this?

How do you use Sentry?

Sentry Saas (sentry.io)

Which SDK are you using?

@sentry/node

SDK Version

8.50.0

Framework Version

No response

Link to Sentry event

No response

Reproduction Example/SDK Setup

scope.setUser({
    id: 'test',
    email: '[email protected]'
})

await SentrySDK.startSpan(
      {
        name: ctx.routeKey || 'unknown',
        op: 'http.server',
        scope,
      },
      async () => {
        await next()
      }
  )

https://github.com/RomainLanz/sentry/blob/v0.2.0/src/middleware.ts

Steps to Reproduce

I am using AdonisJS v6 framework with the Sentry integration rlanz/sentry v0.2.0.

It works well, except for one single issue.

On the Performance Back-end dashboard, I see no count of Users and no value for User Misery.

Image

First I thought there is some issue with setting the user on the scope after starting the span as you can see in the section Assigning User Context. But even if I call setUser directly on the scope before the startSpan function is called with the scope passed to it as a parameter, as you can see in the reproduction example, the user is still never sent in the data of the transaction. When error happens on the scope and the exception is captured, the user is there and I expected the same will apply to the transactions.

Expected Result

User will be included in Transaction event data and Performance dashboard will show count of Users and User Misery value.

Actual Result

User is not included in Transaction event data and Performance dashboard shows count of Users as 0 and no value for User Misery.

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 3 Jan 17, 2025
@github-actions github-actions bot added the Package: browser Issues related to the Sentry Browser SDK label Jan 17, 2025
@lforst
Copy link
Member

lforst commented Jan 17, 2025

Hi, can you share the exact code with which you assign a user to the scope? Thanks!

@lforst
Copy link
Member

lforst commented Jan 17, 2025

Please also note that we don't build (or support) @rlanz/sentry so you might get better milage reaching out to the maintainer of that package.

@enixsoft
Copy link
Author

enixsoft commented Jan 17, 2025

@lforst
It's in the reproduction example.
A scope gets created, the user is set on the scope and this scope is passed to the startSpan function. When the request finishes, the user is not included in the data. If an error happens during the request, the user is present in the error event, but still missing in the transaction event.

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 3 Jan 17, 2025
@lforst
Copy link
Member

lforst commented Jan 17, 2025

Sorry for some reason I completely missed that. Instead of doing scope.setUser(), can you try doing SentrySDK.setUser()? It should still be isolated to the request.

@enixsoft
Copy link
Author

@lforst

Thank you for fast response, your solution seems to work, I will test it further on environment with many users.

Still, it's very confusing that the user which is set on scope is ignored even though the scope gets passed to the transaction. And isn't SentrySDK a global scope?

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 3 Jan 17, 2025
@lforst
Copy link
Member

lforst commented Jan 17, 2025

I am gonna go ahead and assume @rlanz/sentry wrongly implemented scoping.

The global Sentry.setUser() sets the user on the "Isolation Scope", which always wrap a request - even before the transaction for the request is started. This is the reason why mutating the isolation scope that way will work for you. The scope that the other library gives you, likely isn't the scope the same scope that is pointed to from the request transaction, which is why it wont be sent along.

@mydea
Copy link
Member

mydea commented Jan 17, 2025

Just expanding a bit on what @lforst already wrote:

In the example, you are not using setUser, so it is hard to reproduce it like this.

Generally, I think the problem you are running into is that the scope you pass in is not used directly, but will be forked. So only changes made to the scope before it is passed to startSpan will be reflected. Think this:

scope.setTag('before', '1');

Sentry.startSpan({ name: 'span', scope }, () => {
  scope.setTag('after', '2');
  // ... do something
  // if you really want to do that, do:
  Sentry.getCurrentScope().setTag('after2', '2');
});
// the `span` will only have the `before` tag, not the `after` tag

The same applies to user and anything else put on the scope. You cannot mutate this scope afterwards.

Why would you even need to manually create scopes and pass them around? All of this should be handled for you by @sentry/node. Scopes should be automatically forked and set for you, no need to do anything. If you want to set the user for a span, just use Sentry.setUser() and it will auto-scope it to the request.

You should never rely on the scope being something specific somewhere, as this may be forked and changed under the hood at any time. Always use Sentry.getCurrentScope() if you really need it, and in 99% of cases just use Sentry.setXXX APIs directly which will auto-scope to request and ensure it is applied to everything in the request, which is usually what you want.

@enixsoft
Copy link
Author

Thank you both for explanation. It's clear now this needs to be solved in rlanz/sentry and there is no issue in @sentry/node.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Package: browser Issues related to the Sentry Browser SDK
Projects
Archived in project
Development

No branches or pull requests

3 participants