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() 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() 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}' patch.assert_called_with(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 logal 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() 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() 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.94851048466947, "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}' patch.assert_called_with(expected)
class TestCarbonReporter(TimedTestCase): def setUp(self): super(TestCarbonReporter, self).setUp() self.output = StringIO() 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 test_report_now(self): r = CarbonReporter(registry=self.registry, reporting_interval=1, clock=self.clock, socket_factory=lambda: self) 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) r.report_now() self.assertEqual(self.output.getvalue().splitlines(), [ 'counter-2.count -2 1', 'gsimple.value 42 1', 'gcb.value 123 1', 't1.1m_rate 0 1', 't1.999_percentile 1 1', 't1.15m_rate 0 1', 't1.99_percentile 1 1', 't1.mean_rate 1.0 1', 't1.95_percentile 1 1', 't1.min 1 1', 't1.5m_rate 0 1', 't1.count 1.0 1', 't1.75_percentile 1 1', 't1.std_dev 0.0 1', 't1.max 1 1', 't1.avg 1.0 1', 'hist.count 10.0 1', 'hist.999_percentile 512 1', 'hist.99_percentile 512 1', 'hist.min 1 1', 'hist.95_percentile 512 1', 'hist.75_percentile 160.0 1', 'hist.std_dev 164.948510485 1', 'hist.max 512 1', 'hist.avg 102.3 1', 'm1.1m_rate 0 1', 'm1.15m_rate 0 1', 'm1.5m_rate 0 1', 'm1.mean_rate 1.0 1', 'c1.count 1 1' ])
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() 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() self.assertEqual(self.output.getvalue().splitlines(), [ '== 1970-01-01 00:00:01 ===================================', 'counter-2:', ' count = -2', 'gsimple:', ' value = 42', 'gcb:', ' value = 123', 't1:', ' 1m_rate = 0', ' 999_percentile = 1', ' 15m_rate = 0', ' 99_percentile = 1', ' mean_rate = 1.0', ' 95_percentile = 1', ' min = 1', ' 5m_rate = 0', ' count = 1.0', ' 75_percentile = 1', ' std_dev = 0.0', ' max = 1', ' avg = 1.0', 'hist:', ' count = 10.0', ' 999_percentile = 512', ' 99_percentile = 512', ' min = 1', ' 95_percentile = 512', ' 75_percentile = 160.0', ' std_dev = 164.948510485', ' max = 512', ' avg = 102.3', 'm1:', ' 1m_rate = 0', ' 15m_rate = 0', ' 5m_rate = 0', ' mean_rate = 1.0', 'c1:', ' count = 1', '' ])
class TestCsvReporter(TimedTestCase): def setUp(self): super(TestCsvReporter, self).setUp() 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) r = CsvReporter( registry=self.registry, reporting_interval=1, clock=self.clock, path=self.path) 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 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) 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'])
def report_metrics(host, server, token): reg = MetricsRegistry() wf_proxy_reporter = WavefrontProxyReporter( host=host, port=2878, registry=reg, source="wavefront-pyformance-example", tags={ "key1": "val1", "key2": "val2" }, prefix="python.proxy.") wf_direct_reporter = WavefrontDirectReporter( server=server, token=token, registry=reg, source="wavefront-pyformance-exmaple", tags={ "key1": "val1", "key2": "val2" }, prefix="python.direct.") # counter c1 = reg.counter("foo_count") c1.inc() # delta counter d1 = delta.delta_counter(reg, "foo_delta_count") d1.inc() d1.inc() # gauge g1 = reg.gauge("foo_gauge") g1.set_value(2) # meter m1 = reg.meter("foo_meter") m1.mark() # timer t1 = reg.timer("foo_timer") timer_ctx = t1.time() time.sleep(3) timer_ctx.stop() # histogram h1 = reg.histogram("foo_histogram") h1.add(1.0) h1.add(1.5) wf_proxy_reporter.report_now() wf_proxy_reporter.stop() wf_direct_reporter.report_now()
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 TestNewRelicReporter(TimedTestCase): def setUp(self): super(TestNewRelicReporter, self).setUp() self.registry = MetricsRegistry(clock=self.clock, sink=NewRelicSink) self.maxDiff = None def tearDown(self): super(TestNewRelicReporter, self).tearDown() def test_report_now(self): r = NewRelicReporter('license_key', registry=self.registry, reporting_interval=1, clock=self.clock, name='foo') h1 = self.registry.histogram("hist", 'a/b') for i in range(10): h1.add(2**i) t1 = self.registry.timer("t1") gauge = self.registry.gauge('g1', SimpleGauge(unit='g')) gauge_value = 10 gauge.set_value(gauge_value) m = self.registry.meter('m1', 'u1') m.mark() with t1.time(): m.mark() c1 = self.registry.counter("counter-1", 'c') c2 = self.registry.counter("counter-2", 'c') c1.inc() c2.dec() c2.dec() self.clock.add(1) m.mark() output = json.loads(r.collect_metrics(self.registry)) expected = { "agent": { "host": socket.gethostname(), "pid": os.getpid(), "version": "0.3.3" }, "components": [{ "duration": 1, "guid": "com.github.pyformance", "metrics": { "Component/hist/raw": { "max": 512, "total": 1023, "min": 1, "count": 10, "sum_of_squares": 349525 }, "Component/t1/95_percentile": 1., "Component/hist/999_percentile": 512, "Component/counter-2/raw": { "total": -3, "max": -1, "count": 2, "sum_of_squares": 5, "min": -2 }, "Component/t1/mean_rate": { "max": 1, "count": 1, "total": 1., "min": 1, "sum_of_squares": 1. }, "Component/t1/999_percentile": 1., "Component/m1/1m_rate": { "min": 0, "total": 0, "max": 0, "sum_of_squares": 0, "count": 0 }, "Component/t1/15m_rate": { "total": 0, "min": 0, "max": 0, "sum_of_squares": 0, "count": 0 }, "Component/hist/99_percentile": 512, "Component/t1/raw": { "min": 1., "count": 1, "sum_of_squares": 1., "max": 1., "total": 1. }, "Component/m1/mean_rate": { "sum_of_squares": 3., "count": 3, "total": 3., "min": 1, "max": 1 }, "Component/hist/std_dev": 164.94851048466947, "Component/counter-1/raw": { "count": 1, "sum_of_squares": 1, "min": 1, "max": 1, "total": 1 }, "Component/t1/50_percentile": 1., "Component/t1/99_percentile": 1., "Component/hist/95_percentile": 512, "Component/m1/15m_rate": { "count": 0, "sum_of_squares": 0, "max": 0, "min": 0, "total": 0 }, "Component/hist/75_percentile": 160., "Component/t1/5m_rate": { "count": 0, "min": 0, "total": 0, "sum_of_squares": 0, "max": 0 }, "Component/hist/mean_rate": { "count": 103, "max": 1, "sum_of_squares": 102.09, "min": 0.29999999999999716, "total": 102.3 }, "Component/t1/count": 1., "Component/g1/gauge": 10, "Component/t1/1m_rate": { "count": 0, "total": 0, "min": 0, "max": 0, "sum_of_squares": 0 }, "Component/t1/75_percentile": 1., "Component/t1/std_dev": 0., "Component/m1/raw": { "min": 1, "sum_of_squares": 3, "count": 3, "total": 3, "max": 1 }, "Component/m1/5m_rate": { "count": 0, "min": 0, "total": 0, "sum_of_squares": 0, "max": 0 } }, "name": "foo" }] } self.assertEqual(json.loads(json.dumps(expected)), json.loads(json.dumps(output)))
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) 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', '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.50_percentile 24.0 2', 'hist.std_dev 164.94851048466947 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.50_percentile', (2, 24.0)), ('hist.std_dev', (2, 164.94851048466947)), ('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 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) 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", "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 TestInfluxReporter(TimedTestCase): def setUp(self): super(TestInfluxReporter, self).setUp() self.registry = MetricsRegistry() def tearDown(self): super(TestInfluxReporter, self).tearDown() def test_report_now(self): influx_reporter = InfluxReporter(registry=self.registry) with mock.patch("pyformance.reporters.influx.urlopen") as patch: influx_reporter.report_now() patch.assert_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 != 2: raise AssertionError( "Expected 2 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", wraps=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", wraps=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_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", wraps=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_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", wraps=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")
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")
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() 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() self.assertEqual( self.output.getvalue().splitlines().sort(), [ "== 1970-01-01 00:00:01 ===================================", "counter-2:", " count = -2", "gsimple:", " value = 42", "gcb:", " value = 123", "t1:", " 1m_rate = 0", " 999_percentile = 1", " 15m_rate = 0", " 99_percentile = 1", " mean_rate = 1.0", " 95_percentile = 1", " min = 1", " 5m_rate = 0", " count = 1.0", " 75_percentile = 1", " std_dev = 0.0", " max = 1", " avg = 1.0", "hist:", " count = 10.0", " 999_percentile = 512", " 99_percentile = 512", " min = 1", " 95_percentile = 512", " 75_percentile = 160.0", " std_dev = 164.94851048466944", " max = 512", " avg = 102.3", "m1:", " count = 1.0", " 1m_rate = 0", " 15m_rate = 0", " 5m_rate = 0", " mean_rate = 1.0", "c1:", " count = 1", "", ].sort(), )
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_time_calls_with_registry(self): registry = MetricsRegistry() @time_calls(registry=registry, tags={"tag1": "val1"}) def timed_func(): pass timed_func() stats = registry.get_metrics(key="timed_func_calls", tags={"tag1": "val1"}) print(registry.get_metrics(key="timed_func_calls", 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("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), "foo")
class TestCarbonReporter(TimedTestCase): def setUp(self): super(TestCarbonReporter, self).setUp() self.output = StringIO() 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 test_report_now(self): r = CarbonReporter( registry=self.registry, reporting_interval=1, clock=self.clock, socket_factory=lambda: self) 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) r.report_now() self.assertEqual(self.output.getvalue().splitlines(), [ 'counter-2.count -2 1', 'gsimple.value 42 1', 'gcb.value 123 1', 't1.1m_rate 0 1', 't1.999_percentile 1 1', 't1.15m_rate 0 1', 't1.99_percentile 1 1', 't1.mean_rate 1.0 1', 't1.95_percentile 1 1', 't1.min 1 1', 't1.5m_rate 0 1', 't1.count 1.0 1', 't1.75_percentile 1 1', 't1.std_dev 0.0 1', 't1.max 1 1', 't1.avg 1.0 1', 'hist.count 10.0 1', 'hist.999_percentile 512 1', 'hist.99_percentile 512 1', 'hist.min 1 1', 'hist.95_percentile 512 1', 'hist.75_percentile 160.0 1', 'hist.std_dev 164.948510485 1', 'hist.max 512 1', 'hist.avg 102.3 1', 'm1.1m_rate 0 1', 'm1.15m_rate 0 1', 'm1.5m_rate 0 1', 'm1.mean_rate 1.0 1', 'c1.count 1 1' ])
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() 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() self.assertEqual(self.output.getvalue().splitlines(), [ '== 1970-01-01 00:00:01 ===================================', 'counter-2:', ' count = -2', 'gsimple:', ' value = 42', 'gcb:', ' value = 123', 't1:', ' 1m_rate = 0', ' 999_percentile = 1', ' 15m_rate = 0', ' 99_percentile = 1', ' mean_rate = 1.0', ' 95_percentile = 1', ' min = 1', ' 5m_rate = 0', ' count = 1.0', ' 75_percentile = 1', ' std_dev = 0.0', ' max = 1', ' avg = 1.0', 'hist:', ' count = 10.0', ' 999_percentile = 512', ' 99_percentile = 512', ' min = 1', ' 95_percentile = 512', ' 75_percentile = 160.0', ' std_dev = 164.948510485', ' max = 512', ' avg = 102.3', 'm1:', ' 1m_rate = 0', ' 15m_rate = 0', ' 5m_rate = 0', ' mean_rate = 1.0', 'c1:', ' count = 1', ''])
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 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}')