logjam_agent 0.29.6 → 0.32.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +49 -28
- data/Rakefile +5 -0
- data/lib/logjam_agent.rb +14 -4
- data/lib/logjam_agent/active_support/core_ext/array/extract.rb +21 -0
- data/lib/logjam_agent/active_support/parameter_filter.rb +128 -0
- data/lib/logjam_agent/buffered_logger.rb +17 -42
- data/lib/logjam_agent/middleware.rb +16 -4
- data/lib/logjam_agent/monkey_patches/ffi-rzmq-patch.rb +19 -0
- data/lib/logjam_agent/rack/logger.rb +19 -81
- data/lib/logjam_agent/rack/rails_support.rb +26 -0
- data/lib/logjam_agent/rack/sinatra_request.rb +32 -0
- data/lib/logjam_agent/railtie.rb +3 -0
- data/lib/logjam_agent/receiver.rb +22 -0
- data/lib/logjam_agent/sinatra.rb +116 -0
- data/lib/logjam_agent/syslog_like_formatter.rb +15 -7
- data/lib/logjam_agent/util.rb +6 -1
- data/lib/logjam_agent/version.rb +1 -1
- data/lib/logjam_agent/zmq_forwarder.rb +48 -30
- data/test/sinatra_app.rb +32 -0
- data/test/sinatra_classic_app.rb +31 -0
- data/test/sinatra_classic_test.rb +20 -0
- data/test/sinatra_test.rb +54 -0
- data/test/test_helper.rb +8 -3
- data/test/util_test.rb +5 -0
- metadata +70 -16
- data/.gitignore +0 -5
- data/Gemfile +0 -4
- data/logjam_agent.gemspec +0 -34
- data/script/console +0 -28
data/lib/logjam_agent/version.rb
CHANGED
@@ -16,6 +16,7 @@ module LogjamAgent
|
|
16
16
|
@sequence = SEQUENCE_START
|
17
17
|
@socket = nil
|
18
18
|
@ping_ensured = false
|
19
|
+
@socket_mutex = Mutex.new
|
19
20
|
end
|
20
21
|
|
21
22
|
def connection_specs
|
@@ -35,11 +36,11 @@ module LogjamAgent
|
|
35
36
|
}
|
36
37
|
end
|
37
38
|
|
38
|
-
@@
|
39
|
+
@@context_mutex = Mutex.new
|
39
40
|
@@zmq_context = nil
|
40
41
|
|
41
42
|
def self.context
|
42
|
-
@@
|
43
|
+
@@context_mutex.synchronize do
|
43
44
|
@@zmq_context ||=
|
44
45
|
begin
|
45
46
|
require 'ffi-rzmq'
|
@@ -50,24 +51,9 @@ module LogjamAgent
|
|
50
51
|
end
|
51
52
|
end
|
52
53
|
|
53
|
-
def socket
|
54
|
-
return @socket if @socket
|
55
|
-
@socket = self.class.context.socket(ZMQ::DEALER)
|
56
|
-
ensure_ping_at_exit
|
57
|
-
@socket.setsockopt(ZMQ::LINGER, @config[:linger])
|
58
|
-
@socket.setsockopt(ZMQ::SNDHWM, @config[:snd_hwm])
|
59
|
-
@socket.setsockopt(ZMQ::RCVHWM, @config[:rcv_hwm])
|
60
|
-
@socket.setsockopt(ZMQ::RCVTIMEO, @config[:rcv_timeo])
|
61
|
-
@socket.setsockopt(ZMQ::SNDTIMEO, @config[:snd_timeo])
|
62
|
-
spec = connection_specs.sort_by{rand}.first
|
63
|
-
@socket.connect(spec)
|
64
|
-
@socket
|
65
|
-
end
|
66
|
-
|
67
54
|
def reset
|
68
|
-
|
69
|
-
|
70
|
-
@socket = nil
|
55
|
+
@socket_mutex.synchronize do
|
56
|
+
reset_without_locking
|
71
57
|
end
|
72
58
|
end
|
73
59
|
|
@@ -84,27 +70,57 @@ module LogjamAgent
|
|
84
70
|
key += ".#{engine}"
|
85
71
|
end
|
86
72
|
msg = LogjamAgent.encode_payload(data)
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
73
|
+
@socket_mutex.synchronize do
|
74
|
+
if options[:sync]
|
75
|
+
send_receive(app_env, key, msg)
|
76
|
+
else
|
77
|
+
publish(app_env, key, msg)
|
78
|
+
end
|
91
79
|
end
|
92
80
|
rescue => error
|
93
81
|
reraise_expectation_errors!
|
94
82
|
raise ForwardingError.new(error.message)
|
95
83
|
end
|
96
84
|
|
85
|
+
private
|
86
|
+
|
87
|
+
def reset_without_locking
|
88
|
+
if @socket
|
89
|
+
@socket.close
|
90
|
+
@socket = nil
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
# this method assumes the caller holds the socket mutex
|
95
|
+
def socket
|
96
|
+
return @socket if @socket
|
97
|
+
@socket = self.class.context.socket(ZMQ::DEALER)
|
98
|
+
raise "ZMQ error on socket creation: #{ZMQ::Util.error_string}" if @socket.nil?
|
99
|
+
if LogjamAgent.ensure_ping_at_exit
|
100
|
+
ensure_ping_at_exit
|
101
|
+
else
|
102
|
+
at_exit { reset }
|
103
|
+
end
|
104
|
+
@socket.setsockopt(ZMQ::LINGER, @config[:linger])
|
105
|
+
@socket.setsockopt(ZMQ::SNDHWM, @config[:snd_hwm])
|
106
|
+
@socket.setsockopt(ZMQ::RCVHWM, @config[:rcv_hwm])
|
107
|
+
@socket.setsockopt(ZMQ::RCVTIMEO, @config[:rcv_timeo])
|
108
|
+
@socket.setsockopt(ZMQ::SNDTIMEO, @config[:snd_timeo])
|
109
|
+
spec = connection_specs.sort_by{rand}.first
|
110
|
+
@socket.connect(spec)
|
111
|
+
@socket
|
112
|
+
end
|
113
|
+
|
97
114
|
def publish(app_env, key, data)
|
98
115
|
info = pack_info(@sequence = next_fixnum(@sequence))
|
99
116
|
parts = [app_env, key, data, info]
|
100
117
|
if socket.send_strings(parts, ZMQ::DONTWAIT) < 0
|
118
|
+
error = ZMQ::Util.error_string
|
101
119
|
reset if connection_specs.size > 1
|
102
|
-
raise "ZMQ error on publishing: #{
|
120
|
+
raise "ZMQ error on publishing: #{error}"
|
103
121
|
end
|
104
122
|
end
|
105
123
|
|
106
|
-
private
|
107
|
-
|
108
124
|
def log_warning(message)
|
109
125
|
LogjamAgent.error_handler.call ForwardingWarning.new(message)
|
110
126
|
end
|
@@ -117,12 +133,12 @@ module LogjamAgent
|
|
117
133
|
answer_parts = []
|
118
134
|
if socket.send_strings(request_parts) < 0
|
119
135
|
log_warning "ZMQ error on sending: #{ZMQ::Util.error_string}"
|
120
|
-
|
136
|
+
reset_without_locking
|
121
137
|
return nil
|
122
138
|
end
|
123
139
|
if socket.recv_strings(answer_parts) < 0
|
124
140
|
log_warning "ZMQ error on receiving: #{ZMQ::Util.error_string}"
|
125
|
-
|
141
|
+
reset_without_locking
|
126
142
|
return nil
|
127
143
|
end
|
128
144
|
if answer_parts.first != "" || !VALID_RESPONSE_CODES.include?(answer_parts.second.to_s.to_i)
|
@@ -132,8 +148,10 @@ module LogjamAgent
|
|
132
148
|
end
|
133
149
|
|
134
150
|
def ping
|
135
|
-
|
136
|
-
|
151
|
+
@socket_mutex.synchronize do
|
152
|
+
if @socket && !send_receive("ping", @app_env, "{}", NO_COMPRESSION)
|
153
|
+
log_warning "failed to receive pong"
|
154
|
+
end
|
137
155
|
end
|
138
156
|
end
|
139
157
|
|
data/test/sinatra_app.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
$:.unshift File.expand_path('../../lib', __FILE__)
|
2
|
+
|
3
|
+
require 'logjam_agent/sinatra'
|
4
|
+
|
5
|
+
class SinatraTestApp < Sinatra::Base
|
6
|
+
register LogjamAgent::Sinatra
|
7
|
+
|
8
|
+
use LogjamAgent::Sinatra::Middleware
|
9
|
+
|
10
|
+
configure do
|
11
|
+
set :root, File.expand_path('../..', __FILE__)
|
12
|
+
set :environment, :test
|
13
|
+
set :loglevel, :debug
|
14
|
+
setup_logjam_logger
|
15
|
+
|
16
|
+
LogjamAgent.application_name = "myapp"
|
17
|
+
LogjamAgent.add_forwarder(:zmq, :host => "inproc://app")
|
18
|
+
LogjamAgent.parameter_filters << :password
|
19
|
+
LogjamAgent.ensure_ping_at_exit = false
|
20
|
+
end
|
21
|
+
|
22
|
+
before '/index' do
|
23
|
+
action_name "Simple#index"
|
24
|
+
end
|
25
|
+
|
26
|
+
get '/index' do
|
27
|
+
logger.info 'Hello World!'
|
28
|
+
'Hello World!'
|
29
|
+
end
|
30
|
+
|
31
|
+
run! if __FILE__ == $0
|
32
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
$:.unshift File.expand_path('../../lib', __FILE__)
|
2
|
+
|
3
|
+
require 'logjam_agent/sinatra'
|
4
|
+
|
5
|
+
use LogjamAgent::Sinatra::Middleware
|
6
|
+
|
7
|
+
configure do
|
8
|
+
set :root, File.expand_path('../..', __FILE__)
|
9
|
+
set :environment, :test
|
10
|
+
set :loglevel, :debug
|
11
|
+
setup_logjam_logger
|
12
|
+
|
13
|
+
LogjamAgent.application_name = "myapp"
|
14
|
+
LogjamAgent.add_forwarder(
|
15
|
+
:zmq,
|
16
|
+
:host => "localhost",
|
17
|
+
:port => 9604,
|
18
|
+
:linger => 10,
|
19
|
+
:snd_hwm => 10,
|
20
|
+
:rcv_hwm => 10,
|
21
|
+
:rcv_timeo => 10,
|
22
|
+
:snd_timeo => 10
|
23
|
+
)
|
24
|
+
LogjamAgent.parameter_filters << :password
|
25
|
+
end
|
26
|
+
|
27
|
+
get '/index' do
|
28
|
+
action_name "Simple#index"
|
29
|
+
logger.info 'Hello World!'
|
30
|
+
'Hello World!'
|
31
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require_relative "test_helper.rb"
|
2
|
+
require_relative "sinatra_classic_app"
|
3
|
+
require "rack/test"
|
4
|
+
|
5
|
+
module LogjamAgent
|
6
|
+
class SinatraClassicTest < MiniTest::Test
|
7
|
+
|
8
|
+
include ::Rack::Test::Methods
|
9
|
+
|
10
|
+
def app
|
11
|
+
::Sinatra::Application
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_root
|
15
|
+
get '/index?mumu=1&password=5'
|
16
|
+
assert_equal 'Hello World!', last_response.body
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require_relative "test_helper.rb"
|
2
|
+
require_relative "sinatra_app"
|
3
|
+
require "rack/test"
|
4
|
+
|
5
|
+
module LogjamAgent
|
6
|
+
class SinatraTest < MiniTest::Test
|
7
|
+
def setup
|
8
|
+
@@receiver ||= LogjamAgent::Receiver.new
|
9
|
+
LogjamAgent.enable!
|
10
|
+
end
|
11
|
+
|
12
|
+
def teardown
|
13
|
+
LogjamAgent.disable!
|
14
|
+
end
|
15
|
+
|
16
|
+
def logjam_message
|
17
|
+
@@receiver.receive
|
18
|
+
end
|
19
|
+
|
20
|
+
include ::Rack::Test::Methods
|
21
|
+
|
22
|
+
def app
|
23
|
+
SinatraTestApp
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_root
|
27
|
+
get '/index?mumu=1&password=5'
|
28
|
+
assert_equal 'Hello World!', last_response.body
|
29
|
+
assert_equal 200, last_response.status
|
30
|
+
|
31
|
+
stream, topic, payload = logjam_message
|
32
|
+
assert_equal "myapp-test", stream
|
33
|
+
assert_equal "logs.myapp.test", topic
|
34
|
+
assert_equal 200, payload["code"]
|
35
|
+
assert_equal "Simple#index", payload["action"]
|
36
|
+
assert_kind_of Float, payload["total_time"]
|
37
|
+
assert_kind_of String, payload["started_at"]
|
38
|
+
assert_kind_of Integer, payload["started_ms"]
|
39
|
+
assert_kind_of String, payload["ip"]
|
40
|
+
# assert_kind_of Float, payload["view_time"]
|
41
|
+
lines = payload["lines"]
|
42
|
+
assert_match(/Started GET.*password=\[FILTERED\]/, lines[0][2])
|
43
|
+
assert_match(/Hello World/, lines[1][2])
|
44
|
+
assert_match(/Completed 200 OK/, lines[2][2])
|
45
|
+
assert_nil(lines[3])
|
46
|
+
request_info = payload["request_info"]
|
47
|
+
method, url, query_parameters = request_info.values_at(*%w(method url query_parameters))
|
48
|
+
assert_equal method, "GET"
|
49
|
+
assert_equal url, "/index?mumu=1&password=[FILTERED]"
|
50
|
+
assert_equal(query_parameters, { "mumu" => "1", "password" => "[FILTERED]" })
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
end
|
data/test/test_helper.rb
CHANGED
@@ -1,11 +1,16 @@
|
|
1
1
|
require 'minitest/autorun'
|
2
2
|
require 'minitest/unit'
|
3
3
|
require 'minitest/pride' if ENV['RAINBOW_COLORED_TESTS'] == "1" && $stdout.tty?
|
4
|
-
require 'mocha/
|
5
|
-
|
6
|
-
require_relative "../lib/logjam_agent"
|
4
|
+
require 'mocha/minitest'
|
7
5
|
|
8
6
|
class MiniTest::Test
|
9
7
|
require "active_support/testing/declarative"
|
10
8
|
extend ActiveSupport::Testing::Declarative
|
11
9
|
end
|
10
|
+
|
11
|
+
$:.unshift File.expand_path('../../lib', __FILE__)
|
12
|
+
require "logjam_agent"
|
13
|
+
require "logjam_agent/receiver"
|
14
|
+
|
15
|
+
# for Sinatra
|
16
|
+
ENV['RACK_ENV'] = "test"
|
data/test/util_test.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: logjam_agent
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.32.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Stefan Kaes
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-12-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -122,6 +122,48 @@ dependencies:
|
|
122
122
|
- - ">="
|
123
123
|
- !ruby/object:Gem::Version
|
124
124
|
version: '0'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: sinatra
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: rack-test
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - ">="
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0'
|
146
|
+
type: :development
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - ">="
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '0'
|
153
|
+
- !ruby/object:Gem::Dependency
|
154
|
+
name: appraisal
|
155
|
+
requirement: !ruby/object:Gem::Requirement
|
156
|
+
requirements:
|
157
|
+
- - ">="
|
158
|
+
- !ruby/object:Gem::Version
|
159
|
+
version: '0'
|
160
|
+
type: :development
|
161
|
+
prerelease: false
|
162
|
+
version_requirements: !ruby/object:Gem::Requirement
|
163
|
+
requirements:
|
164
|
+
- - ">="
|
165
|
+
- !ruby/object:Gem::Version
|
166
|
+
version: '0'
|
125
167
|
- !ruby/object:Gem::Dependency
|
126
168
|
name: activesupport
|
127
169
|
requirement: !ruby/object:Gem::Requirement
|
@@ -142,14 +184,14 @@ dependencies:
|
|
142
184
|
requirements:
|
143
185
|
- - ">="
|
144
186
|
- !ruby/object:Gem::Version
|
145
|
-
version: 0.
|
187
|
+
version: 0.12.2
|
146
188
|
type: :runtime
|
147
189
|
prerelease: false
|
148
190
|
version_requirements: !ruby/object:Gem::Requirement
|
149
191
|
requirements:
|
150
192
|
- - ">="
|
151
193
|
- !ruby/object:Gem::Version
|
152
|
-
version: 0.
|
194
|
+
version: 0.12.2
|
153
195
|
- !ruby/object:Gem::Dependency
|
154
196
|
name: ffi-rzmq-core
|
155
197
|
requirement: !ruby/object:Gem::Requirement
|
@@ -185,32 +227,40 @@ executables: []
|
|
185
227
|
extensions: []
|
186
228
|
extra_rdoc_files: []
|
187
229
|
files:
|
188
|
-
- ".gitignore"
|
189
|
-
- Gemfile
|
190
230
|
- README.md
|
191
231
|
- Rakefile
|
192
232
|
- lib/logjam_agent.rb
|
193
233
|
- lib/logjam_agent/actionpack/lib/action_dispatch/middleware/remote_ip.rb
|
234
|
+
- lib/logjam_agent/active_support/core_ext/array/extract.rb
|
235
|
+
- lib/logjam_agent/active_support/parameter_filter.rb
|
194
236
|
- lib/logjam_agent/buffered_logger.rb
|
195
237
|
- lib/logjam_agent/forwarders.rb
|
196
238
|
- lib/logjam_agent/middleware.rb
|
239
|
+
- lib/logjam_agent/monkey_patches/ffi-rzmq-patch.rb
|
197
240
|
- lib/logjam_agent/rack/logger.rb
|
241
|
+
- lib/logjam_agent/rack/rails_support.rb
|
242
|
+
- lib/logjam_agent/rack/sinatra_request.rb
|
198
243
|
- lib/logjam_agent/railtie.rb
|
244
|
+
- lib/logjam_agent/receiver.rb
|
199
245
|
- lib/logjam_agent/request.rb
|
246
|
+
- lib/logjam_agent/sinatra.rb
|
200
247
|
- lib/logjam_agent/syslog_like_formatter.rb
|
201
248
|
- lib/logjam_agent/util.rb
|
202
249
|
- lib/logjam_agent/version.rb
|
203
250
|
- lib/logjam_agent/zmq_forwarder.rb
|
204
|
-
- logjam_agent.gemspec
|
205
|
-
- script/console
|
206
251
|
- test/request_test.rb
|
252
|
+
- test/sinatra_app.rb
|
253
|
+
- test/sinatra_classic_app.rb
|
254
|
+
- test/sinatra_classic_test.rb
|
255
|
+
- test/sinatra_test.rb
|
207
256
|
- test/test_helper.rb
|
208
257
|
- test/util_test.rb
|
209
258
|
- test/zmq_forwarder_test.rb
|
210
|
-
homepage:
|
211
|
-
licenses:
|
259
|
+
homepage: https://github.com/skaes/logjam_agent
|
260
|
+
licenses:
|
261
|
+
- MIT
|
212
262
|
metadata: {}
|
213
|
-
post_install_message:
|
263
|
+
post_install_message:
|
214
264
|
rdoc_options: []
|
215
265
|
require_paths:
|
216
266
|
- lib
|
@@ -225,12 +275,16 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
225
275
|
- !ruby/object:Gem::Version
|
226
276
|
version: '0'
|
227
277
|
requirements: []
|
228
|
-
rubygems_version: 3.
|
229
|
-
signing_key:
|
278
|
+
rubygems_version: 3.1.4
|
279
|
+
signing_key:
|
230
280
|
specification_version: 4
|
231
281
|
summary: Logjam client library to be used with logjam
|
232
282
|
test_files:
|
283
|
+
- test/sinatra_app.rb
|
284
|
+
- test/sinatra_classic_test.rb
|
285
|
+
- test/sinatra_classic_app.rb
|
233
286
|
- test/request_test.rb
|
234
|
-
- test/test_helper.rb
|
235
|
-
- test/util_test.rb
|
236
287
|
- test/zmq_forwarder_test.rb
|
288
|
+
- test/util_test.rb
|
289
|
+
- test/test_helper.rb
|
290
|
+
- test/sinatra_test.rb
|