Using Python client for Prometheus on IPv6

108 views Asked by At

I am writing a custom exporter for my application using the official Python client for Prometheus My code works fine with the below snippet, (I'm redacting the rest of the code as I think it is irrelevant to the issue I'm facing.)

from prometheus_client import start_http_server, Gauge
start_http_server(9669)

I'm able to get the response if I do

curl --noproxy "*" -v http://localhost:9669/metrics

from the same machine. The issue here is my servers on IPv6. So this endpoint is not reachable from the server where I have Prometheus running. How can make

start_http_server

on IPv6 or is there a workaround for this?

I tried running a flask app and doing curl from the machine where I have Prometheus installed. I do not get response for

from flask import Flask

app = Flask(__name__)

@app.route('/hello')
def hello_world():
    return 'Hello, World!'

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8989) 

but I get response if I modify the line to

app.run(host='::', port=8989)

So I figured I need to run the Prometheus

start_http_server

on IPv6. I tried

start_http_server(9669, addr='[::]')

but I get the error

socket.gaierror: [Errno -2] Name or service not known

and for

start_http_server(9669, addr='::')

I get the error

socket.gaierror: [Errno -9] Address family for hostname not supported
1

There are 1 answers

0
Florian Bach On

You might be using an old version of the Python Prometheus client.

I tried the most recent version 0.19.0 with Python 3.8, and this code is working just fine for me (server listens on IPv6):

from prometheus_client import start_http_server

if __name__ == '__main__':
    start_http_server(9999, addr='::')
    while True:
        pass

According to a Google search, your bug ("Address family for hostname not supported") was reported here, fixed in this PR, and then released in 0.14.0 from April 2022.