Skip to content

Commit 5fadebc

Browse files
committed
basic udp+influx+victoria tests
1 parent fd11daa commit 5fadebc

File tree

7 files changed

+180
-9
lines changed

7 files changed

+180
-9
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
*.egg-info
1313
__pycache__
1414
.vscode*
15+
.coverage
16+
coverage.xml
1517

1618
!.gitkeep
1719
!/.gitignore

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# prometheus-push-client
22

33
![GitHub Workflow Status](https://img.shields.io/github/workflow/status/gistart/prometheus-push-client/test-all)
4+
![Codecov](https://img.shields.io/codecov/c/github/gistart/prometheus-push-client)
45

56
## Default labelvalues
67

prometheus_push_client/clients/bg.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import threading
33
import time
44

5+
from prometheus_push_client import compat
6+
57

68
class BackgroundClient:
79

@@ -56,7 +58,7 @@ class AsyncioBGClient(BackgroundClient):
5658
async def start(self):
5759
self.stop_event = asyncio.Event()
5860
await self.transport.start()
59-
self._runner = asyncio.create_task(self.run())
61+
self._runner = compat.create_task(self.run())
6062

6163
async def stop(self):
6264
self.stop_event.set()

prometheus_push_client/compat.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,12 @@
22

33
if hasattr(asyncio, "get_running_loop"):
44
get_running_loop = asyncio.get_running_loop
5-
else: # < 3.7, may be unsafe outside coroutines
5+
else: # < 3.7, may be unsafe outside coroutines (running loop)
66
get_running_loop = asyncio.get_event_loop
7+
8+
9+
if hasattr(asyncio, "create_task"):
10+
create_task = asyncio.create_task
11+
else: # < 3.7, totally unsafe outside loop
12+
def create_task(coro):
13+
return asyncio.get_event_loop().create_task(coro)

test/test_offline/test_influx.py

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import prometheus_push_client as ppc
44

5-
from testutils import make_metric_fixture, collect_metrics
5+
from testutils import make_metric_fixture, collect_formatter
66

77

88
NS = "test_offline"
@@ -13,7 +13,12 @@
1313
def sum1(request):
1414
return make_metric_fixture(
1515
request,
16-
ppc.Summary(name="s1", namespace=NS, subsystem=SUB)
16+
ppc.Summary(
17+
name="s1",
18+
namespace=NS,
19+
subsystem=SUB,
20+
labelnames=["l1", "l2"]
21+
)
1722
)
1823

1924

@@ -29,13 +34,46 @@ def counter1(request):
2934
def histogram1(request):
3035
return make_metric_fixture(
3136
request,
32-
ppc.Histogram(name="h1", namespace=NS, subsystem=SUB, buckets=[1,2,float("inf")])
37+
ppc.Histogram(
38+
name="h1",
39+
namespace=NS,
40+
subsystem=SUB,
41+
buckets=[1, 2, float("inf")],
42+
labelnames=["l1"],
43+
)
3344
)
3445

3546

36-
37-
def test_influx_formatter(sum1, counter1, histogram1):
38-
pass
47+
@pytest.fixture
48+
def influx_format_expected():
49+
return (""
50+
"""
51+
test_offline_influx_s1,l1=1,l2=2 count=2.0
52+
test_offline_influx_s1,l1=1,l2=2 sum=10.0
53+
test_offline_influx_s1,l1=10,l2=20 count=1.0
54+
test_offline_influx_s1,l1=10,l2=20 sum=10.0
55+
test_offline_influx_c1 total=10.0
56+
test_offline_influx_h1,l1=1,le=1.0 bucket=1.0
57+
test_offline_influx_h1,l1=1,le=2.0 bucket=2.0
58+
test_offline_influx_h1,l1=1,le=+Inf bucket=3.0
59+
test_offline_influx_h1,l1=1 count=3.0
60+
test_offline_influx_h1,l1=1 sum=4.5
61+
""").strip()
62+
63+
64+
def test_influx_formatter(sum1, counter1, histogram1, influx_format_expected):
65+
sum1.labels(1, 2).observe(5)
66+
sum1.labels(l1=1, l2=2).observe(5)
67+
sum1.labels(l1=10, l2=20).observe(10)
68+
69+
counter1.inc(10)
70+
71+
histogram1.labels(l1=1).observe(0.5)
72+
histogram1.labels(l1=1).observe(1.5)
73+
histogram1.labels(l1=1).observe(2.5)
74+
75+
data = collect_formatter(ppc.InfluxFormat)
76+
assert data == influx_format_expected
3977

4078

4179
@pytest.fixture

test/test_online/test_victoria.py

Lines changed: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,112 @@
11
import pytest
2+
import json
3+
import time
4+
import math
5+
import asyncio
26
import requests
7+
from collections import Counter
8+
9+
import prometheus_push_client as ppc
10+
11+
from testutils import make_metric_fixture
12+
13+
14+
NS = "test_vic"
15+
16+
17+
def export(cfg, pattern=NS):
18+
response = requests.get(
19+
cfg.vm_export_url,
20+
params={'match[]': r'{__name__=~"%s.*"}' % pattern}
21+
)
22+
23+
found = []
24+
for line in response.iter_lines():
25+
metric = json.loads(line)
26+
found.append(metric)
27+
return found
28+
29+
30+
def count_samples(found, pattern):
31+
n_samples = Counter()
32+
for metric in found:
33+
mname = metric["metric"]["__name__"]
34+
if mname.startswith(pattern):
35+
n_samples[mname] += len(metric["values"])
36+
return n_samples
37+
338

439

540
def test_ping(cfg):
641
response = requests.get(cfg.vm_ping_url)
742
content = response.text
843

944
assert response.status_code == 200, content
10-
assert content == "OK", content
45+
assert content == "OK", content
46+
47+
48+
@pytest.fixture
49+
def counter1(request):
50+
return make_metric_fixture(
51+
request,
52+
ppc.Counter("c1", namespace=NS, subsystem="influx_udp")
53+
)
54+
55+
56+
def test_vic_influx_udp_thread(cfg, counter1):
57+
58+
found_before = export(cfg)
59+
count_before = count_samples(found_before, counter1._name)
60+
61+
period = 0.4
62+
sleeps = 2
63+
sleep_sec = 1.0
64+
samples_expected = math.ceil(sleeps / period) # + graceful one
65+
66+
@ppc.influx_udp_thread(cfg.vm_host, cfg.vm_influx_port, period=period)
67+
def _test():
68+
for _ in range(sleeps):
69+
counter1.inc()
70+
time.sleep(sleep_sec)
71+
return 1 / 0 # testing graceful stop
72+
73+
with pytest.raises(ZeroDivisionError):
74+
_test()
75+
76+
time.sleep(2.0) # let them sync
77+
78+
found_after = export(cfg)
79+
count_after = count_samples(found_after, counter1._name)
80+
81+
for k in count_after.keys():
82+
assert samples_expected == count_after[k] - count_before[k]
83+
84+
85+
@pytest.mark.asyncio
86+
async def test_vic_influx_udp_async(cfg, counter1):
87+
88+
found_before = export(cfg)
89+
count_before = count_samples(found_before, counter1._name)
90+
91+
period = 0.4
92+
sleeps = 2
93+
sleep_sec = 1.0
94+
samples_expected = math.ceil(sleeps / period) # + graceful one
95+
96+
@ppc.influx_udp_async(cfg.vm_host, cfg.vm_influx_port, period=period)
97+
async def _test():
98+
for _ in range(sleeps):
99+
counter1.inc()
100+
await asyncio.sleep(sleep_sec)
101+
return 1 / 0 # testing graceful stop
102+
103+
with pytest.raises(ZeroDivisionError):
104+
await _test()
105+
106+
await asyncio.sleep(2.0) # let them sync
107+
108+
found_after = export(cfg)
109+
count_after = count_samples(found_after, counter1._name)
110+
111+
for k in count_after.keys():
112+
assert samples_expected == count_after[k] - count_before[k]

test/testutils.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,22 @@ def only_interesting(line):
2929

3030
interesting_lines = filter(only_interesting, all_data.split("\n"))
3131
return "\n".join(interesting_lines)
32+
33+
34+
def collect_formatter(formatter_cls, *metric_names):
35+
fmt = formatter_cls()
36+
37+
collected = []
38+
for line in fmt.iter_samples():
39+
line = line.decode()
40+
41+
if metric_names:
42+
for mnane in metric_names:
43+
if line.startswith(mname):
44+
collected.append(line)
45+
break
46+
else:
47+
collected.append(line)
48+
49+
collected = filter(lambda line: " created=" not in line, collected)
50+
return "\n".join(collected)

0 commit comments

Comments
 (0)