Logging and Sentry

Sentry in a software monitoring tools which have the ability to group errors, this is very useful to don’t be flood by repeated errors which can hide or unfocus to important errors. Here is a tip to well log error to manage this grouping feature using python sentry sdk.

Using sentry_sdk python library, By default, every error logged with logging.error opens an issue on Sentry. Sentry will group errors based on their labels. but it use the non-resolved string of error to group. Let’s have an illustration.

Using f-string won’t group error if args are different :

logger.error(f"my application failed with this parameters : {args}")

where using string format style will group it whatever the args parameter

logger.error("my application failed with this parameters : %s", args)

And here is the result:

To demonstrate this behavior, you’ll find code example shared on my github : https://github.com/kolok/errors-grouping-in-sentry

Conclusion

To group errors with sentry_sdk library, do not use f-string

Leave a Reply

Your email address will not be published. Required fields are marked *