Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add writeback option to metrics #2

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@
CHANGES
=======

2.3 (2015-10-22)

- Add MANIFEST file

2.2 (2015-10-22)
----------------

- Update CHANGES

2.1 (2015-10-22)
----------------

- Detect if decorated method is class method or object method and behave
accordingly.

2.0 (2013-12-10)
----------------

Expand Down
3 changes: 3 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
include *.txt
include *.cfg
include *.py
14 changes: 12 additions & 2 deletions perfmetrics/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import functools
import os
import random
import six


try: # pragma no cover
Expand Down Expand Up @@ -98,14 +99,15 @@ def somefunc():

def __init__(self, stat=None, rate=1, method=False,
count=True, timing=True, timing_format='%s.t',
random=random.random): # testing hook
random=random.random, wb_list=None): # testing hook
self.stat = stat
self.rate = rate
self.method = method
self.count = count
self.timing = timing
self.timing_format = timing_format
self.random = random
self.wb_list = wb_list

def __call__(self, f):
"""Decorate a function or method so it can send statistics to statsd.
Expand All @@ -120,6 +122,7 @@ def __call__(self, f):
timing = self.timing
timing_format = self.timing_format
random = self.random
wb_list = self.wb_list

def call_with_metric(*args, **kw):
if rate < 1 and random() >= rate:
Expand All @@ -135,7 +138,10 @@ def call_with_metric(*args, **kw):
if instance_stat:
stat = instance_stat
elif method:
cls = args[0].__class__
if isinstance(args[0], (type, six.class_types)):
cls = args[0]
else:
cls = args[0].__class__
stat = '%s.%s.%s' % (cls.__module__, cls.__name__, func_name)
else:
stat = func_full_name
Expand All @@ -151,6 +157,8 @@ def call_with_metric(*args, **kw):
return f(*args, **kw)
finally:
elapsed_ms = int((time() - start) * 1000.0)
if isinstance(wb_list, list):
wb_list.append(elapsed_ms)
client.timing(timing_format % stat, elapsed_ms,
rate, buf=buf, rate_applied=True)
if buf:
Expand Down Expand Up @@ -184,6 +192,8 @@ def __exit__(self, _typ, _value, _tb):
client.incr(stat, rate=rate, buf=buf, rate_applied=True)
if self.timing:
elapsed = int((time() - self.start) * 1000.0)
if isinstance(self.wb_list, list):
self.wb_list.append(elapsed)
client.timing(self.timing_format % stat, elapsed,
rate=rate, buf=buf, rate_applied=True)
if buf:
Expand Down
6 changes: 5 additions & 1 deletion perfmetrics/tests/test_perfmetrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,17 @@ def test_ctor_with_defaults(self):
self.assertEqual(obj.rate, 1)
self.assertTrue(obj.count)
self.assertTrue(obj.timing)
self.assertIsNone(obj.wb_list)

def test_ctor_with_options(self):
obj = self._class('spam.n.eggs', 0.1, count=False, timing=False)
m_wb = list()
obj = self._class('spam.n.eggs', 0.1, count=False, timing=False,
wb_list=m_wb)
self.assertEqual(obj.stat, 'spam.n.eggs')
self.assertEqual(obj.rate, 0.1)
self.assertFalse(obj.count)
self.assertFalse(obj.timing)
self.assertEqual(obj.wb_list, m_wb)

def test_decorate_function(self):
args = []
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import os
import sys

requires = ['setuptools']
requires = ['setuptools', 'six']

if sys.version_info[:2] < (2, 7):
requires.append('unittest2')
Expand All @@ -13,7 +13,7 @@
CHANGES = open(os.path.join(here, 'CHANGES.txt')).read()

setup(name='perfmetrics',
version='2.0',
version='2.3',
author='Shane Hathaway',
author_email='shane@hathawaymix.org',
description='Send performance metrics about Python code to Statsd',
Expand Down