def test_pushgateway_http_sync(cfg, summ1, gauge1): period = 0.4 sleeps = 2 sleep_sec = 1.0 @ppc.openmetrics_http_thread(f"{cfg.pushgw_url}/{JOB}", period=period) def _test(): for _ in range(sleeps): with gauge1.labels("L1").track_inprogress(): with summ1.labels("L2").time(): time.sleep(sleep_sec) return 1 / 0 # testing graceful stop with pytest.raises(ZeroDivisionError): _test() time.sleep(1.0) # let them sync local_res = collect_metrics(summ1._name, gauge1._name) local_res_lines = sorted(local_res.split("\n")) remote_all = get_metrics(cfg) remote_res = collect_metrics(summ1._name, gauge1._name, data=remote_all) remote_res_lines = [] for rem_line in sorted(remote_res.split("\n")): rem_line = RX_INSTANCE_TAG.sub("", rem_line) rem_line = RX_JOB_TAG.sub("", rem_line) remote_res_lines.append(rem_line) assert len(local_res_lines) == len(remote_res_lines), remote_res for loc, rem in zip(local_res_lines, remote_res_lines): assert (loc == rem or loc.startswith(f"{rem}.") # pushgw-side float -> int )
async def test_statsd_udp_asyncio(cfg, hist1, gauge1): for i in range(1, 251): hist1.labels(l1=1).observe(i / 100) period = 0.3 sleeps = 2 sleep_sec = 1.1 async def _test(): for i in range(1, sleeps + 1): hist1.labels(l1=2).observe(i / 10) await asyncio.sleep(sleep_sec) gauge1.set(i) return 1 / 0 with pytest.raises(ZeroDivisionError): async with ppc.statsd_udp_async(cfg.statsd_host, cfg.statsd_udp_port, period): await _test() await asyncio.sleep(2.0) # wait remote sync local_res = collect_metrics(hist1._name, gauge1._name) local_res_lines = sorted(local_res.split("\n")) remote_all = get_metrics(cfg) remote_res = collect_metrics(hist1._name, gauge1._name, data=remote_all) remote_res_lines = sorted(remote_res.split("\n")) assert len(local_res_lines) == len(remote_res_lines), remote_res for loc, rem in zip(local_res_lines, remote_res_lines): assert (loc == rem or loc.startswith(f"{rem}.") # statsd-side float -> int )
def test_statsd_udp_stream(cfg, hist1, gauge1): for i in range(1, 51): hist1.labels(l1=1).observe(i / 100) def _test(): for i in range(1, 31): hist1.labels(l1=2).observe(i / 10) gauge1.set(i) return 1 / 0 with pytest.raises(ZeroDivisionError): with ppc.statsd_udp_stream(cfg.statsd_host, cfg.statsd_udp_port): _test() time.sleep(2.0) # wait remote sync local_res = collect_metrics(hist1._name, gauge1._name) local_res_lines = sorted(local_res.split("\n")) remote_all = get_metrics(cfg) remote_res = collect_metrics(hist1._name, gauge1._name, data=remote_all) remote_res_lines = sorted(remote_res.split("\n")) assert len(local_res_lines) == len(remote_res_lines), remote_res for loc, rem in zip(local_res_lines, remote_res_lines): assert (loc == rem or loc.startswith(f"{rem}.") # statsd-side float -> int )
def test_openmetrics_format(hist1): hist1.labels(l1="Moscow", l2="today").observe(5) # (( hist1.labels(l1=r"a\b", l2='purpose: "escape test"').observe(25) native_result = collect_metrics(hist1._name) fmt_result = collect_formatter(ppc.OpenMetricsFormat, hist1._name) assert native_result == fmt_result
def test_default_labelvalues_usage(counter1, test_defaults_expected): # pairs of equivalent actions: counter1.labels(l2=2).inc() counter1.labels(2).inc() counter1.labels(l1=1, l2=2).inc() counter1.labels(1, 2).inc() counter1.labels(host="H", l1=1, l2=2).inc() counter1.labels("H", 1, 2).inc() res = collect_metrics(counter1._name) assert res == test_defaults_expected