8000 Add function to provide IP as grouping key · QuantumGhost/client_python@3632050 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3632050

Browse files
committed
Add function to provide IP as grouping key
1 parent 6b312cd commit 3632050

File tree

4 files changed

+23
-1
lines changed

4 files changed

+23
-1
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,17 @@ push_to_gateway('localhost:9091', job='batchA', registry=registry)
262262
A separate registry is used, as the default registry may contain other metrics
263263
such as those from the Process Collector.
264264

265+
Pushgateway functions take a grouping key. `push_to_gateway` replaces metrics
266+
with the same grouping key, `pushadd_to_gateway` only replaces metrics with the
267+
same name and grouping key and `delete_from_gateway` deletes metrics with the
268+
given job and grouping key. See the
269+
[Pushgateway documentation](https://github.com/prometheus/pushgateway/blob/master/README.md)
270+
for more information.
271+
272+
`instance_ip_grouping_key` returns a grouping key with the instance label set
273+
to the host's IP address.
274+
275+
265276
## Bridges
266277

267278
It is also possible to expose metrics to systems other than Prometheus.

prometheus_client/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
push_to_gateway = exposition.push_to_gateway
2525
pushadd_to_gateway = exposition.pushadd_to_gateway
2626
delete_from_gateway = exposition.delete_from_gateway
27+
instance_ip_grouping_key = exposition.instance_ip_grouping_key
2728

2829
ProcessCollector = process_collector.ProcessCollector
2930
PROCESS_COLLECTOR = process_collector.PROCESS_COLLECTOR

prometheus_client/exposition.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from __future__ import unicode_literals
44

55
import os
6+
import socket
67
import time
78
import threading
89

@@ -121,3 +122,9 @@ def _use_gateway(method, gateway, job, registry, grouping_key, timeout):
121122
if resp.code >= 400:
122123
raise IOError("error talking to pushgateway: {0} {1}".format(
123124
resp.code, resp.msg))
125+
126+
def instance_ip_grouping_key():
127+
'''Grouping key with instance set to the IP Address of this host.'''
128+
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
129+
s.connect(('', 0))
130+
return {'instance': s.getsockname()[0]}

tests/test_client.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from prometheus_client import Gauge, Counter, Summary, Histogram, Metric
88
from prometheus_client import CollectorRegistry, generate_latest, ProcessCollector
99
from prometheus_client import push_to_gateway, pushadd_to_gateway, delete_from_gateway
10-
from prometheus_client import CONTENT_TYPE_LATEST
10+
from prometheus_client import CONTENT_TYPE_LATEST, instance_ip_grouping_key
1111

1212
try:
1313
from BaseHTTPServer import BaseHTTPRequestHandler
@@ -456,6 +456,9 @@ def test_pushadd_with_groupingkey(self):
456456
self.assertEqual(self.requests[0][0].headers.get('content-type'), CONTENT_TYPE_LATEST)
457457
self.assertEqual(self.requests[0][1], b'')
458458

459+
def test_instance_ip_grouping_key(self):
460+
self.assertTrue('' != instance_ip_grouping_key()['instance'])
461+
459462

460463
if __name__ == '__main__':
461464
unittest.main()

0 commit comments

Comments
 (0)
0