Home | History | Annotate | Download | only in graphite
      1 # Copyright 2015 The Chromium OS Authors. All rights reserved.
      2 # Use of this source code is governed by a BSD-style license that can be
      3 # found in the LICENSE file.
      4 
      5 import socket
      6 
      7 import common
      8 from autotest_lib.client.common_lib import global_config
      9 from autotest_lib.client.common_lib.cros.graphite import autotest_es
     10 from autotest_lib.client.common_lib.cros.graphite import es_utils
     11 from autotest_lib.client.common_lib.cros.graphite import stats
     12 
     13 
     14 # Pylint locally complains about "No value passed for parameter 'key'" here
     15 # pylint: disable=E1120
     16 # If one has their hostname listed including a domain, ie. |milleral.mtv|,
     17 # then this will show up on Graphite as milleral/mtv/<stats>.  This seems
     18 # silly, so let's replace '.'s with '_'s to disambiguate Graphite folders
     19 # from FQDN hostnames.
     20 STATSD_SERVER = global_config.global_config.get_config_value('CROS',
     21         'STATSD_SERVER', default='')
     22 STATSD_PORT = global_config.global_config.get_config_value('CROS',
     23         'STATSD_PORT', type=int, default=0)
     24 hostname = global_config.global_config.get_config_value(
     25         'SERVER', 'hostname', default='localhost')
     26 
     27 if hostname.lower() in ['localhost', '127.0.0.1']:
     28     hostname = socket.gethostname()
     29 hostname = hostname.replace('.', '_')
     30 
     31 _default_es = es_utils.ESMetadata(use_http=autotest_es.ES_USE_HTTP,
     32                                   host=autotest_es.METADATA_ES_SERVER,
     33                                   port=autotest_es.ES_PORT,
     34                                   index=autotest_es.INDEX_METADATA,
     35                                   udp_port=autotest_es.ES_UDP_PORT)
     36 _statsd = stats.Statsd(es=_default_es, host=STATSD_SERVER, port=STATSD_PORT,
     37                        prefix=hostname)
     38 
     39 
     40 def _es_init(original):
     41     class _Derived(original):
     42         def __init__(self, *args, **kwargs):
     43             es = kwargs.pop('es', None)
     44             super(_Derived, self).__init__(*args, **kwargs)
     45             if es:
     46                 self.es = es
     47     return _Derived
     48 
     49 
     50 @_es_init
     51 class Average(_statsd.Average):
     52     """Wrapper around _statsd.Average"""
     53 
     54 @_es_init
     55 class Counter(_statsd.Counter):
     56     """Wrapper around _statsd.Counter"""
     57 
     58 @_es_init
     59 class Gauge(_statsd.Gauge):
     60     """Wrapper around _statsd.Gauge"""
     61 
     62 @_es_init
     63 class Timer(_statsd.Timer):
     64     """Wrapper around _statsd.Timer"""
     65 
     66 @_es_init
     67 class Raw(_statsd.Raw):
     68     """Wrapper around _statd.Raw"""
     69