[dogstatsd] Allow to dynamically use default route as a statsd host#134
[dogstatsd] Allow to dynamically use default route as a statsd host#134
Conversation
429c363 to
218e6a1
Compare
When you run in a containerized environment, the dogstatsd server will likely expose its port on the host network. If you have an application that runs within a container, it needs to connect to it using the default route.
71f423a to
60eea4b
Compare
| if not self.socket: | ||
| if self.use_default_route: | ||
| self.host = self._get_default_route() | ||
|
|
There was a problem hiding this comment.
This could be a good candidate for a Python property too.
@property.setter
def use_default_route(self, value):
self._use_default_route = value
# Invalid the socket
self.socket = None
if self._use_default_route:
self.host = self._get_default_route()with the following benefits:
- We don't need to repeat
self.host = self._get_default_route()twice. - The
hostandsocketattributes would be systematically updated onuse_default_routechanges.
There was a problem hiding this comment.
We don't do it for the host or port though. Should we ?
|
Looks good, I simply added a nitpick. Feel free to consider it or not, and merge the PR :) |
|
On Mac OS X this doesn't degrade gracefully: >>> from datadog import initialize, statsd
>>> initialize(statsd_use_default_route=True)
>>> statsd.event("Test event", "Test body")
No handlers could be found for logger "dogstatsd"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/env/lib/python2.7/site-packages/datadog/dogstatsd/base.py", line 363, in event
self._send(string)
File "/Users/env/lib/python2.7/site-packages/datadog/dogstatsd/base.py", line 299, in _send_to_server
(self.socket or self.get_socket()).send(packet.encode(self.encoding))
File "/Users/env/lib/python2.7/site-packages/datadog/dogstatsd/base.py", line 107, in get_socket
sock.connect((self.host, self.port))
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 228, in meth
return getattr(self._sock,name)(*args)
TypeError: coercing to Unicode: need string or buffer, NoneType foundIs this expected / Is there a fix for this? |
|
That's definitely not expected. Thanks for reporting @aeneaswiener. We are working on a fix. |
|
@aeneaswiener thanks for the feedback. The main use case for this option is when you run your application in a (docker) container and you have your statsd server listening on the host. So for this use case, OS X support is not necessary. Can you tell us a bit more about your use case ? |
|
@remh We run our Python applications locally during development, which includes running a test suite before building and pushing docker images. Of course we could introduce an |
|
@aeneaswiener not sure that failing silently is the best thing to do here. This is a bad practice that could hide underlying issues. We could raise a better exception though if we can't find the default router, something like raise IOError("Unable to find default route. This option is only supported on Linux")Would that work ? |
|
That would work, yes. Though I am not sure if IO error is the best one to raise as it leaks an implementation detail of the API. In any case I think the behaviour (exception) should be the same as when an unreachable statsd_host parameter is passed to the datadog.initialize method. |
When you run in a containerized environment, the dogstatsd server will
likely expose its port on the host network. If you have an application
that runs within a container, it needs to connect to it using the
default route.