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

Fix IPv6 interpolation for Prometheus sizer #97

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions kafka/tools/assigner/sizers/prometheus.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ def _parse_prometheus_metric(self, text):
return metric or None

def _get_prometheus_metrics(self, hostname, port, path):
# When hostname is an ipv6 addr, add brackets to separate addr from port
if hostname.count(':') >= 2 and hostname[0] != '[' and hostname[-1] != ']':
hostname = '[{}]'.format(hostname)
url = 'http://{}:{}{}'.format(hostname, port, path)
body = ''
try:
Expand Down
17 changes: 17 additions & 0 deletions tests/tools/assigner/sizers/test_prometheus.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,23 @@ def test_sizer_get_prometheus_metrics_200(self, mock_urlopen):
metrics = sizer._get_prometheus_metrics('localhost', 1234, '/metrics')
self.assertSequenceEqual(metrics, ['404 not found'])

@patch('kafka.tools.assigner.sizers.prometheus.urlopen')
def test_sizer_get_prometheus_metrics_ipv6(self, mock_urlopen):
test_paths = [
('1.2.3.4', '1.2.3.4'),
('username:[email protected]', 'username:[email protected]'),
('::1', '[::1]'),
('[2001:db8::1]', '[2001:db8::1]'),
]
for before, after in test_paths:
m = Mock()
m.getcode.return_value = 200
mock_urlopen.return_value = m
sizer = SizerPrometheus(self.args, self.cluster)
sizer._get_prometheus_metrics(before, 1234, '/metrics')
mock_urlopen.assert_called_once_with('http://{}:1234/metrics'.format(after))
mock_urlopen.reset_mock()

@patch('kafka.tools.assigner.sizers.prometheus.urlopen')
def test_sizer_get_prometheus_metrics_404(self, mock_urlopen):
m = Mock()
Expand Down