cubicweb/statsd_logger.py
changeset 11057 0b59724cb3f2
parent 10650 28b3d39bcbc6
child 11767 432f87a63057
equal deleted inserted replaced
11052:058bb3dc685f 11057:0b59724cb3f2
       
     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     packed = None
       
    79     for family in (socket.AF_INET6, socket.AF_INET):
       
    80         try:
       
    81             packed = socket.inet_pton(family, address[0])
       
    82             break
       
    83         except socket.error:
       
    84             continue
       
    85     if packed is None:
       
    86         return
       
    87     _bucket, _address = bucket, address
       
    88     _socket = socket.socket(family, socket.SOCK_DGRAM)
       
    89 
       
    90 
       
    91 def statsd_c(context, n=1):
       
    92     if _address is not None:
       
    93         _socket.sendto('{0}.{1}:{2}|c\n'.format(_bucket, context, n), _address)
       
    94 
       
    95 
       
    96 def statsd_g(context, value):
       
    97     if _address is not None:
       
    98         _socket.sendto('{0}.{1}:{2}|g\n'.format(_bucket, context, value), _address)
       
    99 
       
   100 
       
   101 def statsd_t(context, value):
       
   102     if _address is not None:
       
   103         _socket.sendto('{0}.{1}:{2:.4f}|ms\n'.format(_bucket, context, value), _address)
       
   104 
       
   105 
       
   106 class statsd_timeit(object):
       
   107     __slots__ = ('callable',)
       
   108 
       
   109     def __init__(self, callableobj):
       
   110         self.callable = callableobj
       
   111 
       
   112     @property
       
   113     def __doc__(self):
       
   114         return self.callable.__doc__
       
   115     @property
       
   116     def __name__(self):
       
   117         return self.callable.__name__
       
   118     
       
   119     def __call__(self, *args, **kw):
       
   120         if _address is None:
       
   121             return self.callable(*args, **kw)
       
   122         t0 = time.time()
       
   123         try:
       
   124             return self.callable(*args, **kw)
       
   125         finally:
       
   126             dt = 1000*(time.time()-t0)
       
   127             msg = '{0}.{1}:{2:.4f}|ms\n{0}.{1}:1|c\n'.format(_bucket, self.__name__, dt)
       
   128             _socket.sendto(msg, _address)
       
   129                 
       
   130     def __get__(self, obj, objtype):
       
   131         """Support instance methods."""
       
   132         if obj is None: # class method or some already wrapped method
       
   133             return self
       
   134         import functools
       
   135         return functools.partial(self.__call__, obj)