Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Measure all the things

Eloy Coto
April 16, 2015

Measure all the things

Eloy Coto

April 16, 2015
Tweet

More Decks by Eloy Coto

Other Decks in Programming

Transcript

  1. with statsd.timer('foo'): for i in xrange(0, 100000): i ** 2

    statsd.gauge('foo', 70) # Set the 'foo' gauge to 70. statsd.gauge('foo', 1, delta=True) # Set 'foo' to 71. statsd.gauge('foo', -3, delta=True) # Set 'foo' to 68. statsd.incr('some.event') statsd.incr('some.other.event', 10) statsd.incr('some.third.event', rate=0.1) statsd.set('users', userid)
  2. from functools import wraps from statsd import StatsClient from time

    import sleep statsd = StatsClient('127.0.0.1', 8125) class Metric(object): def __init__(self, metric): self.metric = metric def __call__(self, f): @wraps(f) def decorated(*args, **kwargs): with statsd.timer(self.metric): return f(*args, **kwargs) return decorated @Metric('test') def test(): sleep(5)
  3. from flask import Flask from time import sleep app =

    Flask(__name__) @app.route('/login/<username>') @Metric('view.login') def login(username): if not username: statsd.incr('login.failed') return 'Login failed sleep(1) statsd.incr('login.success') return 'Login success' if __name__ == '__main__': app.run()
  4. from django.core.urlresolvers import resolve from statsd.defaults.env import statsd class Statsd(object):

    def process_request(self, request): self.current_view = resolve(request.path).url_name statsd.incr('view.stats.{0}'.format(self.current_view)) self.timer = statsd.timer('view.{0}'.format(self.current_view)) self.timer.start() def process_response(self, request, response): self.timer.stop() return response def process_exception(self, request, exception): statsd.incr('view.exceptions.{0}'.format(self.current_view))
  5. [server] …. [[StatsdHandler]] host = statsd.example.com port = 8125 [collectors]

    [[default]] [[CPUCollector]] enabled = True [[DiskSpaceCollector]] enabled = True [[DiskUsageCollector]] enabled = True [[LoadAverageCollector]] enabled = True …
  6. $ curl -X POST \ -d '[{"name":"foo","columns":["val"],"points":[[23]]}]' \ 'http://localhost:8086/db/demo/series?u=root&p=root' $

    curl -G 'http://localhost:8086/db/demo/series?u=root&p=root' \ --data-urlencode "q=select * from foo limit 1" #Result [{ "name": "foo", "columns": [ "time", "sequence_number", "val" ], "points": [ [ 1428909579501, 25996840001, 23 ] ] }]
  7. select value from "view.choicelist.timer.mean" where time > now() - 1h

    limit 1000; select mean(value) from "view.choicelist.timer.mean" where time > now() - 2h group by time(10m); select max(value) from "view.choicelist.timer.mean" where time > now() - 2h group by time(10m);