Instrument Your Python App Automatically With The Honeycomb Beeline for Python

We’ve been on a roll this year with Beelines, our integrations for quick, easy, and automagic instrumentation of your apps. You may have already seen our Node.jsRuby, and Go beelines – today, we’re excited to announce the release of the Honeycomb Beeline for Python!

The Python Beeline automatically instruments HTTP requests and DB queries and sends the events to Honeycomb, so you can start asking questions like “Which endpoint has the worst P99 latency?” and “Which DB calls take up the most time?”  

With support for Honeycomb Tracing included out of the box, you can also figure out where your app is spending the most time, visualize execution, and more!

bee wiggling animation

Requirements

  • Python 2.7 or 3.x
  • Flask, Django (>1.10), Bottle (support for more frameworks is on the way)
  • Your Honeycomb API key, found on your Honeycomb Team Settings page.

Setup

First, install the package:

pip install honeycomb-beeline

Flask

If you’re running Flask, your HTTP requests will be instrumented, as will DB queries if you are using Flask’s SQLAlchemy extension.

Initialize the beeline module, then pass your app to HoneyMiddleware:

import beeline
from beeline.middleware.flask import HoneyMiddleware

beeline.init(writekey="yourApiKey", dataset="my-flask-app", service_name="my-app-name")
app = Flask(__name__)
# db_events defaults to True, set to False if not using our db middleware with Flask-SQLAlchemy
HoneyMiddleware(app, db_events=True)

DJANGO

  • If you’re running a Django version greater than 1.10, your HTTP requests will be automatically instrumented.
  • If you’re running a Django version greater than 2.0, you’ll also get database instrumentation out of the box.

First, add the Beeline as middleware to your project’s settings.py. If you’d like HTTP instrumentation only, choose HoneyMiddlewareHttp. If you’d like db instrumentation as well, choose HoneyMiddleware.

MIDDLEWARE = [
 ...
 'beeline.middleware.django.HoneyMiddleware',
 ...
]

Then, initialize the beeline module in your app’s apps.py:

from django.apps import AppConfig
import beeline


class MyAppConfig(AppConfig):

    def ready(self):
        beeline.init(
            writekey='yourApiKey',
            dataset='my-django-app',
            service_name='my-app-name'
        )

Don’t forget to set your app’s default config in your app’s __init__.py:

default_app_config = 'myapp.apps.MyAppConfig'

Bottle

If you’re running Bottle, your HTTP requests will be instrumented via WSGI middleware.

Initialize the beeline module, and pass your app to HoneyWSGIMiddleware:

import beeline
from beeline.middleware.bottle import HoneyWSGIMiddleware

beeline.init(
  writekey='<MY HONEYCOMB API KEY>',
  dataset='my-app',
  service_name='my-app'
)

app = bottle.app()
myapp = HoneyWSGIMiddleware(app)
bottle.run(app=myapp)

Tracing

Events instrumented with the Python Beeline will automatically be traced, allowing you to gain insight into how your program executes.

cropped screenshot of a tracing waterfall diagram
Want to measure some other part of your program? Use the Beeline tracer to easily include parts of your code in the trace.

with beeline.tracer("external api call"):
    result = call_external_api()
    
# do something with result

cropped screenshot of tracing waterfall diagram

Adding more context

You know your app better than we do! You can easily add more data to your events at any point in your program. Want to log the user ID associated with a request? It’s simple:

beeline.add_field('user_id', user_id)

Enjoy!

Check out the Beeline documentation for more information on what else is supported.

Want automagical instrumentation but haven’t tried Honeycomb before? Sign up for free!