Search code examples
pythondotcloudnewrelic

Setting NewRelic environment on Dotcloud (Python)


I have a Python application that is set up using the new New Relic configuration variables in the dotcloud.yml file, which works fine.

However I want to run a sandbox instance as a test/staging environment, so I want to be able to set the environment of the newrelic agent so that it uses the different configuration sections of the ini configuration. My dotcloud.yml is set up as follows:

www:
    type: python
    config:
        python_version: 'v2.7'
        enable_newrelic: True
    environment:
        NEW_RELIC_LICENSE_KEY: *****************************************
        NEW_RELIC_APP_NAME: Application Name
        NEW_RELIC_LOG: /var/log/supervisor/newrelic.log
        NEW_RELIC_LOG_LEVEL: info
        NEW_RELIC_CONFIG_FILE: /home/dotcloud/current/newrelic.ini 

I have custom environment variables so that the sanbox is set as "test" and the live application is set to "production"

I am then calling the following in my uswsgi.py

NEWRELIC_CONFIG = os.environ.get('NEW_RELIC_CONFIG_FILE')
ENVIRONMENT = os.environ.get('MY_ENVIRONMENT', 'test')

newrelic.agent.initialize(NEWRELIC_CONFIG, ENVIRONMENT)

However the dotcloud instance is already enabling newrelic because I get this in the uwsgi.log file:

Sun Nov 18 18:50:12 2012 - unable to load app 0 (mountpoint='') (callable not found or import error)
Traceback (most recent call last):
  File "/home/dotcloud/current/wsgi.py", line 15, in <module>
    newrelic.agent.initialize(NEWRELIC_CONFIG, ENVIRONMENT)
  File "/opt/ve/2.7/local/lib/python2.7/site-packages/newrelic-1.8.0.13/newrelic/config.py", line 1414, in initialize
    log_file, log_level)
  File "/opt/ve/2.7/local/lib/python2.7/site-packages/newrelic-1.8.0.13/newrelic/config.py", line 340, in _load_configuration
    'environment "%s".' % (_config_file, _environment))
newrelic.api.exceptions.ConfigurationError: Configuration has already been done against differing configuration file or environment. Prior configuration file used was "/home/dotcloud/current/newrelic.ini" and environment "None".

So it would seem that the newrelic agent is being initialised before uwsgi.py is called.

So my question is:

Is there a way to initialise the newrelic environment?


Solution

  • The easiest way to do this, without changing any code would be to do the following.

    Create a new sandbox app on dotCloud (see http://docs.dotcloud.com/0.9/guides/flavors/ for more information about creating apps in sandbox mode)

    $ dotcloud create -f sandbox  <app_name>
    

    Deploy your code to the new sandbox app.

    $ dotcloud push
    

    Now you should have the same code running in both your live and sandbox apps. But because you want to change some of the ENV variables for the sandbox app, you need to do one more step.

    According to this page http://docs.dotcloud.com/0.9/guides/environment/#adding-environment-variables there are 2 different ways of adding ENV variables.

    1. Using the dotcloud.yml's environment section.
    2. Using the dotcloud env cli command

    Whereas dotcloud.yml allows you to define different environment variables for each service, dotcloud env set environment variables for the whole application. Moreover, environment variables set with dotcloud env supersede environment variables defined in dotcloud.yml.

    That means that if we want to have different values for our sandbox app, we just need to run a dotcloud env command to set those variables on the sandbox app, which will override the ones in your dotcloud.yml

    If we just want to change on variable we would run this command.

    $ dotcloud env set NEW_RELIC_APP_NAME='Test Application Name'
    

    If we want to update more then one at a time we would do the following.

    $ dotcloud env set \
    'NEW_RELIC_APP_NAME="Test Application Name"' \
    'NEW_RELIC_LOG_LEVEL=debug'
    

    To make sure that you have your env varibles set correctly you can run the following command.

    $ dotcloud env list
    

    Notes

    • The commands above, are using the new dotCloud 0.9.x CLI, if you are using the older one, you will need to either upgrade to the new one, or refer to the documentation for the old CLI http://docs.dotcloud.com/0.4/guides/environment/
    • When you set your environment variables it will restart your application so that it can install the variables, so to limit your downtime, set all of them in one command.