Problem
Important
Note this is only an issue if no data is being reported.
You're 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:
Move all calls to
newrelic.agent.register_application
ornewrelic.agent.application
inside of functions inside of a__name__ == __main__
check.If you're unsure where the calls to
newrelic.agent.register_application
ornewrelic.agent.application
are occurring, use the agent debug logs to search for an entry containing:newrelic.core.agent DEBUG - Application was activated from: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_initializerapplication_instance().activate()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 importedapp = newrelic.agent.application()def custom_event():newrelic.agent.record_custom_event('CustomEvent', {}, application=app)After:
import newrelic.agentdef 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
There're two primary causes of this issue. The first is creating an application instance multiple times or creating an application instance prior to forking.
This is usually caused by a call to
newrelic.agent.register_application
ornewrelic.agent.application
occurring at import time.Example:
import newrelic.agent# This will cause the agent to activate whenever custom_event is importedapp = newrelic.agent.application()def custom_event():newrelic.agent.record_custom_event('CustomEvent', {}, application=app)This issue can also occur when monitoring services like celery where a main parent process launches worker processes. It occurs when an agent application instance is created on the main process prior to forking the worker processes. Since the agent is launched in the parent process, no data is collected in the child process.