I was recently inspired to clean up my settings file by a Twitter exchange involving a few of this forum’s members. I’ve been meaning to do this for quite some time, so I plunged head first into the task. I’ve set out to remove as many
if statements as possible. I don’t want to have multiple settings files if I can avoid it and ideally I don’t want if statements in my settings file as I’m of the opinion that the file then becomes a combination of configuration and implementation file. I’d rather just keep it purely as a configuration file or as close to the idea of configuration file as possible.
The only sticking point I have at the moment is the initialisation of Rollbar and Sentry (I’m trialling Sentry), which I do not want initialised when running my test suite. I run my test suite every time my app is deployed to our production and development servers, and until recently, I had a complete copy of my project’s setting file minus the Rollbar and Sentry stuff as my test settings file. Such duplication is annoying to maintain, at least in my opinion.
I deploy using Dokku which runs my code in a Docker container. I run my tests in the same container as my production code (I have several nodes for redundancy) to ensure that production behaves as expected, but this means that the env vars are the same for my tests as they are for my production code and hence, are set to initialise Sentry and Rollbar.
Disabling Rollbar is trivial enough:
settings.ROLLBAR["enabled"] = False
but disabling Sentry is what is giving me some grief.
What I have done is:
I have an ENV var
SENTRY which should be able to turn Sentry and Rollbar on or off and an if statement to intialise Sentry if
SENTRY = os.getenv("SENTRY", default='False').lower() in ('true', '1', 't') if SENTRY: import rollbar # do Sentry and Rollbar stuff
I thought, and appears somewhat daftly, that I could use a
test_settings file which looks like this:
from . import settings settings.ROLLBAR["enabled"] = False settings.SENTRY = False
But this doesn’t work as I’ve just called my primary settings file which initialised Sentry.
So my question is to those of you who have a single settings file and use some sort of monitoring such as Rollbar and Sentry, how for the love of space have you solved this challenge? Do you have a strategy to not use Sentry when running your tests? Have you a clever way to avoid having multiple configuration files? Have you managed to avoid an
if statement determining whether Sentry should be initialised? Or perhaps you just don’t run your tests on your production servers?
As always, any ideas and help are much appreciated.
All the best,