Python statsd.StatsClient() Examples

The following are 30 code examples of statsd.StatsClient(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module statsd , or try the search function .
Example #1
Source File: statsd-agent.py    From dino with Apache License 2.0 7 votes vote down vote up
def disk():
    c = statsd.StatsClient(STATSD_HOST, 8125, prefix=PREFIX + 'system.disk')
    while True:
        for path, label in PATHS:
            disk_usage = psutil.disk_usage(path)

            st = os.statvfs(path)
            total_inode = st.f_files
            free_inode = st.f_ffree
            inode_percentage = int(100*(float(total_inode - free_inode) / total_inode))

            c.gauge('%s.inodes.percent' % label, inode_percentage)
            c.gauge('%s.total' % label, disk_usage.total)
            c.gauge('%s.used' % label, disk_usage.used)
            c.gauge('%s.free' % label, disk_usage.free)
            c.gauge('%s.percent' % label, disk_usage.percent)
        time.sleep(GRANULARITY) 
Example #2
Source File: graphite_influxdb.py    From graphite-influxdb with Apache License 2.0 7 votes vote down vote up
def __init__(self, config=None):
        # Shouldn't be trying imports in __init__.
        # It turns what should be a load error into a runtime error
        config = normalize_config(config)
        self.config = config
        self.client = InfluxDBClient(config['host'], config['port'], config['user'], config['passw'], config['db'], config['ssl'])
        self.schemas = [(re.compile(patt), step) for (patt, step) in config['schema']]
        try:
            self.statsd_client = statsd.StatsClient(config['statsd'].get('host'),
                                                    config['statsd'].get('port', 8125)) \
                                                    if 'statsd' in config and config['statsd'].get('host') else NullStatsd()
        except NameError:
            logger.warning("Statsd client configuration present but 'statsd' module"
                           "not installed - ignoring statsd configuration..")
            self.statsd_client = NullStatsd()
        self._setup_logger(config['log_level'], config['log_file'])
        self.es = None
        if config['es_enabled']:
            try:
                from elasticsearch import Elasticsearch
            except ImportError:
                logger.warning("Elasticsearch configuration present but 'elasticsearch'"
                               "module not installed - ignoring elasticsearch configuration..")
            else:
                self.es = Elasticsearch(config['es_hosts']) 
Example #3
Source File: statsd-agent.py    From dino with Apache License 2.0 7 votes vote down vote up
def memory():
    c = statsd.StatsClient(STATSD_HOST, 8125, prefix=PREFIX + 'system.memory')
    while True:
        swap = psutil.swap_memory()
        c.gauge('swap.total', swap.total)
        c.gauge('swap.used', swap.used)
        c.gauge('swap.free', swap.free)
        c.gauge('swap.percent', swap.percent)

        virtual = psutil.virtual_memory()
        c.gauge('virtual.total', virtual.total)
        c.gauge('virtual.available', virtual.available)
        c.gauge('virtual.used', virtual.used)
        c.gauge('virtual.free', virtual.free)
        c.gauge('virtual.percent', virtual.percent)
        c.gauge('virtual.active', virtual.active)
        c.gauge('virtual.inactive', virtual.inactive)
        c.gauge('virtual.buffers', virtual.buffers)
        c.gauge('virtual.cached', virtual.cached)
        time.sleep(GRANULARITY) 
Example #4
Source File: MessageProcessor.py    From ReadableWebProxy with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def __init__(self, interface_dict):
		self.log = logging.getLogger('Main.Feeds.RPC')

		self.worker_pools   = {}
		self.interface_dict = interface_dict

		self.mon_con = statsd.StatsClient(
				host = settings_file.GRAPHITE_DB_IP,
				port = 8125,
				prefix = 'ReadableWebProxy.FetchAgent',
				)

		self.debug_interval = 10
		self.chunk_flush_interval = 10
		self.last_debug = time.time() - self.debug_interval
		self.last_chunk_flush = time.time() - self.chunk_flush_interval

		os.makedirs(settings_file.CHUNK_CACHE_DIR, exist_ok=True) 
Example #5
Source File: test_statsd_utilities.py    From amundsenmetadatalibrary with Apache License 2.0 6 votes vote down vote up
def test_get_statsd_client(self) -> None:
        with patch.object(current_app, 'config') as mock_config, \
                patch.object(StatsClient, '__init__', return_value=None) as mock_statsd_init:
            mock_config.return_value.single.return_value = True

            statsd_client1 = _get_statsd_client(prefix='foo')
            self.assertIsNotNone(statsd_client1)
            statsd_client2 = _get_statsd_client(prefix='foo')
            self.assertIsNotNone(statsd_client2)
            self.assertEqual(statsd_client1, statsd_client2)

            self.assertEqual(mock_statsd_init.call_count, 1)

            statsd_client3 = _get_statsd_client(prefix='bar')
            self.assertIsNotNone(statsd_client3)
            statsd_client4 = _get_statsd_client(prefix='bar')
            self.assertIsNotNone(statsd_client4)
            self.assertEqual(statsd_client3, statsd_client4)

            self.assertNotEqual(statsd_client1, statsd_client3)
            self.assertEqual(mock_statsd_init.call_count, 2) 
Example #6
Source File: statsd_utilities.py    From amundsenmetadatalibrary with Apache License 2.0 6 votes vote down vote up
def _get_statsd_client(*, prefix: str) -> StatsClient:
    """
    Object pool method that reuse already created StatsClient based on prefix
    :param prefix:
    :return:
    """
    if not has_app_context() or not current_app.config[config.IS_STATSD_ON]:
        return None
    else:
        if prefix not in __STATSD_POOL:
            with __STATSD_POOL_LOCK:
                if prefix not in __STATSD_POOL:
                    LOGGER.info('Instantiate StatsClient with prefix {}'.format(prefix))
                    statsd_client = StatsClient(prefix=prefix)
                    __STATSD_POOL[prefix] = statsd_client
                    return statsd_client

        if LOGGER.isEnabledFor(logging.DEBUG):
            LOGGER.debug('Reuse StatsClient with prefix {}'.format(prefix))
        return __STATSD_POOL[prefix] 
Example #7
Source File: stats.py    From airflow with Apache License 2.0 6 votes vote down vote up
def get_statsd_logger(self):
        if conf.getboolean('scheduler', 'statsd_on'):
            from statsd import StatsClient

            if conf.has_option('scheduler', 'statsd_custom_client_path'):
                stats_class = conf.getimport('scheduler', 'statsd_custom_client_path')

                if not issubclass(stats_class, StatsClient):
                    raise AirflowConfigException(
                        "Your custom Statsd client must extend the statsd.StatsClient in order to ensure "
                        "backwards compatibility."
                    )
                else:
                    log.info("Successfully loaded custom Statsd client")

            else:
                stats_class = StatsClient

        statsd = stats_class(
            host=conf.get('scheduler', 'statsd_host'),
            port=conf.getint('scheduler', 'statsd_port'),
            prefix=conf.get('scheduler', 'statsd_prefix'))
        allow_list_validator = AllowListValidator(conf.get('scheduler', 'statsd_allow_list', fallback=None))
        return SafeStatsdLogger(statsd, allow_list_validator) 
Example #8
Source File: cloud_region.py    From openstacksdk with Apache License 2.0 6 votes vote down vote up
def get_statsd_client(self):
        if not statsd:
            return None
        statsd_args = {}
        if self._statsd_host:
            statsd_args['host'] = self._statsd_host
        if self._statsd_port:
            statsd_args['port'] = self._statsd_port
        if statsd_args:
            try:
                return statsd.StatsClient(**statsd_args)
            except Exception:
                self.log.warning('Cannot establish connection to statsd')
                return None
        else:
            return None 
Example #9
Source File: statsd.py    From dino with Apache License 2.0 6 votes vote down vote up
def __init__(self, env):
        self.env = env

        conf = env.config.get(ConfigKeys.STATS_SERVICE)
        host = conf.get(ConfigKeys.HOST)

        if env.config.get(ConfigKeys.TESTING, False) or host == 'mock':
            self.statsd = MockStatsd()
        else:
            import statsd

            port = conf.get(ConfigKeys.PORT)
            prefix = 'dino'
            if ConfigKeys.PREFIX in conf:
                prefix = conf.get(ConfigKeys.PREFIX)
            if ConfigKeys.INCLUDE_HOST_NAME in conf:
                include_host_name = conf.get(ConfigKeys.INCLUDE_HOST_NAME)
                if include_host_name is not None and str(include_host_name).strip().lower() in ['yes', '1', 'true']:
                    import socket
                    prefix = '%s.%s' % (prefix, socket.gethostname())

            self.statsd = statsd.StatsClient(host, int(port), prefix=prefix) 
Example #10
Source File: metrics.py    From isthislegit with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def init():
    global _client

    statsd_host = os.environ.get('FLANKER_METRICS_HOST')
    statsd_port = os.environ.get('FLANKER_METRICS_PORT')
    statsd_prfx = os.environ.get('FLANKER_METRICS_PREFIX')

    if not statsd_host or not statsd_port or not statsd_prfx:
        return

    try:
        import statsd
    except ImportError:
        return

    _client = statsd.StatsClient(statsd_host, statsd_port, prefix=statsd_prfx) 
Example #11
Source File: connection.py    From insightconnect-plugins with MIT License 6 votes vote down vote up
def connect(self, params):
        self.logger.info("Connect: Connecting..")

        host = params.get('host')
        port = params.get('port')
        prefix = params.get('prefix', None)
        protocol = params.get('protocol')

        self.host = host
        self.port = port
        self.protocol = protocol

        if protocol == 'TCP':
            timeout = params.get('tcp', None)['timeout']
            timeout = None if timeout == 0 else timeout
            self.instance = statsd.TCPStatsClient(host, port, prefix, timeout)
        elif protocol == 'UDP':
            maxudpsize = params.get('udp', 512)
            if maxudpsize is not None:
                maxudpsize = maxudpsize['maxudpsize']
            self.instance = statsd.StatsClient(host, port, prefix, maxudpsize) 
Example #12
Source File: statsd_utilities.py    From amundsensearchlibrary with Apache License 2.0 6 votes vote down vote up
def _get_statsd_client(*, prefix: str) -> StatsClient:
    """
    Object pool method that reuse already created StatsClient based on prefix
    :param prefix:
    """
    if not current_app.config[config.STATS_FEATURE_KEY]:
        # return if stats feature is not enabled
        return None
    else:
        if prefix not in _STATSD_POOL:
            with _STATSD_POOL_LOCK:
                if prefix not in _STATSD_POOL:
                    LOGGER.info('Instantiate StatsClient with prefix {}'.format(prefix))
                    statsd_client = StatsClient(prefix=prefix)
                    _STATSD_POOL[prefix] = statsd_client
                    return statsd_client

        if LOGGER.isEnabledFor(logging.DEBUG):
            LOGGER.debug('Reuse StatsClient with prefix {}'.format(prefix))
        return _STATSD_POOL[prefix] 
Example #13
Source File: statsd-online-count.py    From dino with Apache License 2.0 6 votes vote down vote up
def online_count():
    c = statsd.StatsClient(STATSD_HOST, 8125, prefix=PREFIX + 'online')
    while True:
        for _, _, community, db_num in hosts:
            count = r_servers[community].scard('users:multicast')
            c.gauge('%s.count' % community, count)

            sessions = r_servers[community].hgetall('session:count')
            session_count = 0

            for _, value in sessions.items():
                session_count += int(float(str(value, 'utf-8')))

            c.gauge('%s.count' % community, count)
            c.gauge('%s.sessions' % community, session_count)
        time.sleep(GRANULARITY) 
Example #14
Source File: statsd-agent.py    From dino with Apache License 2.0 6 votes vote down vote up
def cpu_times_percent():
    c = statsd.StatsClient(STATSD_HOST, 8125, prefix=PREFIX + 'system.cpu')
    while True:
        value = psutil.cpu_percent(interval=1)
        c.gauge('system_wide.percent', value)

        cpu_t_percent = psutil.cpu_times_percent(interval=1)
        c.gauge('system_wide.times_percent.user', cpu_t_percent.user)
        c.gauge('system_wide.times_percent.nice', cpu_t_percent.nice)
        c.gauge('system_wide.times_percent.system', cpu_t_percent.system)
        c.gauge('system_wide.times_percent.idle', cpu_t_percent.idle)
        c.gauge('system_wide.times_percent.iowait', cpu_t_percent.iowait)
        c.gauge('system_wide.times_percent.irq', cpu_t_percent.irq)
        c.gauge('system_wide.times_percent.softirq', cpu_t_percent.softirq)
        c.gauge('system_wide.times_percent.steal', cpu_t_percent.steal)
        c.gauge('system_wide.times_percent.guest', cpu_t_percent.guest)
        c.gauge('system_wide.times_percent.guest_nice', cpu_t_percent.guest_nice)
        time.sleep(GRANULARITY) 
Example #15
Source File: stats_logger.py    From incubator-superset with Apache License 2.0 6 votes vote down vote up
def __init__(  # pylint: disable=super-init-not-called
            self,
            host: str = "localhost",
            port: int = 8125,
            prefix: str = "superset",
            statsd_client: Optional[StatsClient] = None,
        ) -> None:
            """
            Initializes from either params or a supplied, pre-constructed statsd client.

            If statsd_client argument is given, all other arguments are ignored and the
            supplied client will be used to emit metrics.
            """
            if statsd_client:
                self.client = statsd_client
            else:
                self.client = StatsClient(host=host, port=port, prefix=prefix) 
Example #16
Source File: job.py    From amundsendatabuilder with Apache License 2.0 6 votes vote down vote up
def __init__(self,
                 conf,
                 task,
                 publisher=NoopPublisher()):
        # type: (ConfigTree, Task, Publisher) -> None
        self.task = task
        self.conf = conf
        self.publisher = publisher
        self.scoped_conf = Scoped.get_scoped_conf(self.conf,
                                                  self.get_scope())
        if self.scoped_conf.get_bool(DefaultJob.IS_STATSD_ENABLED, False):
            prefix = 'amundsen.databuilder.job.{}'.format(self.scoped_conf.get_string(DefaultJob.JOB_IDENTIFIER))
            LOGGER.info('Setting statsd for job metrics with prefix: {}'.format(prefix))
            self.statsd = StatsClient(prefix=prefix)
        else:
            self.statsd = None 
Example #17
Source File: chassis.py    From microservices-in-action with MIT License 5 votes vote down vote up
def init_statsd(prefix=None, host=None, port=8125):
    statsd = StatsClient(host, port, prefix=prefix)
    return statsd 
Example #18
Source File: chassis.py    From microservices-in-action with MIT License 5 votes vote down vote up
def init_statsd(prefix=None, host=None, port=8125):
    statsd = StatsClient(host, port, prefix=prefix)
    return statsd 
Example #19
Source File: chassis.py    From microservices-in-action with MIT License 5 votes vote down vote up
def init_statsd(prefix=None, host=None, port=8125):
    statsd = StatsClient(host, port, prefix=prefix)
    return statsd 
Example #20
Source File: chassis.py    From microservices-in-action with MIT License 5 votes vote down vote up
def init_statsd(prefix=None, host=None, port=8125):
    statsd = StatsClient(host, port, prefix=prefix)
    return statsd 
Example #21
Source File: chassis.py    From microservices-in-action with MIT License 5 votes vote down vote up
def init_statsd(prefix=None, host=None, port=8125):
    statsd = StatsClient(host, port, prefix=prefix)
    return statsd 
Example #22
Source File: chassis.py    From microservices-in-action with MIT License 5 votes vote down vote up
def init_statsd(prefix=None, host=None, port=8125):
    statsd = StatsClient(host, port, prefix=prefix)
    return statsd 
Example #23
Source File: chassis.py    From microservices-in-action with MIT License 5 votes vote down vote up
def init_statsd(prefix=None, host=None, port=8125):
    statsd = StatsClient(host, port, prefix=prefix)
    return statsd 
Example #24
Source File: test_statsd_utilities.py    From amundsensearchlibrary with Apache License 2.0 5 votes vote down vote up
def test_no_statsd_client(self) -> None:
        with patch.object(StatsClient, '__init__'):
            statsd_client = _get_statsd_client(prefix='foo')
            self.assertIsNone(statsd_client) 
Example #25
Source File: chassis.py    From microservices-in-action with MIT License 5 votes vote down vote up
def init_statsd(prefix=None, host=None, port=8125):
    statsd = StatsClient(host, port, prefix=prefix)
    return statsd 
Example #26
Source File: chassis.py    From microservices-in-action with MIT License 5 votes vote down vote up
def init_statsd(prefix=None, host=None, port=8125):
    statsd = StatsClient(host, port, prefix=prefix)
    return statsd 
Example #27
Source File: chassis.py    From microservices-in-action with MIT License 5 votes vote down vote up
def init_statsd(prefix=None, host=None, port=8125):
    statsd = StatsClient(host, port, prefix=prefix)
    return statsd 
Example #28
Source File: chassis.py    From microservices-in-action with MIT License 5 votes vote down vote up
def init_statsd(prefix=None, host=None, port=8125):
    statsd = StatsClient(host, port, prefix=prefix)
    return statsd 
Example #29
Source File: client.py    From flytekit with Apache License 2.0 5 votes vote down vote up
def _get_stats_client():
    global _stats_client
    if _stats_client is None:
        _stats_client = statsd.StatsClient(_statsd_config.HOST.get(), _statsd_config.PORT.get())
    return _stats_client 
Example #30
Source File: command.py    From tilequeue with MIT License 5 votes vote down vote up
def make_statsd_client_from_cfg(cfg):
    if cfg.statsd_host:
        import statsd
        stats = statsd.StatsClient(cfg.statsd_host, cfg.statsd_port,
                                   prefix=cfg.statsd_prefix)
    else:
        stats = FakeStatsd()
    return stats