After you install the Python agent package and create a config file, New Relic's 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.
There are two integration methods:
- Running the admin script via command line: We recommend this method because it is easy and doesn't require making changes to your app code.
- Manual integration: If you cannot use the admin script method, you can manually initialize the Python agent in your app code.
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.
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, use custom instrumentation.)
Here are examples of running the script:
Old command form
New command form
The following examples give instructions for a Bourne-style shell. You may need to adjust these instructions for a different shell.
For framework-specific documentation on using the admin script, see Web frameworks and servers.
Here are some advanced instructions for running the admin script:
For more information, see Admin script details.
If you cannot or do not want to use the recommended admin script integration method, 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 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 agent package must be imported first.
In this example, /some/path/newrelic.ini represents the location of the copy of the config file created during Python agent installation. The config file must be readable by your web application.
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 is 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:
@newrelic.agent.wsgi_application()def application(environ, start_response):...
If the WSGI application entry point is a function or object imported from a different module, wrap it with a wrapper object:
import myappapplication = myapp.WSGIHandler()application = newrelic.agent.WSGIApplicationWrapper(application)
If a supported web framework is being used, you can still use the decorator or wrapper explicitly if, for example, you want to configure 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.