Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update rqworker command Sentry configuration (alternative version) #442

Merged
merged 12 commits into from
Sep 24, 2020
Merged

Update rqworker command Sentry configuration (alternative version) #442

merged 12 commits into from
Sep 24, 2020

Conversation

hugorodgerbrown
Copy link
Contributor

@hugorodgerbrown hugorodgerbrown commented Aug 22, 2020

This is a simpler alternative to #441. Fixes #440 and #427.


This PR changes how the rqworker command integrates with Sentry.

At the current time, if you pass in sentry-dsn, or have django.conf.settings.SENTRY_DSN set, then the command will reinitialise Sentry using a limited subset of options, overriding any existing Django setup. The options supported are debug and ca_certs, and the list of integrations is restricted to RqIntegration.

This PR prevents the possible clash with Django settings by removing the fallback to SENTRY_DSN. If you pass sentry-dsn to the command, it will use it, and ignore any existing Django settings configuration. If you don't pass it, it will do nothing - i.e. it will rely on any existing Django configuration.

In addition, this does away with the rq.contrib.sentry.register_sentry function call in favour of a direct sentry_sdk.init call. The RQ function explicitly sets up Sentry with integrations=[RqIntegration()], preventing the use of any other integrations. This makes sense within the context of rq, but if you are using django-rq then it makes more sense to use [RedisIntegration(), RqIntegration(), DjangoIntegration()].

This PR will be a breaking change for anyone who does not pass sentry-dsn to the command, but does have django.conf.settings.SENTRY_DSN set. This may be a dealbreaker, but that group seems like an edge case?

I have added a method to the command to combine existing Sentry config
options with the options passed in on the command invocation. This means
that if you have initialised Sentry in the Django settings module with specific
options, these will be maintained.

Fixes #440
In the version I have attempted to tackle the issue of Sentry
configuration from a clean slate. I have removed the code in
rqworker that falls back the the `SENTRY_DSN` Django
setting, on the basis that if the client user has set this in
their project settings.py, then they are capable of configuring
Sentry there.

If the user passes `sentry_dsn` explicitly to the command,
then this will override any configuration in the Django
settings, and replace them with a configuration that includes
the RqIntegration and DjangoIntegration.
@codecov
Copy link

codecov bot commented Aug 22, 2020

Codecov Report

Merging #442 into master will increase coverage by 0.37%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #442      +/-   ##
==========================================
+ Coverage   89.78%   90.16%   +0.37%     
==========================================
  Files          27       27              
  Lines        1635     1637       +2     
==========================================
+ Hits         1468     1476       +8     
+ Misses        167      161       -6     
Impacted Files Coverage Δ
django_rq/management/commands/rqworker.py 96.72% <100.00%> (+8.84%) ⬆️
django_rq/tests/tests.py 93.83% <100.00%> (+0.08%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update f2dbf8c...17f1fbd. Read the comment docs.

Comment on lines +104 to +110
sentry_dsn = options.pop('sentry_dsn')
if sentry_dsn:
try:
configure_sentry(sentry_dsn, **options)
except ImportError:
self.stderr.write("Please install sentry-sdk using `pip install sentry-sdk`")
sys.exit(1)
Copy link
Contributor Author

@hugorodgerbrown hugorodgerbrown Aug 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the big change in this PR - there is no fallback to django.conf.settings.SENTRY_DSN - if you don't pass in an explicit sentry-dsn to the command, then it will use the existing Django configuration.

Comment on lines +37 to +46
sentry_options = {
'debug': options.get('sentry_debug', False),
'ca_certs': options.get('sentry_ca_certs', None),
'integrations': [
sentry_sdk.integrations.redis.RedisIntegration(),
sentry_sdk.integrations.rq.RqIntegration(),
sentry_sdk.integrations.django.DjangoIntegration()
]
}
sentry_sdk.init(sentry_dsn, **sentry_options)
Copy link
Contributor Author

@hugorodgerbrown hugorodgerbrown Aug 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a direct replacement for the call to rq.contrib.sentry.register_sentry. The only functional change that this results in is the addition of the RedisIntegration and DjangoIntegration to the list of configured sentry integrations.

@selwin
Copy link
Collaborator

selwin commented Sep 10, 2020

Hey, sorry for the late reply. I’m ok with this change.

@selwin
Copy link
Collaborator

selwin commented Sep 10, 2020

Give me a few days to further process this to think of other potential ramifications.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use existing Sentry client if configured
2 participants