With the Python agent, you can add browser monitoring to your webpages either automatically or manually. To enable in the user interface, follow the procedures to install the browser agent. Then follow the procedures in this section to set up the Python agent.
To use the browser agent with your Python agent, ensure you have the latest release. For more information, see the agent installation instructions.
Support for automatic instrumentation
The Python agent only supports automatic instrumentation for WSGI applications that are hosted directly on a dedicated WSGI server with a native WSGI interface, and where the WSGI application returns the HTML page response via an iterable/generator.
Automatic instrumentation requires a dedicated WSGI server.
- WSGI servers such as Apache/mod_wsgi, gunicorn and uWSGI are supported.
- Using WSGI adapters on top of either the Tornado or Twisted frameworks is not supported.
The Python agent does not support insertion of instrumentation when using the write()
callback returned from the WSGI start_response()
function to write back the HTML page response. For more information about requirements for automatic instrumentation, see Troubleshooting browser instrumentation in Python.
Use automatic instrumentation
The Python agent can automatically add the browser JavaScript agent to any HTML page responses. For WSGI applications, the agent uses a WSGI middleware installed into the request pipeline. Since the middleware is at WSGI level, automatic instrumentation should work for any WSGI application regardless of the web framework you are using, including popular Python web frameworks such as Django, Flask and Pyramid.
Disable instrumentation for specific pages
If you want to use automatic instrumentation, but have an HTML page response that you do not want instrumented, you can disable instrumentation for that request. Add this Python agent API call into the code for that request handler:
newrelic.agent.disable_browser_autorum()
If you are using a WSGI server that allows per-request configuration, such as Apache/mod_wsgi, you can also disable automatic instrumentation by setting the newrelic.disable_browser_autorum
key for that request in the WSGI environ dictionary passed into your monitored application.
For example, if using Apache/mod_wsgi, you could disable automatic instrumentation by filtering on the URL:
<Location /login>SetEnv newrelic.disable_browser_autorum true</Location>
Manually enable browser monitoring for specific pages
You can manually add browser instrumentation to specific pages. This is useful, for example, if automatic instrumentation is not possible for your application, if you use a nonced content security policy, or if you want to collect browser data for only specific pages.
The calls in this procedure must be placed on every request you want to monitor. You cannot call these functions once at global scope and cache the result for later use.
Important
Starting with Python Agent version 9.4.0+, the get_browser_timing_footer
API is deprecated and always returns an empty string. The previous functionality of get_browser_timing_footer()
is now included in get_browser_timing_header()
.
On older versions of the agent, the footer is required to load the browser agent.
Disable automatic instrumentation: In the newrelic.ini file, set
browser_monitoring.auto_instrument
tofalse
.Restart your application.
Place these calls into the request handler of every page you want to monitor:
import newrelic.agentheader = newrelic.agent.get_browser_timing_header()# The footer is required on Python Agent versions 9.3.0 and below.footer = newrelic.agent.get_browser_timing_footer()Pass the
header
and possiblyfooter
variables to the HTML response.On the corresponding page, insert the
header
variable in the<head>
element. Place theheader
variable after anycharset
properties or any meta tags forhttp-equiv
with a value ofX-UA-Compatible
.If required, insert the
footer
variable before the end of the<body>
element.
Wait a few minutes, then check your browser monitoring data. If no data appears after waiting a few minutes, see Troubleshooting browser instrumentation in Python.
CSP Nonce support
CSP Nonce usage in our browser instrumentation is supported in version 9.4.0+ of the Python agent. To use a nonce with the browser instrumentation, you must disable browser monitoring auto instrumentation and use the manual instrumentation via the agent API.
You may now pass a nonce as an argument into the API method to allow the agent to insert the nonce into the browser instrumentation.
Disable automatic instrumentation: In the
newrelic.ini
file, setbrowser_monitoring.auto_instrument
tofalse
.Restart your application.
Place these calls into the request handler of every page you want to monitor, replacing "NONCE" with your CSP nonce:
import newrelic.agentheader = newrelic.agent.get_browser_timing_header("NONCE")Pass the
header
variable to the HTML response.On the corresponding page, insert the
header
variable in the<head>
element. Place theheader
variable after anycharset
properties or any meta tags forhttp-equiv
with a value ofX-UA-Compatible
.