Skip to content

Commit

Permalink
use 0.0.0.0 as server address when non_local_traffic is passed (#2691)
Browse files Browse the repository at this point in the history
  • Loading branch information
masci committed Jul 25, 2016
1 parent c3783ee commit 7b25511
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
13 changes: 8 additions & 5 deletions dogstatsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,7 @@ def init(config_path=None, use_watchdog=False, use_forwarder=False, args=None):
forward_to_port = c.get('statsd_forward_port')
event_chunk_size = c.get('event_chunk_size')
recent_point_threshold = c.get('recent_point_threshold', None)
server_host = c['bind_host']

target = c['dd_url']
if use_forwarder:
Expand All @@ -508,12 +509,14 @@ def init(config_path=None, use_watchdog=False, use_forwarder=False, args=None):
# Start the reporting thread.
reporter = Reporter(interval, aggregator, target, api_key, use_watchdog, event_chunk_size)

# Start the server on an IPv4 stack
# Default to loopback
server_host = c['bind_host']
# If specified, bind to all addressses
# NOTICE: when `non_local_traffic` is passed we need to bind to any interface on the box. The forwarder uses
# Tornado which takes care of sockets creation (more than one socket can be used at once depending on the
# network settings), so it's enough to just pass an empty string '' to the library.
# In Dogstatsd we use a single, fullstack socket, so passing '' as the address doesn't work and we default to
# '0.0.0.0'. If someone needs to bind Dogstatsd to the IPv6 '::', they need to turn off `non_local_traffic` and
# use the '::' meta address as `bind_host`.
if non_local_traffic:
server_host = ''
server_host = '0.0.0.0'

server = Server(aggregator, server_host, port, forward_to_host=forward_to_host, forward_to_port=forward_to_port)

Expand Down
17 changes: 16 additions & 1 deletion tests/core/test_dogstatsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
import socket
import threading
import Queue
from collections import defaultdict

# 3p
import mock

# project
from dogstatsd import mapto_v6, get_socket_address
from dogstatsd import Server
from dogstatsd import Server, init
from utils.net import IPV6_V6ONLY, IPPROTO_IPV6


Expand All @@ -29,6 +30,20 @@ def test_get_socket_address(self):
self.assertEqual(get_socket_address('example.com', 80), ('::1', 80, 0, 0))
self.assertIsNone(get_socket_address('foo', 80))

@mock.patch('dogstatsd.get_config')
@mock.patch('dogstatsd.Server')
def test_init(self, s, gc):
gc.return_value = defaultdict(str)
gc.return_value['non_local_traffic'] = True
gc.return_value['use_dogstatsd'] = True

init()

# if non_local_traffic was passed, use IPv4 wildcard
s.assert_called_once()
args, _ = s.call_args
self.assertEqual(args[1], '0.0.0.0')


class TestServer(TestCase):
@mock.patch('dogstatsd.get_socket_address')
Expand Down

0 comments on commit 7b25511

Please sign in to comment.