honeybadger-io / honeybadger-python

Send Python and Django errors to Honeybadger.
https://www.honeybadger.io/
MIT License
15 stars 25 forks source link

Local variables not showing in stacktrace #122

Closed remstone7 closed 1 year ago

remstone7 commented 1 year ago

It looks like this isn't a possibility yet but it would be great to be able to have local variables present in the stack trace. As far as I can tell, the only way you can do this in the current system would be to use context.

the inspect package trace allows you to get the f_locals and this could be added to the context of a project.

In [1]: def run():
   ...:     count = 0
   ...:     data = {"foo":"bar"}
   ...:     for x in range(0, 3):
   ...:         count+=1
   ...:     data['keyerror']
   ...: 
In [2]: import inspect

In [3]: try:
   ...:     run()
   ...: except Exception as e:
   ...:     print(inspect.trace()[-1][0].f_locals)
   ...: 
{'count': 3, 'data': {'foo': 'bar'}, 'x': 2}
joshuap commented 1 year ago

We have a local variables feature in our Ruby gem—I'm wondering if this would be similar to that? These are the important parts:

https://github.com/honeybadger-io/honeybadger-ruby/blob/dd52c352567026b891484b4a1c62fa6e739cbd1f/lib/honeybadger/notice.rb#L228 https://github.com/honeybadger-io/honeybadger-ruby/blob/dd52c352567026b891484b4a1c62fa6e739cbd1f/lib/honeybadger/notice.rb#L228 https://github.com/honeybadger-io/honeybadger-ruby/blob/master/lib/honeybadger/plugins/local_variables.rb

cc @subzero10 @Kelvin4664

Not sure if this is also related to #90 at all.

remstone7 commented 1 year ago

@joshuap yea it looks similar to what i'm asking for here, do the local variables get added to the context or is there another part in the UI that they get added to?

joshuap commented 1 year ago

@joshuap yea it looks similar to what i'm asking for here, do the local variables get added to the context or is there another part in the UI that they get added to?

They would get added to the backtrace—not the context, assuming the info is available from Python. This may still be a slightly different feature from what you're asking for, but I'm not sure. I think #90 is more like if the backtrace shows the method foo() was called with two args, it would include the values, i.e. foo('value one', 'value two'). It would do that for every line of the backtrace that includes a method call.

The local variable feature in Ruby displays the local variables only for the first line of the application trace, and it includes regular variable assignments, i.e.:

def my_method
  foo = "foo value"
  bar = "bar value"

  # `foo` and `bar` will be included in local variables
  raise "error here"
end

It does not include them in context, but I think the UI is similar (it may actually display them inline w/ the first line of the backtrace).

remstone7 commented 1 year ago

@joshuap i'll make a pr, but to confirm, local_variables will just be for the block that the exception is raised?

For 1 function

def run():
    count=0
    try:
        a['a']
    except Exception as e:
        honeybadger.notify(e)

image

for a multiple-caller:

def runner(x, y):
    test_data = {"foo": "bar"}

    try:
        y / x
    except Exception as e:
        honeybadger.notify(e)

def caller():
    runner(0, 3)
caller()

image

joshuap commented 1 year ago

@joshuap i'll make a pr, but to confirm, local_variables will just be for the block that the exception is raised?

For 1 function

def run():
    count=0
    try:
        a['a']
    except Exception as e:
        honeybadger.notify(e)

image

for a multiple-caller:

def runner(x, y):
    test_data = {"foo": "bar"}

    try:
        y / x
    except Exception as e:
        honeybadger.notify(e)

def caller():
    runner(0, 3)
caller()

image

Yeah, that seems right. Thanks, we'll review the PR soon!

subzero10 commented 1 year ago

@remstone7 You can try your changes with v0.13.0! Thank you for submitting the PR!

remstone7 commented 1 year ago

@subzero10 thank you, working on my end 👍