sprockets.mixins.metrics

Version Status Coverage License

Adjust counter and timer metrics in StatsD using the same API.

The mix-in is configured through the tornado.web.Application settings property using a key defined by the specific mix-in.

Deprecation Notice

This library is deprecated. It will not receive features or bug fixes. Please switch to either sprockets-influxdb or sprockets-statsd.

Statsd Mixin

The following snippet configures the StatsD mix-in from common environment variables. This simple handler will emit a timer metric that identifies each call to the get method as well as a separate metric for the database query.

import os

from sprockets.mixins import mediatype
from sprockets.mixins.metrics import statsd
from tornado import web
import queries

def make_application():
    application = web.Application([
        web.url(r'/', MyHandler),
    ], **settings)

    statsd.install({'namespace': 'my-application',
                    'host': os.environ.get('STATSD_HOST', '127.0.0.1'),
                    'port': os.environ.get('STATSD_PORT', '8125')})
    return application

class MyHandler(statsd.StatsdMixin,
                mediatype.ContentMixin,
                web.RequestHandler):

    def initialize(self):
        super(MyHandler, self).initialize()
        self.db = queries.TornadoSession(os.environ['MY_PGSQL_DSN'])

    async def get(self, obj_id):
        with self.execution_timer('dbquery', 'get'):
           result = await self.db.query('SELECT * FROM foo WHERE id=%s',
                                        obj_id)
        self.send_response(result)

Settings

namespace:The namespace for the measurements
host:The Statsd host
port:The Statsd port
prepend_metric_type:
 Optional flag to prepend bucket path with the StatsD metric type

Development Quickstart

$ python3.4 -mvenv env
$ . ./env/bin/activate
(env)$ env/bin/pip install -r requires/development.txt
(env)$ nosetests
test_metrics_with_buffer_not_flush (tests.InfluxDbTests) ... ok
test_that_cached_db_connection_is_used (tests.InfluxDbTests) ... ok
test_that_counter_is_tracked (tests.InfluxDbTests) ... ok
test_that_execution_timer_is_tracked (tests.InfluxDbTests) ... ok
test_that_http_method_call_details_are_recorded (tests.InfluxDbTests) ... ok
test_that_metric_tag_is_tracked (tests.InfluxDbTests) ... ok
test_that_add_metric_tag_is_ignored (tests.StatsdMethodTimingTests) ... ok
test_that_cached_socket_is_used (tests.StatsdMethodTimingTests) ... ok
test_that_counter_accepts_increment_value (tests.StatsdMethodTimingTests) ... ok
test_that_counter_increment_defaults_to_one (tests.StatsdMethodTimingTests) ... ok
test_that_default_prefix_is_stored (tests.StatsdMethodTimingTests) ... ok
test_that_execution_timer_records_time_spent (tests.StatsdMethodTimingTests) ... ok
test_that_http_method_call_is_recorded (tests.StatsdMethodTimingTests) ... ok

----------------------------------------------------------------------
Ran 13 tests in 3.572s

OK
(env)$ ./setup.py build_sphinx -q
running build_sphinx
(env)$ open build/sphinx/html/index.html

License

Copyright (c) 2016-2019 AWeber Communications All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  • Neither the name of Sprockets nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.