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
5import socket
6
7import common
8from autotest_lib.client.common_lib import global_config
9from autotest_lib.client.common_lib.cros.graphite import autotest_es
10from autotest_lib.client.common_lib.cros.graphite import es_utils
11from 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.
20STATSD_SERVER = global_config.global_config.get_config_value('CROS',
21        'STATSD_SERVER', default='')
22STATSD_PORT = global_config.global_config.get_config_value('CROS',
23        'STATSD_PORT', type=int, default=0)
24hostname = global_config.global_config.get_config_value(
25        'SERVER', 'hostname', default='localhost')
26
27if hostname.lower() in ['localhost', '127.0.0.1']:
28    hostname = socket.gethostname()
29hostname = 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
40def _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
51class Average(_statsd.Average):
52    """Wrapper around _statsd.Average"""
53
54@_es_init
55class Counter(_statsd.Counter):
56    """Wrapper around _statsd.Counter"""
57
58@_es_init
59class Gauge(_statsd.Gauge):
60    """Wrapper around _statsd.Gauge"""
61
62@_es_init
63class Timer(_statsd.Timer):
64    """Wrapper around _statsd.Timer"""
65
66@_es_init
67class Raw(_statsd.Raw):
68    """Wrapper around _statd.Raw"""
69