Detecting that You're Deployed to Heroku

January 22, 2014

How do you detect that you’re deployed to Heroku, so you can make your code run differently when deployed? You don’t. Instead, you look to see if a particular behavior has been requested, and act accordingly.

For some contract work we integrated Bugsnag into a Rails project. Even though we only had Bugsnag turned on in Rails production mode, we would always throw a few errors during our final local “production” testing, cluttering up the logs and HipChat.

To turn off Bugsnag when working locally, I wanted to figure out how to detect when we were being served from Heroku. I finally figured out that that’s not what I wanted; I wanted to know that I was running in an environment that required Bugsnag integration. That’s deployment-platform-agnostic, so I shouldn’t really be looking to see if I’m on Heroku or not!

Environment Variables for Toggling Capabilities

We decided to go with:

Choose a new environment variable. If it’s nonexistent or “falsy” (false, or 0, or whatever) behave “like a developer machine.” If it exists, with a “truthy” value, behave “like you’re really deployed.”

That way, when a new developer joins the team, he or she will get the “local developer behavior” “for free.” You have to tweak your installation a bit to work like “true deployment.”

In our case, we used the environment variable “REPORT_EXCEPTIONS.” We set it to ‘true’ in the Heroku shell, like this…

heroku config:set REPORT_EXCEPTIONS=true

… and left it nonexistent on our development machines. See the Heroku configuration vars page. (Today, I would probably use Foreman and heroku-config. Again, see the configuration vars page.)

Then, in the code, act on that environment variable, or lack thereof.

Tags: heroku
comments powered by Disqus