Record spans in Sentry even if they occur while the response is being streamed
-
Install this with the following command
pip install shr_span_recorder -
Add
StreamAwareDjangoIntegration()to your integrations.Example:
from shr_span_recorder import StreamAwareDjangoIntegration sentry_sdk.init( # ... integrations=[ StreamAwareDjangoIntegration(), ], )If you have custom configuration for
DjangoIntegration(), that can be passed toStreamAwareDjangoIntegration(). It understands all of the same options asDjangoIntegration().import sentry_sdk from shr_span_recorder import StreamAwareDjangoIntegration sentry_sdk.init( # ... integrations=[ StreamAwareDjangoIntegration( # Configuration for DjangoIntegration goes here. Any options that # StreamAwareDjangoIntegration does not understand are passed on # unmodified to DjangoIntegration ), ], ) -
Remove
DjangoIntegration()from integrations, if you have explicitly specified it.In other words, you should not do this:
sentry_sdk.init( # ... integrations=[ # WRONG WRONG WRONG StreamAwareDjangoIntegration(), # DO NOT MIX THESE DjangoIntegration(), ], )You should use one or the other. If you use both, Sentry will pick one of them to enable, and the other will be ignored.
-
Remove Django from
disabled_integrations, if you have explicitly disabled it.If
DjangoIntegration()is disabled, this will also disableStreamAwareDjangoIntegration().It is also not required to disable default integrations.
- Compatible with Sentry 2.12.0 to 2.29.1
- I will probably not update this for Sentry 3.x - you're on your own
- Compatibile with WSGI. ASGI probably doesn't work.
This integration is a replacement for DjangoIntegration. It subclasses DjangoIntegration, and so it is able to provide all of the same features as DjangoIntegration. It differs in one major way. Specifically, when DjangoIntegration manages transactions, it begins transactions when calling your app, and ends when your app returns a value.
In contrast, this integration begins the transaction when calling your app, and ends the transaction when your app returns a value AND the WSGI server calls close() on that value.
As a fallback option, the transaction is also closed after five minutes, in case the WSGI server is refusing to close the transaction.
The transaction timeout is implemented by spawning one thread per request. If your application must deal with a high volume of requests, consider whether this performance cost is prohibitive.
This project is licensed under the MIT license.
This project is not endorsed or approved by Functional Software, Inc, the owners of the Sentry trademark.