#Sentry didn't capture user data (user ID)

3 messages · Page 1 of 1 (latest)

cursive estuary
#

Hi there! We have a bit of a convoluted set up, but we're running tRPC inside of Fastify inside of a Lambda.

For some reason, we just had an influx of some 403 errors caused by tRPC - but the user ID data was totally missing in the Sentry events?
This is what we're using to add the data to Sentry:

import { fastifyIntegration, init } from '@sentry/aws-serverless';

const dsn = process.env.SENTRY_DSN;
const environment = process.env.SENTRY_ENVIRONMENT;
const release = process.env.SENTRY_RELEASE;

if (dsn) {
  console.log(`Initializing Sentry release "${release}" for environment: ${environment}`);

  init({
    dsn,
    environment,
    release,

    debug: environment !== 'production',

    integrations: [
      fastifyIntegration(),
    ],

    tracePropagationTargets: [
      /^(?!.*amazonaws\.com).+$/gm, // Don't add trace headers to AWS SDK calls
    ],
  });
} else {
  console.log('No Sentry DSN provided');
}

and the fastify setup:

// Add user to sentry scope
authenticated.addHook('onRequest', (request, _, done) => {
  const { userId } = getAuth(request);

  if (userId !== null) {
    request.log = request.log.child({
      userId,
    });

    const scope = getCurrentScope();

    scope.setUser({
      id: userId,
    });
  }

  done();
});

And we also have the tRPC middleware registered first thing, before our authorization gates/middlewares:


const sentryMiddleware = t.middleware(
  trpcMiddleware({
    attachRpcInput: process.env.SENTRY_ENVIRONMENT !== 'production',
  }),
);

export const protectedProcedure = t.procedure
  .use(sentryMiddleware)
  .use(isAuthenticated) // Errors were caused by this middleware

Any ideas as to what's wrong here?

I also checked and the docs are a bit confusing: apparently, there's a setupFastifyErrorHandler method, but it's not exposed in the @sentry/serverless package? What's the ideal way to set up this Lambda -> Fastify -> tRPC chain of things? I am a bit lost as to what we need and what can be ignored - and I fear the issue of us not getting the proper user data might have to do with different, isolated scopes or something?

Thanks in advance!

#

here's what showed up in Sentry for one of the events:

#

(and yes, i checked the CloudWatch logs - the fastify logging does contain the proper user ID, before the tRPC stuff errored)