Skip to content

Commit f07676e

Browse files
committed
Fix transport attribute not being set
1 parent 3252664 commit f07676e

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

datadog/dogstatsd/base.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,13 @@ def socket(self, new_socket):
534534
except AttributeError: # _socket can't have a type if it doesn't have sockopts
535535
log.info("Unexpected socket provided with no support for getsockopt")
536536
self._socket_kind = None
537+
if self.socket_path:
538+
if self.socket_path.startswith(UNIX_ADDRESS_STREAM_SCHEME):
539+
self._transport = "uds-stream"
540+
else:
541+
self._transport = "uds"
542+
else:
543+
self._transport = "udp"
537544
# When the socket is None, we use the UDP optimal payload length
538545
self._max_payload_size = UDP_OPTIMAL_PAYLOAD_LENGTH
539546

tests/unit/dogstatsd/test_statsd.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2192,3 +2192,21 @@ def send(self, _):
21922192
statsd.increment("test", 1)
21932193

21942194
assert statsd.socket is not None
2195+
2196+
def test_transport_attribute_present_on_connection_error(self):
2197+
"""
2198+
Ensure `_transport` attribute is present for telemetry even if the socket is None.
2199+
"""
2200+
# This test will fail with an AttributeError before the fix.
2201+
# Use a non-resolvable host to trigger a connection error.
2202+
statsd = DogStatsd(
2203+
host='non.existent.host.datadog.internal',
2204+
telemetry_min_flush_interval=0 # Flush telemetry immediately
2205+
)
2206+
2207+
# This call will attempt to send a metric, fail to create a socket,
2208+
# and then attempt to send telemetry, which requires `_transport`.
2209+
statsd.gauge('test.metric', 1)
2210+
2211+
assert statsd.socket is None
2212+
assert statsd._transport is not None

0 commit comments

Comments
 (0)