def init(self, prefix='glances'): """Init the connection to the Statsd server.""" if not self.export_enable: return None logger.info("Stats will be exported to StatsD server: {}:{}".format( self.host, self.port)) return StatsClient(self.host, int(self.port), prefix=prefix)
def __init__(self, metric_dict): self.metric_dict = metric_dict self.meta = Meta(metric_dict['meta']) self.metrics = Metrics(metric_dict['metrics']) self.logger = MetricLogger("metrics_4.csv", self.DATA_POINTS) self.statsd = StatsClient() self.api = ApiClient()
def init(self): """ Start point for application configuration """ ### logging if self.is_test: self.log_dict = Mock() else: self.__loggers = self.__setup_loggers() self.statsd = StatsClient(**self.config.STATSD) # Application server setup app = Flask('onyx') CORS(app, resources={r'/*': { 'origins': '*' }}, allow_headers='Content-Type') app.config.from_object(self.__config_filename) if app.config['ENVIRONMENT'] not in app.config['STATIC_ENABLED_ENVS']: app.config['STATIC_FOLDER'] = None self.__application = app # spawn off the tile index reader if not self.is_test: gevent.spawn(_read_tile_index_loop, self) return app
def __init__(self, host, port, prefix): if host: self.statsd = StatsClient( host=host, port=port, prefix=prefix, )
def test_check_health(host): dragon = Dragon(host, api_timeout=1, credentials=test_config['main']['credentials'], configs=test_config['configs'], statsd=StatsClient(host='127.0.0.1', prefix='dragon')) dragon.check_health()
def __new__(cls): if StatsClientSingleton.__instance is None: StatsClientSingleton.__instance = StatsClient( MonitoringConfig.metrics_host, MonitoringConfig.metrics_port, prefix=MonitoringConfig.metrics_prefix) return StatsClientSingleton.__instance
def _client(prefix=None, batch_len=1): sc = StatsClient(host=ADDR[0], port=ADDR[1], prefix=prefix, batch_len=batch_len) sc._sock = mock.Mock() return sc
class Gateway: name = "gateway" orders = RpcProxy("orders_service") statsd = StatsClient('statsd-agent', 8125, prefix='simplebank-demo.gateway') @http('POST', '/shares/sell') @statsd.timer('sell_shares') def sell_shares(self, request): req_id = uuid.uuid4() res = u"{}".format(req_id) req = self.__sell_shares(res) return Response(json.dumps( {"ok": "sell order {} placed".format(req_id)}), mimetype='application/json') @rpc def __sell_shares(self, payload): print("[{}] rpc to orders service : sell_shares".format(payload)) res = u"{}".format(payload) return self.orders.sell_shares(res) @http('GET', '/health') @statsd.timer('health') def health(self, _request): return json.dumps({'ok': datetime.datetime.utcnow().__str__()})
class MarketService: name = "market_service" statsd = StatsClient('statsd-agent', 8125, prefix='simplebank-demo.market') dispatch = EventDispatcher() @event_handler("orders_service", "order_created") @statsd.timer('request_reservation') def place_order(self, payload): print("service {} received: {} ... placing order to exchange".format( self.name, payload)) # place order in stock exchange exchange_resp = self.__place_order_exchange(payload) # event: emit order placed event self.__create_event("order_placed", payload) return json.dumps({'exchange_response': exchange_resp}) @rpc @statsd.timer('create_event') def __create_event(self, event, payload): print("[{}] {} emiting {} event".format(payload, self.name, event)) return self.dispatch(event, payload) @statsd.timer('place_order_stock_exchange') @circuit(failure_threshold=5, expected_exception=ConnectionError) def __place_order_exchange(self, request): print("[{}] {} placing order to stock exchange".format( request, self.name)) response = requests.get('https://jsonplaceholder.typicode.com/posts/1') return json.dumps({ 'code': response.status_code, 'body': response.text })
def init(self, prefix='glances'): """Init the connection to the Statsd server.""" if not self.export_enable: return None return StatsClient(self.host, self.port, prefix=prefix)
def run_with_config(sync, config): """ Execute the cartography.sync.Sync.run method with parameters built from the given configuration object. This function will create a Neo4j driver object from the given Neo4j configuration options (URI, auth, etc.) and will choose a sensible update tag if one is not specified in the given configuration. :type sync: cartography.sync.Sync :param sync: A sync task to run. :type config: cartography.config.Config :param config: The configuration to use to run the sync task. """ # Initialize statsd client if enabled if config.statsd_enabled: set_stats_client( StatsClient( host=config.statsd_host, port=config.statsd_port, prefix=config.statsd_prefix, ), ) neo4j_auth = None if config.neo4j_user or config.neo4j_password: neo4j_auth = (config.neo4j_user, config.neo4j_password) try: neo4j_driver = GraphDatabase.driver( config.neo4j_uri, auth=neo4j_auth, max_connection_lifetime=config.neo4j_max_connection_lifetime, ) except neobolt.exceptions.ServiceUnavailable as e: logger.debug("Error occurred during Neo4j connect.", exc_info=True) logger.error( ("Unable to connect to Neo4j using the provided URI '%s', an error occurred: '%s'. Make sure the Neo4j " "server is running and accessible from your network."), config.neo4j_uri, e, ) return 1 except neobolt.exceptions.AuthError as e: logger.debug("Error occurred during Neo4j auth.", exc_info=True) if not neo4j_auth: logger.error( ("Unable to auth to Neo4j, an error occurred: '%s'. cartography attempted to connect to Neo4j " "without any auth. Check your Neo4j server settings to see if auth is required and, if it is, " "provide cartography with a valid username and password."), e, ) else: logger.error( ("Unable to auth to Neo4j, an error occurred: '%s'. cartography attempted to connect to Neo4j with " "a username and password. Check your Neo4j server settings to see if the username and password " "provided to cartography are valid credentials."), e, ) return 1 default_update_tag = int(time.time()) if not config.update_tag: config.update_tag = default_update_tag return sync.run(neo4j_driver, config)
def main(): args = parseArguments() # initialize statsdclient global statsd_client statsd_client = StatsClient(host=args.server, port=args.port, prefix=args.source) value = None try: with open(args.value, 'r') as yamlfile: server_state = yaml.load(yamlfile) value = server_state['code'] except yaml.YAMLError as ex: if hasattr(ex, 'problem_mark'): mark = ex.problem_mark print "YAML load error at position (%s:%s)" % (mark.line + 1, mark.column + 1) sys.exit(1) print "%s sends metric [%s] with value [%s] to %s:%d" % ( args.source, args.metric, value, args.server, args.port) statsd_client.gauge(args.metric, int(value)) return 0
def __init__(self, worker_id, manager, redis_connection_params, sleep_time=0.1): self.manager = manager self.statsd_client = StatsClient(host=settings.STATSD_HOST, port=settings.STATSD_PORT, prefix=settings.STATSD_PREFIX) self.redis_connection_params = { k: v for k, v in redis_connection_params.iteritems() if k in ('host', 'db', 'password', 'port') } self.worker_id = None self.continue_working = True self.sleep_time = sleep_time self.child_pid = None self.current_job_id = None self.status = { 'jobs_count': 0, 'cancelled_jobs_count': 0, 'done_jobs_count': 0, 'updated_at': time.time(), 'started_at': time.time() } super(Worker, self).__init__(name="Worker")
def __init__(self): self.prefix = __app__.lower() self.host = os.environ.get("STATSD_HOST", "localhost") try: self.port = int(os.environ.get('STATSD_PORT', 8125)) except ValueError: self.port = 8125 # check that STATSD_HOST is a valid hostname or IPv4 address if self.host != "localhost": try: socket.gethostbyname(self.host) except: try: socket.inet_aton(self.host) except: structured_log(level='error', msg="Invalid value provided for STATSD_HOST. Defaulting to localhost") self.host = "localhost" self.client = StatsClient(host=self.host, port=self.port, prefix=self.prefix) structured_log( level='info', msg="Statsd client configured to export metrics to %s:%s" % (self.host, self.port) )
class User(object): statsd = StatsClient() def __init__(self, datastore): self._name = None self._email = None self._datastore = datastore @statsd.timer('create_user') def create(self, user_id, name, email): try: body = {"name": name, "email": email} self._datastore.create(user_id, body) return "User Created", 201 except KeyError: return "Conflict: User Exists", 409 except: return "Error", 500 @statsd.timer('read_user') def read(self, user_id): try: body = self._datastore.read(user_id) return body, 200 except KeyError: return "Not found", 404 except: return "Error", 500 def update(self, user_id, name, email): pass def delete(self, user_id): pass
def rtl_433_probe(): statsd = StatsClient(host=STATSD_HOST, port=STATSD_PORT, prefix=STATSD_PREFIX) while True: line, addr = sock.recvfrom(1024) try: line = parse_syslog(line) data = json.loads(line) label = sanitize(data["model"]) if "channel" in data: label += ".CH" + str(data["channel"]) if "battery_ok" in data: statsd.gauge(label + '.battery', data["battery_ok"]) if "humidity" in data: statsd.gauge(label + '.humidity', data["humidity"]) statsd.gauge(label + '.temperature', data["temperature_C"]) except KeyError: pass except ValueError: pass
def __init__( self, username: str=None, password: str=None ): logging.config.dictConfig(logging_config.get_logging_conf()) self._logger = structlog.getLogger(__name__) self._logger.addHandler(logging.NullHandler()) # we are now using a retain session to get all missed messages in # case we disconnect self._client = mqtt.Client( clean_session=True, userdata=None, protocol=mqtt.MQTTv311 ) self._client.on_connect = self._on_connect self._client.on_disconnect = self._on_disconnect self._client.on_message = self.on_message self._set_message_callbacks() self._client.username_pw_set( username=str(username), password=str(password) ) # run the connect function self._connect() self._stats_client = StatsClient(host=STATSD_ADDRESS, port=STATSD_PORT) self._logger.info("Local MQTT Client init called")
def main(): log_level = os.getenv("LOG_LEVEL", "warn").upper() logging.basicConfig(level=log_level, format="%(asctime)s %(levelname)s: %(message)s") # initialize collector config add_hostname_suffix = os.getenv("ADD_HOSTNAME_SUFFIX", "false") == "true" config = CollectorConfig( recon_path = os.getenv("SWIFT_RECON", "swift-recon"), dispersion_report_path = os.getenv("SWIFT_DISPERSION_REPORT", "swift-dispersion-report"), add_hostname_suffix = add_hostname_suffix, ) # initialize statsd client statsd = StatsClient( host = os.getenv("STATSD_HOST", "localhost"), port = int(os.getenv("STATSD_PORT", "8125")), ) # run collectors ok = True for collector_class in [SwiftReconCollector, SwiftDispersionCollector]: if not collector_class(config).run(statsd): ok = False if not ok: sys.exit(1)
def spider_opened(self, spider): try: hostname = spider.settings["STATSD_HOSTNAME"] if hostname is None: raise ValueError except (KeyError, ValueError): hostname = socket.gethostname() try: name = spider.name except AttributeError: name = "spider" try: prefix = spider.settings["STATSD_PREFIX"] if prefix is None: raise ValueError self.statsd_prefix = prefix.format(hostname=hostname, name=name) except (AttributeError, ValueError): self.statsd_prefix = "{}.{}.".format(hostname, name) try: host_ip = spider.settings["STATSD_HOST_IP"] if host_ip is None: raise ValueError except (KeyError, ValueError): host_ip = "0.0.0.0" self.statsd_client = StatsClient(host=host_ip, port=8125, prefix=self.statsd_prefix, maxudpsize=512, ipv6=False) spider.logger.info("Initialised statsd client with name {} " "on host {}".format(self.statsd_prefix, host_ip))
def rtl_433_probe(): statsd_host = "127.0.0.1" statsd_port = 8125 statsd_prefix = 'rtlsdr' statsd = StatsClient(host=statsd_host, port=statsd_port, prefix=statsd_prefix) while True: line = sys.stdin.readline() if not line: break try: data = json.loads(line) label = sanitize(data["model"]) if "channel" in data: label += ".CH" + str(data["channel"]) if "battery_ok" in data: statsd.gauge(label + '.battery', data["battery_ok"]) if "humidity" in data: statsd.gauge(label + '.humidity', data["humidity"]) statsd.gauge(label + '.temperature', data["temperature_C"]) except KeyError: pass except ValueError: pass
def get_request_method(request): statsd = StatsClient( host=request.registry.settings['metrics.host'], port=request.registry.settings['metrics.port'], prefix=request.registry.settings.get('metrics.prefix')) route_name = get_route_name(request) return MetricsUtility(statsd=statsd, route_name=route_name)
def make_client(self): if not self.enabled: return DummyStatsdClient(prefix=self.prefix) else: return StatsClient(host=self.host, port=self.port, prefix=self.prefix, ipv6=False)
def main(args): format = "%(asctime)s %(levelname)s %(module)s:%(filename)s:%(lineno)s - %(funcName)20s() %(message)s" level = getattr(logging, args.log_level.upper()) logging.basicConfig(format=format, level=level) assert isinstance(args.stats_prefix, basestring) stats.set_default_client( StatsClient(host="localhost", port=8125, prefix=args.stats_prefix)) run_server(args.host, args.port, Master())
def get_statsd_logger(self): from statsd import StatsClient statsd = StatsClient(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)
def client(self): if self._client is None: if self.protocol is Protocols.udp: self._client = StatsClient(**self.config) else: # self.protocol is Protocols.tcp self._client = TCPStatsClient(**self.config) return self._client
def _udp_client(prefix=None, addr=None, port=None, ipv6=False): if not addr: addr = ADDR[0] if not port: port = ADDR[1] sc = StatsClient(host=addr, port=port, prefix=prefix, ipv6=ipv6) sc._sock = mock.Mock() return sc
def get(cls): if cls.stats_client is None: cls.stats_client = StatsClient(host=settings.STATSD_HOST, port=settings.STATSD_PORT, prefix='%s.smbproxy.%d' % (platform.node(), os.getpid()), maxudpsize=512) return cls.stats_client
def create_client(self, statsd_config: StatsParamConfig): from statsd import StatsClient statsd = StatsClient( host=statsd_config.statsd_host, port=statsd_config.statsd_port, prefix=statsd_config.statsd_prefix, ) self.statsd = statsd
def Pressure_Data_Handler(jsonData): #Parse Data json_Dict = json.loads(jsonData) SensorID = json_Dict['Sensor_ID'] Pressure = json_Dict['Pressure'] Light = json_Dict['Light'] #Push into statsd pres_statsd = StatsClient(host='172.18.138.55', port=8125, prefix='Pressure') pres_statsd.gauge(SensorID, Pressure) print("Inserted Pressure Data into Database.") print("") light_statsd = StatsClient(host='localhost', port=8125, prefix='Light') light_statsd.gauge(SensorID, Light) print("Inserted Light Data into Database.") print("")
class FeesService: name = "fees_service" statsd = StatsClient('statsd-agent', 8125, prefix='simplebank-demo.fees') @event_handler("market_service", "order_placed") @statsd.timer('charge_fee') def charge_fee(self, payload): print("[{}] {} received order_placed event ... charging fee".format( payload, self.name))