Skip to content

Commit

Permalink
Fix instance_ip_grouping_key not working on macOS (#687)
Browse files Browse the repository at this point in the history
* Fix instance_ip_grouping_key not working on macOS

Fixes #629

The solution is adapted from this StackOverflow answer: https://stackoverflow.com/a/28950776

Signed-off-by: Jethro Muller <[email protected]>

* Remove conditional skip for macos

Signed-off-by: Jethro Muller <[email protected]>

* Add platform check with explanatory comment

Signed-off-by: Jethro Muller <[email protected]>
  • Loading branch information
AceFire6 authored Sep 15, 2021
1 parent c49e55a commit 09fb459
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
11 changes: 10 additions & 1 deletion prometheus_client/exposition.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,16 @@ def _escape_grouping_key(k, v):
def instance_ip_grouping_key():
"""Grouping key with instance set to the IP Address of this host."""
with closing(socket.socket(socket.AF_INET, socket.SOCK_DGRAM)) as s:
s.connect(('localhost', 0))
if sys.platform == 'darwin':
# This check is done this way only on MacOS devices
# it is done this way because the localhost method does
# not work.
# This method was adapted from this StackOverflow answer:
# https://stackoverflow.com/a/28950776
s.connect(('10.255.255.255', 1))
else:
s.connect(('localhost', 0))

return {'instance': s.getsockname()[0]}


Expand Down
4 changes: 0 additions & 4 deletions tests/test_exposition.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,10 +376,6 @@ def my_redirect_handler(url, method, timeout, headers, data):
# ensure the redirect took place at the expected redirect location.
self.assertEqual(self.requests[1][0].path, "/" + self.redirect_flag)

@unittest.skipIf(
sys.platform == "darwin",
"instance_ip_grouping_key() does not work on macOS."
)
def test_instance_ip_grouping_key(self):
self.assertTrue('' != instance_ip_grouping_key()['instance'])

Expand Down

0 comments on commit 09fb459

Please sign in to comment.