Skip to content

Commit

Permalink
Close socket during initialization
Browse files Browse the repository at this point in the history
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().
  • Loading branch information
vickenty committed Dec 21, 2023
1 parent ff9f020 commit 55c4b4a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
1 change: 1 addition & 0 deletions datadog/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ def initialize(
statsd.host = statsd.resolve_host(statsd_host, statsd_use_default_route)
if statsd_port:
statsd.port = int(statsd_port)
statsd.close_socket()
if statsd_namespace:
statsd.namespace = text(statsd_namespace)
if statsd_constant_tags:
Expand Down
6 changes: 6 additions & 0 deletions tests/unit/dogstatsd/test_statsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,12 @@ def test_dogstatsd_initialization_with_env_vars(self):
self.assertEqual(dogstatsd.host, "myenvvarhost")
self.assertEqual(dogstatsd.port, 4321)

def test_initialization_closes_socket(self):
statsd.socket = FakeSocket()
self.assertIsNotNone(statsd.socket)
initialize()
self.assertIsNone(statsd.socket)

def test_default_route(self):
"""
Dogstatsd host can be dynamically set to the default route.
Expand Down

0 comments on commit 55c4b4a

Please sign in to comment.