gelf_redux 3.2.2 → 4.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 04ecbd3c8710b26b4c7b2f292e17a15837e1879cd8b807671bfa18110994fb73
4
- data.tar.gz: efc1b6c00d89f2286f42dacd4693fb8ed2e1476e0f1329a0740e914486d48424
3
+ metadata.gz: aec46d611ec7a79e16d8a48a91d95eb95d0c296e50ee9d0b6c26dd3533c5226b
4
+ data.tar.gz: c533a7819a33d5290652c91ea2aa6c4d2d58d7a5cd2a582da46d41964e8d0278
5
5
  SHA512:
6
- metadata.gz: 004e466c37f91d3c4da2b63da20f161d32ded4ee01496a992f7466f36c103ce82943d9baa515231ff461ccd60a8421c21e829fa1b066e26c5e600dff5350796d
7
- data.tar.gz: 1c583a57fbab8c0749d7e9b580a612e52bc3a0dc10c9b9c50580139a5c38021ba651b76d83b7d5456a8a2748b5575a9ea405e8bc4f1248735b144e5193a900bf
6
+ metadata.gz: 4b4e41d8fd082bbb4970957df73a1591e2cec1dd6addfa0aa3bdc0e862d1d7d401541333394f2d1e79b4026bd8db8ebe9f1961d58ce35618cbcb4240b1284924
7
+ data.tar.gz: a4d3426cff78ec20f00d28e5e5dafa5e9e37d040ddff4a3cd7269740d2bee23ab07551ef7bbcd0a3ff03f39bde40d7ce0e2a043f0dcf962754020008b7821281
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- ## GELF (anew) Ruby library
1
+ ## GELF (redux) Ruby library
2
2
  This gem is intended to replace the existing and unmaintained gelf-rb https://github.com/graylog-labs/gelf-rb - since
3
3
  the project went silent for a few years and there seems to be no intention in continuing any kind of support
4
4
  (https://github.com/graylog-labs/gelf-rb/issues/93). So we decided to not just fork but set up a new gem.
@@ -65,6 +65,16 @@ Since it's compatible with the Logger interface, you can also use it in your Rai
65
65
  (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
66
66
  * Send me a pull request. Bonus points for topic branches.
67
67
 
68
- ## Copyright
68
+ ## Contributions
69
+ * Feel free to open up pull request
69
70
 
70
- Copyright (c) 2010-2016 Lennart Koopmann and Alexey Palazhchenko. See LICENSE for details.
71
+ ### Running the tests
72
+ * install docker with compose
73
+
74
+ ```bash
75
+ # install gems
76
+ docker-compose run app bundle
77
+
78
+ # run tests
79
+ docker-compose up
80
+ ```
data/lib/gelf/logger.rb CHANGED
@@ -14,7 +14,8 @@ module GELF
14
14
  progname = default_options['facility']
15
15
  end
16
16
 
17
- message_hash = { 'facility' => progname }
17
+ message_hash = {}
18
+ message_hash['facility'] = progname if progname
18
19
 
19
20
  if message.is_a?(Hash)
20
21
  message.each do |key, value|
data/lib/gelf/notifier.rb CHANGED
@@ -1,12 +1,19 @@
1
1
  require 'gelf/transport/udp'
2
2
  require 'gelf/transport/tcp'
3
3
  require 'gelf/transport/tcp_tls'
4
+ require 'gelf/transport/http'
4
5
  require 'gelf/transport/https'
5
6
 
6
7
  # replace JSON and #to_json with Yajl if available
7
8
  begin
8
- require 'yajl/json_gem'
9
+ require 'yajl'
10
+ def json_dump(obj)
11
+ Yajl.dump(obj)
12
+ end
9
13
  rescue LoadError
14
+ def json_dump(obj)
15
+ JSON.dump(obj)
16
+ end
10
17
  end
11
18
 
12
19
  module GELF
@@ -36,7 +43,6 @@ module GELF
36
43
  self.default_options['version'] = SPEC_VERSION
37
44
  self.default_options['host'] ||= Socket.gethostname
38
45
  self.default_options['level'] ||= GELF::UNKNOWN
39
- self.default_options['facility'] ||= 'gelf-rb'
40
46
  self.default_options['protocol'] ||= GELF::Protocol::UDP
41
47
 
42
48
  self.level_mapping = :logger
@@ -59,12 +65,12 @@ module GELF
59
65
  # Default (safe) value is 'WAN'.
60
66
  def max_chunk_size=(size)
61
67
  case size.to_s.downcase
62
- when 'wan'
63
- @max_chunk_size = MAX_CHUNK_SIZE_WAN
64
- when 'lan'
65
- @max_chunk_size = MAX_CHUNK_SIZE_LAN
66
- else
67
- @max_chunk_size = size.to_int
68
+ when 'wan'
69
+ @max_chunk_size = MAX_CHUNK_SIZE_WAN
70
+ when 'lan'
71
+ @max_chunk_size = MAX_CHUNK_SIZE_LAN
72
+ else
73
+ @max_chunk_size = size.to_int
68
74
  end
69
75
  end
70
76
 
@@ -84,12 +90,12 @@ module GELF
84
90
  # Default (compatible) value is 'logger'.
85
91
  def level_mapping=(mapping)
86
92
  case mapping.to_s.downcase
87
- when 'logger'
88
- @level_mapping = GELF::LOGGER_MAPPING
89
- when 'direct'
90
- @level_mapping = GELF::DIRECT_MAPPING
91
- else
92
- @level_mapping = mapping
93
+ when 'logger'
94
+ @level_mapping = GELF::LOGGER_MAPPING
95
+ when 'direct'
96
+ @level_mapping = GELF::DIRECT_MAPPING
97
+ else
98
+ @level_mapping = mapping
93
99
  end
94
100
  end
95
101
 
@@ -133,11 +139,12 @@ module GELF
133
139
  end
134
140
  end
135
141
 
136
- private
142
+ private
137
143
 
138
144
  def create_sender(host, port)
139
- return create_https(host, port) if default_options['protocol'] == GELF::Protocol::HTTPS
140
-
145
+ if [GELF::Protocol::HTTP, GELF::Protocol::HTTPS].include? default_options['protocol']
146
+ return create_http(host, port)
147
+ end
141
148
  addresses = [[host, port]]
142
149
  if default_options['protocol'] == GELF::Protocol::TCP
143
150
  if default_options.key?('tls')
@@ -151,8 +158,13 @@ module GELF
151
158
  end
152
159
  end
153
160
 
154
- def create_https(host, port)
155
- GELF::Transport::HTTPS.new host, port: port, path: default_options.delete('path')
161
+ def create_http(host, port)
162
+ klass = if default_options['protocol'] == GELF::Protocol::HTTPS
163
+ GELF::Transport::HTTPS
164
+ else
165
+ GELF::Transport::HTTP
166
+ end
167
+ klass.new host, port: port, path: default_options.delete('path'), headers: default_options.delete('headers')
156
168
  end
157
169
 
158
170
  def notify_with_level(message_level, *args)
@@ -168,11 +180,12 @@ module GELF
168
180
  hash = extract_hash(*args)
169
181
  hash['level'] = message_level unless message_level.nil?
170
182
  if hash['level'] >= level
171
- return @sender.transfer(hash) if default_options['protocol'] == GELF::Protocol::HTTPS
172
-
183
+ if [GELF::Protocol::HTTP, GELF::Protocol::HTTPS].include? default_options['protocol']
184
+ return @sender.transfer(hash)
185
+ end
173
186
  if default_options['protocol'] == GELF::Protocol::TCP
174
187
  validate_hash(hash)
175
- @sender.send(hash.to_json + "\0")
188
+ @sender.send(json_dump(hash) + "\0")
176
189
  else
177
190
  @sender.send_datagrams(datagrams_from_hash(hash))
178
191
  end
@@ -190,7 +203,9 @@ module GELF
190
203
  { 'short_message' => object.to_s }
191
204
  end
192
205
 
193
- hash = default_options.merge(self.class.stringify_keys(args.merge(primary_data)))
206
+ default_message_data = default_options.dup
207
+ default_message_data.delete('protocol')
208
+ hash = default_message_data.merge(self.class.stringify_keys(args.merge(primary_data)))
194
209
  convert_hoptoad_keys_to_graylog2(hash)
195
210
  set_file_and_line(hash) if @collect_file_and_line
196
211
  set_timestamp(hash)
@@ -269,7 +284,7 @@ module GELF
269
284
  def serialize_hash(hash)
270
285
  validate_hash(hash)
271
286
 
272
- Zlib::Deflate.deflate(hash.to_json).bytes
287
+ Zlib::Deflate.deflate(json_dump(hash)).bytes
273
288
  end
274
289
 
275
290
  def self.stringify_keys(data)
@@ -0,0 +1,37 @@
1
+ module GELF
2
+ module Transport
3
+ class HTTP
4
+ def initialize(host, port: 80, path: '', headers: nil)
5
+ @uri = URI::HTTP.build host: host, port: port
6
+ @headers = headers
7
+ @path = path
8
+ end
9
+
10
+ def start_connection
11
+ @uri.path = "/#{@path.to_s.delete_prefix('/')}"
12
+ @http = Net::HTTP.new(@uri.host, @uri.port)
13
+ @http.use_ssl = true if @uri.instance_of? URI::HTTPS
14
+ given_headers = @headers || {}
15
+ @headers = default_headers.merge(given_headers)
16
+ end
17
+
18
+ def transfer(message)
19
+ start_connection
20
+ request = Net::HTTP::Post.new(@uri.request_uri)
21
+ request.body = json_dump(message)
22
+ @headers.each do |key, value|
23
+ # pass header only if value present -> allows clearing default headers
24
+ request[key] = value if value
25
+ end
26
+ @http.request(request)
27
+ end
28
+
29
+ def default_headers
30
+ {
31
+ 'Accept' => 'application/json',
32
+ 'Content-Type' => 'application/json'
33
+ }
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,11 @@
1
+ module GELF
2
+ module Transport
3
+ class HTTPS < HTTP
4
+ def initialize(host, port: 443, path: nil, headers: nil)
5
+ @uri = URI::HTTPS.build host: host, port: port
6
+ @headers = headers
7
+ @path = path
8
+ end
9
+ end
10
+ end
11
+ end
data/lib/gelf.rb CHANGED
@@ -4,11 +4,12 @@ require 'zlib'
4
4
  require 'digest/md5'
5
5
 
6
6
  module GELF
7
- SPEC_VERSION = '1.0'
7
+ SPEC_VERSION = '1.1'
8
8
  module Protocol
9
9
  UDP = 0
10
10
  TCP = 1
11
- HTTPS = 2
11
+ HTTP = 2
12
+ HTTPS = 3
12
13
  end
13
14
  end
14
15
 
data/lib/gelf_redux.rb ADDED
@@ -0,0 +1 @@
1
+ require 'gelf'
metadata CHANGED
@@ -1,19 +1,20 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gelf_redux
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.2.2
4
+ version: 4.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexey Palazhchenko
8
8
  - Lennart Koopmann
9
9
  - Zac Sprackett
10
10
  - Marcus Ilgner
11
- - Sebastian Seidel
12
11
  - Christian Rolle
12
+ - Sebastian Seidel
13
+ - Ronald Sacher
13
14
  autorequire:
14
15
  bindir: bin
15
16
  cert_chain: []
16
- date: 2023-06-19 00:00:00.000000000 Z
17
+ date: 2023-06-27 00:00:00.000000000 Z
17
18
  dependencies:
18
19
  - !ruby/object:Gem::Dependency
19
20
  name: shoulda
@@ -21,56 +22,98 @@ dependencies:
21
22
  requirements:
22
23
  - - "~>"
23
24
  - !ruby/object:Gem::Version
24
- version: 2.11.3
25
+ version: 4.0.0
25
26
  type: :development
26
27
  prerelease: false
27
28
  version_requirements: !ruby/object:Gem::Requirement
28
29
  requirements:
29
30
  - - "~>"
30
31
  - !ruby/object:Gem::Version
31
- version: 2.11.3
32
+ version: 4.0.0
32
33
  - !ruby/object:Gem::Dependency
33
- name: rack
34
+ name: mocha
34
35
  requirement: !ruby/object:Gem::Requirement
35
36
  requirements:
36
- - - "<"
37
+ - - "~>"
37
38
  - !ruby/object:Gem::Version
38
- version: '2.0'
39
+ version: 2.0.4
39
40
  type: :development
40
41
  prerelease: false
41
42
  version_requirements: !ruby/object:Gem::Requirement
42
43
  requirements:
43
- - - "<"
44
+ - - "~>"
44
45
  - !ruby/object:Gem::Version
45
- version: '2.0'
46
+ version: 2.0.4
46
47
  - !ruby/object:Gem::Dependency
47
- name: mocha
48
+ name: test-unit
48
49
  requirement: !ruby/object:Gem::Requirement
49
50
  requirements:
50
51
  - - "~>"
51
52
  - !ruby/object:Gem::Version
52
- version: 1.1.0
53
+ version: 3.6.0
53
54
  type: :development
54
55
  prerelease: false
55
56
  version_requirements: !ruby/object:Gem::Requirement
56
57
  requirements:
57
58
  - - "~>"
58
59
  - !ruby/object:Gem::Version
59
- version: 1.1.0
60
+ version: 3.6.0
60
61
  - !ruby/object:Gem::Dependency
61
- name: test-unit
62
+ name: simplecov
62
63
  requirement: !ruby/object:Gem::Requirement
63
64
  requirements:
64
65
  - - "~>"
65
66
  - !ruby/object:Gem::Version
66
- version: 3.2.0
67
+ version: 0.20.0
67
68
  type: :development
68
69
  prerelease: false
69
70
  version_requirements: !ruby/object:Gem::Requirement
70
71
  requirements:
71
72
  - - "~>"
72
73
  - !ruby/object:Gem::Version
73
- version: 3.2.0
74
+ version: 0.20.0
75
+ - !ruby/object:Gem::Dependency
76
+ name: webmock
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: 3.18.1
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: 3.18.1
89
+ - !ruby/object:Gem::Dependency
90
+ name: timecop
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - "~>"
94
+ - !ruby/object:Gem::Version
95
+ version: 0.9.6
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - "~>"
101
+ - !ruby/object:Gem::Version
102
+ version: 0.9.6
103
+ - !ruby/object:Gem::Dependency
104
+ name: rake
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
74
117
  - !ruby/object:Gem::Dependency
75
118
  name: json
76
119
  requirement: !ruby/object:Gem::Requirement
@@ -86,37 +129,24 @@ dependencies:
86
129
  - !ruby/object:Gem::Version
87
130
  version: '0'
88
131
  description: Library to send GELF messages to Graylog logging server. Supports plain-text,
89
- GELF messages and exceptions via UDP, TCP and HTTPS
132
+ GELF messages and exceptions via UDP, TCP and HTTP(S)
90
133
  email: admins@manet-marketing.de
91
134
  executables: []
92
135
  extensions: []
93
- extra_rdoc_files:
94
- - LICENSE
95
- - README.md
136
+ extra_rdoc_files: []
96
137
  files:
97
- - ".gemtest"
98
- - CHANGELOG
99
- - CONTRIBUTING.md
100
- - Gemfile
101
- - Gemfile.lock
102
138
  - LICENSE
103
139
  - README.md
104
- - Rakefile
105
- - VERSION
106
- - benchmarks/notifier.rb
107
- - gelf.gemspec
108
140
  - lib/gelf.rb
109
141
  - lib/gelf/logger.rb
110
142
  - lib/gelf/notifier.rb
111
143
  - lib/gelf/severity.rb
144
+ - lib/gelf/transport/http.rb
145
+ - lib/gelf/transport/https.rb
112
146
  - lib/gelf/transport/tcp.rb
113
147
  - lib/gelf/transport/tcp_tls.rb
114
148
  - lib/gelf/transport/udp.rb
115
- - test/helper.rb
116
- - test/test_logger.rb
117
- - test/test_notifier.rb
118
- - test/test_ruby_sender.rb
119
- - test/test_severity.rb
149
+ - lib/gelf_redux.rb
120
150
  homepage: https://github.com/manet-marketing/gelf_redux
121
151
  licenses:
122
152
  - MIT
@@ -129,7 +159,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
129
159
  requirements:
130
160
  - - ">="
131
161
  - !ruby/object:Gem::Version
132
- version: '1.9'
162
+ version: '2.6'
133
163
  required_rubygems_version: !ruby/object:Gem::Requirement
134
164
  requirements:
135
165
  - - ">="
data/.gemtest DELETED
File without changes
data/CHANGELOG DELETED
@@ -1,54 +0,0 @@
1
- 3.0.0, 2016-08-21
2
- + Overhaul TCP support
3
- + Include automatic support for Celluloid::IO if available
4
- + Add TLS support to TCP transport
5
- - Remove support for ancient Rubygems versions
6
- - Remove support for Ruby 1.9.2 (1.9.3 works!)
7
- - Remove already-deprecated `host` and `port` methods on GELF::Notifier
8
-
9
- 2.0.0, 2016-02-02
10
- + Added GELF TCP support.
11
-
12
- 1.3.2, 2011-12-02:
13
- * support for rubygems-test.
14
- * rescue from more network errors.
15
-
16
- 1.3.1, 2011-10-28:
17
- + allow to rescue from network errors.
18
-
19
- 1.3.0, 2011-07-27:
20
- + allow to set timestamp manually.
21
-
22
- 1.2.0.beta1, 2011-05-23:
23
- + compatibility with GELF specification 1.0:
24
- * requires modern graylog2-server and graylog2-web-interface;
25
- + Notifier#default_options, Notifier#default_options=;
26
- + severity (level) threshold;
27
- + automatically set 'file', 'line' and 'timestamp' fields;
28
- + wrappers for GELF::Notifier#notify with severity:
29
- + GELF::Notifier.debug
30
- + GELF::Notifier.info
31
- + GELF::Notifier.warn
32
- + GELF::Notifier.error
33
- + GELF::Notifier.fatal
34
- + GELF::Notifier.unknown
35
- + full compatibility with Ruby Logger and other loggers:
36
- + GELF::Logger#fatal { "Argument 'foo' not given." }
37
- + GELF::Logger#error "Argument #{ @foo } mismatch."
38
- + GELF::Logger#info('initialize') { "Initializing..." }
39
- + GELF::Logger#add(GELF::FATAL) { 'Fatal error!' }
40
- + GELF::Logger#close
41
- + GELF::Logger#level = GELF::INFO
42
- + allow to change severity mapping;
43
- + send messages to receivers in round-robin;
44
- * GELF::Notifier#host and #port are attr_readers now and deprecated (were attr_accessor);
45
- + allow to disable file and line collection (GELF::Notifier#collect_file_and_line = false);
46
- - deprecated Gelf class removed.
47
-
48
- 1.0.2, 2010-11-29:
49
- 1.0.1, 2010-11-29:
50
- - added more tests for chunking in attempt to locate not existing bug.
51
-
52
- 1.0.0, 2010-11-10:
53
- + initial stable version;
54
- * deprecated Gelf class is still there.
data/CONTRIBUTING.md DELETED
@@ -1 +0,0 @@
1
- Please follow [the instructions on graylog.org](https://www.graylog.org/contributing-to-graylog/).
data/Gemfile DELETED
@@ -1,10 +0,0 @@
1
- source "https://rubygems.org"
2
-
3
- group :development do
4
- gem "shoulda", "~> 2.11.3"
5
- # Because of a dependency chain jeweler->github_api->oauth2->rack,
6
- # pin the version: Rack 2.0.x doesn't work on < Ruby 2.2
7
- gem 'rack', '< 2.0'
8
- gem "mocha", "~> 1.1.0"
9
- gem "test-unit", "~> 3.2.0"
10
- end
data/Gemfile.lock DELETED
@@ -1,24 +0,0 @@
1
- GEM
2
- remote: https://rubygems.org/
3
- specs:
4
- metaclass (0.0.4)
5
- mocha (1.1.0)
6
- metaclass (~> 0.0.1)
7
- power_assert (0.3.0)
8
- rack (1.6.4)
9
- shoulda (2.11.3)
10
- test-unit (3.2.1)
11
- power_assert
12
-
13
- PLATFORMS
14
- java
15
- ruby
16
-
17
- DEPENDENCIES
18
- mocha (~> 1.1.0)
19
- rack (< 2.0)
20
- shoulda (~> 2.11.3)
21
- test-unit (~> 3.2.0)
22
-
23
- BUNDLED WITH
24
- 1.17.3
data/Rakefile DELETED
@@ -1,41 +0,0 @@
1
- require 'rake'
2
-
3
- begin
4
- require 'ci/reporter/rake/test_unit'
5
- rescue LoadError
6
- # nothing
7
- end
8
-
9
- require 'rake/testtask'
10
- Rake::TestTask.new(:test) do |test|
11
- test.libs << 'lib' << 'test'
12
- test.pattern = 'test/**/test_*.rb'
13
- test.verbose = true
14
- end
15
-
16
- task :default => :test
17
-
18
- begin
19
- require 'rcov/rcovtask'
20
- Rcov::RcovTask.new do |test|
21
- test.libs << 'test'
22
- test.pattern = 'test/**/test_*.rb'
23
- test.rcov_opts << '--exclude gem'
24
- test.verbose = true
25
- end
26
- rescue LoadError => e
27
- task :rcov do
28
- puts e
29
- abort "rcov is not available. Run: gem install rcov"
30
- end
31
- end
32
-
33
- #require 'rake/rdoctask'
34
- #Rake::RDocTask.new do |rdoc|
35
- # version = File.exist?('VERSION') ? File.read('VERSION') : ""
36
- #
37
- # rdoc.rdoc_dir = 'rdoc'
38
- # rdoc.title = "gelf #{version}"
39
- # rdoc.rdoc_files.include('README*')
40
- # rdoc.rdoc_files.include('lib/**/*.rb')
41
- #end
data/VERSION DELETED
@@ -1 +0,0 @@
1
- 3.1.0
@@ -1,39 +0,0 @@
1
- #! /usr/bin/env ruby
2
-
3
- puts "Loading..."
4
-
5
- require 'benchmark'
6
- require 'rubygems'
7
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
8
- $LOAD_PATH.unshift(File.dirname(__FILE__))
9
- require 'gelf'
10
-
11
- puts "Generating random data..."
12
- srand(1)
13
- RANDOM_DATA = ('A'..'z').to_a
14
- k3_message = (1..3*1024).map { RANDOM_DATA[rand(RANDOM_DATA.count)] }.join
15
-
16
- TARGET_HOST = 'localhost'
17
- TARGET_PORT = 12201
18
- DEFAULT_OPTIONS = { '_host' => 'localhost' }
19
- TIMES = 5000
20
-
21
- SHORT_HASH = { 'short_message' => 'message' }
22
- LONG_HASH = { 'short_message' => 'message', 'long_message' => k3_message }
23
-
24
-
25
- notifier_lan = GELF::Notifier.new(TARGET_HOST, TARGET_PORT, 'LAN', DEFAULT_OPTIONS)
26
- notifier_wan = GELF::Notifier.new(TARGET_HOST, TARGET_PORT, 'WAN', DEFAULT_OPTIONS)
27
-
28
- # to create mongo collections, etc.
29
- notifier_lan.notify!(LONG_HASH)
30
- sleep(5)
31
-
32
- puts "Sending #{TIMES} notifications...\n"
33
- tms = Benchmark.bm(25) do |b|
34
- b.report('lan, short data, 1 chunk ') { TIMES.times { notifier_lan.notify!(SHORT_HASH) } }
35
- sleep(5)
36
- b.report('lan, long data, 1 chunk ') { TIMES.times { notifier_lan.notify!(LONG_HASH) } }
37
- sleep(5)
38
- b.report('wan, long data, 2 chunks') { TIMES.times { notifier_wan.notify!(LONG_HASH) } }
39
- end
data/gelf.gemspec DELETED
@@ -1,73 +0,0 @@
1
- Gem::Specification.new do |s|
2
- s.name = "gelf_redux"
3
- s.version = "3.2.2"
4
-
5
- s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
6
- s.required_ruby_version = ">= 1.9"
7
- s.require_paths = ["lib"]
8
- s.authors = ["Alexey Palazhchenko", "Lennart Koopmann", "Zac Sprackett", "Marcus Ilgner", "Sebastian Seidel", "Christian Rolle"]
9
- s.date = "2023-06-19"
10
- s.description = "Library to send GELF messages to Graylog logging server. Supports plain-text, GELF messages and exceptions via UDP, TCP and HTTPS"
11
- s.email = "admins@manet-marketing.de"
12
- s.extra_rdoc_files = [
13
- "LICENSE",
14
- "README.md"
15
- ]
16
- s.files = [
17
- ".gemtest",
18
- "CHANGELOG",
19
- "CONTRIBUTING.md",
20
- "Gemfile",
21
- "Gemfile.lock",
22
- "LICENSE",
23
- "README.md",
24
- "Rakefile",
25
- "VERSION",
26
- "benchmarks/notifier.rb",
27
- "gelf.gemspec",
28
- "lib/gelf.rb",
29
- "lib/gelf/logger.rb",
30
- "lib/gelf/notifier.rb",
31
- "lib/gelf/severity.rb",
32
- "lib/gelf/transport/tcp.rb",
33
- "lib/gelf/transport/tcp_tls.rb",
34
- "lib/gelf/transport/udp.rb",
35
- "test/helper.rb",
36
- "test/test_logger.rb",
37
- "test/test_notifier.rb",
38
- "test/test_ruby_sender.rb",
39
- "test/test_severity.rb"
40
- ]
41
- s.homepage = "https://github.com/manet-marketing/gelf_redux"
42
- s.licenses = ["MIT"]
43
- s.rubygems_version = "2.5.1"
44
- s.summary = "Library to send GELF messages to Graylog logging server."
45
-
46
- if s.respond_to? :specification_version then
47
- s.specification_version = 4
48
-
49
- if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('3.2.0') then
50
- s.add_development_dependency(%q<typhoeus>, ["~> 1.4.0"])
51
- end
52
- if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
53
- s.add_development_dependency(%q<shoulda>, ["~> 2.11.3"])
54
- s.add_development_dependency(%q<rack>, ["< 2.0"])
55
- s.add_development_dependency(%q<mocha>, ["~> 1.1.0"])
56
- s.add_development_dependency(%q<test-unit>, ["~> 3.2.0"])
57
- s.add_runtime_dependency(%q<json>, [">= 0"])
58
- else
59
- s.add_dependency(%q<shoulda>, ["~> 2.11.3"])
60
- s.add_dependency(%q<rack>, ["< 2.0"])
61
- s.add_dependency(%q<mocha>, ["~> 1.1.0"])
62
- s.add_dependency(%q<test-unit>, ["~> 3.2.0"])
63
- s.add_dependency(%q<json>, [">= 0"])
64
- end
65
- else
66
- s.add_dependency(%q<shoulda>, ["~> 2.11.3"])
67
- s.add_dependency(%q<rack>, ["< 2.0"])
68
- s.add_dependency(%q<mocha>, ["~> 1.1.0"])
69
- s.add_dependency(%q<test-unit>, ["~> 3.2.0"])
70
- s.add_dependency(%q<json>, [">= 0"])
71
- end
72
- end
73
-
data/test/helper.rb DELETED
@@ -1,11 +0,0 @@
1
- require 'rubygems'
2
- require 'test/unit'
3
- require 'shoulda'
4
- require 'mocha/setup'
5
-
6
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
7
- $LOAD_PATH.unshift(File.dirname(__FILE__))
8
- require 'gelf'
9
-
10
- class Test::Unit::TestCase
11
- end
data/test/test_logger.rb DELETED
@@ -1,247 +0,0 @@
1
- require 'helper'
2
-
3
- class TestLogger < Test::Unit::TestCase
4
- context "with logger with mocked sender" do
5
- setup do
6
- Socket.stubs(:gethostname).returns('stubbed_hostname')
7
- @logger = GELF::Logger.new
8
- @sender = mock
9
- @logger.instance_variable_set('@sender', @sender)
10
- end
11
-
12
- should "respond to #close" do
13
- assert @logger.respond_to?(:close)
14
- end
15
-
16
- context "#add" do
17
- # logger.add(Logger::INFO, nil)
18
- should 'implement add method with level, message and facility from defaults' do
19
- @logger.expects(:notify_with_level!).with do |level, hash|
20
- level == GELF::INFO &&
21
- hash['short_message'] == 'gelf-rb' &&
22
- hash['facility'] == 'gelf-rb'
23
- end
24
- @logger.add(GELF::INFO, nil)
25
- end
26
-
27
- # logger.add(Logger::INFO, 'Message')
28
- should "implement add method with level and message from parameters, facility from defaults" do
29
- @logger.expects(:notify_with_level!).with do |level, hash|
30
- level == GELF::INFO &&
31
- hash['short_message'] == 'Message' &&
32
- hash['facility'] == 'gelf-rb'
33
- end
34
- @logger.add(GELF::INFO, nil, 'Message')
35
- end
36
-
37
- # logger.add(Logger::INFO, RuntimeError.new('Boom!'))
38
- should "implement add method with level and exception from parameters, facility from defaults" do
39
- @logger.expects(:notify_with_level!).with do |level, hash|
40
- level == GELF::INFO &&
41
- hash['short_message'] == 'RuntimeError: Boom!' &&
42
- hash['full_message'] =~ /^Backtrace/ &&
43
- hash['facility'] == 'gelf-rb'
44
- end
45
- @logger.add(GELF::INFO, nil, RuntimeError.new('Boom!'))
46
- end
47
-
48
- # logger.add(Logger::INFO) { 'Message' }
49
- should "implement add method with level from parameter, message from block, facility from defaults" do
50
- @logger.expects(:notify_with_level!).with do |level, hash|
51
- level == GELF::INFO &&
52
- hash['short_message'] == 'Message' &&
53
- hash['facility'] == 'gelf-rb'
54
- end
55
- @logger.add(GELF::INFO, nil, nil) { 'Message' }
56
- end
57
-
58
- # logger.add(Logger::INFO) { RuntimeError.new('Boom!') }
59
- should "implement add method with level from parameter, exception from block, facility from defaults" do
60
- @logger.expects(:notify_with_level!).with do |level, hash|
61
- level == GELF::INFO &&
62
- hash['short_message'] == 'RuntimeError: Boom!' &&
63
- hash['full_message'] =~ /^Backtrace/ &&
64
- hash['facility'] == 'gelf-rb'
65
- end
66
- @logger.add(GELF::INFO, nil, nil) { RuntimeError.new('Boom!') }
67
- end
68
-
69
- # logger.add(Logger::INFO, 'Message', 'Facility')
70
- should "implement add method with level, message and facility from parameters" do
71
- @logger.expects(:notify_with_level!).with do |level, hash|
72
- level == GELF::INFO &&
73
- hash['short_message'] == 'Message' &&
74
- hash['facility'] == 'Facility'
75
- end
76
- @logger.add(GELF::INFO, 'Message', 'Facility')
77
- end
78
-
79
- # logger.add(Logger::INFO, 'Message', nil)
80
- should "use facility from initialization if facility is nil" do
81
- logger = GELF::Logger.new('localhost', 12202, 'WAN', :facility => 'foo-bar')
82
- logger.expects(:notify_with_level!).with do |level, hash|
83
- level == GELF::INFO &&
84
- hash['short_message'] == 'Message' &&
85
- hash['facility'] == 'foo-bar'
86
- end
87
- logger.add(GELF::INFO, 'Message', nil)
88
- end
89
-
90
- # logger.add(Logger::INFO, 'Message', nil)
91
- should "use default facility if facility is nil" do
92
- @logger.expects(:notify_with_level!).with do |level, hash|
93
- level == GELF::INFO &&
94
- hash['short_message'] == 'Message' &&
95
- hash['facility'] == 'gelf-rb'
96
- end
97
- @logger.add(GELF::INFO, 'Message', nil)
98
- end
99
-
100
- # logger.add(Logger::INFO, RuntimeError.new('Boom!'), 'Facility')
101
- should "implement add method with level, exception and facility from parameters" do
102
- @logger.expects(:notify_with_level!).with do |level, hash|
103
- level == GELF::INFO &&
104
- hash['short_message'] == 'RuntimeError: Boom!' &&
105
- hash['full_message'] =~ /^Backtrace/ &&
106
- hash['facility'] == 'Facility'
107
- end
108
- @logger.add(GELF::INFO, RuntimeError.new('Boom!'), 'Facility')
109
- end
110
-
111
- # logger.add(Logger::INFO, nil, 'Facility') { 'Message' }
112
- should "implement add method with level and facility from parameters, message from block" do
113
- @logger.expects(:notify_with_level!).with do |level, hash|
114
- level == GELF::INFO &&
115
- hash['short_message'] == 'Message' &&
116
- hash['facility'] == 'Facility'
117
- end
118
- @logger.add(GELF::INFO, nil, 'Facility') { 'Message' }
119
- end
120
-
121
- # logger.add(Logger::INFO, nil, 'Facility') { RuntimeError.new('Boom!') }
122
- should "implement add method with level and facility from parameters, exception from block" do
123
- @logger.expects(:notify_with_level!).with do |level, hash|
124
- level == GELF::INFO &&
125
- hash['short_message'] == 'RuntimeError: Boom!' &&
126
- hash['full_message'] =~ /^Backtrace/ &&
127
- hash['facility'] == 'Facility'
128
- end
129
- @logger.add(GELF::INFO, nil, 'Facility') { RuntimeError.new('Boom!') }
130
- end
131
-
132
- # logger.add(Logger::INFO, { :short_message => "Some message" })
133
- should "implement add method with level and message from hash, facility from defaults" do
134
- @logger.expects(:notify_with_level!).with do |level, hash|
135
- level == GELF::INFO &&
136
- hash['short_message'] == 'Some message' &&
137
- hash['facility'] == 'gelf-rb'
138
- end
139
- @logger.add(GELF::INFO, { :short_message => "Some message" })
140
- end
141
-
142
- # logger.add(Logger::INFO, { :short_message => "Some message", :_foo => "bar", "_zomg" => "wat" })
143
- should "implement add method with level and message from hash, facility from defaults and some additional fields" do
144
- @logger.expects(:notify_with_level!).with do |level, hash|
145
- level == GELF::INFO &&
146
- hash['short_message'] == 'Some message' &&
147
- hash['facility'] == 'gelf-rb' &&
148
- hash['_foo'] == 'bar' &&
149
- hash['_zomg'] == 'wat'
150
- end
151
- @logger.add(GELF::INFO, { :short_message => "Some message", :_foo => "bar", "_zomg" => "wat"})
152
- end
153
-
154
- # logger.add(Logger::INFO, { :short_message => "Some message", :_foo => "bar", "_zomg" => "wat" }, 'somefac')
155
- should "implement add method with level and message from hash, facility from parameters and some additional fields" do
156
- @logger.expects(:notify_with_level!).with do |level, hash|
157
- level == GELF::INFO &&
158
- hash['short_message'] == 'Some message' &&
159
- hash['facility'] == 'somefac' &&
160
- hash['_foo'] == 'bar' &&
161
- hash['_zomg'] == 'wat'
162
- end
163
- @logger.add(GELF::INFO, { :short_message => "Some message", :_foo => "bar", "_zomg" => "wat"}, "somefac")
164
- end
165
-
166
- should 'implement add method with level and ignore zero-length message strings' do
167
- @logger.expects(:notify_with_level!).never
168
- @logger.add(GELF::INFO, '')
169
- end
170
-
171
- should 'implement add method with level and ignore hash without short_message key' do
172
- @logger.expects(:notify_with_level!).never
173
- @logger.add(GELF::INFO, { :message => 'Some message' })
174
- end
175
-
176
- should 'implement add method with level and ignore hash with zero-length short_message entry' do
177
- @logger.expects(:notify_with_level!).never
178
- @logger.add(GELF::INFO, { :short_message => '' })
179
- end
180
-
181
- should 'implement add method with level and ignore hash with nil short_message entry' do
182
- @logger.expects(:notify_with_level!).never
183
- @logger.add(GELF::INFO, { :short_message => nil })
184
- end
185
- end
186
-
187
- GELF::Levels.constants.each do |const|
188
- # logger.error "Argument #{ @foo } mismatch."
189
- should "call add with level #{const} from method name, message from parameter" do
190
- @logger.expects(:notify_with_level!).with do |level, hash|
191
- level == GELF.const_get(const) &&
192
- hash['short_message'] == 'message' &&
193
- hash['facility'] == 'gelf-rb'
194
- end
195
- @logger.__send__(const.downcase, 'message')
196
- end
197
-
198
- # logger.fatal { "Argument 'foo' not given." }
199
- should "call add with level #{const} from method name, message from block" do
200
- @logger.expects(:notify_with_level!).with do |level, hash|
201
- level == GELF.const_get(const) &&
202
- hash['short_message'] == 'message' &&
203
- hash['facility'] == 'gelf-rb'
204
- end
205
- @logger.__send__(const.downcase) { 'message' }
206
- end
207
-
208
- # logger.info('initialize') { "Initializing..." }
209
- should "call add with level #{const} from method name, facility from parameter, message from block" do
210
- @logger.expects(:notify_with_level!).with do |level, hash|
211
- level == GELF.const_get(const) &&
212
- hash['short_message'] == 'message' &&
213
- hash['facility'] == 'facility'
214
- end
215
- @logger.__send__(const.downcase, 'facility') { 'message' }
216
- end
217
-
218
- should "respond to #{const.downcase}?" do
219
- @logger.level = GELF.const_get(const) - 1
220
- assert @logger.__send__(const.to_s.downcase + '?')
221
- @logger.level = GELF.const_get(const)
222
- assert @logger.__send__(const.to_s.downcase + '?')
223
- @logger.level = GELF.const_get(const) + 1
224
- assert !@logger.__send__(const.to_s.downcase + '?')
225
- end
226
- end
227
-
228
- should "support Logger#<<" do
229
- @logger.expects(:notify_with_level!).with do |level, hash|
230
- level == GELF::UNKNOWN &&
231
- hash['short_message'] == "Message"
232
- end
233
- @logger << "Message"
234
- end
235
-
236
- should "have formatter attribute" do
237
- @logger.formatter
238
- end
239
-
240
- context "close" do
241
- should "close socket" do
242
- @sender.expects(:close).once
243
- @logger.close
244
- end
245
- end
246
- end
247
- end
@@ -1,317 +0,0 @@
1
- require 'helper'
2
-
3
- RANDOM_DATA = ('A'..'Z').to_a
4
-
5
- class TestNotifier < Test::Unit::TestCase
6
- should "allow access to host, port, max_chunk_size and default_options" do
7
- Socket.expects(:gethostname).returns('default_hostname')
8
- n = GELF::Notifier.new
9
- assert_equal [[['localhost', 12201]], 1420], [n.addresses, n.max_chunk_size]
10
- assert_equal( { 'version' => '1.0', 'level' => GELF::UNKNOWN, 'protocol' => 0,
11
- 'host' => 'default_hostname', 'facility' => 'gelf-rb' },
12
- n.default_options )
13
- n.addresses, n.max_chunk_size, n.default_options = [['graylog2.org', 7777]], :lan, {:host => 'grayhost'}
14
- assert_equal [['graylog2.org', 7777]], n.addresses
15
- assert_equal 8154, n.max_chunk_size
16
- assert_equal({'host' => 'grayhost'}, n.default_options)
17
-
18
- n.max_chunk_size = 1337.1
19
- assert_equal 1337, n.max_chunk_size
20
- end
21
-
22
- context "with notifier with mocked sender" do
23
- setup do
24
- Socket.stubs(:gethostname).returns('stubbed_hostname')
25
- @notifier = GELF::Notifier.new('host', 12345)
26
- @sender = mock
27
- @notifier.instance_variable_set('@sender', @sender)
28
- end
29
-
30
- context "extract_hash" do
31
- should "check arguments" do
32
- assert_raise(ArgumentError) { @notifier.__send__(:extract_hash) }
33
- assert_raise(ArgumentError) { @notifier.__send__(:extract_hash, 1, 2, 3) }
34
- end
35
-
36
- should "work with hash" do
37
- hash = @notifier.__send__(:extract_hash, { 'version' => '1.0', 'short_message' => 'message' })
38
- assert_equal '1.0', hash['version']
39
- assert_equal 'message', hash['short_message']
40
- end
41
-
42
- should "work with any object which responds to #to_hash" do
43
- o = Object.new
44
- o.expects(:to_hash).returns({ 'version' => '1.0', 'short_message' => 'message' })
45
- hash = @notifier.__send__(:extract_hash, o)
46
- assert_equal '1.0', hash['version']
47
- assert_equal 'message', hash['short_message']
48
- end
49
-
50
- should "work with exception with backtrace" do
51
- e = RuntimeError.new('message')
52
- e.set_backtrace(caller)
53
- hash = @notifier.__send__(:extract_hash, e)
54
- assert_equal 'RuntimeError: message', hash['short_message']
55
- assert_match(/Backtrace/, hash['full_message'])
56
- assert_equal GELF::ERROR, hash['level']
57
- end
58
-
59
- should "work with exception without backtrace" do
60
- e = RuntimeError.new('message')
61
- hash = @notifier.__send__(:extract_hash, e)
62
- assert_match(/Backtrace is not available/, hash['full_message'])
63
- end
64
-
65
- should "work with exception and hash" do
66
- e, h = RuntimeError.new('message'), {'param' => 1, 'level' => GELF::FATAL, 'short_message' => 'will be hidden by exception'}
67
- hash = @notifier.__send__(:extract_hash, e, h)
68
- assert_equal 'RuntimeError: message', hash['short_message']
69
- assert_equal GELF::FATAL, hash['level']
70
- assert_equal 1, hash['param']
71
- end
72
-
73
- should "work with plain text" do
74
- hash = @notifier.__send__(:extract_hash, 'message')
75
- assert_equal 'message', hash['short_message']
76
- assert_equal GELF::INFO, hash['level']
77
- end
78
-
79
- should "work with plain text and hash" do
80
- hash = @notifier.__send__(:extract_hash, 'message', 'level' => GELF::WARN)
81
- assert_equal 'message', hash['short_message']
82
- assert_equal GELF::WARN, hash['level']
83
- end
84
-
85
- should "covert hash keys to strings" do
86
- hash = @notifier.__send__(:extract_hash, :short_message => :message)
87
- assert hash.has_key?('short_message')
88
- assert !hash.has_key?(:short_message)
89
- end
90
-
91
- should "not overwrite keys on convert" do
92
- assert_raise(ArgumentError) { @notifier.__send__(:extract_hash, :short_message => :message1, 'short_message' => 'message2') }
93
- end
94
-
95
- should "use default_options" do
96
- @notifier.default_options = {:foo => 'bar', 'short_message' => 'will be hidden by explicit argument', 'host' => 'some_host'}
97
- hash = @notifier.__send__(:extract_hash, { 'version' => '1.0', 'short_message' => 'message' })
98
- assert_equal 'bar', hash['foo']
99
- assert_equal 'message', hash['short_message']
100
- end
101
-
102
- should "be compatible with HoptoadNotifier" do
103
- # https://github.com/thoughtbot/hoptoad_notifier/blob/master/README.rdoc, section Going beyond exceptions
104
- hash = @notifier.__send__(:extract_hash, :error_class => 'Class', :error_message => 'Message')
105
- assert_equal 'Class: Message', hash['short_message']
106
- end
107
-
108
- should "set file and line" do
109
- line = __LINE__
110
- hash = @notifier.__send__(:extract_hash, { 'version' => '1.0', 'short_message' => 'message' })
111
- assert_match(/test_notifier.rb/, hash['file'])
112
- assert_equal line + 1, hash['line']
113
- end
114
-
115
- should "set timestamp to current time if not set" do
116
- hash = @notifier.__send__(:extract_hash, { 'version' => '1.0', 'short_message' => 'message' })
117
- assert_instance_of Float, hash['timestamp']
118
- now = Time.now.utc.to_f
119
- assert ((now - 1)..(now + 1)).include?(hash['timestamp'])
120
- end
121
-
122
- should "set timestamp to specified time" do
123
- timestamp = 1319799449.13765
124
- hash = @notifier.__send__(:extract_hash, { 'version' => '1.0', 'short_message' => 'message', 'timestamp' => timestamp })
125
- assert_equal timestamp, hash['timestamp']
126
- end
127
- end
128
-
129
- context "serialize_hash" do
130
- setup do
131
- @notifier.level_mapping = :direct
132
- hash = { 'level' => GELF::WARN, 'field' => 'value' }
133
- @data = @notifier.__send__(:serialize_hash, hash)
134
- assert @data.respond_to?(:each) # Enumerable::Enumerator in 1.8, ::Enumerator in 1.9, so...
135
- @deserialized_hash = JSON.parse(Zlib::Inflate.inflate(@data.to_a.pack('C*')))
136
- assert_instance_of Hash, @deserialized_hash
137
- end
138
-
139
- should "map level using mapping" do
140
- assert_not_equal GELF::WARN, @deserialized_hash['level']
141
- assert_not_equal GELF::LOGGER_MAPPING[GELF::WARN], @deserialized_hash['level']
142
- assert_equal GELF::DIRECT_MAPPING[GELF::WARN], @deserialized_hash['level']
143
- end
144
- end
145
-
146
- context "datagrams_from_hash" do
147
- should "not split short data" do
148
- hash = { 'version' => '1.0', 'short_message' => 'message' }
149
- datagrams = @notifier.__send__(:datagrams_from_hash, hash)
150
- assert_equal 1, datagrams.count
151
- assert_instance_of String, datagrams[0]
152
-
153
- asserted = "\x78\x9c"
154
- if RUBY_VERSION[0,1].to_i >= 2
155
- puts "I'm a Ruby > 2.0.0. Enforcing ASCII-8BIT. (#{RUBY_VERSION}/#{RUBY_VERSION[0,1].to_i})"
156
- # lol well yeah, Rubby.
157
- # http://stackoverflow.com/questions/15843684/binary-string-literals-in-ruby-2-0
158
- asserted = asserted.b
159
- end
160
-
161
- assert_equal asserted, datagrams[0][0..1] # zlib header
162
- end
163
-
164
- should "split long data" do
165
- srand(1) # for stable tests
166
- hash = { 'version' => '1.0', 'short_message' => 'message' }
167
- hash.merge!('something' => (0..3000).map { RANDOM_DATA[rand(RANDOM_DATA.count)] }.join) # or it will be compressed too good
168
- datagrams = @notifier.__send__(:datagrams_from_hash, hash)
169
- assert_equal 2, datagrams.count
170
- datagrams.each_index do |i|
171
- datagram = datagrams[i]
172
- assert_instance_of String, datagram
173
- assert datagram[0..1] == "\x1e\x0f" # chunked GELF magic number
174
- # datagram[2..9] is a message id
175
- assert_equal i, datagram[10].ord
176
- assert_equal datagrams.count, datagram[11].ord
177
- end
178
- end
179
-
180
- should "split long data when subclassed" do
181
- class MyNotifier < GELF::Notifier; end
182
-
183
- @notifier = MyNotifier.new('host', 1234)
184
- @sender = mock
185
- @notifier.instance_variable_set('@sender', @sender)
186
-
187
- srand(1) # for stable tests
188
- hash = { 'version' => '1.0', 'short_message' => 'message' }
189
- hash.merge!('something' => (0..3000).map { RANDOM_DATA[rand(RANDOM_DATA.count)] }.join) # or it will be compressed too good
190
- datagrams = @notifier.__send__(:datagrams_from_hash, hash)
191
- assert_equal 2, datagrams.count
192
- datagrams.each_index do |i|
193
- datagram = datagrams[i]
194
- assert_instance_of String, datagram
195
- assert datagram[0..1] == "\x1e\x0f" # chunked GELF magic number
196
- # datagram[2..9] is a message id
197
- assert_equal i, datagram[10].ord
198
- assert_equal datagrams.count, datagram[11].ord
199
- end
200
- end
201
-
202
- should "throw an error if more than MAX_CHUNKS will be created" do
203
- srand(1) # for stable tests
204
- hash = { 'version' => '1.0', 'short_message' => 'message' }
205
- hash.merge!('something' => (0..3000).map { RANDOM_DATA[rand(RANDOM_DATA.count)] }.join) # or it will be compressed too good
206
- @notifier.max_chunk_size = 10
207
- @notifier.instance_variable_set('@hash', hash)
208
- assert_raise(ArgumentError) do
209
- @notifier.__send__(:datagrams_from_hash)
210
- end
211
- end
212
- end
213
-
214
- context "level threshold" do
215
- setup do
216
- @notifier.level = GELF::WARN
217
- @hash = { 'version' => '1.0', 'short_message' => 'message' }
218
- end
219
-
220
- ['debug', 'DEBUG', :debug].each do |l|
221
- should "allow to set threshold as #{l.inspect}" do
222
- @notifier.level = l
223
- assert_equal GELF::DEBUG, @notifier.level
224
- end
225
- end
226
-
227
- should "not send notifications with level below threshold" do
228
- @sender.expects(:send_datagrams).never
229
- @notifier.notify!(@hash.merge('level' => GELF::DEBUG))
230
- end
231
-
232
- should "not notifications with level equal or above threshold" do
233
- @sender.expects(:send_datagrams).once
234
- @notifier.notify!(@hash.merge('level' => GELF::WARN))
235
- end
236
- end
237
-
238
- context "close" do
239
- should "close sender" do
240
- @sender.expects(:close).once
241
- @notifier.close
242
- end
243
- end
244
-
245
- context "when disabled" do
246
- setup do
247
- @notifier.disable
248
- end
249
-
250
- should "not send datagrams" do
251
- @sender.expects(:send_datagrams).never
252
- @notifier.expects(:extract_hash).never
253
- @notifier.notify!({ 'version' => '1.0', 'short_message' => 'message' })
254
- end
255
-
256
- context "and enabled again" do
257
- setup do
258
- @notifier.enable
259
- end
260
-
261
- should "send datagrams" do
262
- @sender.expects(:send_datagrams)
263
- @notifier.notify!({ 'version' => '1.0', 'short_message' => 'message' })
264
- end
265
- end
266
- end
267
-
268
- should "pass valid data to sender" do
269
- @sender.expects(:send_datagrams).with do |datagrams|
270
- datagrams.is_a?(Array) && datagrams[0].is_a?(String)
271
- end
272
- @notifier.notify!({ 'version' => '1.0', 'short_message' => 'message' })
273
- end
274
-
275
- should "not mutate arguments" do
276
- data = { 'version' => '1.0', 'short_message' => 'message', foo: { bar: "BAZ" } }
277
- original_hash = data.hash
278
-
279
- @sender.expects(:send_datagrams)
280
- @notifier.notify!(data)
281
-
282
- assert_equal(data.hash, original_hash)
283
- end
284
-
285
- GELF::Levels.constants.each do |const|
286
- should "call notify with level #{const} from method name" do
287
- @notifier.expects(:notify_with_level).with(GELF.const_get(const), { 'version' => '1.0', 'short_message' => 'message' })
288
- @notifier.__send__(const.downcase, { 'version' => '1.0', 'short_message' => 'message' })
289
- end
290
- end
291
-
292
- should "not rescue from invalid invocation of #notify!" do
293
- assert_raise(ArgumentError) { @notifier.notify!(:no_short_message => 'too bad') }
294
- end
295
-
296
- should "rescue from invalid invocation of #notify" do
297
- @notifier.expects(:notify_with_level!).with(nil, instance_of(Hash)).raises(ArgumentError)
298
- @notifier.expects(:notify_with_level!).with(GELF::UNKNOWN, instance_of(ArgumentError))
299
- assert_nothing_raised { @notifier.notify(:no_short_message => 'too bad') }
300
- end
301
- end
302
-
303
- context "with notifier with real sender" do
304
- setup do
305
- @notifier = GELF::Notifier.new('no_such_host_321')
306
- end
307
-
308
- should "raise exception" do
309
- assert_raise(SocketError) { @notifier.notify('Hello!') }
310
- end
311
-
312
- should "not raise exception if asked" do
313
- @notifier.rescue_network_errors = true
314
- assert_nothing_raised { @notifier.notify('Hello!') }
315
- end
316
- end
317
- end
@@ -1,28 +0,0 @@
1
- require 'helper'
2
-
3
- class TestRubyUdpSender < Test::Unit::TestCase
4
- context "with ruby sender" do
5
- setup do
6
- @addresses = [['localhost', 12201], ['localhost', 12202]]
7
- @sender = GELF::Transport::UDP.new(@addresses)
8
- @datagrams1 = %w(d1 d2 d3)
9
- @datagrams2 = %w(e1 e2 e3)
10
- end
11
-
12
- context "send_datagrams" do
13
- setup do
14
- @sender.send_datagrams(@datagrams1)
15
- @sender.send_datagrams(@datagrams2)
16
- end
17
-
18
- before_should "be called 3 times with 1st and 2nd address" do
19
- UDPSocket.any_instance.expects(:send).times(3).with do |datagram, _, host, port|
20
- datagram.start_with?('d') && host == 'localhost' && port == 12201
21
- end
22
- UDPSocket.any_instance.expects(:send).times(3).with do |datagram, _, host, port|
23
- datagram.start_with?('e') && host == 'localhost' && port == 12202
24
- end
25
- end
26
- end
27
- end
28
- end
@@ -1,9 +0,0 @@
1
- require 'helper'
2
-
3
- class TestSeverity < Test::Unit::TestCase
4
- should "map Ruby Logger levels to syslog levels as SyslogLogger" do
5
- GELF::LOGGER_MAPPING.each do |ruby_level, syslog_level|
6
- assert_not_equal syslog_level, ruby_level
7
- end
8
- end
9
- end