ms_header_trail 0.1.1
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 +7 -0
- data/.rspec +3 -0
- data/.rubocop.yml +12 -0
- data/CHANGELOG.md +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +75 -0
- data/Rakefile +12 -0
- data/doc/usage.jpg +0 -0
- data/lib/faraday/ms_header_trail.rb +48 -0
- data/lib/ms_header_trail/configuration.rb +32 -0
- data/lib/ms_header_trail/version.rb +5 -0
- data/lib/ms_header_trail.rb +65 -0
- data/lib/rack/ms_header_trail.rb +68 -0
- data/sig/ms_header_trail.rbs +4 -0
- metadata +73 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: f6bc222d2e57099d4d6e318372f496e58a1bf549e0becf485b2fe11f5af0e9f2
|
4
|
+
data.tar.gz: da9b2a4f6fc1976441e305201801b9d34ffab86a21d156f614e9e84a052c3d96
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9c0471b0c4b26c6178a09dae300506d502af588b3fcb14d81ffcc027acb3b6f02249704779f16da4d95770c6e049278464a87e123c9fd6dd81f8236ef60b888d
|
7
|
+
data.tar.gz: b4186c2f09842f5fefc6f53a75ab1edcf2c8c5d8b3478c8fc4ad5d848eac96a6cad1ab5f3d0c9e1ea8567d3c5a6c578491ff658669969292ec37ae2861b82abe
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/CHANGELOG.md
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2024 Adriano Dadario
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
# Microservice Header Trail
|
2
|
+
|
3
|
+
The objective is pass through all microservices the information captured into first interface and sending them when it is necessary.
|
4
|
+
|
5
|
+
This gem operates including the captured information and passing through the HTTP header.
|
6
|
+
It contains an interface for defining the attributes, a rack middleware to retrieve from
|
7
|
+
header, a faraday middleware to include the information for next microservice and a interface to retrieve the information.
|
8
|
+
|
9
|
+

|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
|
14
|
+
Install the gem and add to the application's Gemfile by executing:
|
15
|
+
|
16
|
+
$ bundle add ms_header_trail
|
17
|
+
|
18
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
19
|
+
|
20
|
+
$ gem install ms_header_trail
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
Setup the project with rack middleware and includes the middleware into the microsservices
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
config.middleware.use Rack::MsHeaderTrail
|
28
|
+
```
|
29
|
+
|
30
|
+
When calling the the service, use the faraday middleware to call the microservice
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
Faraday.new(configuration.host) do |builder|
|
34
|
+
builder.request :json
|
35
|
+
builder.response :json, content_type: /\bjson$/
|
36
|
+
builder.use Faraday::MsHeaderTrail
|
37
|
+
builder.adapter Faraday.default_adapter
|
38
|
+
end
|
39
|
+
```
|
40
|
+
|
41
|
+
When start collect information, initiate the process using the code
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
MsHeaderTrail.collect(
|
45
|
+
user_id: '123',
|
46
|
+
user_type: 'employee'
|
47
|
+
interface: 'portal'
|
48
|
+
)
|
49
|
+
```
|
50
|
+
|
51
|
+
To retrieve the information on any microservice, just use:
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
data = MsHeaderTrail.retrieve
|
55
|
+
=>
|
56
|
+
{
|
57
|
+
user_id: '123',
|
58
|
+
user_type: 'employee'
|
59
|
+
interface: 'portal'
|
60
|
+
}
|
61
|
+
```
|
62
|
+
|
63
|
+
## Development
|
64
|
+
|
65
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
66
|
+
|
67
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
68
|
+
|
69
|
+
## Contributing
|
70
|
+
|
71
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/ms_header_trail.
|
72
|
+
|
73
|
+
## License
|
74
|
+
|
75
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/doc/usage.jpg
ADDED
Binary file
|
@@ -0,0 +1,48 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "ms_header_trail"
|
4
|
+
require "faraday"
|
5
|
+
|
6
|
+
module Faraday
|
7
|
+
# Public: Faraday middleware that stores audit data local thread into http header
|
8
|
+
# request
|
9
|
+
#
|
10
|
+
# Examples
|
11
|
+
#
|
12
|
+
# Faraday.new(configuration.host) do |builder|
|
13
|
+
# builder.use Faraday::MsHeaderTrail
|
14
|
+
#
|
15
|
+
class MsHeaderTrail < Faraday::Middleware
|
16
|
+
RESPONSE_HEADER_PREFIX = "X-"
|
17
|
+
|
18
|
+
def initialize(app, options = nil)
|
19
|
+
super
|
20
|
+
@app = app
|
21
|
+
end
|
22
|
+
|
23
|
+
def call(env)
|
24
|
+
::MsHeaderTrail.retrieve.each_pair do |key, value|
|
25
|
+
set_header(env, key, value)
|
26
|
+
end
|
27
|
+
@app.call(env)
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def set_header(env, key, value)
|
33
|
+
env[:request_headers][header_attr_name(key)] = value
|
34
|
+
end
|
35
|
+
|
36
|
+
def header_attr_name(key)
|
37
|
+
"#{header_prefix_name}-#{header_key(key)}"
|
38
|
+
end
|
39
|
+
|
40
|
+
def header_key(key)
|
41
|
+
key.to_s.split(/[-_]/).map(&:capitalize).join("-")
|
42
|
+
end
|
43
|
+
|
44
|
+
def header_prefix_name
|
45
|
+
"#{RESPONSE_HEADER_PREFIX}#{::MsHeaderTrail.configuration.http_response_prefix_keyname}"
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module MsHeaderTrail
|
4
|
+
# Class responsible for configure the process
|
5
|
+
class Configuration
|
6
|
+
# Key to identify the data included into process, default: msht-
|
7
|
+
attr_accessor :prefix_keyname
|
8
|
+
|
9
|
+
# If the header should be returned into rack response, default: false
|
10
|
+
attr_accessor :rack_header_response
|
11
|
+
|
12
|
+
def initialize
|
13
|
+
@prefix_keyname = "msht-"
|
14
|
+
end
|
15
|
+
|
16
|
+
def to_store(key)
|
17
|
+
"#{prefix_keyname}#{key}"
|
18
|
+
end
|
19
|
+
|
20
|
+
def from_store(key)
|
21
|
+
key.to_s.gsub(prefix_keyname, "").to_sym
|
22
|
+
end
|
23
|
+
|
24
|
+
def http_request_prefix_keyname
|
25
|
+
prefix_keyname.gsub("-", "_").upcase
|
26
|
+
end
|
27
|
+
|
28
|
+
def http_response_prefix_keyname
|
29
|
+
prefix_keyname.to_s.split(/[-_]/).map(&:capitalize).join("-")
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "ms_header_trail/version"
|
4
|
+
|
5
|
+
# Rack autoload module
|
6
|
+
module Rack
|
7
|
+
autoload :MsHeaderTrail, "rack/ms_header_trail"
|
8
|
+
end
|
9
|
+
|
10
|
+
# This service include the collected information passed into method `collect`
|
11
|
+
# to be retrieved later where will be used. In rack process, faraday connection
|
12
|
+
# or Bunny process
|
13
|
+
module MsHeaderTrail
|
14
|
+
autoload :Configuration, "ms_header_trail/configuration"
|
15
|
+
|
16
|
+
class << self
|
17
|
+
# Public: Collect the given parameters, which is set by user on
|
18
|
+
# start the capture process or during the Rack middleware
|
19
|
+
def collect(attributes)
|
20
|
+
attributes.each_pair do |key, value|
|
21
|
+
set(configuration.to_store(key), value.to_s)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
# Public: Retrive the stored parameters, which was set by user
|
26
|
+
# during the Rack middleware, faraday connection or manually
|
27
|
+
def retrieve
|
28
|
+
Thread.current
|
29
|
+
.keys
|
30
|
+
.select { |key| key.start_with?(configuration.prefix_keyname) }
|
31
|
+
.each_with_object({}) { |key, memo| memo[configuration.from_store(key)] = get(key) }
|
32
|
+
end
|
33
|
+
|
34
|
+
# Public: Execute a block code after data collected
|
35
|
+
def with(attributes)
|
36
|
+
collect(attributes)
|
37
|
+
yield
|
38
|
+
end
|
39
|
+
|
40
|
+
# Public: Retrieve the given key
|
41
|
+
def get(key)
|
42
|
+
Thread.current[key]
|
43
|
+
end
|
44
|
+
|
45
|
+
# Public: Set the given key to the given value
|
46
|
+
def set(key, value)
|
47
|
+
Thread.current[key] = value
|
48
|
+
end
|
49
|
+
|
50
|
+
def configuration
|
51
|
+
@configuration ||= Configuration.new
|
52
|
+
end
|
53
|
+
|
54
|
+
# Public: Configure the process.
|
55
|
+
#
|
56
|
+
# Examples
|
57
|
+
#
|
58
|
+
# MsHeaderTrail.configure do |config|
|
59
|
+
# config.prefix_keyname = 'msht-'
|
60
|
+
# end
|
61
|
+
def configure
|
62
|
+
yield configuration
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Rack
|
4
|
+
|
5
|
+
# Public: Rack middleware that stores the collection data from header in a
|
6
|
+
# thread local variable.
|
7
|
+
#
|
8
|
+
# Examples
|
9
|
+
#
|
10
|
+
# use Rack::MsHeaderTrail
|
11
|
+
class MsHeaderTrail
|
12
|
+
REQUEST_HEADER_PREFIX = "HTTP_X_"
|
13
|
+
RESPONSE_HEADER_PREFIX = "X-"
|
14
|
+
|
15
|
+
def initialize(app, options = nil)
|
16
|
+
@app = app
|
17
|
+
@options = options
|
18
|
+
end
|
19
|
+
|
20
|
+
def call(env)
|
21
|
+
::MsHeaderTrail.with(retrive_from_header(env)) do
|
22
|
+
status, headers, body = @app.call(env)
|
23
|
+
|
24
|
+
into_headers(headers) if ::MsHeaderTrail.configuration.rack_header_response
|
25
|
+
|
26
|
+
[status, headers, body]
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def retrive_from_header(env)
|
33
|
+
env.keys
|
34
|
+
.select { |key| header_key?(key) }
|
35
|
+
.each_with_object({}) { |key, memo| memo[local_attr_name(key)] = env[key] }
|
36
|
+
end
|
37
|
+
|
38
|
+
def into_headers(headers)
|
39
|
+
::MsHeaderTrail.retrieve.each_pair do |key, value|
|
40
|
+
headers[response_attr_name(key)] = value
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def response_attr_name(key)
|
45
|
+
"#{response_prefix_name}-#{response_key(key)}"
|
46
|
+
end
|
47
|
+
|
48
|
+
def response_key(key)
|
49
|
+
key.to_s.split(/[-_]/).map(&:capitalize).join("-")
|
50
|
+
end
|
51
|
+
|
52
|
+
def header_key?(key)
|
53
|
+
key.to_s.start_with?(request_prefix_name)
|
54
|
+
end
|
55
|
+
|
56
|
+
def local_attr_name(key)
|
57
|
+
key.to_s.gsub(request_prefix_name, "").downcase
|
58
|
+
end
|
59
|
+
|
60
|
+
def response_prefix_name
|
61
|
+
"#{RESPONSE_HEADER_PREFIX}#{::MsHeaderTrail.configuration.http_response_prefix_keyname}"
|
62
|
+
end
|
63
|
+
|
64
|
+
def request_prefix_name
|
65
|
+
"#{REQUEST_HEADER_PREFIX}#{::MsHeaderTrail.configuration.http_request_prefix_keyname}"
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
metadata
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ms_header_trail
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Adriano Dadario
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-06-17 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: faraday
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description:
|
28
|
+
email:
|
29
|
+
- dadario@gmail.com
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- ".rspec"
|
35
|
+
- ".rubocop.yml"
|
36
|
+
- CHANGELOG.md
|
37
|
+
- LICENSE.txt
|
38
|
+
- README.md
|
39
|
+
- Rakefile
|
40
|
+
- doc/usage.jpg
|
41
|
+
- lib/faraday/ms_header_trail.rb
|
42
|
+
- lib/ms_header_trail.rb
|
43
|
+
- lib/ms_header_trail/configuration.rb
|
44
|
+
- lib/ms_header_trail/version.rb
|
45
|
+
- lib/rack/ms_header_trail.rb
|
46
|
+
- sig/ms_header_trail.rbs
|
47
|
+
homepage: https://github.com/dadario/ms_header_trail
|
48
|
+
licenses:
|
49
|
+
- MIT
|
50
|
+
metadata:
|
51
|
+
homepage_uri: https://github.com/dadario/ms_header_trail
|
52
|
+
source_code_uri: https://github.com/dadario/ms_header_trail
|
53
|
+
changelog_uri: https://github.com/dadario/ms_header_trail/CHANGELOG.md
|
54
|
+
post_install_message:
|
55
|
+
rdoc_options: []
|
56
|
+
require_paths:
|
57
|
+
- lib
|
58
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: 3.0.0
|
63
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
requirements: []
|
69
|
+
rubygems_version: 3.3.26
|
70
|
+
signing_key:
|
71
|
+
specification_version: 4
|
72
|
+
summary: Transport collected data through HTTP header from microservices requests.
|
73
|
+
test_files: []
|