def on_finish(self):
        """Invoked once the request has been finished. Increments a counter
        created in the format:

        .. code::

            <STATSD_PREFIX>.counters.package[.module].Class.METHOD.STATUS
            sprockets.counters.tornado.web.RequestHandler.GET.200

        Adds a value to a timer in the following format:

        .. code::

            <STATSD_PREFIX>.timers.package[.module].Class.METHOD.STATUS
            sprockets.timers.tornado.web.RequestHandler.GET.200

        """
        if hasattr(self, 'request') and self.request:
            statsd.add_timing(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,
                        'counters',
                        self.__module__,
                        self.__class__.__name__,
                        self.request.method,
                        str(self._status_code))
        super(RequestMetricsMixin, self).on_finish()
    def on_finish(self):
        """Invoked once the request has been finished. Increments a counter
        created in the format:

        .. code::

            <PREFIX>.counters.<host>.package[.module].Class.METHOD.STATUS
            sprockets.counters.localhost.tornado.web.RequestHandler.GET.200

        Adds a value to a timer in the following format:

        .. code::

            <PREFIX>.timers.<host>.package[.module].Class.METHOD.STATUS
            sprockets.timers.localhost.tornado.web.RequestHandler.GET.200

        """
        if self.statsd_prefix != statsd.STATSD_PREFIX:
            statsd.set_prefix(self.statsd_prefix)

        if hasattr(self, 'request') and self.request:
            if self.statsd_use_hostname:
                timer_prefix = 'timers.{0}'.format(socket.gethostname())
                counter_prefix = 'counters.{0}'.format(socket.gethostname())
            else:
                timer_prefix = 'timers'
                counter_prefix = 'counters'

            statsd.add_timing(timer_prefix,
                              self.__module__,
                              str(self.__class__.__name__),
                              self.request.method,
                              str(self._status_code),
                              value=self.request.request_time() * 1000)

            statsd.incr(counter_prefix,
                        self.__module__,
                        self.__class__.__name__,
                        self.request.method,
                        str(self._status_code))

        super(RequestMetricsMixin, self).on_finish()
 def test_invokes_statsd_send_default_value(self):
     with mock.patch('sprockets.clients.statsd._send') as send:
         statsd.incr('foo', 'bar', 'baz', 'qux')
         send.assert_called_once_with('foo.bar.baz.qux', 1, 'c')
 def test_single_delimited_key_invokes_send(self):
     with mock.patch('sprockets.clients.statsd._send') as send:
         statsd.incr('foo.bar.baz', value=2)
         send.assert_called_once_with('foo.bar.baz', 2, 'c')
 def test_invokes_statsd_send_default_value(self):
     with mock.patch('sprockets.clients.statsd._send') as send:
         statsd.incr('foo', 'bar', 'baz', 'qux')
         send.assert_called_once_with('foo.bar.baz.qux', 1, 'c')
 def test_single_delimited_key_invokes_send(self):
     with mock.patch('sprockets.clients.statsd._send') as send:
         statsd.incr('foo.bar.baz', value=2)
         send.assert_called_once_with('foo.bar.baz', 2, 'c')