class StatsdSinkTests(unittest.TestCase): def setUp(self): self.base = Sink(SinkConfig()) self.base.sink = StatsdSink(StatsdOptions()) self.base.sink.client.gauge = lambda x, y, d=None: (x, y, d) def test_ok_count_no_tag(self): x = self.base.ok_count(3, "srv1", "dc1", None) self.assertTrue(x == ( 'consul.service.dc1.srv1.ok.count', 3, None)) def test_ok_count_tag(self): x = self.base.ok_count(3, "srv1", "dc1", "tag") self.assertTrue(x == ( 'consul.service.dc1.srv1.tag.ok.count', 3, None)) def test_ok_percent_no_tag(self): x = self.base.ok_percent(0.9, "srv1", "dc1", None) self.assertTrue(x == ( 'consul.service.dc1.srv1.ok.percent', 0.9, None)) def test_ok_percent_tag(self): x = self.base.ok_percent(0.9, "srv1", "dc1", "tag") self.assertTrue(x == ( 'consul.service.dc1.srv1.tag.ok.percent', 0.9, None)) def test_critical_percent_no_tag(self): x = self.base.critical_percent(0.9, "srv1", "dc1", None) self.assertTrue(x == ( 'consul.service.dc1.srv1.critical.percent', 0.9, None)) def test_critical_percent_tag(self): x = self.base.critical_percent(0.9, "srv1", "dc1", "tag") self.assertTrue(x == ( 'consul.service.dc1.srv1.tag.critical.percent', 0.9, None)) def test_critical_count_no_tag(self): x = self.base.critical_count(3, "srv1", "dc1", None) self.assertTrue(x == ( 'consul.service.dc1.srv1.critical.count', 3, None)) def test_critical_count_tag(self): x = self.base.critical_count(3, "srv1", "dc1", "tag") self.assertTrue(x == ( 'consul.service.dc1.srv1.tag.critical.count', 3, None)) def test_invalid_sink(self): config = SinkConfig(type="statsd") config.type = "invalid" self.assertRaises(SinkException, Sink, config)
def check_service(check_service_job): """ Check the availability of a consul service and send stats to a Sink. :param check_service_job: Dictionary containing job specification :type check_service_job: dict[str, str, ConsulConfig, SinkConfig] :return: Integer return code :rtype: int """ try: service = check_service_job['service'] tag = check_service_job['tag'] consul_config = check_service_job['consul_config'] sink_config = check_service_job['sink_config'] except KeyError as e: log.error( "check_service | Missing key {e} in check_service_job dict".format( e=e)) raise e consul = Consul(consul_config) sink = Sink(sink_config) dc = consul.get_dc() log.debug('check_service | Service:{service} Tag:{tag} DC:{dc}'.format( service=service, tag=tag, dc=dc)) consul_health_service = consul.get_health_service(service, tag) ok, critical = get_node_status(consul_health_service) sink.ok_count(ok, service, dc, tag) sink.critical_count(critical, service, dc, tag) if ok + critical > 0: sink.ok_percent((ok / (ok + critical)) * 100, service, dc, tag) sink.critical_percent((critical / (ok + critical)) * 100, service, dc, tag) return 0
def setUp(self): self.base = Sink(SinkConfig()) self.base.sink = StatsdSink(StatsdOptions()) self.base.sink.client.gauge = lambda x, y, d=None: (x, y, d)