This library defines mix-ins that record application metrics. Each mix-in implements the same interface:
self.application.settingsthat contains this particular mix-in’s configuration data.
- duration (float) – number of seconds to record
- path – timing path to record
self.record_timing(self.request.request_time(), 'request', 'lookup')
- path – counter path to increment
- amount (int) – value to increase the counter by
self.increase_counter('db', 'query', 'foo')
Parameters: path – timing path to record
This method returns a context manager that records a timing metric to the specified path.
with self.execution_timer('db', 'query', 'foo'): rows = yield self.session.query('SELECT * FROM foo')
Mix this class in to record metrics to a Statsd server.
Record the time it takes to perform an arbitrary code block.
Parameters: path – elements of the metric path to record
This method returns a context manager that records the amount of time spent inside of the context and submits a timing metric to the specified path using (
Increase a counter.
This method increases a counter within the application’s namespace. Each element of path is converted to a string and normalized before joining the elements by periods. The normalization process is little more than replacing periods with dashes.
- path – elements of the metric path to incr
- amount (int) – amount to increase the counter by. If omitted, the counter is increased by one.
Records the time taken to process the request.
This method records the amount of time taken to process the request (as reported by
request_time()) under the path defined by the class’s module, it’s name, the request method, and the status code. The
record_timing()method is used to send the metric, so the configured namespace is used as well.
Record a timing.
This method records a timing to the application’s namespace followed by a calculated path. Each element of path is converted to a string and normalized before joining the elements by periods. The normalization process is little more than replacing periods with dashes.
- duration (float) – timing to record in seconds
- path – elements of the metric path to record
StatsDCollector(host, port, protocol='udp', namespace='sprockets', prepend_metric_type=True)¶
Collects and submits stats to StatsD.
Gracefully close the socket.
Before you can use the mixin, you have to install the client by calling
install function on your application instance.
Call this to install StatsD for the Tornado application.
- host The StatsD host. If host is not specified, the
STATSD_HOSTenvironment variable, or default 127.0.0.1, will be pass into the
- port The StatsD port. If port is not specified, the
STATSD_PORTenvironment variable, or default 8125, will be pass into the
- namespace The StatsD bucket to write metrics into.
So who actually tests that their metrics are emitted as they expect?
Usually the answer is no one. Why is that? The
contains some helper that make testing a little easier.
Implements something resembling a statsd server.
Create an instance of this class in your asynchronous test case attached to the IOLoop and configure your application to send metrics to it. The received datagrams are available in the
datagramsattribute for validation in your tests.
The socket address that the server is listening on. This is a tuple returned from
A list of datagrams that have been received by the server.
Yields captured datagrams that start with prefix.
yields (path, value, metric_type) tuples for each captured metric that matches
AssertionError – if no metrics match.
Override to handle a new .IOStream from an incoming connection.
This method may be a coroutine; if so any exceptions it raises asynchronously will be logged. Accepting of incoming connections will not be blocked by this coroutine.
If this TCPServer is configured for SSL,
handle_streammay be called before the SSL handshake has completed. Use .SSLIOStream.wait_for_handshake if you need to verify the client’s certificate or use NPN/ALPN.
Changed in version 4.2: Added the option for this method to be a coroutine.