statsd.timer

class statsd.timer.Timer(name, connection=None, min_send_threshold=-1)[source]

Statsd Timer Object

Additional documentation is available at the parent class Client

Parameters:
  • name (str) – The name for this timer
  • connection (Connection) – The connection to use, will be automatically created if not given
  • min_send_threshold (int) – Timings smaller than this will not be sent so -1 can be used for all.
>>> timer = Timer('application_name').start()
>>>  # do something
>>> timer.stop('executed_action')
True
decorate(function_or_name)[source]

Decorate a function to time the execution

The method can be called with or without a name. If no name is given the function defaults to the name of the function.

Parameters:function_or_name – The name to post to or the function to wrap
>>> from statsd import Timer
>>> timer = Timer('application_name')
>>>
>>> @timer.decorate
... def some_function():
...     # resulting timer name: application_name.some_function
...     pass
>>>
>>> @timer.decorate('my_timer')
... def some_other_function():
...     # resulting timer name: application_name.my_timer
...     pass
intermediate(subname)[source]

Send the time that has passed since our last measurement

Parameters:subname (str) – The subname to report the data to (appended to the client name)
send(subname, delta)[source]

Send the data to statsd via self.connection

Parameters:
  • subname (str) – The subname to report the data to (appended to the client name)
  • delta (float) – The time delta (time.time() - time.time()) to report
start()[source]

Start the timer and store the start time, this can only be executed once per instance

It returns the timer instance so it can be chained when instantiating the timer instance like this: timer = Timer('application_name').start()

stop(subname='total')[source]

Stop the timer and send the total since start() was run

Parameters:subname (str) – The subname to report the data to (appended to the client name)
time(**kwds)[source]

Returns a context manager to time execution of a block of code.

Parameters:
>>> from statsd import Timer
>>> timer = Timer('application_name')
>>>
>>> with timer.time():
...     # resulting timer name: application_name
...     pass
>>>
>>>
>>> with timer.time('context_timer'):
...     # resulting timer name: application_name.context_timer
...     pass