Skip to content

Fixes #330: Make Metrics server listen on IPv4 and IPv6 sockets. #331

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion django_prometheus/exports.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
import os
import threading
import socket

import prometheus_client
from django.conf import settings
Expand All @@ -18,6 +19,15 @@
logger = logging.getLogger(__name__)


class DualStackHTTPServer(HTTPServer):
"""
Default for the python HTTPServer address_family is socket.AF_INET.
This will by default create a server which works IPv4 only.
To add compatibility with IPv6, we set the address_family.
"""
address_family = socket.AF_INET6


def SetupPrometheusEndpointOnPort(port, addr=""):
"""Exports Prometheus metrics on an HTTPServer running in its own thread.

Expand Down Expand Up @@ -83,7 +93,7 @@ def SetupPrometheusEndpointOnPortRange(port_range, addr=""):
)
for port in port_range:
try:
httpd = HTTPServer((addr, port), prometheus_client.MetricsHandler)
httpd = DualStackHTTPServer((addr, port), prometheus_client.MetricsHandler)
except OSError:
# Python 2 raises socket.error, in Python 3 socket.error is an
# alias for OSError
Expand Down