mn_middleware_gem 0.2.0 → 0.3.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
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 063321b466f6a0888755389b5ae7b48c6ff6ac8c869e6f8bae56a3d1e7c3d696
|
4
|
+
data.tar.gz: ad0cabcd0f4adbdd27da6e995d346c0f0c1e74aa802f0dbbe6a0dbb1d64b7297
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 16d9818d742c9303fd1907f16060ce810ccafbee275cbe98ce3e521b5e78563bf6fa2072fbd0c0dc357afbfd4d5427e0bc08dd712f2a4f2565a33d8b6ff05063
|
7
|
+
data.tar.gz: 2e609cbcb57f17cafdd6583666dda787b27e529663f45b58d5a63846d6a558002e2d38ead00ef27b54adcad786e820c56b70a15db75044780bb44924283e01d8
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'securerandom'
|
2
|
+
require 'net/http'
|
3
|
+
|
4
|
+
class CorrelationId
|
5
|
+
def initialize app
|
6
|
+
@app = app
|
7
|
+
end
|
8
|
+
|
9
|
+
def call(env)
|
10
|
+
request_id = nil
|
11
|
+
|
12
|
+
# see if you can get an existing request id
|
13
|
+
# from these sources, in order of priority
|
14
|
+
if env['HTTP_X_REQUEST_ID']
|
15
|
+
request_id = env['HTTP_X_REQUEST_ID']
|
16
|
+
Rails.logger.debug "Found HTTP_X_REQUEST_ID #{request_id}"
|
17
|
+
elsif env['HTTP_X_AMZN_TRACE_ID']
|
18
|
+
request_id = env['HTTP_X_REQUEST_ID'] = env['HTTP_X_AMZN_TRACE_ID'].match(/^.*Root=([^;]*).*$/).captures[0]
|
19
|
+
Rails.logger.debug "Found HTTP_X_AMZN_TRACE_ID #{request_id}"
|
20
|
+
end
|
21
|
+
|
22
|
+
# if no request id is available, generate one
|
23
|
+
if request_id.nil?
|
24
|
+
request_id = env['HTTP_X_REQUEST_ID'] = SecureRandom.uuid
|
25
|
+
Rails.logger.debug "Set request_id #{request_id}"
|
26
|
+
end
|
27
|
+
|
28
|
+
# set the request id in the request store
|
29
|
+
# so we can use it later in the Net::HTTPHeader monkey patch
|
30
|
+
RequestStore.store[:request_id] = request_id
|
31
|
+
|
32
|
+
# call the superclass and get the app return values
|
33
|
+
status, headers, response = @app.call(env)
|
34
|
+
|
35
|
+
# set the request id in the response header
|
36
|
+
headers['X-Request-Id'] = request_id
|
37
|
+
|
38
|
+
# return all the values back up
|
39
|
+
[status, headers, response]
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
# monkey patch net http to inject our extra header
|
44
|
+
module Net::HTTPHeader
|
45
|
+
alias original_initialize_http_header initialize_http_header
|
46
|
+
|
47
|
+
def initialize_http_header(initheader)
|
48
|
+
if RequestStore.store[:request_id]
|
49
|
+
Rails.logger.debug "Adding X-Request-Id to outgoing header #{RequestStore.store[:request_id]}"
|
50
|
+
initheader ||= {}
|
51
|
+
initheader['X-Request-Id'] = RequestStore.store[:request_id]
|
52
|
+
end
|
53
|
+
original_initialize_http_header(initheader)
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
class RemoteIpLogger
|
2
|
+
def initialize(app)
|
3
|
+
@app = app
|
4
|
+
end
|
5
|
+
|
6
|
+
def call(env)
|
7
|
+
if env["HTTP_X_FORWARDED_FOR"]
|
8
|
+
remote_ip = env["HTTP_X_FORWARDED_FOR"].split(",")[0]
|
9
|
+
env['REMOTE_ADDR'] = env["action_dispatch.remote_ip"] = env["HTTP_X_FORWARDED_FOR"] = remote_ip
|
10
|
+
@app.call(env)
|
11
|
+
else
|
12
|
+
@app.call(env)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mn_middleware_gem
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Shamim Mirzai
|
@@ -73,6 +73,8 @@ extensions: []
|
|
73
73
|
extra_rdoc_files: []
|
74
74
|
files:
|
75
75
|
- lib/mn_middleware_gem.rb
|
76
|
+
- lib/mn_middleware_gem/middleware/correlation_id.rb
|
77
|
+
- lib/mn_middleware_gem/middleware/remote_ip_logger.rb
|
76
78
|
- lib/mn_middleware_gem/version.rb
|
77
79
|
homepage: https://github.com/mumsnet/mn_middleware_gem
|
78
80
|
licenses: []
|