class TestCsvReporter(TimedTestCase): def setUp(self): super(TestCsvReporter, self).setUp() self.clock = ManualClock() self.path = tempfile.mktemp() self.registry = MetricsRegistry(clock=self.clock) self.maxDiff = None def tearDown(self): super(TestCsvReporter, self).tearDown() if os.path.exists(self.path): shutil.rmtree(self.path) def test_report_now(self): g1 = self.registry.gauge("gauge1") g1.set_value(123) # CSV reporter is extremely simple, doesn't have a metric name or # field name definition, meaning it'll be useless for tracking events that may have several # different values for different fields for the same timestamp # So expected behaviour here is just ignore the events, doubt anyone will find such simple # implementation useful in any case. e1 = self.registry.event("e1") e1.add({"field": 1}) with CsvReporter( registry=self.registry, reporting_interval=1, clock=self.clock, path=self.path, ) as r: r.report_now() output_filename = os.path.join(self.path, "gauge1.csv") output = open(output_filename).read() self.assertEqual(output.splitlines(), ["timestamp\tvalue", "1970-01-01 00:00:00\t123"])
class TestCarbonReporter(TimedTestCase): def setUp(self): super(TestCarbonReporter, self).setUp() self.output = BytesIO() self.registry = MetricsRegistry(clock=self.clock) self.maxDiff = None def connect(self, *args): # part of fake socket interface pass def sendall(self, data): # part of fake socket interface self.output.write(data) def close(self): # part of fake socket interface pass def tearDown(self): super(TestCarbonReporter, self).tearDown() def capture_test_metrics(self): self.clock.now = 1 h1 = self.registry.histogram("hist") for i in range(10): h1.add(2**i) gcb = self.registry.gauge("gcb", lambda: 123) gsimple = self.registry.gauge("gsimple").set_value(42) t1 = self.registry.timer("t1") m1 = self.registry.meter("m1") m1.mark() with t1.time(): c1 = self.registry.counter("c1") c2 = self.registry.counter("counter-2") c1.inc() c2.dec() c2.dec() self.clock.add(1) e1 = self.registry.event("e1") e1.add({"field": 1}) def test_report_now_plain(self): r = CarbonReporter( registry=self.registry, reporting_interval=1, clock=self.clock, socket_factory=lambda: self, ) self.capture_test_metrics() r.report_now() test_data = sorted(self.output.getvalue().decode().splitlines()) expected_data = sorted([ "counter-2.count -2 2", "c1.count 1 2", "gsimple.value 42 2", "e1.field 1 2", "gcb.value 123 2", "t1.1m_rate 0 2", "t1.999_percentile 1 2", "t1.15m_rate 0 2", "t1.99_percentile 1 2", "t1.mean_rate 1.0 2", "t1.95_percentile 1 2", "t1.min 1 2", "t1.50_percentile 1 2", "t1.5m_rate 0 2", "t1.count 1.0 2", "t1.75_percentile 1 2", "t1.std_dev 0.0 2", "t1.max 1 2", "t1.sum 1.0 2", "t1.avg 1.0 2", "hist.count 10.0 2", "hist.999_percentile 512 2", "hist.99_percentile 512 2", "hist.min 1 2", "hist.95_percentile 512 2", "hist.75_percentile 160.0 2", "hist.std_dev 164.94851048466944 2" if PY3 else "hist.std_dev 164.948510485 2", "hist.max 512 2", "hist.avg 102.3 2", "m1.count 1.0 2", "m1.1m_rate 0 2", "m1.15m_rate 0 2", "m1.5m_rate 0 2", "m1.mean_rate 1.0 2", ]) self.assertEqual(test_data, expected_data) def test_report_now_pickle(self): r = CarbonReporter( registry=self.registry, reporting_interval=1, clock=self.clock, socket_factory=lambda: self, pickle_protocol=True, ) self.capture_test_metrics() r.report_now() test_data = sorted(pickle.loads(self.output.getvalue()[4:])) expected_data = sorted([ ("counter-2.count", (2, -2.0)), ("c1.count", (2, 1)), ("gsimple.value", (2, 42.0)), ("gcb.value", (2, 123.0)), ("t1.1m_rate", (2, 0.0)), ("t1.999_percentile", (2, 1)), ("t1.15m_rate", (2, 0.0)), ("t1.99_percentile", (2, 1)), ("t1.mean_rate", (2, 1)), ("t1.95_percentile", (2, 1)), ("t1.min", (2, 1)), ("t1.50_percentile", (2, 1)), ("t1.5m_rate", (2, 0.0)), ("t1.count", (2, 1)), ("t1.75_percentile", (2, 1)), ("t1.std_dev", (2, 0.0)), ("t1.max", (2, 1)), ("t1.sum", (2, 1)), ("t1.avg", (2, 1)), ("hist.count", (2, 10.0)), ("hist.999_percentile", (2, 512.0)), ("hist.99_percentile", (2, 512.0)), ("hist.min", (2, 1)), ("hist.95_percentile", (2, 512.0)), ("hist.75_percentile", (2, 160.0)), ("hist.std_dev", (2, 164.94851048466944)), ("hist.max", (2, 512.0)), ("hist.avg", (2, 102.3)), ("m1.count", (2, 1)), ("m1.1m_rate", (2, 0.0)), ("m1.15m_rate", (2, 0.0)), ("m1.5m_rate", (2, 0.0)), ("m1.mean_rate", (2, 1)), ]) self.assertEqual(test_data, expected_data)
class TestConsoleReporter(TimedTestCase): def setUp(self): super(TestConsoleReporter, self).setUp() self.output = StringIO() self.registry = MetricsRegistry(clock=self.clock) self.maxDiff = None self.clock.now = 0 def tearDown(self): super(TestConsoleReporter, self).tearDown() self.clock.now = 0 def test_report_now(self): r = ConsoleReporter( registry=self.registry, reporting_interval=1, stream=self.output, clock=self.clock, ) h1 = self.registry.histogram("hist") for i in range(10): h1.add(2**i) gcb = self.registry.gauge("gcb", lambda: 123) gsimple = self.registry.gauge("gsimple").set_value(42) t1 = self.registry.timer("t1") m1 = self.registry.meter("m1") m1.mark() e1 = self.registry.event("e1") e1.add({"field": 1}) with t1.time(): c1 = self.registry.counter("c1") c2 = self.registry.counter("counter-2") c1.inc() c2.dec() c2.dec() self.clock.add(1) r.report_now() input_arr = self.output.getvalue().splitlines() input_arr.sort() expected = [ '== 1970-01-01 00:00:01 ===================================', 'c1:', ' count = 1', 'counter-2:', ' count = -2', 'hist:', ' avg = 102.3', ' count = 10.0', ' max = 512', ' min = 1', ' std_dev = 164.94851048466944', ' 75_percentile = 160.0', ' 95_percentile = 512', ' 99_percentile = 512', ' 999_percentile = 512', 'm1:', ' count = 1.0', ' 15m_rate = 0', ' 5m_rate = 0', ' 1m_rate = 0', ' mean_rate = 1.0', 't1:', ' avg = 1.0', ' sum = 1.0', ' count = 1.0', ' max = 1', ' min = 1', ' std_dev = 0.0', ' 15m_rate = 0', ' 5m_rate = 0', ' 1m_rate = 0', ' mean_rate = 1.0', ' 50_percentile = 1', ' 75_percentile = 1', ' 95_percentile = 1', ' 99_percentile = 1', ' 999_percentile = 1', 'gcb:', ' value = 123', 'gsimple:', ' value = 42', '', '== 1970-01-01 00:00:00 ===================================', 'e1:', ' field = 1', '' ] expected.sort() self.assertEqual(input_arr, expected)
class TestSysLogReporter(TimedTestCase): def setUp(self): super(TestSysLogReporter, self).setUp() self.registry = MetricsRegistry(clock=self.clock) self.maxDiff = None self.clock.now = 0 def tearDown(self): super(TestSysLogReporter, self).tearDown() self.clock.now = 0 def test_report_now(self): # connect to a local rsyslog server r = SysLogReporter(registry=self.registry, reporting_interval=1, clock=self.clock) h1 = self.registry.histogram("hist") for i in range(10): h1.add(2**i) gcb = self.registry.gauge("gcb", lambda: 123) gsimple = self.registry.gauge("gsimple").set_value(42) t1 = self.registry.timer("t1") m1 = self.registry.meter("m1") m1.mark() e1 = self.registry.event("e1") e1.add({"field": 1}) with t1.time(): c1 = self.registry.counter("c1") c2 = self.registry.counter("counter-2") c1.inc() c2.dec() c2.dec() self.clock.add(1) with mock.patch( "pyformance.reporters.syslog_reporter.logging.Logger.info" ) as patch: r.report_now() self.assertEqual(patch.call_count, 2) calls = patch.call_args_list expected = ( '{"c1.count": 1, "counter-2.count": -2, ' '"gcb.value": 123, "gsimple.value": 42, ' '"hist.75_percentile": 160.0, "hist.95_percentile": 512, ' '"hist.999_percentile": 512, "hist.99_percentile": 512, ' '"hist.avg": 102.3, "hist.count": 10.0, "hist.max": 512, ' '"hist.min": 1, "hist.std_dev": 164.94851048466944, ' '"m1.15m_rate": 0, "m1.1m_rate": 0, "m1.5m_rate": 0, "m1.count": 1.0, ' '"m1.mean_rate": 1.0, "t1.15m_rate": 0, "t1.1m_rate": 0, ' '"t1.50_percentile": 1, "t1.5m_rate": 0, "t1.75_percentile": 1, ' '"t1.95_percentile": 1, "t1.999_percentile": 1, "t1.99_percentile": 1, ' '"t1.avg": 1.0, "t1.count": 1.0, "t1.max": 1, "t1.mean_rate": 1.0, ' '"t1.min": 1, "t1.std_dev": 0.0, "t1.sum": 1.0, "timestamp": 1}' ) # First call should be event free because events are reported based on submission # rather than report timestamp. And a single line contains a single timestamp. self.assertEqual(calls[0][0][0], expected) # Second call should only be events self.assertEqual(calls[1][0][0], '{"e1.field": 1, "timestamp": 0}')
class RegistryTestCase(TimedTestCase): def setUp(self): super(RegistryTestCase, self).setUp() self.registry = MetricsRegistry(TimedTestCase.clock) def tearDown(self): super(RegistryTestCase, self).tearDown() def test__add(self): self.registry.add("foo", Meter(TimedTestCase.clock)) def test_updating_counter(self): self.registry.counter("test_counter").inc() self.registry.counter("test_counter").inc() self.assertEqual(self.registry.counter("test_counter").get_count(), 2) def test_updating_counter_with_tags(self): self.registry.counter("test_counter", {"weather": "sunny"}).inc() self.registry.counter("test_counter", {"weather": "sunny"}).inc() self.assertEqual( self.registry.counter("test_counter", { "weather": "sunny" }).get_count(), 2) def test_updating_counters_with_same_key_different_tags(self): self.registry.counter("test_counter", { "weather": "sunny", "cloudy": False }).inc() self.registry.counter("test_counter", { "weather": "rainy", "cloudy": True }).inc() self.registry.counter("test_counter", { "cloudy": False, "weather": "sunny" }).inc() self.registry.counter("test_counter", { "cloudy": True, "weather": "rainy" }).inc() self.assertEqual( self.registry.counter("test_counter", { "weather": "sunny", "cloudy": False }).get_count(), 2) self.assertEqual( self.registry.counter("test_counter", { "weather": "rainy", "cloudy": True }).get_count(), 2) def test_get_metrics(self): self.registry.counter("test_counter").inc() self.assertEqual(self.registry.get_metrics("test_counter"), {"count": 1}) self.registry.gauge("test_gauge").set_value(10) self.assertEqual(self.registry.get_metrics("test_gauge"), {"value": 10}) def test_dump_metrics(self): self.registry.counter("test_counter", {"tag1": "val1"}).inc() self.assertEqual(self.registry.dump_metrics(), {"test_counter": { "count": 1 }}) def test_dump_metrics_with_tags(self): self.registry.counter("test_counter", {"tag1": "val1"}).inc() self.assertEqual( self.registry.dump_metrics(key_is_metric=True), {BaseMetric("test_counter", {"tag1": "val1"}): { "count": 1 }}) def test_dump_events(self): self.registry.event("test_event", {"tag1": "val1"}).add({"field": 1}) self.assertEqual( self.registry.dump_metrics(key_is_metric=True), { BaseMetric("test_event", {"tag1": "val1"}): { "events": [EventPoint(time=self.clock.time(), values={"field": 1})] } }) # Make sure the same event is never dumped twice self.assertEqual(self.registry.dump_metrics(key_is_metric=True), {BaseMetric("test_event", {"tag1": "val1"}): {}}) def test_time_calls_with_registry(self): registry = MetricsRegistry() @time_calls(registry=registry, tags={"tag1": "val1"}) def timed_func(): pass timed_func() metric_name = "RegistryTestCase.test_time_calls_with_registry.<locals>.timed_func_calls" stats = registry.get_metrics(key=metric_name, tags={"tag1": "val1"}) print(registry.get_metrics(key=metric_name, tags={"tag1": "val1"})) self.assertEqual(stats["count"], 1) self.assertTrue(stats["mean_rate"]) def test_time_calls(self): @time_calls def timed_func(): pass timed_func() func_timer = timer( "RegistryTestCase.test_time_calls.<locals>.timed_func_calls") self.assertEqual(func_timer.get_count(), 1) self.assertTrue(func_timer.get_mean()) def test_get_qualname(self): def foo(): pass self.assertEqual(get_qualname(foo), "RegistryTestCase.test_get_qualname.<locals>.foo")
class TestOpenTSDBReporter(TimedTestCase): def setUp(self): super(TestOpenTSDBReporter, self).setUp() self.registry = MetricsRegistry(clock=self.clock) self.maxDiff = None def tearDown(self): super(TestOpenTSDBReporter, self).tearDown() def test_report_now(self): r = OpenTSDBReporter( application_name="app", write_key="key", registry=self.registry, reporting_interval=1, clock=self.clock, prefix="prefix.", url="http://opentsdb.com/api/put", ) h1 = self.registry.histogram("hist") for i in range(10): h1.add(2**i) t1 = self.registry.timer("t1") m1 = self.registry.meter("m1") m1.mark() e1 = self.registry.event("e1") e1.add({"field1": 1, "field2": 2}) with t1.time(): c1 = self.registry.counter("c1") c2 = self.registry.counter("counter-2") c1.inc() c2.dec() c2.dec() self.clock.add(1) output = r._collect_metrics(registry=self.registry) self.assertEqual(len(output), 33) for data in output: assert data["metric"].startswith("prefix.") def test_send_request(self): r = OpenTSDBReporter( application_name="app", write_key="key", registry=self.registry, reporting_interval=1, clock=self.clock, prefix="prefix.", url="http://opentsdb.com/api/put", ) h1 = self.registry.histogram("hist") for i in range(10): h1.add(2**i) t1 = self.registry.timer("t1") m1 = self.registry.meter("m1") m1.mark() with t1.time(): c1 = self.registry.counter("c1") c2 = self.registry.counter("counter-2") c1.inc() c2.dec() c2.dec() self.clock.add(1) with mock.patch("pyformance.reporters.opentsdb_reporter.urllib.urlopen" ) as patch: r.report_now() patch.assert_called()
class TestInfluxReporter(TimedTestCase): def setUp(self): super(TestInfluxReporter, self).setUp() self.registry = MetricsRegistry() def tearDown(self): super(TestInfluxReporter, self).tearDown() def test_not_called_on_blank(self): influx_reporter = InfluxReporter(registry=self.registry) with mock.patch("pyformance.reporters.influx.urlopen") as patch: influx_reporter.report_now() patch.assert_not_called() def test_create_database(self): r1 = InfluxReporter(registry=self.registry, autocreate_database=True) with mock.patch("pyformance.reporters.influx.urlopen") as patch: r1.report_now() if patch.call_count != 1: raise AssertionError( "Expected 1 calls to 'urlopen'. Received: {}".format( patch.call_count)) def test_gauge_without_tags(self): self.registry.gauge("cpu").set_value(65) influx_reporter = InfluxReporter(registry=self.registry, clock=self.clock, autocreate_database=False) with mock.patch.object(influx_reporter, "_try_send") as send_mock: influx_reporter.report_now() expected_url = "http://127.0.0.1:8086/write?db=metrics&precision=s" expected_data = "cpu value=65 " + self.clock.time_string() send_mock.assert_called_once_with(expected_url, expected_data) def test_gauge_with_tags(self): tags = {"region": "us - west"} self.registry.gauge(key="cpu", tags=tags).set_value(65) influx_reporter = InfluxReporter(registry=self.registry, clock=self.clock, autocreate_database=False) with mock.patch.object(influx_reporter, "_try_send") as send_mock: influx_reporter.report_now() expected_url = "http://127.0.0.1:8086/write?db=metrics&precision=s" expected_data = "cpu,region=us\\ -\\ west value=65 " + \ self.clock.time_string() send_mock.assert_called_once_with(expected_url, expected_data) def test_gauge_with_global_tags(self): tags = {"region": "us-west-2"} self.registry.gauge(key="cpu", tags=tags).set_value(65) influx_reporter = InfluxReporter(registry=self.registry, clock=self.clock, autocreate_database=False, global_tags={ "stage": "dev", "region": "override" }) with mock.patch.object(influx_reporter, "_try_send") as send_mock: influx_reporter.report_now() expected_url = "http://127.0.0.1:8086/write?db=metrics&precision=s" expected_data = "cpu,stage=dev,region=us-west-2 value=65 " + \ self.clock.time_string() send_mock.assert_called_once_with(expected_url, expected_data) def test_counter_with_tags(self): tags = {"host": "server1"} counter = self.registry.counter(key="cpu", tags=tags) for i in range(5): counter.inc(1) influx_reporter = InfluxReporter(registry=self.registry, clock=self.clock, autocreate_database=False) with mock.patch.object(influx_reporter, "_try_send") as send_mock: influx_reporter.report_now() expected_url = "http://127.0.0.1:8086/write?db=metrics&precision=s" expected_data = "cpu,host=server1 count=5 " + \ self.clock.time_string() send_mock.assert_called_once_with(expected_url, expected_data) def test_events_with_tags(self): tags = {"host": "server1"} self.registry._clock = self.clock event = self.registry.event(key="event", tags=tags) event.add({"field": 1, "float": 0.12, "int": MarkInt(1)}) influx_reporter = InfluxReporter(registry=self.registry, clock=self.clock, autocreate_database=False) with mock.patch.object(influx_reporter, "_try_send") as send_mock: influx_reporter.report_now() expected_url = "http://127.0.0.1:8086/write?db=metrics&precision=s" expected_data = "event,host=server1 field=1,float=0.12,int=1i " + \ self.clock.time_string() send_mock.assert_called_once_with(expected_url, expected_data) def test_combined_events_with_counter(self): tags = {"host": "server1"} self.registry._clock = self.clock event = self.registry.event(key="event", tags=tags) event.add({"field": 1}) counter = self.registry.counter("event", tags=tags) counter.inc(5) influx_reporter = InfluxReporter(registry=self.registry, clock=self.clock, autocreate_database=False) with mock.patch.object(influx_reporter, "_try_send") as send_mock: influx_reporter.report_now() expected_url = "http://127.0.0.1:8086/write?db=metrics&precision=s" expected_data = [ "event,host=server1 count=5 " + self.clock.time_string(), "event,host=server1 field=1 " + self.clock.time_string() ] send_mock.assert_called_once_with(expected_url, "\n".join(expected_data)) def test_count_calls_with_tags(self): tags = {"host": "server1"} counter = self.registry.counter(key="cpu", tags=tags) for i in range(5): counter.inc(1) influx_reporter = InfluxReporter(registry=self.registry, clock=self.clock, autocreate_database=False) with mock.patch.object(influx_reporter, "_try_send") as send_mock: influx_reporter.report_now() expected_url = "http://127.0.0.1:8086/write?db=metrics&precision=s" expected_data = "cpu,host=server1 count=5 " + \ self.clock.time_string() send_mock.assert_called_once_with(expected_url, expected_data) def test__format_tag_value(self): self.assertEqual(_format_tag_value("no_special_chars"), "no_special_chars") self.assertEqual(_format_tag_value("has space"), "has\\ space") self.assertEqual(_format_tag_value("has,comma"), "has\\,comma") self.assertEqual(_format_tag_value("has=equals"), "has\\=equals")