0

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?

Mark Unsworth
  • 3,027
  • 2
  • 20
  • 21

2 Answers2

1

Unless they are doing something odd, you should be able to override the app_name supplied by the agent configuration file by doing:

import newrelic.agent
newrelic.agent.global_settings().app_name = 'Test Application Name'

Don't call newrelic.agent.initialize() a second time.

This will only work if app_name is listing a single application to report data to.

Graham Dumpleton
  • 57,726
  • 6
  • 119
  • 134
  • Works perfectly, thank you. This is great also if you need to run your server with `newrelic-admin runprogram ...` and can't dynamically set the environment variable. – Liam Horne Jan 23 '15 at 00:01
1

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.
Ken Cochrane
  • 75,357
  • 9
  • 52
  • 60
  • Thanks Ken. This will work to give me different app names (which is enough for now) but if you want to be able to set other New Relic specific configurations (perhaps turning off browser monitoring in test or parameter capture in live) it would require a different configuration file for each environment. – Mark Unsworth Nov 19 '12 at 21:45
  • @MarkUnsworth unless I'm wrong I think all of those parameters can be set from environment variables. I think you might even be able to specify a different ini file to use via the env variables. but Graham Dumpleton would know best. – Ken Cochrane Nov 20 '12 at 02:28
  • Thanks Ken. I took the approach of adding separate ini files for each environment. It would be good to be able to override the new relic environment based on a DotCloud environment variable though, so we only need to maintain 1 config file though. I'll raise a support ticket for this. – Mark Unsworth Nov 26 '12 at 09:48