spotify/aioshumway

Name: aioshumway

Owner: Spotify

Description: An asyncio-supported micro metrics framework for FFWD

Created: 2018-01-24 18:17:17.0

Updated: 2018-05-16 17:45:13.0

Pushed: 2018-05-16 17:59:29.0

Homepage: null

Size: 13

Language: Python

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

AIOShumway

Build Status

A wrapper around the shumway library that uses asyncio to make all IO operations non-blocking and compatible with the async/await syntax.

NOTE: This is a lightly touched up copy of shumway's README since the only difference between these libraries is that this one provides an awaitable interface.

Requirements
To Use
) $ pip install aioshumway
Counters

Create a default counter and send to FFWD:

rt aioshumway

 aioshumway.MetricRelay(SERVICE_NAME)
ncr(METRIC_NAME)
side an async context
t mr.flush()
Initialize a counter with a value
rt aioshumway

 aioshumway.MetricRelay(SERVICE_NAME)
ter = aioshumway.Counter(metric_name, SERVICE_NAME, value=10)
et_counter(metric_name, counter)
ncr(metric_name)
side an async context
t mr.flush()
Different increment values

Create a named counter and increment by a value different than 1:

rt aioshumway

 aioshumway.MetricRelay(SERVICE_NAME)
ncr(METRIC_NAME, 2)
side an async context
t mr.flush()
Custom Counter Attributes

Set custom attributes for metrics:

rt aioshumway

 aioshumway.MetricRelay(SERVICE_NAME)
ter = aioshumway.counter(metric_name, SERVICE_NAME,
                      {attr_1: value_1,
                       attr_2: value_2})

et_counter(metric_name, counter)
ncr(metric_name)
side an async context
t mr.flush()

NB: If you use duplicate names when calling set_counter it will overwrite the counter. You will likely want to use a unique metric name for each set of attributes you are setting.

Timers
rt aioshumway

 aioshumway.MetricRelay(SERVICE_NAME)
r = mr.timer('timing-this-thing')

side an async context
 timer:
...task you want to time

t mr.flush()
Custom Timer Attributes

Timers can also be created independently in order to set custom attributes:

rt aioshumway

 aioshumway.MetricRelay(SERVICE_NAME)
r = aioshumway.Timer('timing-this-thing', SERVICE_NAME,
                     {'attr_1': value_1, 'attr_2': value_2})

side an async context
 timer:
# ...task you want to time

et_timer('timing-this-thing', timer)
t mr.flush()
Sending Metrics

There are two ways to send metrics to the ffwd agent:

Emit one metric

You can emit a one-off, event-type metric immediately:

rt aioshumway

 aioshumway.MetricRelay('my-service')

side an async context

me event happened
t mr.emit('a-successful-event', 1)

me event happened with attributes
t mr.emit('a-successful-event', 1, {'attr_1': value_1, 'attr_2': value_2})

 event with a multiple value happened
t mr.emit('a-successful-event', 5)
Flushing all metrics

For batch-like metrics, you can flush metrics once you're ready:

rt aioshumway

 aioshumway.MetricRelay('my-service')

asure all the things
me all the things

side an async context
ot dry_run:
await mr.flush()
Existing Metrics

Check for existence of metrics in the MetricRelay with in:

import aioshumway
mr = aioshumway.MetricRelay('my-service')
counter = aioshumway.Counter('thing-to-count', 'my-service', value=5)
mr.set_counter('thing-to-count', counter)
'thing-to-count' in mr

'not-a-counter' in mr
e
Custom FFWD agents

By default, aioshumway will send metrics to a local ffwd agent at 127.0.0.1:19000.

If your ffwd agent is elsewhere, then pass that information through when initializing the MetricRelay:

rt aioshumway

 aioshumway.MetricRelay(SERVICE_NAME, ffwd_ip='10.99.0.1', ffwd_port=19001)

 the thing

Developer Setup

For development and running tests, your system must have all supported versions of Python installed. We suggest using pyenv.

Setup
t clone git@github.com:spotify/aioshumway.git && cd aioshumway
ke a virtualenv
) $ pip install -r dev-requirements.txt
Running tests

To run the entire test suite:

tside of the virtualenv
 tox is not yet installed
p install tox
x

To run an individual test, pass the test's name to pytest, through tox:

side virtualenv

) $ tox -- tests/test_aioshumway.py::TestSomething

Code of Conduct

This project adheres to the Open Code of Conduct. By participating, you are expected to honor this code.


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.