1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
|
.. _reference-chapter:
=============
API Reference
=============
The ``StatsClient`` provides accessors for all the types of data the
statsd_ server supports.
.. note::
Each public stats API method supports a ``rate`` parameter, but
statsd doesn't always use it the same way. See the
:ref:`types-chapter` for more information.
.. _StatsClient:
``StatsClient``
===============
::
StatsClient(host='localhost', port=8125, prefix=None, maxudpsize=512)
Create a new ``StatsClient`` instance with the appropriate connection
and prefix information.
* ``host``: the hostname or IPv4 address of the statsd_ server.
* ``port``: the port of the statsd server.
* ``prefix``: a prefix to distinguish and group stats from an
application or environment.
* ``maxudpsize``: the largest safe UDP packet to save. 512 is generally
considered safe for the public internet, but private networks may
support larger packet sizes.
.. _incr:
``incr``
--------
::
StatsClient().incr(stat, count=1, rate=1)
Increment a :ref:`counter <counter-type>`.
* ``stat``: the name of the counter to increment.
* ``count``: the amount to increment by. Typically an integer. May be
negative, but see also :ref:`decr`.
* ``rate``: a sample rate, a float between 0 and 1. Will only send data
this percentage of the time. The statsd server will take the sample
rate into account for counters.
.. _decr:
``decr``
--------
::
StatsClient().decr(stat, count=1, rate=1)
Decrement a :ref:`counter <counter-type>`.
* ``stat``: the name of the counter to decrement.
* ``count``: the amount to decrement by. Typically an integer. May be
negative but that will have the impact of incrementing the counter.
See also :ref:`incr`.
* ``rate``: a sample rate, a float between 0 and 1. Will only send data
this percentage of the time. The statsd server will take the sample
rate into account for counters.
.. _gauge:
``gauge``
---------
::
StatsClient().gauge(stat, value, rate=1, delta=False)
Set a :ref:`gauge <gauge-type>` value.
* ``stat``: the name of the gauge to set.
* ``value``: the current value of the gauge.
* ``rate``: a sample rate, a float between 0 and 1. Will only send data
this percentage of the time. The statsd server does *not* take the
sample rate into account for gauges. Use with care.
* ``delta``: whether or not to consider this a delta value or an
absolute value. See the :ref:`gauge <gauge-type>` type for more
detail.
.. note::
Gauges were added to the statsd server in commit 0ed78be_. If you try
to use this method with an older version of the server, the data will
not be recorded.
.. _set:
``set``
---------
::
StatsClient().set(stat, value, rate=1)
Increment a :ref:`set <set-type>` value.
* ``stat``: the name of the set to update.
* ``value``: the unique value to count.
* ``rate``: a sample rate, a float between 0 and 1. Will only send data
this percentage of the time. The statsd server does *not* take the
sample rate into account for sets. Use with care.
.. note::
Sets were added to the statsd server in commit 1c10cfc0ac_. If you
try to use this method with an older version of the server, the
data will not be recorded.
.. _timing:
``timing``
----------
::
StatsClient().timing(stat, delta, rate=1)
Record :ref:`timer <timer-type>` information.
* ``stat``: the name of the timer to use.
* ``delta``: the number of milliseconds whatever action took. Should
always be milliseconds.
* ``rate``: a sample rate, a float between 0 and 1. Will only send data
this percentage of the time. The statsd server does *not* take the
sample rate into account for timers.
.. _timer:
``timer``
=========
::
with StatsClient().timer(stat, rate=1):
pass
::
@StatsClient().timer(stat, rate=1)
def foo():
pass
::
timer = StatsClient().timer('foo', rate=1)
Automatically record timing information for a managed block or function
call. See also the :ref:`chapter on timing <timing-chapter>`.
* ``stat``: the name of the timer to use.
* ``rate``: a sample rate, a float between 0 and 1. Will only send data
this percentage of the time. The statsd server does *not* take the
sample rate into account for timers.
.. warning::
Decorators are not thread-safe and may cause errors when decorated
functions are called concurrently. Use context managers or raw timers
instead.
.. _timer-start:
``start``
---------
::
StatsClient().timer('foo').start()
Causes a timer object to start counting. Called automatically when the
object is used as a decorator or context manager. Returns the timer
object for simplicity.
.. _timer-stop:
``stop``
--------
::
timer = StatsClient().timer('foo').start()
timer.stop()
Causes the timer object to stop timing and send the results to statsd_.
Can be called with ``send=False`` to prevent immediate sending
immediately, and use ``send()``. Called automatically when the object is
used as a decorator or context manager. Returns the timer object.
If ``stop()`` is called before ``start()``, a ``RuntimeError`` is
raised.
.. _timer-send:
``send``
--------
::
timer = StatsClient().timer('foo').start()
timer.stop(send=False)
timer.send()
Causes the timer to send any unsent data. If the data has already been
sent, or has not yet been recorded, a ``RuntimeError`` is raised.
.. note::
See the note abbout :ref:`timer objects and pipelines <timer-direct-note>`.
.. _pipeline:
``pipeline``
============
::
StatsClient().pipeline()
Returns a :ref:`Pipeline <pipeline-chapter>` object for collecting
several stats. Can also be used as a context manager::
with StatsClient().pipeline() as pipe:
pipe.incr('foo')
.. _pipeline-send:
``send``
--------
::
pipe = StatsClient().pipeline()
pipe.incr('foo')
pipe.send()
Causes a :ref:`Pipeline <pipeline-chapter>` object to send all batched
stats.
.. note::
This method is not implemented on the base StatsClient class.
.. _TCPStatsClient:
``TCPStatsClient``
==================
::
TCPStatsClient(host='localhost', port=8125, prefix=None, timeout=None)
Create a new ``TCPStatsClient`` instance with the appropriate connection
and prefix information.
* ``host``: the hostname or IPv4 address of the statsd_ server.
* ``port``: the port of the statsd server.
* ``prefix``: a prefix to distinguish and group stats from an
application or environment.
* ``timeout``: socket timeout for any actions on the connection socket.
``TCPStatsClient`` implements all methods of ``StatsClient``, including
``pipeline()``, with the difference that it is not thread safe and it
can raise exceptions on connection errors. Unlike ``StatsClient`` it
uses a TCP connection to communicate with StatsD.
In addition to the stats methods, ``TCPStatsClient`` supports the
following TCP-specific methods.
.. _tcp_close:
``close``
---------
::
from statsd import TCPStatsClient
statsd = TCPStatsClient()
statsd.incr('some.event')
statsd.close()
Closes a connection that's currently open and deletes it's socket. If
this is called on a ``TCPStatsClient`` which currently has no open
connection it is a non-action.
.. _tcp_connect:
``connect``
-----------
::
from statsd import TCPStatsClient
statsd = TCPStatsClient()
statsd.incr('some.event') # calls connect() internally
statsd.close()
statsd.connect() # creates new connection
Creates a connection to StatsD. If there are errors like connection
timed out or connection refused, the according exceptions will be
raised. It is usually not necessary to call this method because sending
data to StatsD will call ``connect`` implicitely if the current instance
of ``TCPStatsClient`` does not already hold an open connection.
.. _tcp_reconnect:
``reconnect``
-------------
::
from statsd import TCPStatsClient
statsd = TCPStatsClient()
statsd.incr('some.event')
statsd.reconnect() # closes open connection and creates new one
Closes a currently existing connection and replaces it with a new one.
If no connection exists already it will simply create a new one.
Internally this does nothing else than calling ``close()`` and
``connect()``.
.. _statsd: https://github.com/etsy/statsd
.. _0ed78be: https://github.com/etsy/statsd/commit/0ed78be7
.. _1c10cfc0ac: https://github.com/etsy/statsd/commit/1c10cfc0ac
|