request_recorder 0.2.4 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +3 -1
- data/gemfiles/rails2.gemfile.lock +3 -1
- data/gemfiles/rails3.gemfile.lock +3 -1
- data/lib/request_recorder/middleware.rb +9 -19
- data/lib/request_recorder/version.rb +1 -1
- data/request_recorder.gemspec +1 -0
- data/spec/request_recorder_spec.rb +15 -44
- data/spec/spec_helper.rb +4 -4
- metadata +20 -5
- data/lib/request_recorder/repeater.rb +0 -31
data/Gemfile.lock
CHANGED
@@ -1,8 +1,9 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
request_recorder (0.
|
4
|
+
request_recorder (0.3.0)
|
5
5
|
activerecord
|
6
|
+
logcast (>= 0.1.4)
|
6
7
|
multi_json
|
7
8
|
rack
|
8
9
|
|
@@ -31,6 +32,7 @@ GEM
|
|
31
32
|
redis (~> 3.0.0)
|
32
33
|
i18n (0.6.1)
|
33
34
|
json (1.7.7)
|
35
|
+
logcast (0.1.4)
|
34
36
|
multi_json (1.7.2)
|
35
37
|
rack (1.5.2)
|
36
38
|
rake (10.0.4)
|
@@ -1,8 +1,9 @@
|
|
1
1
|
PATH
|
2
2
|
remote: /Users/mgrosser/code/tools/request_recorder
|
3
3
|
specs:
|
4
|
-
request_recorder (0.2.
|
4
|
+
request_recorder (0.2.4)
|
5
5
|
activerecord
|
6
|
+
logcast (>= 0.1.4)
|
6
7
|
multi_json
|
7
8
|
rack
|
8
9
|
|
@@ -20,6 +21,7 @@ GEM
|
|
20
21
|
fakeredis (0.4.2)
|
21
22
|
redis (~> 3.0.0)
|
22
23
|
json (1.7.7)
|
24
|
+
logcast (0.1.4)
|
23
25
|
multi_json (1.7.2)
|
24
26
|
rack (1.1.6)
|
25
27
|
rake (10.0.4)
|
@@ -1,8 +1,9 @@
|
|
1
1
|
PATH
|
2
2
|
remote: /Users/mgrosser/code/tools/request_recorder
|
3
3
|
specs:
|
4
|
-
request_recorder (0.2.
|
4
|
+
request_recorder (0.2.4)
|
5
5
|
activerecord
|
6
|
+
logcast (>= 0.1.4)
|
6
7
|
multi_json
|
7
8
|
rack
|
8
9
|
|
@@ -31,6 +32,7 @@ GEM
|
|
31
32
|
redis (~> 3.0.0)
|
32
33
|
i18n (0.6.1)
|
33
34
|
json (1.7.7)
|
35
|
+
logcast (0.1.4)
|
34
36
|
multi_json (1.7.2)
|
35
37
|
rack (1.5.2)
|
36
38
|
rake (10.0.4)
|
@@ -1,20 +1,19 @@
|
|
1
1
|
require "stringio"
|
2
2
|
require "rack/request"
|
3
3
|
require "rack/response"
|
4
|
-
require "request_recorder/repeater"
|
5
4
|
require "request_recorder/frontend"
|
6
5
|
require "active_record"
|
7
6
|
require "rack"
|
8
7
|
require "rack/body_proxy" if defined?(Rack.release) && Rack.release >= "1.5"
|
9
8
|
require "base64"
|
10
9
|
require "multi_json"
|
10
|
+
require "logcast/rails"
|
11
11
|
|
12
12
|
module RequestRecorder
|
13
13
|
class Middleware
|
14
14
|
MARKER = "request_recorder"
|
15
15
|
MAX_STEPS = 100
|
16
16
|
SEPARATOR = "-"
|
17
|
-
NEED_AUTOFLUSH = (ActiveRecord::VERSION::MAJOR == 2)
|
18
17
|
AUTH = :frontend_auth
|
19
18
|
|
20
19
|
def initialize(app, options={})
|
@@ -154,27 +153,18 @@ module RequestRecorder
|
|
154
153
|
string.gsub(/\e\[[\d;]+m/, "")
|
155
154
|
end
|
156
155
|
|
157
|
-
def capture_logging
|
158
|
-
|
159
|
-
ActiveRecord::Base.logger.instance_variable_get("@log"),
|
160
|
-
(ActiveRecord::Base.logger.auto_flushing if NEED_AUTOFLUSH),
|
161
|
-
ActiveRecord::Base.logger.level
|
162
|
-
]
|
163
|
-
|
156
|
+
def capture_logging(&block)
|
157
|
+
logger = ActiveRecord::Base.logger
|
164
158
|
recorder = StringIO.new
|
165
|
-
|
159
|
+
debug_level(logger){ logger.subscribe(Logger.new(recorder), &block) }
|
160
|
+
recorder.string
|
161
|
+
end
|
166
162
|
|
167
|
-
|
168
|
-
|
169
|
-
ActiveRecord::Base.logger.level = Logger::DEBUG
|
163
|
+
def debug_level(logger)
|
164
|
+
old, logger.level = logger.level, Logger::DEBUG
|
170
165
|
yield
|
171
|
-
recorder.string
|
172
166
|
ensure
|
173
|
-
|
174
|
-
ActiveRecord::Base.logger.instance_variable_set("@log", old[0])
|
175
|
-
ActiveRecord::Base.logger.auto_flushing = old[1] if NEED_AUTOFLUSH
|
176
|
-
ActiveRecord::Base.logger.level = old[2]
|
177
|
-
end
|
167
|
+
logger.level = old
|
178
168
|
end
|
179
169
|
end
|
180
170
|
end
|
data/request_recorder.gemspec
CHANGED
@@ -3,28 +3,27 @@ require "spec_helper"
|
|
3
3
|
describe RequestRecorder do
|
4
4
|
class FooError < RuntimeError;end
|
5
5
|
|
6
|
-
let(:
|
7
|
-
let(:
|
8
|
-
let(:inner_app)
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
6
|
+
let(:activate_logger){{ "QUERY_STRING" => "request_recorder=10" }}
|
7
|
+
let(:middleware) { RequestRecorder::Middleware.new(inner_app, :store => RequestRecorder::RedisLogger.new(redis)) }
|
8
|
+
let(:inner_app) do
|
9
|
+
lambda do |env|
|
10
|
+
Car.first
|
11
|
+
|
12
|
+
(env["log"] || []).each { |line| ActiveRecord::Base.logger.info(line) }
|
13
|
+
|
14
|
+
if env["raise"]
|
15
|
+
# rails also logs errors
|
16
|
+
ActiveRecord::Base.logger.error(env["raise"])
|
17
|
+
raise FooError.new(env["raise"])
|
18
|
+
end
|
19
|
+
[200, {}, "assadasd"]
|
17
20
|
end
|
18
|
-
|
19
|
-
} }
|
20
|
-
let(:middleware){ RequestRecorder::Middleware.new(inner_app, :store => RequestRecorder::RedisLogger.new(redis)) }
|
21
|
+
end
|
21
22
|
let(:redis){ FakeRedis::Redis.new }
|
22
23
|
let(:redis_key){ RequestRecorder::RedisLogger::KEY }
|
23
24
|
let(:existing_request_id){ redis.hset(redis_key, "123_456", "BEFORE") ; "123_456"}
|
24
25
|
|
25
26
|
before do
|
26
|
-
ActiveRecord::Base.logger.instance_variable_set("@log", original_logger)
|
27
|
-
ActiveRecord::Base.logger.auto_flushing = 1000 if RequestRecorder::Middleware::NEED_AUTOFLUSH
|
28
27
|
ActiveRecord::Base.logger.level = Logger::ERROR
|
29
28
|
end
|
30
29
|
|
@@ -50,21 +49,6 @@ describe RequestRecorder do
|
|
50
49
|
stored.values.last.should include "FooBarError"
|
51
50
|
end
|
52
51
|
|
53
|
-
it "still writes to the old log to keep us compliant with 'logging all requests'" do
|
54
|
-
if ActiveRecord::VERSION::MAJOR == 2
|
55
|
-
recorder = StringIO.new
|
56
|
-
ActiveRecord::Base.logger.instance_variable_set("@log", recorder)
|
57
|
-
middleware.call(activate_logger)
|
58
|
-
recorder.string.should == stored.values.last
|
59
|
-
else
|
60
|
-
Tempfile.open("xx") do |f|
|
61
|
-
ActiveRecord::Base.logger.instance_variable_set("@log", Logger.new(f.path))
|
62
|
-
middleware.call(activate_logger)
|
63
|
-
File.read(f.path).strip.should == stored.values.last.strip
|
64
|
-
end
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
52
|
it "starts with a given key" do
|
69
53
|
middleware.call({"QUERY_STRING" => "request_recorder=10-abcdefg"})
|
70
54
|
redis.hget(redis_key, "abcdefg").should include "SELECT"
|
@@ -120,9 +104,6 @@ describe RequestRecorder do
|
|
120
104
|
|
121
105
|
it "restores the AR logger after executing" do
|
122
106
|
middleware.call(activate_logger)
|
123
|
-
|
124
|
-
ActiveRecord::Base.logger.instance_variable_get("@log").object_id.should == original_logger.object_id
|
125
|
-
ActiveRecord::Base.logger.auto_flushing.should == 1000 if RequestRecorder::Middleware::NEED_AUTOFLUSH
|
126
107
|
ActiveRecord::Base.logger.level.should == Logger::ERROR
|
127
108
|
end
|
128
109
|
|
@@ -194,16 +175,6 @@ describe RequestRecorder do
|
|
194
175
|
}
|
195
176
|
end
|
196
177
|
|
197
|
-
it "removes header length above max" do
|
198
|
-
status, headers, body = middleware.call(
|
199
|
-
"QUERY_STRING" => "request_recorder=10-xxx",
|
200
|
-
"success" => true,
|
201
|
-
"log" => Array.new(20).fill(("a" * 100))
|
202
|
-
)
|
203
|
-
headers["X-ChromeLogger-Data"].size.should be_within(200).of(1700)
|
204
|
-
decode(headers)["rows"].inspect.should include '"Removed:", "12", "lines"' # tells users that something was removed
|
205
|
-
end
|
206
|
-
|
207
178
|
it "does not log without frontend_auth" do
|
208
179
|
status, headers, body = middleware.call("QUERY_STRING" => "request_recorder=10-xxx")
|
209
180
|
headers["X-ChromeLogger-Data"].should == nil
|
data/spec/spec_helper.rb
CHANGED
@@ -1,13 +1,13 @@
|
|
1
|
-
require "request_recorder"
|
2
|
-
require "request_recorder/redis_logger"
|
3
|
-
require "request_recorder/cache_logger"
|
4
|
-
|
5
1
|
require "active_record"
|
6
2
|
require "fakeredis"
|
7
3
|
require "tempfile"
|
8
4
|
|
9
5
|
ActiveRecord::Base.logger = ActiveSupport::BufferedLogger.new("/dev/null")
|
10
6
|
|
7
|
+
require "request_recorder"
|
8
|
+
require "request_recorder/redis_logger"
|
9
|
+
require "request_recorder/cache_logger"
|
10
|
+
|
11
11
|
# connect
|
12
12
|
ActiveRecord::Base.establish_connection(
|
13
13
|
:adapter => "sqlite3",
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: request_recorder
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
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-04-
|
12
|
+
date: 2013-04-25 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rack
|
@@ -59,6 +59,22 @@ dependencies:
|
|
59
59
|
- - ! '>='
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: logcast
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: 0.1.4
|
70
|
+
type: :runtime
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: 0.1.4
|
62
78
|
description:
|
63
79
|
email: michael@grosser.it
|
64
80
|
executables: []
|
@@ -80,7 +96,6 @@ files:
|
|
80
96
|
- lib/request_recorder/frontend.rb
|
81
97
|
- lib/request_recorder/middleware.rb
|
82
98
|
- lib/request_recorder/redis_logger.rb
|
83
|
-
- lib/request_recorder/repeater.rb
|
84
99
|
- lib/request_recorder/version.rb
|
85
100
|
- request_recorder.gemspec
|
86
101
|
- spec/request_recorder/cache_logger_spec.rb
|
@@ -104,7 +119,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
104
119
|
version: '0'
|
105
120
|
segments:
|
106
121
|
- 0
|
107
|
-
hash:
|
122
|
+
hash: 1116155128447744450
|
108
123
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
109
124
|
none: false
|
110
125
|
requirements:
|
@@ -113,7 +128,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
113
128
|
version: '0'
|
114
129
|
segments:
|
115
130
|
- 0
|
116
|
-
hash:
|
131
|
+
hash: 1116155128447744450
|
117
132
|
requirements: []
|
118
133
|
rubyforge_project:
|
119
134
|
rubygems_version: 1.8.25
|
@@ -1,31 +0,0 @@
|
|
1
|
-
module RequestRecorder
|
2
|
-
class Repeater
|
3
|
-
def initialize(targets)
|
4
|
-
@targets = targets
|
5
|
-
end
|
6
|
-
|
7
|
-
# Rails 2
|
8
|
-
def write(*args)
|
9
|
-
@targets.each{|t| t.write(*args) }
|
10
|
-
end
|
11
|
-
|
12
|
-
# Rails 3
|
13
|
-
def add(*args)
|
14
|
-
@targets.each do |t|
|
15
|
-
if t.respond_to?(:add)
|
16
|
-
t.add(*args)
|
17
|
-
else
|
18
|
-
t.write("#{args[1]}\n")
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
def level=(x)
|
24
|
-
@targets.each{|t| t.level = x if t.respond_to?(:level=) }
|
25
|
-
end
|
26
|
-
|
27
|
-
def level
|
28
|
-
@targets.detect{|t| t.respond_to?(:level) }.level
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|