Skip to content

Add IPv6 support to the UDP sender #42

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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
rvm:
- 1.9.2
- 1.9.3
- 2.0.0
- 2.1.0
- 2.2.0
- 2.1.8
- 2.2.4
- 2.3.0
- jruby-19mode
- jruby

6 changes: 3 additions & 3 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
source "https://rubygems.org"

group :development do
gem "shoulda", "~> 2.11.3"
gem "jeweler", "~> 1.8.7"
gem "mocha", "~> 0.14.0"
gem "shoulda", "~> 3.5.0"
gem "jeweler", "~> 2.0.1"
gem "mocha", "~> 1.1.0"
gem "test-unit", "~> 3.0.8"
end
98 changes: 59 additions & 39 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,61 +1,81 @@
GEM
remote: https://rubygems.org/
specs:
addressable (2.3.5)
activesupport (4.2.6)
i18n (~> 0.7)
json (~> 1.7, >= 1.7.7)
minitest (~> 5.1)
thread_safe (~> 0.3, >= 0.3.4)
tzinfo (~> 1.1)
addressable (2.4.0)
builder (3.2.2)
faraday (0.8.8)
multipart-post (~> 1.2.0)
git (1.2.6)
github_api (0.10.1)
addressable
faraday (~> 0.8.1)
hashie (>= 1.2)
multi_json (~> 1.4)
nokogiri (~> 1.5.2)
descendants_tracker (0.0.4)
thread_safe (~> 0.3, >= 0.3.1)
faraday (0.9.2)
multipart-post (>= 1.2, < 3)
git (1.3.0)
github_api (0.13.1)
addressable (~> 2.4.0)
descendants_tracker (~> 0.0.4)
faraday (~> 0.8, < 0.10)
hashie (>= 3.4)
multi_json (>= 1.7.5, < 2.0)
oauth2
hashie (2.0.5)
highline (1.6.19)
httpauth (0.2.0)
jeweler (1.8.7)
hashie (3.4.3)
highline (1.7.8)
i18n (0.7.0)
jeweler (2.0.1)
builder
bundler (~> 1.0)
bundler (>= 1.0)
git (>= 1.2.5)
github_api (= 0.10.1)
github_api
highline (>= 1.6.15)
nokogiri (= 1.5.10)
nokogiri (>= 1.5.10)
rake
rdoc
json (1.8.2)
jwt (0.1.8)
multi_json (>= 1.5)
metaclass (0.0.1)
mocha (0.14.0)
json (1.8.3)
jwt (1.5.1)
metaclass (0.0.4)
mini_portile2 (2.0.0)
minitest (5.8.4)
mocha (1.1.0)
metaclass (~> 0.0.1)
multi_json (1.7.9)
multi_json (1.11.2)
multi_xml (0.5.5)
multipart-post (1.2.0)
nokogiri (1.5.10)
oauth2 (0.9.2)
faraday (~> 0.8)
httpauth (~> 0.2)
jwt (~> 0.1.4)
multi_json (~> 1.0)
multipart-post (2.0.0)
nokogiri (1.6.7.2)
mini_portile2 (~> 2.0.0.rc2)
oauth2 (1.1.0)
faraday (>= 0.8, < 0.10)
jwt (~> 1.0, < 1.5.2)
multi_json (~> 1.3)
multi_xml (~> 0.5)
rack (~> 1.2)
power_assert (0.2.3)
rack (1.5.2)
rake (10.1.0)
rdoc (4.0.1)
rack (>= 1.2, < 3)
power_assert (0.2.7)
rack (1.6.4)
rake (11.1.1)
rdoc (4.2.2)
json (~> 1.4)
shoulda (2.11.3)
shoulda (3.5.0)
shoulda-context (~> 1.0, >= 1.0.1)
shoulda-matchers (>= 1.4.1, < 3.0)
shoulda-context (1.2.1)
shoulda-matchers (2.8.0)
activesupport (>= 3.0.0)
test-unit (3.0.9)
power_assert
thread_safe (0.3.5)
tzinfo (1.2.2)
thread_safe (~> 0.1)

PLATFORMS
ruby

DEPENDENCIES
jeweler (~> 1.8.7)
mocha (~> 0.14.0)
shoulda (~> 2.11.3)
jeweler (~> 2.0.1)
mocha (~> 1.1.0)
shoulda (~> 3.5.0)
test-unit (~> 3.0.8)

BUNDLED WITH
1.11.2
55 changes: 38 additions & 17 deletions lib/gelf/transport/udp.rb
Original file line number Diff line number Diff line change
@@ -1,40 +1,61 @@
module GELF
module Transport
class UDP
attr_accessor :addresses
attr_reader :addresses

def initialize(addresses)
@addresses = addresses
def initialize(initial_addresses)
self.addresses = initial_addresses
end

def addresses=(new_addresses)
@addresses = new_addresses
reset_sockets
end

def send_datagrams(datagrams)
socket = get_socket
idx = get_address_index

host, port = @addresses[idx]
set_address_index((idx + 1) % @addresses.length)
sock = socket
datagrams.each do |datagram|
socket.send(datagram, 0, host, port)
sock.send(datagram, 0)
end
end

def close
socket = get_socket
socket.close if socket
reset_sockets
end

private

def get_socket
Thread.current[:gelf_udp_socket] ||= UDPSocket.open
def socket
idx = socket_index
sock = sockets[idx]
set_socket_index((idx + 1) % @addresses.length)
sock
end

def get_address_index
Thread.current[:gelf_udp_address_idx] ||= 0
def sockets
Thread.current[:gelf_udp_sockets] ||= configure_sockets
end

def set_address_index(value)
Thread.current[:gelf_udp_address_idx] = value
def reset_sockets
return unless Thread.current.key?(:gelf_udp_sockets)
Thread.current[:gelf_udp_sockets].each(&:close)
Thread.current[:gelf_udp_sockets] = nil
end

def socket_index
Thread.current[:gelf_udp_socket_idx] ||= 0
end

def set_socket_index(value)
Thread.current[:gelf_udp_socket_idx] = value
end

def configure_sockets
@addresses.map do |host, port|
UDPSocket.new(Addrinfo.ip(host).afamily).tap do |socket|
socket.connect(host, port)
end
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/helper.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'rubygems'
require 'test/unit'
require 'shoulda'
require 'mocha'
require 'mocha/setup'

$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
$LOAD_PATH.unshift(File.dirname(__FILE__))
Expand Down
6 changes: 3 additions & 3 deletions test/test_notifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ class TestNotifier < Test::Unit::TestCase
e.set_backtrace(caller)
hash = @notifier.__send__(:extract_hash, e)
assert_equal 'RuntimeError: message', hash['short_message']
assert_match /Backtrace/, hash['full_message']
assert_match(/Backtrace/, hash['full_message'])
assert_equal GELF::ERROR, hash['level']
end

should "work with exception without backtrace" do
e = RuntimeError.new('message')
hash = @notifier.__send__(:extract_hash, e)
assert_match /Backtrace is not available/, hash['full_message']
assert_match(/Backtrace is not available/, hash['full_message'])
end

should "work with exception and hash" do
Expand Down Expand Up @@ -108,7 +108,7 @@ class TestNotifier < Test::Unit::TestCase
should "set file and line" do
line = __LINE__
hash = @notifier.__send__(:extract_hash, { 'version' => '1.0', 'short_message' => 'message' })
assert_match /test_notifier.rb/, hash['file']
assert_match(/test_notifier.rb/, hash['file'])
assert_equal line + 1, hash['line']
end

Expand Down
25 changes: 21 additions & 4 deletions test/test_ruby_sender.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,35 @@ class TestRubyUdpSender < Test::Unit::TestCase
@datagrams2 = %w(e1 e2 e3)
end

context "setup_sockets" do
setup do
@sender.send_datagrams(%w(a1))
@sender.send_datagrams(%w(b1))
end

before_should "be configured with a socket for each address" do
UDPSocket.any_instance.expects(:connect).with do |host,port|
host == 'localhost' && port == 12201
end
UDPSocket.any_instance.expects(:connect).with do |host,port|
host == 'localhost' && port == 12202
end
UDPSocket.any_instance.expects(:send).times(2).returns(nil)
end
end

context "send_datagrams" do
setup do
@sender.send_datagrams(@datagrams1)
@sender.send_datagrams(@datagrams2)
end

before_should "be called 3 times with 1st and 2nd address" do
UDPSocket.any_instance.expects(:send).times(3).with do |datagram, _, host, port|
datagram.start_with?('d') && host == 'localhost' && port == 12201
UDPSocket.any_instance.expects(:send).times(3).with do |datagram,_|
datagram.start_with?('d')
end
UDPSocket.any_instance.expects(:send).times(3).with do |datagram, _, host, port|
datagram.start_with?('e') && host == 'localhost' && port == 12202
UDPSocket.any_instance.expects(:send).times(3).with do |datagram,_|
datagram.start_with?('e')
end
end
end
Expand Down