Search code examples
pythondjangoaws-lambdanewrelic

New Relic on AWS lambda not reading its config file


I'm using zappa to deploy a python/django wsgi app to AWS API Gateway and Lambda.

I have all of these in my environment:

    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"

I'm doing "manual agent start" in my wsgi.py as 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)

I'm not using @newrelic.agent.wsgi_application since django should be auto-magically detected

I've added a middleware to shutdown the agent before the lambda gets frozen, but the logging suggests that only the first request is being sent to New Relic. Without the shutdown, I get no logging from the New Relic agent, and there are no events in 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)

In my newrelic.ini I have the following, but when I log newrelic.agent.global_settings() it contains the default App name (which did get created in APM) and enabled = False, which led to some of the hacks above (environment var, and just editing newrelic.agent.global_settings() before initialize :

[newrelic:dev-zappa]                                                                                          
app_name = DEV APP zappa                                                                                      
monitor_mode = true                                                                                           

TL;DR - Two questions:

  1. How to get New Relic to read it's ini file when it doesn't want to?
  2. How to get New Relic to record data for all requests in AWS lambda?

Solution

  • Zappa does not use your wsgi.py file (currently), so the hooks there aren't happening. Take a look at this PR which allows for it: https://github.com/Miserlou/Zappa/pull/1251