diff --git a/lib/datadog/statsd/udp_connection.rb b/lib/datadog/statsd/udp_connection.rb index df7a90a..c40547a 100644 --- a/lib/datadog/statsd/udp_connection.rb +++ b/lib/datadog/statsd/udp_connection.rb @@ -29,7 +29,9 @@ def close def connect close if @socket - @socket = UDPSocket.new + family = Addrinfo.udp(host, port).afamily + + @socket = UDPSocket.new(family) @socket.connect(host, port) end diff --git a/spec/statsd/udp_connection_spec.rb b/spec/statsd/udp_connection_spec.rb index 62bf5fc..3a6abd8 100644 --- a/spec/statsd/udp_connection_spec.rb +++ b/spec/statsd/udp_connection_spec.rb @@ -71,6 +71,34 @@ instance_double(Datadog::Statsd::Telemetry, sent: true, dropped_writer: true) end + context 'using an IPv6 address' do + let(:host) { '2001:db8::dead:beef' } + + it 'connects to an IPv6 host with the right address family' do + expect(UDPSocket) + .to receive(:new) + .with(Socket::AF_INET6) + + subject.write('test') + end + + it 'connects to the right host and port' do + expect(udp_socket) + .to receive(:connect) + .with('2001:db8::dead:beef', 4567) + + subject.write('test') + end + end + + it 'connects to an IPv4 host with the right address family' do + expect(UDPSocket) + .to receive(:new) + .with(Socket::AF_INET) + + subject.write('test') + end + it 'connects to the right host and port' do expect(udp_socket) .to receive(:connect)