GithubHelp home page GithubHelp logo

doytsujin / aioshumway Goto Github PK

View Code? Open in Web Editor NEW

This project forked from spotify/aioshumway

0.0 0.0 0.0 13 KB

An asyncio-supported micro metrics framework for FFWD

License: Apache License 2.0

Python 100.00%

aioshumway's Introduction

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

  • Python 3.6
  • Support for Linux & OS X

To Use

(env) $ pip install aioshumway

Counters

Create a default counter and send to FFWD:

import aioshumway

mr = aioshumway.MetricRelay(SERVICE_NAME)
mr.incr(METRIC_NAME)
# inside an async context
await mr.flush()

Initialize a counter with a value

import aioshumway

mr = aioshumway.MetricRelay(SERVICE_NAME)
counter = aioshumway.Counter(metric_name, SERVICE_NAME, value=10)
mr.set_counter(metric_name, counter)
mr.incr(metric_name)
# inside an async context
await mr.flush()

Different increment values

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

import aioshumway

mr = aioshumway.MetricRelay(SERVICE_NAME)
mr.incr(METRIC_NAME, 2)
# inside an async context
await mr.flush()

Custom Counter Attributes

Set custom attributes for metrics:

import aioshumway

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

mr.set_counter(metric_name, counter)
mr.incr(metric_name)
# inside an async context
await 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

import aioshumway

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

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

await mr.flush()

Custom Timer Attributes

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

import aioshumway

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

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

mr.set_timer('timing-this-thing', timer)
await 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:

import aioshumway

mr = aioshumway.MetricRelay('my-service')

# inside an async context

# some event happened
await mr.emit('a-successful-event', 1)

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

# an event with a multiple value happened
await mr.emit('a-successful-event', 5)

Flushing all metrics

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

import aioshumway

mr = aioshumway.MetricRelay('my-service')

# measure all the things
# time all the things

# inside an async context
if not 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
True
>>> 'not-a-counter' in mr
False

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:

import aioshumway

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

# do the thing

Developer Setup

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

Setup

$ git clone [email protected]:spotify/aioshumway.git && cd aioshumway
# make a virtualenv
(env) $ pip install -r dev-requirements.txt

Running tests

To run the entire test suite:

# outside of the virtualenv
# if tox is not yet installed
$ pip install tox
$ tox

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

# inside virtualenv

(env) $ 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.

aioshumway's People

Contributors

matt0bi avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.