Skip to content

Commit

Permalink
Merge pull request #282 from DataDog/jerome.v/support-dogstatsd-socket
Browse files Browse the repository at this point in the history
Add support for `statsd_socket_path` in initialize function
  • Loading branch information
j-vizcaino committed Jun 26, 2018
2 parents bdc85a7 + d82c324 commit 14dabcb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
19 changes: 14 additions & 5 deletions datadog/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@


def initialize(api_key=None, app_key=None, host_name=None, api_host=None,
statsd_host=None, statsd_port=None, statsd_use_default_route=False, **kwargs):
statsd_host=None, statsd_port=None, statsd_use_default_route=False,
statsd_socket_path=None, **kwargs):
"""
Initialize and configure Datadog.api and Datadog.statsd modules
Expand All @@ -56,6 +57,9 @@ def initialize(api_key=None, app_key=None, host_name=None, api_host=None,
(Useful when running the client in a container)
:type statsd_use_default_route: boolean
:param statsd_socket_path: path to the DogStatsd UNIX socket. Supersedes statsd_host
and stats_port if provided.
:param cacert: Path to local certificate file used to verify SSL \
certificates. Can also be set to True (default) to use the systems \
certificate store, or False to skip SSL verification
Expand All @@ -74,10 +78,15 @@ def initialize(api_key=None, app_key=None, host_name=None, api_host=None,

# Statsd configuration
# ...overrides the default `statsd` instance attributes
if statsd_host or statsd_use_default_route:
statsd.host = statsd.resolve_host(statsd_host, statsd_use_default_route)
if statsd_port:
statsd.port = int(statsd_port)
if statsd_socket_path:
statsd.socket_path = statsd_socket_path
statsd.host = None
statsd.port = None
else:
if statsd_host or statsd_use_default_route:
statsd.host = statsd.resolve_host(statsd_host, statsd_use_default_route)
if statsd_port:
statsd.port = int(statsd_port)

# HTTP client and API options
for key, value in iteritems(kwargs):
Expand Down
7 changes: 7 additions & 0 deletions tests/unit/dogstatsd/test_statsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,13 @@ def test_initialization(self):
t.assert_equal(statsd.host, "172.17.0.1")
t.assert_equal(statsd.port, 1234)

# Add UNIX socket
options['statsd_socket_path'] = '/var/run/dogstatsd.sock'
initialize(**options)
t.assert_equal(statsd.socket_path, options['statsd_socket_path'])
t.assert_equal(statsd.host, None)
t.assert_equal(statsd.port, None)

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

0 comments on commit 14dabcb

Please sign in to comment.