sentry-python icon indicating copy to clipboard operation
sentry-python copied to clipboard

feat(django): add instrumentation for django signals

Open BeryJu opened this issue 3 years ago • 10 comments

Add instrumentation for Django signals

BeryJu avatar Jul 28 '22 16:07 BeryJu

Hey @BeryJu ! Thanks for this contribution! On the first glance it looks great. Please fix the one problem the linter is reporting. I will see If I can review and test this properly this week, so we can get this merged!

antonpirker avatar Aug 02 '22 08:08 antonpirker

Here is a request without the Signals instrumentation:

Screenshot 2022-08-03 at 13 20 47

Here is the same request with the Signals instrumentation:

Screenshot 2022-08-03 at 13 20 23

antonpirker avatar Aug 03 '22 11:08 antonpirker

Hey @BeryJu !

I have now tried your signals instrumentation. See the screenshots above. Are you creating a span for each signal that is triggered, right? Because those timings are always 0.01 it is not really adding a lot of value.

But, would be it be very difficult to add a span for all the receiver functions? So we would get the timing of the actual functions that where running because of signals? This would add really a LOT of value for all the Django developers out there!

antonpirker avatar Aug 03 '22 11:08 antonpirker

Hey @antonpirker, the PR should add a span for each signal receiver function. I used to run this modified library for https://github.com/goauthentik/authentik but have since reverted to the upstream SDK so I haven't actually tested this in a bit. I'll have a look this evening

BeryJu avatar Aug 03 '22 11:08 BeryJu

image

I just re-checked and indeed a trace is created for each handler, the dotted-import-path for each entry is a signal handler (the times still seem a bit low though)

BeryJu avatar Aug 03 '22 19:08 BeryJu

I just updated my sample project to actually doing some work in the signal receivers (added a time.sleep()) and now the screenshot looks like I envisioned it:

Screenshot 2022-08-04 at 09 34 03

So this adds really value to Django projects!

Now we just need to find a way to instrument individual signal handlers with wrapping instead of overriding the send function and we are good. Having it like as is is not going to work, because it could change the behavior of users code when they update the django version and we never do that.

antonpirker avatar Aug 04 '22 07:08 antonpirker

I added a comment with a possible solution to my review @BeryJu . Please have a look.

And a heads up: I will be on vacation next week and the following week. So this will probably be merged after my vacation.

antonpirker avatar Aug 04 '22 08:08 antonpirker

Thanks for the quick update @BeryJu ! This is now working for .send() and .send_robut() right?

Unfortunately I will not be able to review it befor my 2 week vacation, but maybe a colleague (@sl0thentr0py ) picks it up and reviews it.

antonpirker avatar Aug 05 '22 12:08 antonpirker

Hey @BeryJu ! Before I head into my vacation: if you send me your shipping address to: anton (dot) pirker at sentry |dot| io, I can send you a little token of appreciation. (after my vacation) :-)

antonpirker avatar Aug 05 '22 15:08 antonpirker

Thanks for the quick update @BeryJu ! This is now working for .send() and .send_robut() right?

Should work for both, I have not actually tested it myself though (will see if I get some time to do so this weekend). Funnily enough I'll also be on vacation for the next two weeks, so no worries about the review

BeryJu avatar Aug 05 '22 15:08 BeryJu

Hey @BeryJu

Sorry for the delay! I will have a look at the PR tomorrow, promised!

antonpirker avatar Sep 12 '22 14:09 antonpirker

The new behavior breaks some our our tests. Most notably this one: https://github.com/getsentry/sentry-python/blob/master/tests/integrations/django/test_basic.py#L687-L731

Could you change this test (and maybe other failing ones) to reflect the new behavior @BeryJu ?

HINT: Navigating the test errors is still a bit cumbersome. But If you click on the details of the "CI / Run Tests" check above and then search for "error:" you find the actual error message the fastest.

Thanks a lot!

antonpirker avatar Sep 13 '22 07:09 antonpirker

Tests should be fixed now, also added type annotations

btw, stickers arrived, thank you very much!

BeryJu avatar Sep 13 '22 08:09 BeryJu

That was fast! Amazing! Only the tests for old versions (Python 2.7 and 3.5) are failing. Can you please take a look @BeryJu ?

antonpirker avatar Sep 14 '22 09:09 antonpirker

Should be all good now, missed some special cases for older django versions

BeryJu avatar Sep 14 '22 13:09 BeryJu

I will do some manual testing again, and then it will be released with the next version. (so probably early next week) Congratulations and Thankyou @BeryJu

antonpirker avatar Sep 16 '22 08:09 antonpirker

Hey @BeryJu

Really great work. You are not by any chance a Go Freelancer and want to help us give our Go SDK a little love?

antonpirker avatar Sep 16 '22 15:09 antonpirker

If yes, you can message me at anton dot pirker ät sentry dot io

antonpirker avatar Sep 19 '22 07:09 antonpirker