Python agent and FASTCGI

The New Relic Python agent can be used with FASTCGI in conjunction with flup. There are no known special requirements or restrictions.

Wrapper script

For FASTCGI the web application is always managed as an external process. If you have integrated the FASTCGI/WSGI adapter for flup, as in:

#!/usr/bin/env python

import sys
from fcgi import WSGIServer

def application(environ, start_response):
    status = '200 OK'
    output = 'Hello World!'

    response_headers = [('Content-type', 'text/plain'),
                        ('Content-Length', str(len(output)))]
    start_response(status, response_headers)

    return [output]

ret = WSGIServer(application).run()

and you are starting your FASTCGI process as a Python script directly with Python as:

python app.py

all you need to instead do is run:

NEW_RELIC_CONFIG_FILE=newrelic.ini newrelic-admin run-program python app.py

When doing this, instead of defining the NEW_RELIC_CONFIG_FILE environment variable on the same line as executing the command, it can be separately exported and set in the process environment before running the command.

NEW_RELIC_CONFIG_FILE=newrelic.ini
export NEW_RELIC_CONFIG_FILE

newrelic-admin run-program python app.py

The path newrelic.ini should be replaced with the appropriate absolute or relative path to the location of your actual Python agent configuration file.

Agent initialization

When using the newrelic-admin wrapper script with the flup FASTCGI/WSGI adapter, the agent will be automatically initialized when the web application starts. You do not need to make any code changes to your web application.

If you are not able to, or do not wish to use the newrelic-admin wrapper script, you can still manually add in calls to the agent API to initialize it when your web application starts.

As per instructions for integration with your Python application, this should be done as the very first step by your web application, after any module import paths are setup, but before any imports of the FASTCGI/WSGI adapter, separate application modules or any modules for frameworks being used.

For more help

Join the discussion about Python in the New Relic Online Technical Community! The Technical Community is a public platform to discuss and troubleshoot your New Relic toolset.

If you need additional help, get support at support.newrelic.com.