Skip to content
This repository has been archived by the owner on Jul 22, 2021. It is now read-only.

Commit

Permalink
Merge pull request #6 from sprockets/move-statsd-prefix-to-class-level
Browse files Browse the repository at this point in the history
Move statsd prefix to class level
  • Loading branch information
gmr committed Sep 19, 2014
2 parents 0073f1c + 400b7c2 commit 8fa5b59
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 14 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Expand Up @@ -17,6 +17,7 @@ after_success:
deploy:
provider: pypi
user: sprockets
distributions: "sdist bdist_wheel"
on:
python: 2.7
tags: true
Expand Down
23 changes: 21 additions & 2 deletions README.rst
Expand Up @@ -17,7 +17,7 @@ and can be installed via ``pip`` or ``easy_install``:
Documentation
-------------
https://sprocketsmixinsstatsd.readthedocs.org
https://sprocketsmixinsstatsd.readthedocs.org/

Requirements
------------
Expand All @@ -36,14 +36,33 @@ and add a request duration timing value to statsd when the request finishes.
class MyRequestHandler(statsd.RequestMetricsMixin,
web.RequestHandler):
def prepare(self):
self.statsd_prefix = 'some.overriden.value'
super(MyRequestHandler, self).prepare()
def get(self, *args, **kwargs):
self.finish({'hello': 'world'})
def on_finish(self):
super(MyRequestHandler, self).on_finish()
self.do_cleanup_things()
When the request has finished, the following keys would be used:

- Counter: ``sprockets.counter.example.RequestHandler.GET.200``
- Timing: ``sprockets.timers.example.RequestHandler.GET.200``

Mixin Behavior
--------------
Whenever you mix in a class in Python always ensure that the mixins, which
should inherit from ``object``, are the first ones in the inheritance list.
The concrete class, in this case `web.RequestHandler` should be the final
class inherited.

Should your Request Handler extend the ``finish`` or the ``prepare`` methods
ensure that your call ``super`` otherwise you may run into strange behavior.

Version History
---------------
Available at https://sprocketsmixinsstatsd.readthedocs.org/en/latest/history.html
Expand All @@ -61,4 +80,4 @@ Available at https://sprocketsmixinsstatsd.readthedocs.org/en/latest/history.htm
:target: https://pypi.python.org/pypi/sprockets.mixins.statsd

.. |License| image:: https://pypip.in/license/sprockets.mixins.statsd/badge.svg?
:target: https://sprocketsmixinsstatsd.readthedocs.org
:target: https://sprocketsmixinsstatsd.readthedocs.org
5 changes: 5 additions & 0 deletions docs/history.rst
@@ -1,4 +1,9 @@
Version History
---------------
- 1.0.4 [2014-08-29]
- Status code bug fixes
- Version string interpolation fixes
- Move the global STATSD_PREFIX to a class attribute to allow modifications

- 1.0.0 [2014-08-29]
- Initial version
5 changes: 4 additions & 1 deletion setup.cfg
@@ -1,3 +1,6 @@
[bdist_wheel]
universal = 1

[build_sphinx]
all-files = 1
source-dir = docs
Expand All @@ -9,4 +12,4 @@ cover-package = sprockets.mixins.statsd
verbose = 1

[flake8]
exclude = build,dist,docs,env
exclude = build,dist,docs,env
5 changes: 3 additions & 2 deletions setup.py
Expand Up @@ -24,12 +24,13 @@ def read_requirements_file(req_name):

if sys.version_info < (2, 7):
tests_require.append('unittest2')

if sys.version_info < (3, 0):
tests_require.append('mock')

setuptools.setup(
name='sprockets.mixins.statsd',
version='1.0.3',
version='1.0.4',
description='Handler mixins for automated metric reporting',
long_description=codecs.open('README.rst', encoding='utf-8').read(),
url='https://github.com/sprockets/sprockets.mixins.statsd.git',
Expand Down Expand Up @@ -65,4 +66,4 @@ def read_requirements_file(req_name):
setup_requires=setup_requires,
tests_require=tests_require,
test_suite='nose.collector',
zip_safe=False)
zip_safe=False)
2 changes: 1 addition & 1 deletion sprockets/__init__.py
@@ -1 +1 @@
__import__('pkg_resources').declare_namespace(__name__)
__import__('pkg_resources').declare_namespace(__name__)
2 changes: 1 addition & 1 deletion sprockets/mixins/__init__.py
@@ -1 +1 @@
__import__('pkg_resources').declare_namespace(__name__)
__import__('pkg_resources').declare_namespace(__name__)
36 changes: 29 additions & 7 deletions sprockets/mixins/statsd/__init__.py
Expand Up @@ -39,17 +39,37 @@ def get(self, *args, **kwargs):

from sprockets.clients import statsd

version_info = (1, 0, 3)
version_info = (1, 0, 4)
__version__ = '.'.join(str(v) for v in version_info)

STATSD_PREFIX = os.getenv('STATSD_PREFIX', 'sprockets')


class RequestMetricsMixin(object):
"""The ``RequestMetricsMixin`` automatically sends statsd metrics upon the
completion of each request.
"""Automatically sends statsd metrics upon the completion of each request.
As with all mixins, ensure that you inherit from the mixin classes
*before* you inherit from a concrete class. In addition to this, alway
remember to ``super`` the ``on_finish`` and ``prepare`` methods should
you decide to extend them.
Example Usage
-------------
class MyRequestHandler(
sprockets.mixins.statsd.RequestMetricsMixin,
tornado.web.RequestHandler):
def prepare(self):
super(RequestMetricsMixin, self).prepare()
do_prepare_stuff()
@gen.coroutine
def post(self):
self.write(yield self.foo())
"""

statsd_prefix = os.getenv('STATSD_PREFIX', 'sprockets')

def on_finish(self):
"""Invoked once the request has been finished. Increments a counter
created in the format:
Expand All @@ -68,17 +88,19 @@ def on_finish(self):
"""
if hasattr(self, 'request') and self.request:
statsd.add_timing(STATSD_PREFIX,
statsd.add_timing(self.statsd_prefix,
'timers',
self.__module__,
self.__class__.__name__,
self.request.method,
str(self._status_code),
value=self.request.request_time() * 1000)
statsd.incr(STATSD_PREFIX,

statsd.incr(self.statsd_prefix,
'counters',
self.__module__,
self.__class__.__name__,
self.request.method,
str(self._status_code))

super(RequestMetricsMixin, self).on_finish()

0 comments on commit 8fa5b59

Please sign in to comment.