rack-logstasher 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/rack/logstasher/logger.rb +11 -4
- data/lib/rack/logstasher/version.rb +1 -1
- data/spec/logger_spec.rb +72 -23
- metadata +4 -4
@@ -6,12 +6,13 @@ module Rack
|
|
6
6
|
class Logger < Rack::CommonLogger
|
7
7
|
def initialize(app, logger, opts = {})
|
8
8
|
super(app, logger)
|
9
|
-
@
|
9
|
+
@extra_request_headers = opts[:extra_request_headers] || {}
|
10
|
+
@extra_response_headers = opts[:extra_response_headers] || {}
|
10
11
|
end
|
11
12
|
|
12
13
|
private
|
13
14
|
|
14
|
-
def log(env, status,
|
15
|
+
def log(env, status, response_headers, began_at)
|
15
16
|
now = Time.now
|
16
17
|
|
17
18
|
data = {
|
@@ -22,16 +23,22 @@ module Rack
|
|
22
23
|
:remote_addr => env['REMOTE_ADDR'],
|
23
24
|
:parameters => env["QUERY_STRING"],
|
24
25
|
:request => request_line(env),
|
25
|
-
:length => extract_content_length(
|
26
|
+
:length => extract_content_length(response_headers)
|
26
27
|
}
|
27
28
|
|
28
|
-
@
|
29
|
+
@extra_request_headers.each do |header, log_key|
|
29
30
|
env_key = "HTTP_#{header.upcase.gsub('-', '_')}"
|
30
31
|
if env[env_key]
|
31
32
|
data[log_key] = env[env_key]
|
32
33
|
end
|
33
34
|
end
|
34
35
|
|
36
|
+
@extra_response_headers.each do |header, log_key|
|
37
|
+
if response_headers[header]
|
38
|
+
data[log_key] = response_headers[header]
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
35
42
|
event = LogStash::Event.new('@fields' => data, '@tags' => ['request'])
|
36
43
|
msg = event.to_json + "\n"
|
37
44
|
if @logger.respond_to?(:write)
|
data/spec/logger_spec.rb
CHANGED
@@ -49,47 +49,96 @@ describe "Logger" do
|
|
49
49
|
|
50
50
|
describe "adding extra headers to the log" do
|
51
51
|
before :each do
|
52
|
-
@
|
52
|
+
@extra_request_headers = {}
|
53
|
+
@extra_response_headers = {}
|
53
54
|
end
|
54
55
|
def app
|
55
56
|
Rack::Logstasher::Logger.new(
|
56
57
|
proc {|env|
|
57
|
-
|
58
|
-
[200,
|
58
|
+
headers = @inner_response_headers || {}
|
59
|
+
[200, headers, ["Inner app response"]]
|
59
60
|
},
|
60
61
|
Logger.new(tmp_logfile_path),
|
61
|
-
:
|
62
|
+
:extra_request_headers => @extra_request_headers,
|
63
|
+
:extra_response_headers => @extra_response_headers
|
62
64
|
)
|
63
65
|
end
|
64
66
|
|
65
|
-
|
66
|
-
|
67
|
-
|
67
|
+
context "extra request headers" do
|
68
|
+
it "should add specified extra request headers to the log under the given key" do
|
69
|
+
@extra_request_headers["foo"] = "header_foo"
|
70
|
+
get "/something", {}, {"HTTP_FOO" => "bar"}
|
68
71
|
|
69
|
-
|
70
|
-
|
72
|
+
log_details = JSON.parse(last_log_line)
|
73
|
+
fields = log_details['@fields']
|
71
74
|
|
72
|
-
|
73
|
-
|
75
|
+
expect(fields['header_foo']).to eq('bar')
|
76
|
+
end
|
77
|
+
|
78
|
+
it "should not add the key if the header is missing" do
|
79
|
+
@extra_request_headers["foo"] = "header_foo"
|
80
|
+
get "/something"
|
81
|
+
|
82
|
+
log_details = JSON.parse(last_log_line)
|
83
|
+
fields = log_details['@fields']
|
84
|
+
|
85
|
+
expect(fields).not_to have_key('header_foo')
|
86
|
+
end
|
74
87
|
|
75
|
-
|
76
|
-
|
77
|
-
|
88
|
+
it "should handle dashes in header name" do
|
89
|
+
@extra_request_headers["Varnish-Id"] = "varnish_id"
|
90
|
+
get "/something", {}, {"HTTP_VARNISH_ID" => "1234"}
|
78
91
|
|
79
|
-
|
80
|
-
|
92
|
+
log_details = JSON.parse(last_log_line)
|
93
|
+
fields = log_details['@fields']
|
81
94
|
|
82
|
-
|
95
|
+
expect(fields['varnish_id']).to eq('1234')
|
96
|
+
end
|
83
97
|
end
|
84
98
|
|
85
|
-
|
86
|
-
|
87
|
-
|
99
|
+
context "extra response headers" do
|
100
|
+
it "should add specified extra response headers to the log under the given key" do
|
101
|
+
@extra_response_headers["foo"] = "header_foo"
|
102
|
+
@inner_response_headers = {"Foo" => "bar"}
|
103
|
+
get "/something"
|
104
|
+
|
105
|
+
log_details = JSON.parse(last_log_line)
|
106
|
+
fields = log_details['@fields']
|
107
|
+
|
108
|
+
expect(fields['header_foo']).to eq('bar')
|
109
|
+
end
|
110
|
+
|
111
|
+
it "should not add the key if the header is missing" do
|
112
|
+
@extra_response_headers["foo"] = "header_foo"
|
113
|
+
get "/something"
|
114
|
+
|
115
|
+
log_details = JSON.parse(last_log_line)
|
116
|
+
fields = log_details['@fields']
|
117
|
+
|
118
|
+
expect(fields).not_to have_key('header_foo')
|
119
|
+
end
|
120
|
+
|
121
|
+
it "should handle dashes in header name" do
|
122
|
+
@extra_response_headers["X-Cache"] = "cache_status"
|
123
|
+
@inner_response_headers = {"X-Cache" => "MISS"}
|
124
|
+
get "/something"
|
125
|
+
|
126
|
+
log_details = JSON.parse(last_log_line)
|
127
|
+
fields = log_details['@fields']
|
128
|
+
|
129
|
+
expect(fields['cache_status']).to eq('MISS')
|
130
|
+
end
|
131
|
+
|
132
|
+
it "should match header in a case-insensitive fashion" do
|
133
|
+
@extra_response_headers["X-CacHe"] = "cache_status"
|
134
|
+
@inner_response_headers = {"x-cAche" => "MISS"}
|
135
|
+
get "/something"
|
88
136
|
|
89
|
-
|
90
|
-
|
137
|
+
log_details = JSON.parse(last_log_line)
|
138
|
+
fields = log_details['@fields']
|
91
139
|
|
92
|
-
|
140
|
+
expect(fields['cache_status']).to eq('MISS')
|
141
|
+
end
|
93
142
|
end
|
94
143
|
end
|
95
144
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rack-logstasher
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
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-12-
|
12
|
+
date: 2013-12-18 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rack
|
@@ -161,7 +161,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
161
161
|
version: '0'
|
162
162
|
segments:
|
163
163
|
- 0
|
164
|
-
hash:
|
164
|
+
hash: 946937473393142977
|
165
165
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
166
166
|
none: false
|
167
167
|
requirements:
|
@@ -170,7 +170,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
170
170
|
version: '0'
|
171
171
|
segments:
|
172
172
|
- 0
|
173
|
-
hash:
|
173
|
+
hash: 946937473393142977
|
174
174
|
requirements: []
|
175
175
|
rubyforge_project:
|
176
176
|
rubygems_version: 1.8.23
|