• /
  • EnglishEspañol日本語한국어Português
  • ログイン今すぐ開始

wsgi_application (Python agent API)

Syntax

newrelic.agent.wsgi_application(application=None, name=None, group=None, framework=None)

Monitor web transactions by marking the WSGI application entry point.

Description

wsgi_application is a Python decorator used to monitor web transactions by instrumenting the WSGI application entry point. The Python agent automatically supports most frameworks and servers that use WSGI. If your framework or web server is not supported, you may need to use this API as part of the advanced integration process.

If you cannot use the wsgi_application decorator, you can use one of these other call formats:

  • The wrapper: The wrapper call is WSGIApplicationWrapper. You can use the wrapper in more than one place for distinct WSGI application components that may be stacked. In that case, the first wrapper encountered marks the start of the transaction and the agent determines the target app based on that first wrapper (and ignores subsequent ones).
  • The path-based wrapper: The path-based wrapper call is wrap_wsgi_application. Use this if you could not reference the WSGI object as a variable in the instrumentation scope. This takes the same parameters as the decorator with additional module and object_path parameters.

For an explanation of the reasons to use the decorator versus the wrappers, see Different call formats.

Parameters

Decorator parameters

newrelic.agent.wsgi_application(application=None, name=None, group=None, framework=None)

The wsgi_application decorator uses these parameters:

Parameter

Description

application

string, or application object

Optional. The application name to associate with this data. Default is None. If left without a value, the agent uses the application name specified in the agent configuration.

If a string is provided, it must be the exact application name and cannot be a list of application names. For more on generating an application object, see the application method.

The application, even if specified, can still be overridden if newrelic.app_name is defined within the WSGI application per request environ dictionary.

name

string

Optional, rarely used. Sets a transaction name for all requests captured via the WSGI entry point. Generally not used, because you usually would not want all transactions to have the same name (see also Metric grouping issue).

group

string

Optional, rarely used. The group represents the naming structure for the name parameter. Setting this creates a transaction type subcategory. Similar to name, this should be rarely used because you usually do not want the entire application to report as one transaction name or category.

framework

tuple

Optional. A tuple with two strings representing the name of the framework and the version number. For example: ('Flask', '0.12.2')

Wrapper parameters

newrelic.agent.WSGIApplicationWrapper(wrapped, application=None, name=None, group=None, framework=None)

This takes all of the parameters required by wsgi_application in addition to a wrapped parameter:

Parameter

Description

wrapped

object

Required. The WSGI object to be wrapped.

Path-based wrapper parameters

newrelic.agent.wrap_wsgi_application(module, object_path, application=None, name=None, group=None, framework=None)

In addition to the parameters required by wsgi_application, this call requires additional module and object_path parameters:

Parameter

Description

module

object or string

Required. The module containing the WSGI object.

object_path

string

Required. Represents the class method or method for finding the WSGI object within a file.

Examples

wsgi_application example

An example of the decorator being called without the optional application parameter:

@newrelic.agent.wsgi_application()
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]

WSGIApplicationWrapper example

An example of using the WSGIApplicationWrapper, which may be necessary if the wsgi_application decorator doesn't work:

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
application = newrelic.agent.WSGIApplicationWrapper(application)

wrap_wsgi_application example

An example of using the path-based wrapper:

import newrelic.agent
newrelic.agent.initialize('newrelic.ini')
class Application(object):
def application(self, environ, start_response):
status = '200 OK'
response_headers = [('Content-type', 'text/plain')]
start_response(status, response_headers)
return [b'Hello World']
newrelic.agent.wrap_wsgi_application(__name__, 'Application.application')
Copyright © 2024 New Relic株式会社。

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.