Activate application warning (Python)

Problem

You are using the latest Python agent, and you see a log entry with a warning message such as:

Attempt to activate application in a process different to where the agent harvest thread was started.

OR:

Attempt to reactivate application or record transactions in a process different to where the agent was already registered.

The application shows as reporting in the New Relic UI, but no data is reported to New Relic.

Solution

To solve this problem with your app:

  1. Move all calls to newrelic.agent.register_application or newrelic.agent.application inside of functions inside of a __name__ == __main__ check.
  2. If you're unsure where the calls to newrelic.agent.register_application or newrelic.agent.application are occurring, use the agent debug logs to search for an entry containing:

    newrelic.core.agent DEBUG - Application was activated from:
  3. Use the entry's traceback of the call that activated the agent. Refer to the following frames, which are considered normal:

    File "newrelic/api/transaction.py", line x, in __init__
        application.activate()
    
    File "newrelic/hooks/application_celery.py", line x, in process_initializer
        application_instance().activate()
  4. If the activation is occurring from a different location, correct this issue by following this example:

    Before:

    import newrelic.agent
    
    # This will cause the agent to activate whenever custom_event is imported
    app = newrelic.agent.application()
    
    def custom_event():
        newrelic.agent.record_custom_event('CustomEvent', {}, application=app)
    

    After:

    import newrelic.agent
    
    def custom_event():
        app = newrelic.agent.application()
        newrelic.agent.record_custom_event('CustomEvent', {}, application=app)
    

For other tips when no data appears, see the Python troubleshooting documentation.

Cause

This is usually caused by a call to newrelic.agent.register_application or newrelic.agent.application occurring at import time.

Example:

import newrelic.agent

# This will cause the agent to activate whenever custom_event is imported
app = newrelic.agent.application()

def custom_event():
    newrelic.agent.record_custom_event('CustomEvent', {}, application=app)

Code typically is loaded before a fork occurs in frameworks such as Celery or multiprocessing. As a result, the code is imported into the parent process instead of the child, launching the agent. Since the agent is launched in the parent process, no data is collected in the child process.

For more help

Recommendations for learning more: