request_id 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -23,12 +23,11 @@ module Rack
23
23
  end
24
24
 
25
25
  def call(env)
26
- ::RequestId.request_id = env[REQUEST_HEADER]
27
- status, headers, body = @app.call(env)
28
- headers[RESPONSE_HEADER] ||= ::RequestId.request_id
29
- [status, headers, body]
30
- ensure
31
- ::RequestId.request_id = nil
26
+ ::RequestId.with_request_id(env[REQUEST_HEADER]) do
27
+ status, headers, body = @app.call(env)
28
+ headers[RESPONSE_HEADER] ||= ::RequestId.request_id
29
+ [status, headers, body]
30
+ end
32
31
  end
33
32
  end
34
33
  end
@@ -1,3 +1,3 @@
1
1
  module RequestId
2
- VERSION = '0.1.2'
2
+ VERSION = '0.1.3'
3
3
  end
data/lib/request_id.rb CHANGED
@@ -28,6 +28,28 @@ module RequestId
28
28
  Thread.current[:request_id] = request_id
29
29
  end
30
30
 
31
+ # Public: Runs the block with the given request id set.
32
+ #
33
+ # Examples
34
+ #
35
+ # RequestId.request_id
36
+ # # => "9fee77ec37b483983839fe7a753b64d9"
37
+ #
38
+ # RequestId.with_request_id('c8ee330973663097f50686eb17d3324e') do
39
+ # RequestId.request_id
40
+ # # => "c8ee330973663097f50686eb17d3324e"
41
+ # end
42
+ #
43
+ # RequestId.request_id
44
+ # # => "9fee77ec37b483983839fe7a753b64d9"
45
+ def with_request_id(request_id)
46
+ last_request_id = RequestId.request_id
47
+ RequestId.request_id = request_id
48
+ yield
49
+ ensure
50
+ RequestId.request_id = last_request_id
51
+ end
52
+
31
53
  end
32
54
  end
33
55
 
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+ require 'securerandom'
3
+
4
+ describe RequestId do
5
+ let(:request_id) { SecureRandom.hex }
6
+
7
+ describe '.request_id' do
8
+ subject { described_class.request_id }
9
+
10
+ context 'when Thread.current[:request_id] is set' do
11
+ before { Thread.current[:request_id] = request_id }
12
+ it { should eq Thread.current[:request_id] }
13
+ end
14
+ end
15
+
16
+ describe '.request_id=' do
17
+ it 'sets Thread.current[:request_id]' do
18
+ Thread.current.should_receive(:[]=).with(:request_id, request_id)
19
+ described_class.request_id = request_id
20
+ end
21
+ end
22
+
23
+ describe '.with_request_id' do
24
+ before { Thread.current[:request_id] = request_id }
25
+
26
+ it 'sets the request_id to the new request_id' do
27
+ described_class.with_request_id('foobar') do
28
+ expect(described_class.request_id).to eq 'foobar'
29
+ end
30
+ expect(described_class.request_id).to eq request_id
31
+ end
32
+ end
33
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: request_id
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -80,6 +80,7 @@ files:
80
80
  - lib/sidekiq/middleware/server/request_id.rb
81
81
  - request_id.gemspec
82
82
  - spec/rack/request_id_spec.rb
83
+ - spec/request_id_spec.rb
83
84
  - spec/sidekiq/middleware/client/request_id_spec.rb
84
85
  - spec/sidekiq/middleware/server/request_id_spec.rb
85
86
  - spec/spec_helper.rb
@@ -99,7 +100,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
99
100
  version: '0'
100
101
  segments:
101
102
  - 0
102
- hash: -2341764705210223496
103
+ hash: 4275333693123004164
103
104
  required_rubygems_version: !ruby/object:Gem::Requirement
104
105
  none: false
105
106
  requirements:
@@ -108,7 +109,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
108
109
  version: '0'
109
110
  segments:
110
111
  - 0
111
- hash: -2341764705210223496
112
+ hash: 4275333693123004164
112
113
  requirements: []
113
114
  rubyforge_project:
114
115
  rubygems_version: 1.8.23
@@ -117,6 +118,7 @@ specification_version: 3
117
118
  summary: Classes for tracking request id
118
119
  test_files:
119
120
  - spec/rack/request_id_spec.rb
121
+ - spec/request_id_spec.rb
120
122
  - spec/sidekiq/middleware/client/request_id_spec.rb
121
123
  - spec/sidekiq/middleware/server/request_id_spec.rb
122
124
  - spec/spec_helper.rb