問題
最新のPythonエージェント を使用していて、次のような警告メッセージのログエントリが表示されています。
Attempt to activate application in a process different to where the agent harvest thread was started.
または
Attempt to reactivate application or record transactions in a process different to where the agent was already registered.
アプリケーションは New Relic UI でレポートしているように表示されますが、データは New Relic に報告されません。
解決策
この問題をあなたのアプリで解決するために
newrelic.agent.register_application
ornewrelic.agent.application
内部の関数の中に移動させます。__name__ == __main__
チェックします。newrelic.agent.register_application
やnewrelic.agent.application
の呼び出しがどこで発生しているのかわからない場合は、 エージェントデバッグログ を使って、以下の内容を含むエントリを検索してください。newrelic.core.agent DEBUG - Application was activated from:エージェントを起動したコールのエントリーのトレースバックを使用します。正常と考えられる以下のフレームを参照してください。
File "newrelic/api/transaction.py", line x, in __init__application.activate()File "newrelic/hooks/application_celery.py", line x, in process_initializerapplication_instance().activate()別の場所でアクティベーションが発生している場合は、以下の例を参考にして修正してください。
前です。
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)後:
import newrelic.agentdef custom_event():app = newrelic.agent.application()newrelic.agent.record_custom_event('CustomEvent', {}, application=app)
データが表示されない場合のその他のヒントについては、 Pythonのトラブルシューティング・ドキュメント を参照してください。
原因
これは通常、 newrelic.agent.register_application
または newrelic.agent.application
の呼び出しがインポート時に発生したことが原因です。
例:
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)
コードは通常、Celeryやマルチプロセッシングなどのフレームワークでフォークが発生する前にロードされます。その結果、コードは子プロセスではなく親プロセスにインポートされ、エージェントを起動します。エージェントは親プロセスで起動されるため、子プロセスではデータが収集されません。