Python - Extended Manual Installation

Django (Manual)

When the AUTOWRAPT_BOOTSTRAP=instana environment variable is set, the Django framework should be automatically detected and instrumented. If for some reason, you prefer to or need to manually instrument Django, you can instead add instana.instrumentation.django.middleware.InstanaMiddleware to your MIDDLEWARE list in

import os
import instana

# ... <snip> ...


WSGI Stacks

The Instana sensor includes WSGI middleware that can be added to any WSGI compliant stack. This is automated for various stacks but can also be done manually for those we haven’t added support for yet.

The general usage is:

import instana
from instana.wsgi import iWSGIMiddleware

# Wrap the wsgi app in Instana middleware (iWSGIMiddleware)
wsgiapp = iWSGIMiddleware(MyWSGIApplication())

We are working to automate this for all major frameworks but in the meantime, here are some specific quick starts for those we don’t have automatic support for yet.

CherryPy WSGI

import cherrypy
import instana
from instana.wsgi import iWSGIMiddleware

# My CherryPy application
class Root(object):
    def index(self):
        return "hello world"

cherrypy.config.update({'engine.autoreload.on': False})

# Wrap the wsgi app in Instana middleware (iWSGIMiddleware)
wsgiapp = iWSGIMiddleware(cherrypy.tree.mount(Root()))

In this example, we use uwsgi as the webserver and booted with:

uwsgi --socket --protocol=http --wsgi-file --callable wsgiapp -H ~/.local/share/virtualenvs/cherrypyapp-C1BUba0z

Where ~/.local/share/virtualenvs/cherrypyapp-C1BUba0z is the path to my local virtualenv from pipenv

Falcon WSGI

The Falcon framework can also be instrumented via the WSGI wrapper as such:

import falcon
import instana
from instana.wsgi import iWSGIMiddleware

app = falcon.API()

# ...

app = iWSGIMiddleware(app)

Then booting your stack with gunicorn myfalcon:app as an example

uWSGI Webserver

tldr; Make sure enable-threads and lazy-apps is enabled for uwsgi.


This Python instrumentation spawns a lightweight background thread to periodically collect and report process metrics. By default, the GIL and threading is disabled under uWSGI. If you wish to instrument your application running under uWSGI, make sure that you enable threads by passing --enable-threads (or enable-threads = true in ini style). More details in the uWSGI documentation.

Forking off Workers

If you use uWSGI in forking workers mode, you must specify --lazy-apps (or lazy-apps = true in ini style) to load the application in the worker instead of the master process.

uWSGI Example: Command-line

uwsgi --socket --protocol=http -w wsgi -p 4 --enable-threads --lazy-apps

uWSGI Example: ini file

http = :5000
master = true
processes = 4
enable-threads = true # required
lazy-apps = true # if using "processes", set lazy-apps to true

# Set the Instana sensor environment variable here

Want End User Monitoring?

Instana provides deep end user monitoring that links server side traces with browser events to give you a complete view from server to browser.

For Python templates and views, get your EUM API key from your Instana dashboard and you can call instana.helpers.eum_snippet(api_key='abc') from within your layout file. This will output a small javascript snippet of code to instrument browser events. It’s based on Weasel. Check it out.

As an example, you could do the following:

from instana.helpers import eum_snippet

instana.api_key = 'abc'
meta_kvs = { 'username': }

# This will return a string containing the EUM javascript for the layout or view.

The optional second argument to eum_snippet() is a hash of metadata key/values that will be reported along with the browser instrumentation.

Instana EUM example with metadata

See also the End User Monitoring in the Instana documentation portal.

See Also