python-traceview: A Library for the TraceView API

by Dan Riti on June 18, 2014

traceview

Not too long ago, we introduced the TraceView Data API, which exposes high level metrics and performance data related to your TraceView account via a RESTful API. For the unfamiliar, this means you can access server latency timeseries, application error rates, and even browsers used by end users.

In an effort to make accessing the Data API even easier, I have a created python-traceview, an open source Python library for easy access to the TraceView Data API!

Installation

With support for Python 2.7, 3.2, 3.3, and 3.4, install python-traceview by simply:

$ pip install python-traceview 

If you don’t have Python installed, I recommend this guide for installation instructions and best practices.

Getting Started

Begin by importing the traceview module:

>>> import traceview

Now, let’s initialize a TraceView object using your TraceView access (API) key. You can find this key on the Organization Overview page in TraceView:

>>> tv = traceview.TraceView('API KEY HERE')

Now, we have a TraceView object called tv. We can get all the information we need from this object.

For example, let’s get all available applications setup within your TraceView account:

>>> tv.apps() 
[u'Default', u'pyramid_web_app']

Nice, right? We can also get a server side latency summary for the Default application:

 
>>> tv.server.latency_summary(app='Default', time_window='hour') 
{u'count': 2746.0, u'average': 213911.87181354698, u'latest': 35209.87654320987}

TraceView has traced 2746 requests in the last hour, with an average latency of 213ms. Not bad, now let’s examine what application layers are available in the Default application:

  
>>> tv.layers('Default') 
[u'PHP', u'cURL', u'file_get_contents', u'lighttpd', u'php_mysql', u'php_mysqli']

Finally, let’s look at a more advanced example. Using the powerful matplotlib library, let’s create a script for graphing the average latency of the PHP layer over the past week:

  
from datetime import datetime

import matplotlib.pyplot as plt
import traceview

API_KEY = 'API KEY HERE'

def main():
    tv = traceview.TraceView(API_KEY)
    results = tv.server.latency_by_layer('Default', time_window='week')

    # get the php layer
    php = next((x for x in results if x.get('layer') == 'PHP'), None)

    # convert timestamps to datetime objects
    dates = [datetime.utcfromtimestamp(i[0]) for i in php['timeseries']['items']]

    # calculate average latency (total_latency / volume) and convert to
    # milliseconds
    average_latency = [((i[2] / i[1]) / 1000) for i in php['timeseries']['items']]

    # create a matplotlib plot
    plt.figure(figsize=(12, 5), dpi=80)
    plt.stackplot(dates, average_latency, colors=['#1dacd6'])
    plt.title('PHP Average Latency (ms) - by Week')
    plt.savefig('latency.png')

if __name__ == '__main__':
    main()

python traceview api

Conclusion

For more information, check out the documentation, create an issue, or leave a comment on what other information you’d like to see available via the Data API!

TwitterFacebookLinkedInRedditEmail

Get TraceView data on your site

Sign up for a FREE traceview account, and get your API key in 5 minutes. create your free account