8000 Fix instance_ip_grouping_key not working on macOS (#687) · SnoopJ/client_python@09fb459 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit 09fb459

Browse files
authored
Fix instance_ip_grouping_key not working on macOS (prometheus#687)
* Fix instance_ip_grouping_key not working on macOS Fixes prometheus#629 The solution is adapted from this StackOverflow answer: https://stackoverflow.com/a/28950776 Signed-off-by: Jethro Muller <git@jethromuller.co.za> * Remove conditional skip for macos Signed-off-by: Jethro Muller <git@jethromuller.co.za> * Add platform check with explanatory comment Signed-off-by: Jethro Muller <git@jethromuller.co.za>
1 parent c49e55a commit 09fb459

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

prometheus_client/exposition.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,16 @@ def _escape_grouping_key(k, v):
481481
def instance_ip_grouping_key():
482482
"""Grouping key with instance set to the IP Address of this host."""
483483
with closing(socket.socket(socket.AF_INET, socket.SOCK_DGRAM)) as s:
484-
s.connect(('localhost', 0))
484+
if sys.platform == 'darwin':
485+
# This check is done this way only on MacOS devices
486+
# it is done this way because the localhost method does
487+
# not work.
488+
# This method was adapted from this StackOverflow answer:
489+
# https://stackoverflow.com/a/28950776
490+
s.connect(('10.255.255.255', 1))
491+
else:
492+
s.connect(('localhost', 0))
493+
485494
return {'instance': s.getsockname()[0]}
486495

487496

tests/test_exposition.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -376,10 +376,6 @@ def my_redirect_handler(url, method, timeout, headers, data):
376376
# ensure the redirect took place at the expected redirect location.
377377
self.assertEqual(self.requests[1][0].path, "/" + self.redirect_flag)
378378

379-
@unittest.skipIf(
380-
sys.platform == "darwin",
381-
"instance_ip_grouping_key() does not work on macOS."
382-
)
383379
def test_instance_ip_grouping_key(self):
384380
self.assertTrue('' != instance_ip_grouping_key()['instance'])
385381

0 commit comments

Comments
 (0)
0