|
1 # copyright 2015 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
|
2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
|
3 # |
|
4 # This file is part of CubicWeb. |
|
5 # |
|
6 # CubicWeb is free software: you can redistribute it and/or modify it under the |
|
7 # terms of the GNU Lesser General Public License as published by the Free |
|
8 # Software Foundation, either version 2.1 of the License, or (at your option) |
|
9 # any later version. |
|
10 # |
|
11 # CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
|
12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|
13 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
|
14 # details. |
|
15 # |
|
16 # You should have received a copy of the GNU Lesser General Public License along |
|
17 # with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
|
18 |
|
19 """Simple statsd_ logger for cubicweb. |
|
20 |
|
21 This module is meant to be configured by setting a couple of global variables: |
|
22 |
|
23 - ``bucket`` global variable will be used as statsd bucket in every |
|
24 statsd_ UDP sent packet. |
|
25 |
|
26 `- `address`` is a pair (IP, port) specifying the address of the |
|
27 statsd_ server |
|
28 |
|
29 |
|
30 There are 3 kinds of statds_ message:: |
|
31 |
|
32 - ``statsd_c(context, n)`` is a simple function to send statsd_ |
|
33 counter-type of messages like:: |
|
34 |
|
35 <bucket>.<context>:<n>|c\n |
|
36 |
|
37 - ``statsd_g(context, value)`` to send statsd_ gauge-type of messages |
|
38 like:: |
|
39 |
|
40 <bucket>.<context>:<n>|g\n |
|
41 |
|
42 - ``statsd_t(context, ms)`` to send statsd_ time-type of messages |
|
43 like:: |
|
44 |
|
45 <bucket>.<context>:<ms>|ms\n |
|
46 |
|
47 There is also a decorator (``statsd_timeit``) that may be used to |
|
48 measure and send to the statsd_ server the time passed in a function |
|
49 or a method and the number of calls. It will send a message like:: |
|
50 |
|
51 <bucket>.<funcname>:<ms>|ms\n<bucket>.<funcname>:1|c\n |
|
52 |
|
53 |
|
54 .. _statsd: https://github.com/etsy/statsd |
|
55 |
|
56 """ |
|
57 |
|
58 __docformat__ = "restructuredtext en" |
|
59 |
|
60 import time |
|
61 import socket |
|
62 |
|
63 _bucket = 'cubicweb' |
|
64 _address = None |
|
65 _socket = None |
|
66 |
|
67 |
|
68 def setup(bucket, address): |
|
69 """Configure the statsd endpoint |
|
70 |
|
71 :param bucket: the name of the statsd bucket that will be used to |
|
72 build messages. |
|
73 |
|
74 :param address: the UDP endpoint of the statsd server. Must a |
|
75 couple (ip, port). |
|
76 """ |
|
77 global _bucket, _address, _socket |
|
78 _bucket, _address = bucket, address |
|
79 _socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
|
80 |
|
81 |
|
82 def statsd_c(context, n=1): |
|
83 if _address is not None: |
|
84 _socket.sendto('{0}.{1}:{2}|c\n'.format(_bucket, context, n), _address) |
|
85 |
|
86 |
|
87 def statsd_g(context, value): |
|
88 if _address is not None: |
|
89 _socket.sendto('{0}.{1}:{2}|g\n'.format(_bucket, context, value), _address) |
|
90 |
|
91 |
|
92 def statsd_t(context, value): |
|
93 if _address is not None: |
|
94 _socket.sendto('{0}.{1}:{2:.4f}|ms\n'.format(_bucket, context, value), _address) |
|
95 |
|
96 |
|
97 class statsd_timeit(object): |
|
98 __slots__ = ('callable',) |
|
99 |
|
100 def __init__(self, callableobj): |
|
101 self.callable = callableobj |
|
102 |
|
103 @property |
|
104 def __doc__(self): |
|
105 return self.callable.__doc__ |
|
106 @property |
|
107 def __name__(self): |
|
108 return self.callable.__name__ |
|
109 |
|
110 def __call__(self, *args, **kw): |
|
111 if _address is None: |
|
112 return self.callable(*args, **kw) |
|
113 t0 = time.time() |
|
114 try: |
|
115 return self.callable(*args, **kw) |
|
116 finally: |
|
117 dt = 1000*(time.time()-t0) |
|
118 msg = '{0}.{1}:{2:.4f}|ms\n{0}.{1}:1|c\n'.format(_bucket, self.__name__, dt) |
|
119 _socket.sendto(msg, _address) |
|
120 |
|
121 def __get__(self, obj, objtype): |
|
122 """Support instance methods.""" |
|
123 if obj is None: # class method or some already wrapped method |
|
124 return self |
|
125 import functools |
|
126 return functools.partial(self.__call__, obj) |