def __init__(self, name, connection=None): self.name = name if not connection: connection = statsd.Connection() self.connection = connection self.logger = logging.getLogger('%s.%s' % (__name__, self.__class__.__name__))
def __init__(self, args=None, config_path=None): logging.Handler.__init__(self) if args is not None and args != '': config_path = args if not os.path.isfile(config_path): raise Exception('Invalid path to config file.') with open(config_path) as config_file_obj: self.config = load(config_file_obj.read()) for key in self.DEFAULT_CONFIG: setattr( self, key, self.config.get('main', {}).get(key, None) or self.DEFAULT_CONFIG[key]) self.connection = statsd.Connection(host=self.host, port=self.port, sample_rate=self.sample_rate, disabled=self.disabled) self.publish_templates = self.DEFAULT_PUBLISH_TEMPLATES publish_templates = self.config.get('publish_templates', {}) self.publish_templates.update(publish_templates) self.timer = statsd.timer.Timer(self.app_key, self.connection) self.counter = statsd.counter.Counter(self.app_key, self.connection) self.gauge = statsd.gauge.Gauge(self.app_key, self.connection) self.raw = statsd.raw.Raw(self.app_key, self.connection) self.counters = self.config.get('counters', {}) self.gauges = self.config.get('gauges', {}) self.timers = self.config.get('timers', []) self.timers_start_keys = self._get_timers_keys_list('start') self.timers_end_keys = self._get_timers_keys_list('end') self.timers_value_keys = self._get_timers_keys_list('value')
def _connect(self): """ Connect to the statsd server """ # Create socket self.connection = statsd.Connection(host=self.host, port=self.port, sample_rate=1.0)
def setup_stats(app): host = app.config.get('STATSD_HOST') port = app.config.get('STATSD_PORT') name = app.config.get('STATSD_PREFIX', '') connection = statsd.Connection(host=host, port=port, sample_rate=1) client = statsd.Client(name, connection) app.stats = StatsDClient(client)
def __init__(self, settings): with connectionLock: global connection if connection == None: connection = statsd.Connection(host=settings['host'], port=settings['port'], sample_rate=1, disabled=False) self.namespace = settings['namespace']
def get_statsd_conn(): if statsd is None: raise ImproperlyConfigured("You must install 'python-statsd' in order to use this backend.") conn = statsd.Connection( host=getattr(settings, 'APP_METRICS_STATSD_HOST', 'localhost'), port=int(getattr(settings, 'APP_METRICS_STATSD_PORT', 8125)), sample_rate=float(getattr(settings, 'APP_METRICS_STATSD_SAMPLE_RATE', 1)), ) return conn
def get_connection(host=None, port=None, sample_rate=None): if not host: host = settings.STATSD_HOST if not port: port = settings.STATSD_PORT if not sample_rate: sample_rate = settings.STATSD_SAMPLE_RATE return statsd.Connection(host, port, sample_rate)
def get_connection(host=None, port=None, sample_rate=None): if not host: host = getattr(settings, 'STATSD_HOST', '127.0.0.1') if not port: port = getattr(settings, 'STATSD_PORT', 8125) if not sample_rate: sample_rate = getattr(settings, 'STATSD_SAMPLE_RATE', 1.0) return statsd.Connection(host, port, sample_rate)
def setup(hass, config): """ Setup the StatsD component. """ from statsd.compat import NUM_TYPES import statsd conf = config[DOMAIN] host = conf[CONF_HOST] port = util.convert(conf.get(CONF_PORT), int, DEFAULT_PORT) sample_rate = util.convert(conf.get(CONF_RATE), int, DEFAULT_RATE) prefix = util.convert(conf.get(CONF_PREFIX), str, DEFAULT_PREFIX) statsd_connection = statsd.Connection(host=host, port=port, sample_rate=sample_rate, disabled=False) meter = statsd.Gauge(prefix, statsd_connection) def statsd_event_listener(event): """ Listen for new messages on the bus and sends them to StatsD. """ state = event.data.get('new_state') if state is None: return if state.state in (STATE_ON, STATE_LOCKED, STATE_ABOVE_HORIZON): _state = 1 elif state.state in (STATE_OFF, STATE_UNLOCKED, STATE_UNKNOWN, STATE_BELOW_HORIZON): _state = 0 else: _state = state.state if _state == '': return try: _state = float(_state) except ValueError: pass if not isinstance(_state, NUM_TYPES): return _LOGGER.debug('Sending %s.%s', state.entity_id, _state) meter.send(state.entity_id, _state) hass.bus.listen(EVENT_STATE_CHANGED, statsd_event_listener) return True
def get_app(): app = Flask('kardboard') app.config.from_object('kardboard.default_settings') if os.getenv('KARDBOARD_SETTINGS', None): app.config.from_envvar('KARDBOARD_SETTINGS') app.secret_key = app.config['SECRET_KEY'] app.db = MongoEngine(app) app.jinja_env.add_extension('kardboard.util.Markdown2Extension') app.jinja_env.filters['slugify'] = slugify app.jinja_env.filters['timesince'] = timesince app.jinja_env.filters['timeuntil'] = timeuntil app.jinja_env.filters['jsonencode'] = jsonencode app.jinja_env.globals['newrelic_head'] = newrelic_head app.jinja_env.globals['newrelic_foot'] = newrelic_foot if app.config.get('COMPILE_TEMPLATES', False): compiled_templates = os.path.join( os.path.abspath(os.path.dirname(__file__)), 'compiled_templates') compiled_files = path.path(compiled_templates).files() if len(compiled_files) <= 1: app.jinja_env.compile_templates(compiled_templates, zip=None, py_compile=True) app.jinja_env.loader = ModuleLoader(compiled_templates) configure_logging(app) app.wsgi_app = FixGunicorn(app.wsgi_app) statsd_conf = app.config.get('STATSD_CONF', {}) statsd_connection = statsd.Connection( host=statsd_conf.get('host', '127.0.0.1'), port=statsd_conf.get('port', 8125), sample_rate=statsd_conf.get('sample_rate', 1), ) machine_name = socket.getfqdn().split('.')[0] environment_name = app.config.get('ENV_MAPPING', {}).get(machine_name, 'default') prefix_name = '%s.%s.kardboard' % (environment_name, machine_name) app.statsd = statsd.Client(prefix_name, statsd_connection) if SENTRY_SUPPORT and 'SENTRY_DSN' in app.config.keys(): sentry = Sentry(app) sentry return app
def handle(self): statsd_connection = statsd.Connection( host=self.settings.get('statsd').get('host', '127.0.0.1'), port=self.settings.get('statsd').get('port', 8125), sample_rate=self.settings.get('statsd').get('sample_rate', 1), ) meter = statsd.Gauge( self.settings.get('statsd').get('prefix', 'sensu'), statsd_connection) key = '{}.{}'.format(self.event['client']['name'].replace('.', '_'), self.event['check']['name'].replace('.', '_')) meter.send(key, self.event['check']['status'])
def _connect(self): """ Connect to the statsd server """ if not statsd: return if hasattr(statsd, 'StatsClient'): self.connection = statsd.StatsClient(host=self.host, port=self.port).pipeline() else: # Create socket self.connection = statsd.Connection(host=self.host, port=self.port, sample_rate=1.0)
import statsd SAMPLE_RATE = 1 HOST = "10.10.10.180" PORT = 8125 statsd_connection = statsd.Connection( host=HOST, port=PORT, sample_rate=SAMPLE_RATE, disabled=False ) PREFIX = 'gauge.test' gauge = statsd.Gauge(PREFIX, statsd_connection) gauge.send('metric1', 50) gauge.send('metric3', 10)
serverlist = utils.config.get("statsd", "server") hostname = utils.config.get("general", "hostname") #TODO - Fix this for a full list of servers slist = [serverlist] log.debug(serverlist) log.debug(slist) mode = utils.config.get("general", "mode") statsd_conn = raw = [] statsDServerIP = '10.100.200.81' statsDServerPort = 8125 statsd_conn = statsd.Connection(host=statsDServerIP, port=statsDServerPort, sample_rate=1, disabled=False) raw = statsd.Raw(hostname, statsd_conn) def rawsend(name, value, tstamp): #log.debug('trending %s, %s, %s' % (name, value, tstamp)) return raw.send(name, value, int(tstamp)) #for server in slist: #log.debug(server) #svr, prt = server.split(':') #sdc = statsd.Connection(host=svr, port=prt, sample_rate=1, disabled=False) #statsd_conn.append(sdc) #raw.append(statsd.Raw('procstats', sdc))
def get_client(request): settings = request.registry.settings conn = statsd.Connection(host=settings.get('statsd.host'), port=settings.get('statsd.port')) return statsd.Client(__package__, connection=conn)
def setUp(self): self.con = statsd.Connection(host=HOST, port=PORT, disabled=False) self.gauge = statsd.Gauge("core_prod", self.con) self.counter = statsd.Counter("core_prod", self.con)
def __init__(self): self.connection = statsd.Connection(host=settings.STATSD_HOST, port=settings.STATSD_PORT, sample_rate=settings.STATSD_SAMPLE_RATE, disabled=False)
import statsd import time if __name__ == '__main__': # Create a new connection for the client connection = statsd.Connection( host='localhost', port=8125, sample_rate=1, ) # Create the client client = statsd.Client("python", connection) # Create counter counter = client.get_counter("counter") # Create gauge gauge = client.get_gauge("gauge") # Create average average = client.get_average("average") while True: # Create a timer timer = client.get_timer("timer") # Will send the elapsed time once all the block has been executed with timer: counter += 1 # Increment by one the counter
import statsd # from .celery import app from aliyunsdkcms.request.v20180308 import QueryMetricListRequest from aliyunsdkcore import client from aliyunsdkslb.request.v20140515 import DescribeLoadBalancersRequest from aliyunsdkvpc.request.v20160428 import DescribeBandwidthPackagesRequest, \ DescribeBandwidthPackagePublicIpMonitorDataRequest ########################## statsd 配置 ########################## SERVER = '127.0.0.1' PORT = 8125 PREFIX = 'mwee.network.eye' DELAYTIME = 3 STATSD_CONNECTION = statsd.Connection(host=SERVER, port=PORT) STATSD_CLIENT = statsd.Client("mwee.network.eye", STATSD_CONNECTION) STATSD_GAUGE = STATSD_CLIENT.get_client(class_=statsd.Gauge) ########################## statsd 配置 ########################## class AliyunBase(): key = '' secret = '' regionId = '' def __init__(self, regionId='cn-shanghai'): self.regionId = regionId self.initClient(self.regionId, self.key, self.secret)
def prepare_statsd(parameters): r"""Sends data to statsd Sends a value to statsd. host defaults to ``127.0.0.1`` port defaults to ``8125`` sample_rate defaults to ``1.0`` type Accepted values are ``counter``, ``gauge`` and ``timer``, defaults to ``counter`` value The value to send. Defaults to ``1.0`` multiplier The amount to multiply the value by. Defaults to ``1.0`` delta boolean, only used for gauge, whether to send differential values or absolute values. Defaults to ``False`` prefix the prefix for the stat name backreferences not allowed name the name for the stat, backreferences allowed (required) Example: .. code:: yaml match: Duration: (\d+.\d+)s statsd: type: timer value: {1} prefix: appserver.request name: duration statsd: prefix: appserver.request name: count """ import statsd # noqa statsd_connection = statsd.Connection( host=parameters.get('host', '127.0.0.1'), port=int(parameters.get('port', 8125)), sample_rate=float(parameters.get('sample_rate', 1.0)), ) meter_type = parameters.get('type', 'counter') name_template = logshipper.context.prepare_template(parameters['name']) val_template = logshipper.context.prepare_template( parameters.get('value', 1)) multiplier = float(parameters.get('multiplier', 1.0)) if meter_type == 'counter': statsd_client = statsd.Counter(parameters.get('prefix'), statsd_connection) delta = True elif meter_type == 'gauge': statsd_client = statsd.Gauge(parameters.get('prefix'), statsd_connection) delta_str = str(parameters.get("delta", False)).lower() delta = delta_str in filters.TRUTH_VALUES elif meter_type == 'timer': statsd_client = statsd.Timer(parameters.get('prefix'), statsd_connection) delta = False else: raise ValueError("Unknown meter type, should be one of counter, " "gauge or timer") # pragma: nocover def handle_statsd(message, context): name = name_template.interpolate(context) value = val_template.interpolate(context) if delta: statsd_client.increment(name, float(value) * multiplier) else: statsd_client.send(name, float(value) * multiplier) return handle_statsd
def main(): # Stats statsd_connection = statsd.Connection(host='lg-head', port=8125, sample_rate=1) statsd_client = statsd.Client('usage', statsd_connection) #active_counter = statsd_client.get_client( name = 'active', class_ = statsd.Counter ) #idle_counter = statsd_client.get_client( name = 'idle', class_ = statsd.Counter ) # Init timers state = IDLE_STATE idle_timer = statsd_client.get_client(name='idle', class_=statsd.Timer) idle_timer.start() active_timer = None if len(sys.argv) != 2: print "Usage:", sys.argv[0], "<command>" print "<command> will be called every", tour_check_per, "seconds", print "after", (tour_check_per * tour_wait_for_trigger), "seconds" print "if USB devs are not touched." sys.exit(1) cmd = sys.argv[1] cnt = 0 while True: # update tour wait time from shell.conf try: tour_wait_for_trigger = int(checkConfig('LG_SAVER_WAITS')) except ValueError: print 'WARNING: LG_SAVER_WAITS is not an integer!' if checkConfig('LG_MASTERSLAVE').find('master') == -1: print 'Not master, sleeping...' state = DEAD_STATE cnt = 0 if state == IDLE_STATE: idle_timer.stop() elif state == ACTIVE_STATE: active_timer.stop() state = DEAD_STATE time.sleep(tour_check_per) elif Touched(space_nav, quanta_ts): print "Touched." cnt = 0 if state != ACTIVE_STATE: state = ACTIVE_STATE if idle_timer != None: idle_timer.stop() active_timer = statsd_client.get_client(name='active', class_=statsd.Timer) active_timer.start() else: active_timer.intermediate(subname='total') else: cnt += 1 if cnt >= tour_wait_for_trigger: print cmd os.system(cmd) else: print "Wait...", cnt if state != IDLE_STATE: state = IDLE_STATE if active_timer != None: active_timer.stop() idle_timer = statsd_client.get_client(name='idle', class_=statsd.Timer) idle_timer.start() else: idle_timer.intermediate(subname='total')
def __init__(self, es, host, port, prefix): # This is the connection that we're going to reuse for every client # that gets created. This should maximally reduce overhead of stats # logging. self.conn = statsd.Connection(host=host, port=port) @_prepend_init(es, self.conn, prefix) class Average(statsd.Average): """Wrapper around statsd.Average.""" def send(self, subname, value): """Sends time-series data to graphite and metadata (if any) to es. @param subname: The subname to report the data to (i.e. 'daisy.reboot') @param value: Value to be sent. """ statsd.Average.send(self, subname, value) self.es.post(type_str=STATS_ES_TYPE, metadata=self.metadata, subname=subname, value=value) self.Average = Average @_prepend_init(es, self.conn, prefix) class Counter(statsd.Counter): """Wrapper around statsd.Counter.""" def _send(self, subname, value): """Sends time-series data to graphite and metadata (if any) to es. @param subname: The subname to report the data to (i.e. 'daisy.reboot') @param value: Value to be sent. """ statsd.Counter._send(self, subname, value) self.es.post(type_str=STATS_ES_TYPE, metadata=self.metadata, subname=subname, value=value) self.Counter = Counter @_prepend_init(es, self.conn, prefix) class Gauge(statsd.Gauge): """Wrapper around statsd.Gauge.""" def send(self, subname, value): """Sends time-series data to graphite and metadata (if any) to es. @param subname: The subname to report the data to (i.e. 'daisy.reboot') @param value: Value to be sent. """ statsd.Gauge.send(self, subname, value) self.es.post(type_str=STATS_ES_TYPE, metadata=self.metadata, subname=subname, value=value) self.Gauge = Gauge @_prepend_init(es, self.conn, prefix) class Timer(statsd.Timer): """Wrapper around statsd.Timer.""" # To override subname to not implicitly append 'total'. def stop(self, subname=''): statsd.Timer.stop(self, subname) def send(self, subname, value): """Sends time-series data to graphite and metadata (if any) to es. @param subname: The subname to report the data to (i.e. 'daisy.reboot') @param value: Value to be sent. """ statsd.Timer.send(self, subname, value) self.es.post(type_str=STATS_ES_TYPE, metadata=self.metadata, subname=self.name, value=value) def __enter__(self): self.start() return self def __exit__(self, exn_type, exn_value, traceback): if exn_type is None: self.stop() self.Timer = Timer @_prepend_init(es, self.conn, prefix) class Raw(statsd.Raw): """Wrapper around statsd.Raw.""" def send(self, subname, value, timestamp=None): """Sends time-series data to graphite and metadata (if any) to es. The datapoint we send is pretty much unchanged (will not be aggregated) @param subname: The subname to report the data to (i.e. 'daisy.reboot') @param value: Value to be sent. @param timestamp: Time associated with when this stat was sent. """ statsd.Raw.send(self, subname, value, timestamp) self.es.post(type_str=STATS_ES_TYPE, metadata=self.metadata, subname=subname, value=value, timestamp=timestamp) self.Raw = Raw