From 0eee59c9feaf90e80dbfb24ce8874b06e6084c1a Mon Sep 17 00:00:00 2001 From: Dan Tracy Date: Wed, 10 Sep 2014 09:40:24 -0400 Subject: [PATCH 1/5] Move the statsd prefix to the class level --- docs/history.rst | 5 ++++ setup.py | 7 ++++-- sprockets/mixins/statsd/__init__.py | 36 +++++++++++++++++++++++------ 3 files changed, 39 insertions(+), 9 deletions(-) diff --git a/docs/history.rst b/docs/history.rst index 04a294f..25978a0 100644 --- a/docs/history.rst +++ b/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 diff --git a/setup.py b/setup.py index c90a6a8..624ad2d 100644 --- a/setup.py +++ b/setup.py @@ -3,6 +3,8 @@ import setuptools +from sprockets.mixins.statsd import __version__ as version + def read_requirements_file(req_name): requirements = [] @@ -24,12 +26,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=version, 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', @@ -65,4 +68,4 @@ def read_requirements_file(req_name): setup_requires=setup_requires, tests_require=tests_require, test_suite='nose.collector', - zip_safe=False) \ No newline at end of file + zip_safe=False) diff --git a/sprockets/mixins/statsd/__init__.py b/sprockets/mixins/statsd/__init__.py index 9de0772..f5d87db 100644 --- a/sprockets/mixins/statsd/__init__.py +++ b/sprockets/mixins/statsd/__init__.py @@ -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: @@ -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() From 4f039465583fa4e1525025424955ffebaa18b43a Mon Sep 17 00:00:00 2001 From: Dan Tracy Date: Wed, 10 Sep 2014 09:41:01 -0400 Subject: [PATCH 2/5] Fix Flake8 warnings --- sprockets/__init__.py | 2 +- sprockets/mixins/__init__.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/sprockets/__init__.py b/sprockets/__init__.py index b0d6433..de40ea7 100644 --- a/sprockets/__init__.py +++ b/sprockets/__init__.py @@ -1 +1 @@ -__import__('pkg_resources').declare_namespace(__name__) \ No newline at end of file +__import__('pkg_resources').declare_namespace(__name__) diff --git a/sprockets/mixins/__init__.py b/sprockets/mixins/__init__.py index b0d6433..de40ea7 100644 --- a/sprockets/mixins/__init__.py +++ b/sprockets/mixins/__init__.py @@ -1 +1 @@ -__import__('pkg_resources').declare_namespace(__name__) \ No newline at end of file +__import__('pkg_resources').declare_namespace(__name__) From 48f52335946559d890f1ba26c79e1a0f55b843d4 Mon Sep 17 00:00:00 2001 From: Dan Tracy Date: Wed, 10 Sep 2014 09:57:25 -0400 Subject: [PATCH 3/5] setup.py: Remove version import --- setup.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/setup.py b/setup.py index 624ad2d..4c55bf1 100644 --- a/setup.py +++ b/setup.py @@ -3,8 +3,6 @@ import setuptools -from sprockets.mixins.statsd import __version__ as version - def read_requirements_file(req_name): requirements = [] @@ -32,7 +30,7 @@ def read_requirements_file(req_name): setuptools.setup( name='sprockets.mixins.statsd', - version=version, + 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', From 15565b7d6052d4996d2bffe3918c3c3e1b8afc51 Mon Sep 17 00:00:00 2001 From: Dan Tracy Date: Wed, 10 Sep 2014 10:15:54 -0400 Subject: [PATCH 4/5] README: Buff up the README --- README.rst | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/README.rst b/README.rst index 63e2768..cf62c74 100644 --- a/README.rst +++ b/README.rst @@ -17,7 +17,7 @@ and can be installed via ``pip`` or ``easy_install``: Documentation ------------- -https://sprocketsmixinsstatsd.readthedocs.org +https://sprocketsmixinsstatsd.readthedocs.org/ Requirements ------------ @@ -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 @@ -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 \ No newline at end of file + :target: https://sprocketsmixinsstatsd.readthedocs.org From 400b7c2f02a6b0a8b1300dbcc0f8c04b022a6a8d Mon Sep 17 00:00:00 2001 From: Dan Tracy Date: Wed, 10 Sep 2014 10:17:16 -0400 Subject: [PATCH 5/5] setup.cfg: Add wheel distribution support --- .travis.yml | 1 + setup.cfg | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index db20f55..c2fc320 100644 --- a/.travis.yml +++ b/.travis.yml @@ -17,6 +17,7 @@ after_success: deploy: provider: pypi user: sprockets + distributions: "sdist bdist_wheel" on: python: 2.7 tags: true diff --git a/setup.cfg b/setup.cfg index 7c310b2..c252939 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,3 +1,6 @@ +[bdist_wheel] +universal = 1 + [build_sphinx] all-files = 1 source-dir = docs @@ -9,4 +12,4 @@ cover-package = sprockets.mixins.statsd verbose = 1 [flake8] -exclude = build,dist,docs,env \ No newline at end of file +exclude = build,dist,docs,env