Skip to content

Commit e55a78e

Browse files
committed
tests for streaming clients and decorator/context wrappers
1 parent 48cc26f commit e55a78e

File tree

14 files changed

+203
-52
lines changed

14 files changed

+203
-52
lines changed

.github/workflows/tests.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ jobs:
1717
python-version: [3.6, 3.7, 3.8, 3.9, 'pypy3']
1818
services:
1919
victoria:
20-
image: victoriametrics/victoria-metrics:v1.59.0
20+
image: gistart/victoria-metrics:v1.59.0
2121
env:
22-
httpListenAddr: 8429
23-
influxListenAddr: 8491
22+
httpListenAddr: :8428
23+
influxListenAddr: :8491
2424
ports:
2525
- 8428:8428
2626
- 8491:8491

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ __pycache__
1515
.coverage
1616
coverage.xml
1717
test/poke.py
18+
build/*
19+
dist/*
1820
compare/*
1921

2022
!.gitkeep

README.md

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ pip install prometheus-push-client
1717

1818
## Metrics
1919

20-
This library uses `prometheus-client` metric implementation, but adds with some minor tweaks.
20+
This library uses `prometheus-client` metric implementation, but adds some minor tweaks.
2121

2222
### Separate registry
2323

2424
New metric constructors use separate `PUSH_REGISTRY` as a default, not to interfere with other metrics already defined and monitored in existing projects.
2525

2626
### Default labelvalues
2727

28-
With regular prometheus_client, defaults may be defined for either _none_ or _all_ the labels (with `labelvalues`), but that's not enough (and sometimes doesn't work?).
28+
With regular prometheus_client, defaults may be defined for either _none_ or _all_ the labels (with `labelvalues`), but that's not enough. Moreover `labelvalues` sometimes doesn't work as expected.
2929

3030
We probably want to define _some_ defaults, like `hostname`, or more importantly, **if we use VictoriaMetrics cluster**, we always need to push label `VictoriaMetrics_AccountID=<int>` (usually 1) or else our metrics will be ignored.
3131

@@ -63,11 +63,11 @@ To avoid that we'll have to properly isolate each task's metrics, which can be i
6363

6464
Batch clients spawn synchronization jobs "in background" (meaning in a thread or asyncio task) to periodically send all metrics from `ppc.PUSH_REGISTRY` to the destination.
6565

66-
Clients will attempt to stop gracefully, synchronizing registry "one last time" after job exits or crashes. Sometimes this _may_ mess up Grafana sampling, but the worst picture I could atrifically create looks like this:
66+
Clients will attempt to stop gracefully, synchronizing registry "one last time" after job exits or crashes. Sometimes this _may_ mess up Grafana sampling, but the worst picture I could artifically create looks like this:
6767

6868
![graceful push effect](./docs/img/graceful_stop_effect01.png)
6969

70-
Best way to use them is via decorators. These clients are intended to be used with long running, but finite tasks, which could be spawned anywhere, therefor not easily accessible by the scraper. If that's not the case -- just use "passive mode" w/ the scraper instead.
70+
Best way to use them is via decorators / context managers. These clients are intended to be used with long running, but finite tasks, which could be spawned anywhere, therefor not easily accessible by the scraper. If that's not the case -- just use "passive mode" w/ the scraper instead.
7171

7272
``` python
7373
def influx_udp_async(host, port, period=15.0):
@@ -97,10 +97,27 @@ req_hist = ppc.Histogram(
9797
async def main(urls):
9898
# the job ...
9999
req_hist.labels(gethostname(url)).observe(response.elapsed)
100-
# ...
100+
101+
# OR
102+
103+
async def main(urls):
104+
async with ppc.influx_udp_async("victoria.acme.inc.net", 9876, period=15):
105+
# the job ...
106+
req_hist.labels(gethostname(url)).observe(response.elapsed)
101107
```
102108

103109

104-
## Streming clients
110+
## Streaming clients
111+
112+
If for some reason every metric change needs to be synced, UDP streaming clients are implemented in this library.
113+
114+
```python
115+
def influx_udp_aiostream(host, port):
116+
def influx_udp_stream(host, port):
117+
def statsd_udp_aiostream(host, port):
118+
def statsd_udp_stream(host, port):
119+
```
120+
121+
Usage is completely identical to batch clients' decorators / context managers.
105122

106-
:warning: histogram.obsrever doesn't work, fix later
123+
:warning: Histogram and Summary `.time() decorator` doesn't work in this mode atm, because it can't be easily monkey-patched.

prometheus_push_client/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from .formats.influx import InfluxFormat
1818
from .formats.statsd import StatsdFormat
1919
from .formats.openmetrics import OpenMetricsFormat
20-
from .metrics import Counter, Gauge, Summary, Histogram, Info, Enum
20+
from .metrics import Counter, Gauge, Summary, Histogram
2121
from .transports.http import SyncHttpTransport, AioHttpTransport
2222
from .transports.udp import SyncUdpTransport, AioUdpTransport
2323
from .version import __version__

prometheus_push_client/clients/batch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def __init__(self, format, transport, period=15.0, *args, **kwargs):
1616
self.period = float(period)
1717
self.stop_event = None
1818

19-
self._period_step = 0.5 # check event every 0.5 seconds
19+
self._period_step = 0.25 # check event every 0.25 seconds
2020

2121
super().__init__(*args, **kwargs)
2222

prometheus_push_client/decorators.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ def statsd_udp_stream(host, port, registry=ppc.PUSH_REGISTRY):
9393
# decorator AND context manager
9494
#
9595

96+
9697
class _sync_wrap:
9798
def __init__(self, client):
9899
self.client = client

prometheus_push_client/formats/influx.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def format_sample(self, sample, *_):
3535
tag_set = ",".join(tags)
3636

3737
ts = ""
38-
if sample.timestamp:
38+
if sample.timestamp: # pragma: no cover
3939
ts = f" {int(sample.timestamp * 1e9)}" # to nanoseconds
4040

4141
return self.FMT_SAMPLE.format(

prometheus_push_client/formats/openmetrics.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def format_sample(self, sample, metric):
2525
tag_set = "{%s}" % ",".join(tags)
2626

2727
ts = ""
28-
if sample.timestamp:
28+
if sample.timestamp: # pragma: no cover
2929
ts = " %s" % sample.timestamp
3030

3131
# TODO: TYPE string?

prometheus_push_client/metrics.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,4 @@ class Gauge(_PushMixin, pc.Gauge): pass
3030
class Summary(_PushMixin, pc.Summary): pass
3131
class Histogram(_PushMixin, pc.Histogram): pass
3232

33-
# TODO: TEST!!1
34-
class Info(_PushMixin, pc.Info): pass
35-
class Enum(_PushMixin, pc.Enum): pass
33+
# TODO: Info, Enum ?

prometheus_push_client/registry.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
PUSH_REGISTRY = pc.CollectorRegistry(auto_describe=False, target_info=None)
55

66

7-
def transfer_metrics(src_registry, dst_registry):
7+
def transfer_metrics(src_registry, dst_registry): # pragma: no cover
88
"""
99
From ppc.PUSH_REGISTRY to pc.REGISTRY or vice versa
1010
"""

0 commit comments

Comments
 (0)