rack-request-tracker 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c26fe97fae42ee27e8ba04519475e0b2a6e6f7bde6e543dda4e29cf12b47da1e
4
+ data.tar.gz: eeb5d4d28721899334493c1de863e768f988bd4999bc33805bfae21e32dca5ff
5
+ SHA512:
6
+ metadata.gz: 8c4f14fe1450d7f9c83c3caa534b55f46d8371d2f4143111c02d971557702b2cbbd848c2ab8e512828f0d3e716d5345999f46812d0e7b07210fa93f5f7d54661
7
+ data.tar.gz: 7e0d4e70a0418058852b33be972fd29879fbde385490e24d2c331e9b523146ccc63383db646fe5fdf0791cd88df9ca9836ef5dc084438f23f546bc8a31a1e67c
@@ -0,0 +1,48 @@
1
+ require 'request_store'
2
+ require 'mumukit/core'
3
+
4
+ module Rack::RequestTracker
5
+ LOG_TAGS = %i(request_id forwarded_for request_uid)
6
+
7
+ class << self
8
+ def transaction_headers
9
+ LOG_TAGS.map { |it| [to_header(it), send(it)] }.to_h.compact
10
+ end
11
+
12
+ LOG_TAGS.each do |it|
13
+ define_method(it) { RequestStore.store[it] }
14
+ define_method("#{it}=") { |value| RequestStore.store[it] = value }
15
+ end
16
+
17
+ def compute_tags
18
+ LOG_TAGS
19
+ .map { |it| send it }
20
+ .compact
21
+ .join(', ')
22
+ end
23
+
24
+ def with_transaction_headers(args)
25
+ is_safe_domain?(URI.parse(args[:url]).hostname) ? args.deep_merge(headers: transaction_headers) : args
26
+ end
27
+
28
+ private
29
+
30
+ def to_header(log_tag)
31
+ "X-#{log_tag.upcase}".gsub('_', '-')
32
+ end
33
+
34
+ def is_safe_domain?(hostname)
35
+ safe_domains.any? { |it| hostname.end_with? it }
36
+ end
37
+
38
+ def safe_domains
39
+ []
40
+ end
41
+ end
42
+ end
43
+
44
+ require_relative 'request_tracker/logger_formatter'
45
+ require_relative 'request_tracker/rack_common_logger'
46
+ require_relative 'request_tracker/rest_client'
47
+ require_relative 'request_tracker/middleware'
48
+ require_relative 'request_tracker/version'
@@ -0,0 +1,8 @@
1
+ class Logger::Formatter
2
+ remove_const :Format
3
+ Format = "%s, [%s#%d] %5s -- %s: [%s] %s\n".freeze
4
+
5
+ def call(severity, time, progname, msg)
6
+ Format % [severity[0..0], format_datetime(time), $$, severity, progname, Rack::RequestTracker.compute_tags, msg2str(msg)]
7
+ end
8
+ end
@@ -0,0 +1,4 @@
1
+ module Rack::RequestTracker::Middleware
2
+ end
3
+
4
+ require_relative 'middleware/store'
@@ -0,0 +1,23 @@
1
+ module Rack::RequestTracker::Middleware
2
+ class Store
3
+ def initialize(app)
4
+ @app = app
5
+ end
6
+
7
+ def call(env)
8
+ request = ActionDispatch::Request.new(env)
9
+ Rack::RequestTracker.request_id = request.request_id
10
+ Rack::RequestTracker.forwarded_for = first_forward(request.x_forwarded_for) || request.remote_ip
11
+ Rack::RequestTracker.request_uid = request.headers['X-REQUEST-UID']
12
+
13
+ @app.call(env)
14
+ end
15
+
16
+ private
17
+
18
+ def first_forward(forwards)
19
+ return unless forwards
20
+ forwards.split(', ').first
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,33 @@
1
+ class Rack::CommonLogger
2
+ remove_const :FORMAT
3
+ FORMAT = %{%s - %s [%s] [%s] "%s %s%s %s" %d %s %0.4f\n}
4
+
5
+ private
6
+
7
+ def log(env, status, header, began_at)
8
+ now = Time.now
9
+ length = extract_content_length(header)
10
+
11
+ msg = FORMAT % [
12
+ env['HTTP_X_FORWARDED_FOR'] || env["REMOTE_ADDR"] || "-",
13
+ env["REMOTE_USER"] || "-",
14
+ now.strftime("%d/%b/%Y:%H:%M:%S %z"),
15
+ Rack::RequestTracker.compute_tags,
16
+ env[Rack::REQUEST_METHOD],
17
+ env[Rack::PATH_INFO],
18
+ env[Rack::QUERY_STRING].empty? ? "" : "?"+env[Rack::QUERY_STRING],
19
+ env["HTTP_VERSION"],
20
+ status.to_s[0..3],
21
+ length,
22
+ now - began_at ]
23
+
24
+ logger = @logger || env['rack.errors']
25
+ # Standard library logger doesn't support write but it supports << which actually
26
+ # calls to write on the log device without formatting
27
+ if logger.respond_to?(:write)
28
+ logger.write(msg)
29
+ else
30
+ logger << msg
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,7 @@
1
+ module RestClient
2
+ class << Request
3
+ revamp :new do |_, _, args, hyper|
4
+ hyper.(Rack::RequestTracker.with_transaction_headers args)
5
+ end
6
+ end
7
+ end if defined? RestClient::Request
@@ -0,0 +1,5 @@
1
+ module Rack
2
+ module RequestTracker
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,136 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-request-tracker
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Julián Berbel Alt
8
+ - Franco Bulgarelli
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2019-07-22 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: actionpack
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '5.1'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '5.1'
28
+ - !ruby/object:Gem::Dependency
29
+ name: request_store
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '1.4'
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '1.4'
42
+ - !ruby/object:Gem::Dependency
43
+ name: mumukit-core
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '1.14'
49
+ type: :runtime
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '1.14'
56
+ - !ruby/object:Gem::Dependency
57
+ name: bundler
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: '2.0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '2.0'
70
+ - !ruby/object:Gem::Dependency
71
+ name: rake
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - "~>"
75
+ - !ruby/object:Gem::Version
76
+ version: '10.0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - "~>"
82
+ - !ruby/object:Gem::Version
83
+ version: '10.0'
84
+ - !ruby/object:Gem::Dependency
85
+ name: rspec
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - "~>"
89
+ - !ruby/object:Gem::Version
90
+ version: '3.0'
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - "~>"
96
+ - !ruby/object:Gem::Version
97
+ version: '3.0'
98
+ description:
99
+ email:
100
+ - julian@mumuki.org
101
+ - franco@mumuki.org
102
+ executables: []
103
+ extensions: []
104
+ extra_rdoc_files: []
105
+ files:
106
+ - lib/rack/request_tracker.rb
107
+ - lib/rack/request_tracker/logger_formatter.rb
108
+ - lib/rack/request_tracker/middleware.rb
109
+ - lib/rack/request_tracker/middleware/store.rb
110
+ - lib/rack/request_tracker/rack_common_logger.rb
111
+ - lib/rack/request_tracker/rest_client.rb
112
+ - lib/rack/request_tracker/version.rb
113
+ homepage: http://github.com/mumuki/rack-request-tracker
114
+ licenses:
115
+ - MIT
116
+ metadata: {}
117
+ post_install_message:
118
+ rdoc_options: []
119
+ require_paths:
120
+ - lib
121
+ required_ruby_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ required_rubygems_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ requirements: []
132
+ rubygems_version: 3.0.2
133
+ signing_key:
134
+ specification_version: 4
135
+ summary: Bunch of patches and rack middlewares to allow for transactional logs
136
+ test_files: []