Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

instance_ip_grouping_key does not work on macOS #629

Closed
darkclouder opened this issue Feb 15, 2021 · 2 comments · Fixed by #687
Closed

instance_ip_grouping_key does not work on macOS #629

darkclouder opened this issue Feb 15, 2021 · 2 comments · Fixed by #687

Comments

@darkclouder
Copy link

darkclouder commented Feb 15, 2021

macOS 11.1
Python 3.8.5
prometheus-client 0.9.0

To reproduce:

from prometheus_client import instance_ip_grouping_key

instance_ip_grouping_key()

What happens?

metrics.py:10: in <module>
    INSTANCE = instance_ip_grouping_key()
lib/python3.8/site-packages/prometheus_client/exposition.py:379: in instance_ip_grouping_key
    s.connect(('localhost', 0))
E   OSError: [Errno 49] Can't assign requested address
@csmarchbanks
Copy link
Member

Thank you for your report, it is certainly an issue, and there is even a test that is ignored on macOS: https://github.com/prometheus/client_python/blob/master/tests/test_exposition.py#L379

I don't have a macOS environment, but would be happy to review a PR to enable macOS.

@AceFire6
Copy link
Contributor

AceFire6 commented Aug 17, 2021

@csmarchbanks Does this look like an acceptable solution?

Edit: I've submitted a PR #687

It's adapted from this StackOverflow answer: https://stackoverflow.com/a/28950776

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(('10.255.255.255', 1))
        return {'instance': s.getsockname()[0]}

It works on my M1 MacbookPro:

>>> from contextlib import closing
>>> 
>>> 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(('10.255.255.255', 1))
...         return {'instance': s.getsockname()[0]}
... 
>>> instance_ip_grouping_key()
{'instance': '192.168.0.189'}

Screenshot 2021-08-17 at 18 42 21

AceFire6 added a commit to AceFire6/client_python that referenced this issue Aug 17, 2021
AceFire6 added a commit to AceFire6/client_python that referenced this issue Aug 17, 2021
Fixes prometheus#629

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

Signed-off-by: Jethro Muller <[email protected]>
csmarchbanks pushed a commit that referenced this issue Sep 15, 2021
* 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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants