Skip to content

Commit c80f986

Browse files
authored
Close socket during initialization (#808)
If the global statsd instance is used before initialize() is called (for example, statsd used to send metrics from a top-level of a loaded module), statsd instance will already have a socket open when we set connection parameters. To make sure they take effect, call close_socket().
1 parent 1e2cfcf commit c80f986

2 files changed

Lines changed: 7 additions & 0 deletions

File tree

datadog/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ def initialize(
122122
statsd.host = statsd.resolve_host(statsd_host, statsd_use_default_route)
123123
if statsd_port:
124124
statsd.port = int(statsd_port)
125+
statsd.close_socket()
125126
if statsd_namespace:
126127
statsd.namespace = text(statsd_namespace)
127128
if statsd_constant_tags:

tests/unit/dogstatsd/test_statsd.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,12 @@ def test_dogstatsd_initialization_with_env_vars(self):
285285
self.assertEqual(dogstatsd.host, "myenvvarhost")
286286
self.assertEqual(dogstatsd.port, 4321)
287287

288+
def test_initialization_closes_socket(self):
289+
statsd.socket = FakeSocket()
290+
self.assertIsNotNone(statsd.socket)
291+
initialize()
292+
self.assertIsNone(statsd.socket)
293+
288294
def test_default_route(self):
289295
"""
290296
Dogstatsd host can be dynamically set to the default route.

0 commit comments

Comments
 (0)