Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add IPv6 support #46

Merged
merged 3 commits into from Sep 8, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.rdoc
Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions lib/statsd.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
30 changes: 30 additions & 0 deletions spec/statsd_spec.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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']