Python agent: Advanced integration

Overview of integration procedure

After you install the New Relic package and create a config file, the Python agent must be integrated with your application. This step allows the agent to capture and report your application's important functions and web requests.

Manual integration: If, for whatever reason, you cannot use the admin script method, you can manually initialize the Python agent in your app code.

Admin script integration method

For a simple explanation of how to use the admin script via the command line, see the integration section of the advanced install instructions. Here are instructions with more details and context.

The newrelic-admin admin script prefixes the command you use to start your WSGI server or web app. This script works by wrapping your startup command and listening for certain function classes used by common frameworks. (To instrument functions and methods that are not instrumented by default, you would use custom instrumentation.)

If your startup command uses exec, separate the setting of the environment variable from the execution of the admin script. Ensure you substitute your existing command options for YOUR_COMMAND_OPTIONS:

Using run-python will always use the same python executable as is installed in the Python installation or virtual environment that newrelic-admin is installed.

Notes about the location of newrelic-admin

The newrelic-admin program you run must be coming from the same Python installation or virtual environment as your application is using. You cannot mix programs/components from different Python installations. If this is done, the agent will not run correctly.

If you use a process management system such as supervisord in a virtual environment, you could use this configuration:

Note the PATH environment variable that points to the same path that would be used by the virtual environment.

For more information on these options to newrelic-admin and the different configuration options based on user environment variables, see the more detailed documentation for the run-program and run-python options.

Manual integration in app code

If you cannot use the recommended admin script integration method or don't wish to, you must initialize the Python agent manually in your web app code. This process involves importing a Python agent package into your app and making a call to initialize the agent. This call modifies your app's import mechanism so that when libraries are imported, the New Relic agent listens for the function classes it recognizes.

For manual integration, add the following to the beginning of the application script file or module that holds your WSGI entry point.

Unlike standard Python functionality, the import order matters: the New Relic package must be imported first.

If you have installed the New Relic Python package into a Python virtual environment, the above lines must be added after you activated or otherwise have setup sys.path to find your virtual environment.

The above should, whenever possible, precede any imports for modules which are going to be instrumented. For some web frameworks, including Flask, this is mandatory. The instrumentation will not work correctly if not placed before all imports which cause code from that framework to be imported.

If you do not use the admin script, but still wish to use the environment variables NEW_RELIC_CONFIG_FILE and NEW_RELIC_ENVIRONMENT to configure the agent, you can call the initialize() function with no arguments and they will be automatically consulted.

import newrelic.agent
newrelic.agent.initialize()

Alternatively, you can set the NEW_RELIC_LICENSE_KEY and NEW_RELIC_APP_NAME environment variables if the defaults for all other configuration settings are suitable.

Do note however that when using an embedded environment, such as Apache/mod_wsgi, you generally cannot rely on being able to derive configuration from environment variables. This is because in embedded systems, more often than not, you don't have a way of being able to set process environment variables which are in turn available to the WSGI script file.

For additional details see the documentation for the initialize() function.

Unsupported web frameworks

If you are using an unsupported web framework or are constructing a WSGI application using a WSGI component library such as Werkzeug or Paste, you may also need to manually wrap the WSGI application entry point. This would be in addition to doing one of the main integration methods (using the admin script, or manually initializing the Python agent).

If the WSGI application entry point is a function declared in the file itself, use a decorator:

If a supported web framework is being used, you might still use the decorator or wrapper explicitly if, for example, you were adding additional WSGI middleware around the supported web framework. This will ensure that execution of all WSGI middleware is also covered by the monitoring done by the agent.