Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #46 from aw/ipv6-support

Add IPv6 support
  • Loading branch information...
commit 10c844c2d793eab1283093b25bbf9262bbed9a6f 2 parents 78a1ebe + 1d3246e
@raggi raggi authored
Showing with 41 additions and 2 deletions.
  1. +3 −0  README.rdoc
  2. +8 −2 lib/statsd.rb
  3. +30 −0 spec/statsd_spec.rb
View
3  README.rdoc
@@ -12,6 +12,9 @@ Bundler:
# Set up a global Statsd client for a server on localhost:9125
$statsd = Statsd.new 'localhost', 9125
+ # Set up a global Statsd client for a server on IPv6 port 9125
+ $statsd = Statsd.new '::1', 9125
+
# Send some stats
$statsd.increment 'garets'
$statsd.timing 'glork', 320
View
10 lib/statsd.rb
@@ -4,8 +4,10 @@
# = Statsd: A Statsd client (https://github.com/etsy/statsd)
#
-# @example Set up a global Statsd client for a server on localhost:9125
+# @example Set up a global Statsd client for a server on localhost:8125
# $statsd = Statsd.new 'localhost', 8125
+# @example Set up a global Statsd client for a server on IPv6 port 8125
+# $statsd = Statsd.new '::1', 8125
# @example Send some stats
# $statsd.increment 'garets'
# $statsd.timing 'glork', 320
@@ -389,6 +391,10 @@ def send_stats(stat, delta, type, sample_rate=1)
end
def socket
- Thread.current[:statsd_socket] ||= UDPSocket.new
+ Thread.current[:statsd_socket] ||= UDPSocket.new addr_family
+ end
+
+ def addr_family
+ Addrinfo.udp(@host, @port).ipv6? ? Socket::AF_INET6 : Socket::AF_INET
end
end
View
30 spec/statsd_spec.rb
@@ -47,6 +47,11 @@ class Statsd
@statsd.port = nil
@statsd.port.must_equal 8125
end
+
+ it "should allow an IPv6 address" do
+ @statsd.host = '::1'
+ @statsd.host.must_equal '::1'
+ end
end
describe "#increment" do
@@ -371,6 +376,7 @@ class Statsd::SomeClass; end
describe Statsd do
describe "with a real UDP socket" do
it "should actually send stuff over the socket" do
+ Thread.current[:statsd_socket] = nil
socket = UDPSocket.new
host, port = 'localhost', 12345
socket.bind(host, port)
@@ -380,5 +386,29 @@ class Statsd::SomeClass; end
message = socket.recvfrom(16).first
message.must_equal 'foobar:1|c'
end
+
+ it "should send stuff over an IPv4 socket" do
+ Thread.current[:statsd_socket] = nil
+ socket = UDPSocket.new Socket::AF_INET
+ host, port = '127.0.0.1', 12346
+ socket.bind(host, port)
+
+ statsd = Statsd.new(host, port)
+ statsd.increment('foobar')
+ message = socket.recvfrom(16).first
+ message.must_equal 'foobar:1|c'
+ end
+
+ it "should send stuff over an IPv6 socket" do
+ Thread.current[:statsd_socket] = nil
+ socket = UDPSocket.new Socket::AF_INET6
+ host, port = '::1', 12347
+ socket.bind(host, port)
+
+ statsd = Statsd.new(host, port)
+ statsd.increment('foobar')
+ message = socket.recvfrom(16).first
+ message.must_equal 'foobar:1|c'
+ end
end
end if ENV['LIVE']
Please sign in to comment.
Something went wrong with that request. Please try again.