#Include local variables

70 messages · Page 1 of 1 (latest)

young remnant
#

Hello, I'm exploring Sentry for the company I'm working at. I'm using the nodejs-express SDK to track errors in our Apollo GraphQL server running as an Express app.
I was able to capture an error but the event doesn't include local variables.
I want to be able to use Sentry the same way I use a debugger where I'm able to see the actual values of all variables in a given scope.
I tried to include both includeLocalVariables: true and Sentry.localVariablesIntegration() in Sentry.init() but it doesn't work for me. Help will be much appreciated!

late reef
young remnant
#

Thanks for the reply. No, I'm not using ESM. Also, according to the documentation, there shouldn't be an issue when the error is handled which is the case I was testing.

late reef
#

if you enable debug: true, the sdk outputs more details about its internals

#

could you enable that option in your Sentry.init call and then share your debug output?

young remnant
#

These are the logs from the minute I invoked the request that causes the error.

#

The only warning I see is [warn]: Discarded session because of missing or non-string release. Can it be related?

late reef
#

no that's not related.

are you capturing an error here?

young remnant
#

Yeah.. look for “Sentry Logger [log]: Captured error event Cannot read properties of undefined (reading 'map')

late reef
#

can you share the debug logs at startup? Does it show the localvariables integration getting installed?

young remnant
late reef
#

and running in commonjs hmm. Super strange.

It's hard to know what exactly is going wrong without a reproduction of some kind. Are you able to share that?

young remnant
#

It's a proprietary code unfortunately...

#

But it's supposed to work with this kind of setup, right? Meaning, an Apollo GraphQL server that runs on top of an Express app..

young remnant
late reef
#

thanks! we'll take a look on monday. This should be working, but perhaps there is a case we missed

late reef
#

thanks for the reproduction, really helped a lot

young remnant
#

Awesome.. I'm glad it worked out. Thanks!

young remnant
#

@late reef Any estimation when this fix will be released?

late reef
#

hopefully today! otherwise tomorrow morning

young remnant
#

Great..thx!

young remnant
#

@late reef I believe it didn't fix the issue..I still can't see local variables.

#

I used the MRE to test it..

late reef
#

oh interesting

#

^ this is what I got after testing

#

let me take another look

young remnant
#

I don't even see args on the bottom

late reef
#

works for me on latest SDK version 9.32.0

young remnant
#

Weird..I'm also using the latest version and that's what I'm getting..

#

So the screenshot you shared was a result of an older version?

late reef
#

nope, it was from the latest SDK version that I just installed and ran

#

if you add beforeSend to your Sentry.init call, you can debug the payload locally

beforeSend: (event, hint) => {
    console.log(JSON.stringify(event, null, 2));
    return null;
  },
#

this got me something that looks like so:

{
  "filename": "/Users/abhijeetprasad/customer/mini-graphql/app.js",
  "module": "app",
  "function": "Object.testSentry",
  "lineno": 32,
  "colno": 14,
  "in_app": true,
  "pre_context": [
    "  }",
    "`;",
    "",
    "const resolvers = {",
    "  Query: {",
    "    testSentry: (args) => {",
    "      try {"
  ],
  "context_line": "        args.foo.map((x) => x);",
  "post_context": [
    "        return true;",
    "      } catch (error) {",
    "        Sentry.captureException(error);",
    "        return false;",
    "      }",
    "    },",
    "  },"
  ],
  "vars": {
    "args": "<undefined>"
  }
}
young remnant
#

shouldn't args be an empty object though?

#

I guess it depends on what you're passing. I was assuming you're passing an object..

#

Ok I'll give beforeSend a try

#

I don't have a event.vars entry

late reef
#

vars are associated to an individual stacktrace frame

young remnant
#

I don't see it there either.
I ran event.exception.values.flatMap(v => v.stacktrace.frames.map(f => f.vars)) and I'm getting an array of a bunch of undefined as opposed to "<undefined>"

#

Also, in your screenshot you can see the args in the bottom but in mine it's missing..

young remnant
#

This is how my request looks like. Are you sending the same thing?

late reef
#

I was just doing

curl -X POST http://localhost:4434/graphql \
  -H "Content-Type: application/json" \
  -d '{"query": "query { testSentry(input: {}) }"}'

but if I set variables to an empty object I get the same thing

#

@young remnant are you able to send a link to the sentry event? Or if you scroll down to the bottom, what does the SDK section show?

young remnant
#

Can it be related to node version? OS maybe? I'm running out of ideas..

late reef
#

are you able to make it work in a non-graphql app?

also maybe OS/node version related - what OS/node version are you on?

young remnant
#

Haven't tried it on a non-graphql app yet..

#

I tried running the following script and I still don't see vars in the stacktrace frame

require('./instrument.js');
const Sentry = require('@sentry/node');

const dummy = (args) =>  {
  try {
    args.foo.map((x) => x);
  } catch (error) {
    Sentry.captureException(error);
  }
  return true;
};

dummy({});
late reef
#

so for you above example, it's because the local variables inspector is async

#
require("./instrument.js");
const Sentry = require("@sentry/node");

const dummy = (args) => {
  let x = 1;
  try {
    args.foo.map((x) => x);
  } catch (error) {
    Sentry.captureException(error);
  }
  return true;
};

async function run() {
  await new Promise((resolve) => setTimeout(resolve, 1000));
  dummy({});
}

run();

you'll see vars for this

#

@tawny flume I guess we have no way to wait for the inspector before capturing errors right?

young remnant
young remnant
late reef
#

so strange

#

also on node 22 / sdk version 9.32.0

young remnant
#

Can it be a project setting?

late reef
#

theres no project setting for this, but you said it's not showing up in beforeSend right? that means the sdk itself isn't flushing it

young remnant
#

yeah you're right..

#

The contexts are pretty much the same as yours...maybe I should move to Canada

#

Is there anything else I can do to help you figure this out? I'm really out of ideas..
it's seems like a local issue since even the non-graphql example doesn't work by me.

late reef
#

you're always welcome up north 😄

I'll try something else out tommorow

young remnant
#

Lol thx!
I was debugging the non-graphql example and it seems like a fallback logic is being executed in my case. Is that something that can be relevant?

young remnant
#

Another, probably more relevant, clue is that in this function, common.LOCAL_VARIABLES_KEY in hint.originalException is false. That means no local variables are attached to the originalException object.

young remnant
#

Finally got it!!
Apparently I needed to set includeLocalVariables in the init to true 😅
I thought that was the default behavior. Also, it's disabled if I'm using a debugger so it will fetch the local variables only if I run without the debugger. I also tested the graphql app and it works.
Thank you very much and sorry for the hassle..

late reef
#

ahhhh okay shoot

#

glad we got it figured out!

#

thanks for debugging with me!

#

@young remnant shoot me an email at aprasad @ sentry . io (remove the spaces), we can get you some sentry swag for your help

young remnant
#

@late reef Sounds great..thx!