rf_logger 0.3.4 → 0.4.0
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
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 81caf0a521defb598ff45606fa41b4a3716b6cca
|
4
|
+
data.tar.gz: 380ede66b6abbd83af7e7f8bf8ad0023992e96af
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 94eacd45bc0183dcded21c6cab446b0a3025fb98111ef76f505b244e076ecd9cce3995670b7ffc7360c38992825d56e1eb3d2b57682d6343cfbc7662092db156
|
7
|
+
data.tar.gz: 526e62a0c64f1bc1a4801c533d56840547e7f6acecebfbc7d57e23e7d02555a14a183c79c339ef99da2d730d03baafbc1b354c78e738385e263f88f98d24a3f0
|
@@ -2,11 +2,17 @@ require "thread/inheritable_attributes"
|
|
2
2
|
|
3
3
|
module RfLogger
|
4
4
|
class RequestMiddleware
|
5
|
+
REQUEST_ID_ENV = [
|
6
|
+
/\.request_id/, # matches action_dispatch.request_id & rory.request_id
|
7
|
+
"X-Request-Id", # If framework middleware has not been set fallback to default.
|
8
|
+
"HTTP_X_REQUEST_ID"
|
9
|
+
].freeze
|
10
|
+
|
5
11
|
# @param [Hash] options
|
6
|
-
# @option opts [Hash{:label => "header_name"}] :tagged
|
12
|
+
# @option opts [Hash{:label => ["header_name", /or regex/]}] :tagged match on rack request env keys. First value found has priority.
|
7
13
|
def initialize(app, options={})
|
8
|
-
@app
|
9
|
-
@tagged
|
14
|
+
@app = app
|
15
|
+
@tagged = options.fetch(:tagged, { request_id: REQUEST_ID_ENV })
|
10
16
|
end
|
11
17
|
|
12
18
|
def call(env)
|
@@ -16,13 +22,26 @@ module RfLogger
|
|
16
22
|
end
|
17
23
|
|
18
24
|
def tagged
|
19
|
-
@tagged.each_with_object({}) do |(
|
20
|
-
|
25
|
+
@tagged.each_with_object({}) do |(label, matches), tags|
|
26
|
+
[*matches].each do |match|
|
27
|
+
break if (val = find_by(match)) && (tags[label] = val)
|
28
|
+
end
|
21
29
|
end
|
22
30
|
end
|
23
31
|
|
24
32
|
private
|
25
33
|
|
34
|
+
def find_by(match)
|
35
|
+
case match
|
36
|
+
when String
|
37
|
+
@env[match]
|
38
|
+
when Regexp
|
39
|
+
(@env.find { |k, _| match =~ k }|| []).last
|
40
|
+
else
|
41
|
+
raise "Unknown tagged match type: #{match}"
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
26
45
|
def set_tagged_thread_var
|
27
46
|
Thread.current.set_inheritable_attribute(:rf_logger_request_tags, tagged)
|
28
47
|
end
|
data/lib/rf_logger/version.rb
CHANGED
@@ -1,35 +1,66 @@
|
|
1
1
|
require "rf_logger/request/request_middleware"
|
2
|
+
require "thread/inheritable_attributes"
|
2
3
|
|
3
4
|
RSpec.describe RfLogger::RequestMiddleware do
|
4
5
|
subject { RfLogger::RequestMiddleware.new(*init_arguments).call(env) }
|
5
6
|
let(:env) { {} }
|
6
7
|
let(:init_arguments) { [->(_) {}] }
|
7
|
-
|
8
|
+
let(:rf_logger_request_tags) { Thread.current.get_inheritable_attribute(:rf_logger_request_tags) || :tags_not_present }
|
8
9
|
|
9
|
-
|
10
|
-
|
11
|
-
it "defaults to getting the request_id" do
|
12
|
-
subject
|
13
|
-
expect(Thread.current[:inheritable_attributes][:rf_logger_request_tags]).to eq({ :request_id => "uuid-from_env" })
|
14
|
-
end
|
10
|
+
before do
|
11
|
+
Thread.current.set_inheritable_attribute(:rf_logger_request_tags, nil)
|
15
12
|
end
|
16
13
|
|
17
|
-
context "when tagged
|
18
|
-
|
19
|
-
|
14
|
+
context "when tagged: keyword arg is not given it loads defaults" do
|
15
|
+
before { subject }
|
16
|
+
context "when env[/request_id/] is present" do
|
17
|
+
let(:env) { { "some_other_env" => "other_value",
|
18
|
+
"sinatra.request_id" => "framework_request_id_value",
|
19
|
+
"X-Request-Id" => "http_request_id_value" } }
|
20
|
+
it { expect(rf_logger_request_tags).to eq({ :request_id => "framework_request_id_value" }) }
|
21
|
+
end
|
22
|
+
|
23
|
+
context "when env['X-Request-Id'] is the only value" do
|
24
|
+
let(:env) { { "X-Request-Id" => "http_request_id_value", "some_other_env" => "other_value" } }
|
25
|
+
it { expect(rf_logger_request_tags).to eq({ :request_id => "http_request_id_value" }) }
|
26
|
+
end
|
27
|
+
|
28
|
+
context "when env[/request_id/] os env['X-Request-Id'] NOT is present" do
|
29
|
+
let(:env) { {} }
|
20
30
|
|
21
|
-
|
22
|
-
|
23
|
-
|
31
|
+
it "is not set in the tags" do
|
32
|
+
expect(rf_logger_request_tags).to eq({})
|
33
|
+
end
|
24
34
|
end
|
25
35
|
end
|
26
36
|
|
27
|
-
context "when tagged
|
28
|
-
|
37
|
+
context "when tagged: keyword arg is given" do
|
38
|
+
context "when the env exists" do
|
39
|
+
before { subject }
|
40
|
+
let(:env) { { "some_other_env" => "other_value", "X-OtherHeader" => "other_request_id" } }
|
41
|
+
let(:init_arguments) { [->(_) {}, tagged: { custom_env_key: "X-OtherHeader" }] }
|
42
|
+
it "sets current Thread variable" do
|
43
|
+
expect(rf_logger_request_tags).to eq({ :custom_env_key => "other_request_id" })
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
context "when a tagged match type is unknown" do
|
48
|
+
let(:env) { { "X-OtherHeader" => "other_request_id", "some_other_env" => "other_value" } }
|
49
|
+
let(:init_arguments) { [->(_) {}, tagged: { custom_env_key: [1] }] }
|
50
|
+
|
51
|
+
it "sets current Thread variable" do
|
52
|
+
expect { subject }.to raise_error("Unknown tagged match type: 1")
|
53
|
+
expect(rf_logger_request_tags).to eq(:tags_not_present)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
context "when the env does not exists" do
|
58
|
+
before { subject }
|
59
|
+
let(:init_arguments) { [->(_) {}, tagged: { custom_env_key: "X-WontFindMe" }] }
|
29
60
|
|
30
|
-
|
31
|
-
|
32
|
-
|
61
|
+
it "is not set in the tags" do
|
62
|
+
expect(rf_logger_request_tags).to eq({})
|
63
|
+
end
|
33
64
|
end
|
34
65
|
end
|
35
66
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rf_logger
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dustin Zeisler
|
@@ -199,3 +199,4 @@ test_files:
|
|
199
199
|
- spec/lib/rf_logger_spec.rb
|
200
200
|
- spec/spec_helper.rb
|
201
201
|
- spec/support/request_id_shared_examples.rb
|
202
|
+
has_rdoc:
|