loggregator_emitter 2.0.0 → 2.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,11 @@
1
1
  require 'socket'
2
+ require 'resolv'
2
3
 
3
4
  module LoggregatorEmitter
4
5
  class Emitter
6
+
7
+ attr_reader :host
8
+
5
9
  MAX_MESSAGE_BYTE_SIZE = (9 * 1024) - 512
6
10
  TRUNCATED_STRING = "TRUNCATED"
7
11
 
@@ -16,9 +20,13 @@ module LoggregatorEmitter
16
20
 
17
21
  def initialize(loggregator_server, source_type_or_name, source_id = nil, secret=nil)
18
22
  @host, @port = loggregator_server.split(/:([^:]*$)/)
23
+
19
24
  raise ArgumentError, "Must provide valid loggregator server: #{loggregator_server}" if !valid_hostname || !valid_port
20
25
  raise ArgumentError, "Must provide valid source_type_or_name: #{source_type_or_name}" unless source_type_or_name
21
26
 
27
+ @host = ::Resolv.getaddresses(@host).last
28
+ raise ArgumentError, "Must provide valid loggregator server: #{loggregator_server}" unless @host
29
+
22
30
  case source_type_or_name
23
31
  when LogMessage::SourceType::CLOUD_CONTROLLER..LogMessage::SourceType::LOGGREGATOR
24
32
  @source_type = source_type_or_name
@@ -50,7 +58,7 @@ module LoggregatorEmitter
50
58
  end
51
59
 
52
60
  def valid_hostname
53
- @host && !@host.match(/:\/\//)
61
+ @host && !@host.empty? && !@host.match(/:\/\//)
54
62
  end
55
63
 
56
64
  def split_message(message)
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = "loggregator_emitter"
5
- spec.version = '2.0.0'
5
+ spec.version = '2.0.3'
6
6
  spec.authors = ["Pivotal"]
7
7
  spec.email = ["cf-eng@pivotallabs.com"]
8
8
  spec.description = "Library to emit data to Loggregator"
@@ -16,35 +16,20 @@ class MessageFixture
16
16
  end
17
17
  end
18
18
 
19
- class FreePort
20
- def self.next_free_port
21
- @@next_free_port ||= 12345
22
- @@next_free_port += 1
23
- end
24
- end
25
-
26
19
  shared_examples "a performance test" do |fixture, using_server|
27
- let(:free_port) { FreePort.next_free_port }
28
20
  let(:iterations) { using_server ? 100 : 1000 }
29
- subject(:emitter) { LoggregatorEmitter::Emitter.new("localhost:#{free_port}", "API", 42, "my-secret") }
30
21
 
31
22
  before do
32
- if using_server
33
- @server = FakeLoggregatorServer.new(free_port)
34
- @server.start
35
- else
36
- emitter.should_receive(:send_protobuffer).at_least(iterations).times
23
+ @emitter = LoggregatorEmitter::Emitter.new("localhost:#{@free_port}", "API", 42, "my-secret")
24
+ if !using_server
25
+ @emitter.should_receive(:send_protobuffer).at_least(iterations).times
37
26
  end
38
27
  end
39
28
 
40
- after do
41
- @server.stop if using_server
42
- end
43
-
44
29
  it "emits #{fixture.name} within a time threshold #{using_server ? 'with' : 'without'} server" do
45
30
  start_time = Time.now.to_f
46
31
 
47
- iterations.times { emitter.emit("my_app_id", fixture.message) }
32
+ iterations.times { @emitter.emit("my_app_id", fixture.message) }
48
33
 
49
34
  expect(Time.now.to_f - start_time).to be < fixture.expected(using_server)
50
35
  end
@@ -57,6 +42,17 @@ describe LoggregatorEmitter do
57
42
  @fixtures << MessageFixture.new("message with newlines", 10.times.collect { (6*1024).times.collect { "a" }.join("") + "\n" }.join(""), 3.0, 3.0)
58
43
  @fixtures << MessageFixture.new("message worst case", (124*1024).times.collect { "a" }.join("") + "\n", 2.0, 2.0)
59
44
 
45
+ before :all do
46
+ @free_port = 12346
47
+ @server = FakeLoggregatorServer.new(@free_port)
48
+ @server.start
49
+ end
50
+
51
+ after :all do
52
+ @server.stop
53
+ end
54
+
55
+
60
56
  [true, false].each do |using_server|
61
57
  @fixtures.each do |fixture|
62
58
  it_behaves_like "a performance test", fixture, using_server
@@ -2,14 +2,19 @@ require "support/fake_loggregator_server"
2
2
  require "loggregator_emitter"
3
3
 
4
4
  describe LoggregatorEmitter do
5
- class FreePort
6
- def self.next_free_port
7
- @@next_free_port ||= 12345
8
- @@next_free_port += 1
9
- end
5
+
6
+ before :all do
7
+ @free_port = 12345
8
+ @server = FakeLoggregatorServer.new(@free_port)
9
+ @server.start
10
+ end
11
+
12
+ after :all do
13
+ @server.stop
10
14
  end
11
- let(:free_port) do
12
- FreePort.next_free_port
15
+
16
+ before do
17
+ @server.reset
13
18
  end
14
19
 
15
20
  describe "configuring emitter" do
@@ -22,12 +27,8 @@ describe LoggregatorEmitter do
22
27
  expect { LoggregatorEmitter::Emitter.new("localhost:12345", LogMessage::SourceType::DEA) }.not_to raise_error
23
28
  end
24
29
 
25
- it "is valid if a server name is given" do
26
- expect { LoggregatorEmitter::Emitter.new("some-unknown-address:12345", LogMessage::SourceType::DEA) }.not_to raise_error
27
- end
28
-
29
30
  it "accepts a string as source type/name" do
30
- expect { LoggregatorEmitter::Emitter.new("some-unknown-address:12345", "STG") }.not_to raise_error
31
+ expect { LoggregatorEmitter::Emitter.new("localhost:12345", "STG") }.not_to raise_error
31
32
  end
32
33
  end
33
34
 
@@ -37,21 +38,29 @@ describe LoggregatorEmitter do
37
38
  expect { LoggregatorEmitter::Emitter.new("http://0.0.0.0:12345", LogMessage::SourceType::DEA) }.to raise_error(ArgumentError)
38
39
  end
39
40
 
41
+ it "raises if host is blank" do
42
+ expect { LoggregatorEmitter::Emitter.new(":12345", LogMessage::SourceType::DEA) }.to raise_error(ArgumentError)
43
+ end
44
+
45
+ it "raises if host is unresolvable" do
46
+ expect { LoggregatorEmitter::Emitter.new("i.cant.resolve.foo:12345", LogMessage::SourceType::DEA) }.to raise_error(ArgumentError)
47
+ end
48
+
40
49
  it "raises if source is an unknown integer" do
41
- expect { LoggregatorEmitter::Emitter.new("some-unknown-address:12345", 7) }.to raise_error(ArgumentError)
50
+ expect { LoggregatorEmitter::Emitter.new("localhost:12345", 7) }.to raise_error(ArgumentError)
42
51
  end
43
52
 
44
53
  it "raises if source is not an integer or string" do
45
- expect { LoggregatorEmitter::Emitter.new("some-unknown-address:12345", nil) }.to raise_error(ArgumentError)
46
- expect { LoggregatorEmitter::Emitter.new("some-unknown-address:12345", 12.0) }.to raise_error(ArgumentError)
54
+ expect { LoggregatorEmitter::Emitter.new("localhost:12345", nil) }.to raise_error(ArgumentError)
55
+ expect { LoggregatorEmitter::Emitter.new("localhost:12345", 12.0) }.to raise_error(ArgumentError)
47
56
  end
48
57
 
49
58
  it "raises if source is too large of a string" do
50
- expect { LoggregatorEmitter::Emitter.new("some-unknown-address:12345", "ABCD") }.to raise_error(ArgumentError)
59
+ expect { LoggregatorEmitter::Emitter.new("localhost:12345", "ABCD") }.to raise_error(ArgumentError)
51
60
  end
52
61
 
53
62
  it "raises if source is too small of a string" do
54
- expect { LoggregatorEmitter::Emitter.new("some-unknown-address:12345", "AB") }.to raise_error(ArgumentError)
63
+ expect { LoggregatorEmitter::Emitter.new("localhost:12345", "AB") }.to raise_error(ArgumentError)
55
64
  end
56
65
  end
57
66
  end
@@ -60,16 +69,7 @@ describe LoggregatorEmitter do
60
69
 
61
70
  describe "emit_log_envelope" do
62
71
  def make_emitter(host)
63
- LoggregatorEmitter::Emitter.new("#{host}:#{free_port}", LogMessage::SourceType::CLOUD_CONTROLLER, 42, "secret")
64
- end
65
-
66
- before do
67
- @server = FakeLoggregatorServer.new(free_port)
68
- @server.start
69
- end
70
-
71
- after do
72
- @server.stop
72
+ LoggregatorEmitter::Emitter.new("#{host}:#{@free_port}", LogMessage::SourceType::CLOUD_CONTROLLER, 42, "secret")
73
73
  end
74
74
 
75
75
  it "successfully writes envelope protobuffers" do
@@ -114,20 +114,11 @@ describe LoggregatorEmitter do
114
114
  {"emit" => LogMessage::MessageType::OUT, "emit_error" => LogMessage::MessageType::ERR}.each do |emit_method, message_type|
115
115
  describe "##{emit_method}" do
116
116
  def make_emitter(host)
117
- LoggregatorEmitter::Emitter.new("#{host}:#{free_port}", LogMessage::SourceType::CLOUD_CONTROLLER, 42)
118
- end
119
-
120
- before do
121
- @server = FakeLoggregatorServer.new(free_port)
122
- @server.start
123
- end
124
-
125
- after do
126
- @server.stop
117
+ LoggregatorEmitter::Emitter.new("#{host}:#{@free_port}", LogMessage::SourceType::CLOUD_CONTROLLER, 42)
127
118
  end
128
119
 
129
120
  it "successfully writes protobuffers using ipv4" do
130
- emitter = make_emitter("0.0.0.0")
121
+ emitter = make_emitter("127.0.0.1")
131
122
  emitter.send(emit_method, "my_app_id", "Hello there!")
132
123
  emitter.send(emit_method, "my_app_id", "Hello again!")
133
124
  emitter.send(emit_method, nil, "Hello again!")
@@ -208,14 +199,6 @@ describe LoggregatorEmitter do
208
199
  end
209
200
 
210
201
  describe "source" do
211
- before do
212
- @server = FakeLoggregatorServer.new(free_port)
213
- @server.start
214
- end
215
-
216
- after do
217
- @server.stop
218
- end
219
202
 
220
203
  let(:emit_message) do
221
204
  @emitter.emit_error("my_app_id", "Hello there!")
@@ -226,28 +209,28 @@ describe LoggregatorEmitter do
226
209
  end
227
210
 
228
211
  it "when type is known" do
229
- @emitter = LoggregatorEmitter::Emitter.new("0.0.0.0:#{free_port}", LogMessage::SourceType::CLOUD_CONTROLLER)
212
+ @emitter = LoggregatorEmitter::Emitter.new("0.0.0.0:#{@free_port}", LogMessage::SourceType::CLOUD_CONTROLLER)
230
213
  expect(emit_message.source_name).to eq "CLOUD_CONTROLLER"
231
214
  end
232
215
 
233
216
  it "when type is unknown" do
234
- @emitter = LoggregatorEmitter::Emitter.new("0.0.0.0:#{free_port}", "STG")
217
+ @emitter = LoggregatorEmitter::Emitter.new("0.0.0.0:#{@free_port}", "STG")
235
218
  expect(emit_message.source_name).to eq "STG"
236
219
  expect(emit_message.source_type).to eq LogMessage::SourceType::UNKNOWN
237
220
  end
238
221
 
239
222
  it "id can be nil" do
240
- @emitter = LoggregatorEmitter::Emitter.new("0.0.0.0:#{free_port}", LogMessage::SourceType::CLOUD_CONTROLLER)
223
+ @emitter = LoggregatorEmitter::Emitter.new("0.0.0.0:#{@free_port}", LogMessage::SourceType::CLOUD_CONTROLLER)
241
224
  expect(emit_message.source_id).to eq nil
242
225
  end
243
226
 
244
227
  it "id can be passed in as a string" do
245
- @emitter = LoggregatorEmitter::Emitter.new("0.0.0.0:#{free_port}", LogMessage::SourceType::CLOUD_CONTROLLER, "some_source_id")
228
+ @emitter = LoggregatorEmitter::Emitter.new("0.0.0.0:#{@free_port}", LogMessage::SourceType::CLOUD_CONTROLLER, "some_source_id")
246
229
  expect(emit_message.source_id).to eq "some_source_id"
247
230
  end
248
231
 
249
232
  it "id can be passed in as an integer" do
250
- @emitter = LoggregatorEmitter::Emitter.new("0.0.0.0:#{free_port}", LogMessage::SourceType::CLOUD_CONTROLLER, 13)
233
+ @emitter = LoggregatorEmitter::Emitter.new("0.0.0.0:#{@free_port}", LogMessage::SourceType::CLOUD_CONTROLLER, 13)
251
234
  expect(emit_message.source_id).to eq "13"
252
235
  end
253
236
  end
@@ -6,6 +6,7 @@ class FakeLoggregatorServer
6
6
  attr_reader :messages, :port
7
7
 
8
8
  def initialize(port)
9
+
9
10
  @messages = []
10
11
  @port = port
11
12
 
@@ -15,7 +16,7 @@ class FakeLoggregatorServer
15
16
  end
16
17
 
17
18
  def start
18
- bind_and_record(0, @sockets[0], "localhost")
19
+ bind_and_record(0, @sockets[0], "127.0.0.1")
19
20
  bind_and_record(0, @sockets[1], "::1")
20
21
  end
21
22
 
@@ -33,10 +34,14 @@ class FakeLoggregatorServer
33
34
  @threads.each { |thread| Thread.kill(thread) }
34
35
  end
35
36
 
37
+ def reset
38
+ @messages = []
39
+ end
40
+
36
41
  private
37
42
 
38
43
  def bind_and_record(index, socket, server)
39
- socket.bind(server, port)
44
+ socket.bind(server, @port)
40
45
 
41
46
  @threads[index] = Thread.new do
42
47
  while true
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: loggregator_emitter
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-11-05 00:00:00.000000000 Z
12
+ date: 2013-12-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: beefcake
@@ -119,9 +119,6 @@ required_rubygems_version: !ruby/object:Gem::Requirement
119
119
  - - ! '>='
120
120
  - !ruby/object:Gem::Version
121
121
  version: '0'
122
- segments:
123
- - 0
124
- hash: 584721548114788876
125
122
  requirements: []
126
123
  rubyforge_project:
127
124
  rubygems_version: 1.8.25