Python Application Monitoring, Automatic Tracing and OpenTracing

Including OpenTracing 2.0 Support

Instana’s Python Application Monitoring includes automatic end-to-end tracing of all user requests. It’s the easiest and fastest way to get the detailed traces necessary to find and fix problems. With that said, though, we recognize that sometimes, developers have a need to use other tracing methods, such as OpenTracing (that’s just one of the reasons that Instana participates as a member of the OpenTracing Specification Council).

Maybe you saw our recent announcement about Application Perspectives and Personalized APM. Along with those leading edge features, we also released support in our Python Monitoring sensor for OpenTracing 2.0. There are quite a few changes with this new version of the OpenTracing API, so you might have to refactor any of your code using the previous versions of the OpenTracing API.

Instana’s Python sensor supports many popular frameworks for entry and exit points, see the documentation for a complete list. All supported frameworks are automatically instrumented without requiring any code changes. All you have to do is install the instana package into your virtualenv or container.

$ pip install instana

Alternatively add it to your requirements.txt file (the example below is for Flask).

Flask
requests
instana
$ pip install -r requirements.txt
$ export AUTOWRAPT_BOOTSTRAP=flask
$ python my-app.py

That’s it!

Instana will now monitor the Python runtime and trace all requests end-to-end. This simple change is easily accommodated into the Dockerfile or startup script of your Python applications.

OpenTracing API 2.0 in Instana’s Python Sensor

With the standard entry and exit points taken care of automatically by Instana’s Python sensor, you may optionally want to shed some light on some critical code between those points, this is where you can use the OpenTracing API. Installing the Instana package as shown above also includes the OpenTracing package. The code snippet below shows how you can easily insert a custom span to provide additional detail.

import opentracing as ot
import opentracing.ext.tags as tags

# This entry point will be automatically instrumented
@app.route(‘/foo’, methods=[‘GET’])
def foo():
# Some regular code
# e.g. requests call

# Start a new span
with ot.tracer.start_active_span(’mystuff’) as scope:
scope.span.set_tag(tags.SPAN_KIND, tags.SPAN_KING_RPC_CLIENT)
scope.span.set_tag(tags.COMPONENT, ‘python foo’)
scope.span.set_tag(‘version’, ‘4.2.0’)
scope.span.log_kv({
‘Txnid’: ‘foobar’
})

# interesting bit ;-)
time.sleep(0.1)

return ‘OK’

The above code snippet (full example gist) produces a trace in the Instana UI similar to the one below.

python trace

Best of Both Worlds

Instana’s automatic Python tracing takes care of all the mundane instrumentation leaving your developers more time to concentrate on coding core functionality, while also providing the flexibility to dig deeper if required.

Don’t slow down, let the robot do the work.