我正在使用zappa将python / django wsgi应用程序部署到AWS API Gateway和Lambda。
我在我的环境中拥有所有这些:
NEW_RELIC_CONFIG_FILE: /var/task/newrelic.ini
NEW_RELIC_LICENSE_KEY: redacted
NEW_RELIC_ENVIRONMENT: dev-zappa
NEW_RELIC_STARTUP_DEBUG: "on"
NEW_RELIC_ENABLED: "on"
我正在wsgi.py
documented做“手动代理启动”:
import newrelic.agent
# Will collect NEW_RELIC_CONFIG_FILE and NEW_RELIC_ENVIRONMENT from the environment
# Dear god why??!?!
# NB: Looks like this IS what makes it go
newrelic.agent.global_settings().enabled = True
newrelic.agent.initialize('/var/task/newrelic.ini', 'dev-zappa', log_file='stderr', log_level=logging.DEBBUG)
我不使用@newrelic.agent.wsgi_application
,因为django应该自动检测
我在lambda冻结之前添加了一个中间件来关闭代理,但是日志记录表明只有第一个请求被发送到New Relic。如果没有关闭,我将无法从New Relic代理进行日志记录,并且APM中没有事件。
class NewRelicShutdownMiddleware(MiddlewareMixin):
"""Simple middleware that shutsdown the NR agent at the end of a request"""
def process_request(self, request):
pass
# really wait for the agent to register with collector
# Enabling this causes more log messages about starting data samplers, but only on the first request
# newrelic.agent.register_application(timeout=10)
def process_response(self, request, response):
newrelic.agent.shutdown_agent(timeout=2.5)
return response
def process_exception(self, request, exception):
pass
newrelic.agent.shutdown_agent(timeout=2.5)
在我的newrelic.ini
我有以下内容,但是当我记录newrelic.agent.global_settings()
时,它包含默认的应用程序名称(确实是在APM中创建的)并且启用了= False,这导致了一些黑客攻击上面的内容(环境变量,只是在初始化之前编辑newrelic.agent.global_settings()
:
[newrelic:dev-zappa]
app_name = DEV APP zappa
monitor_mode = true
TL; DR - 两个问题: