-
Notifications
You must be signed in to change notification settings - Fork 444
Closed
Labels
Description
Use case
When using the logger and logging an exception, the exception traceback appears as a single string that includes the stack trace, each frame of which has a file, a line number, a function name, and the statement on that line. Parsing that line, either with the Mk I eyeball or with code, is difficult
Solution/User Experience
Because logger is all about structured logging, it would be nice if the stack backtrace were structured as well. Something like:
Traceback = [
{
"File" : "myfile.py",
"Line" : 44,
"Function" : "do_something_risky",
"Statement": "do_division(x, 0)"
},
{
"File" : "myotherfile.py",
"Line": 12,
"Function" : "do_division",
"Statement" : "return numerator/denominator
}
]
Alternative solutions
There are no good ones that I'm aware of. I might be able to overload the existing functions in the traceback module, but doing that for every lambda function in my solution is not ideal
Acknowledgment
- This feature request meets Lambda Powertools TenetsShould this be considered in other Lambda Powertools languages? i.e. Java, TypeScriptTo pick up a draggable item, press the space bar. While dragging, use the arrow keys to move the item. Press space again to drop the item in its new position, or press escape to cancel.
Metadata
Metadata
Assignees
Labels
Type
Projects
Status
Shipped
Activity
heitorlessa commentedon Jul 4, 2022
That's a great idea @Nogginboink! Are you thinking of including
traceback
field in addition toexception_name
andexception
? Or an opt-in likePOWERTOOLS_LOGGER_ENHANCED_TRACEBACK
behaviour that replaces the value ofexception
with that list?We should definitely look into making this a thing after our pause ends (end of the month). We could ping back here after the pause to ask whether you're up to contribute w/ any guidance or for us to schedule it along with our backlog.
Thank you!
ppavlovdev commentedon Jan 5, 2023
Hey @Nogginboink @heitorlessa! I would like to help you guys to implement this feature.
Regarding to your conversation, I think we should stick to additional
traceback
field rather than replacingexception
field. Let's assume that someone somewhere will want to process rawexception
in a custom way.We can add
include_traceback
orformat_traceback
flag toLambdaPowertoolsFormatter
constructor for more flexibility. Keep in mind that there possibly can be a lot of data intraceback
and in some cases it can cause user-specific problems.kbakk commentedon Jan 7, 2023
Wanted to see if there are some standards.
OpenTelemetry came to mind, but they seem to model it as a string under
exception.stacktrace
(https://opentelemetry.io/docs/reference/specification/logs/semantic_conventions/exceptions/#attributes).Google only mentions that it should be part of the message key/val (https://cloud.google.com/logging/docs/structured-logging), but I couldn't find how to actually format it.
Sentry have a lot richer model (full example in their demo site - https://try.sentry-demo.com/organizations/on-caiman/issues/?project=141374&query=&referrer=issue-list&statsPeriod=14d):
This seems to be (at least part of) the code responsible for the frame formatting/extraction:
https://github.com/getsentry/sentry-python/blob/2f67f12e405f8a6f89418d96071158367fcf516f/sentry_sdk/utils.py#L551
Because there are so many different use cases, it could be a good idea to account for that the users might want to format the tracebacks differently, by allowing to pass a richer set of config options when initializing the logger. E.g. I might want
filename
andlineno
, others might want a lot more, maybe even run a custom data scrubbing function.stevrobu commentedon Sep 19, 2023
I'd like to take this request on.
leandrodamascena commentedon Sep 22, 2023
Hi @stevrobu! It will be amazing if you can work on this issue.
Do you need any help? I can help you with some guidance if needed.
Thanks a lot
stevrobu commentedon Sep 22, 2023
@leandrodamascena - Yes. I would appreciate some help. I will connect with you offline.
leandrodamascena commentedon Sep 27, 2023
Hi @stevrobu! I was playing around with this issue and managed to add a new field with the exception formatted in JSON. The code isn't good yet and missing important things, but I think it's a good starting point. What do you think we talk so I can show you what I did and you can follow?
Code example:
Output:
stevrobu commentedon Sep 28, 2023
@leandrodamascena - Sure. Let's talk. I have a pull request ready, but let's compare code first.
leandrodamascena commentedon Oct 2, 2023
I really like the way the Sentry creates exception and we try to add something like that.
I'm not sure if we will have any experience gained if we allow the customer to add/remove fields from the stacktrace. This is a stracktrace, the customer generally wants to know the error that occurred and the frames where the error occurred in order to parse it. If I allowed the customer to remove the line, for example, how will they find the error? I think we should define a standard and use it. What do you think @kbakk and @stevrobu?
We have a PR - #3147. In addition to some bugs that we need to fix, I would like to hear your feedback.
cc @heitorlessa
Thank you.
github-actions commentedon Oct 10, 2023
This issue is now closed. Please be mindful that future comments are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.