minato_logger 0.2.15 → 0.2.16

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: 11f2e7e39012b964d05e807bc6e41196166842c527430f8f94f700f7b7dde6c3
4
- data.tar.gz: f55d180a17a03bac367ec93f8bf8d5939e47ab09f430b71b244abd3c03b5f1dd
3
+ metadata.gz: a92bc90dad9351db28e1822889e30507ec655fbbd8af5d8958e0bd71d0543adf
4
+ data.tar.gz: 116eae9a2b9e93b22227c349bac98bdf939575ab6ffac7c1a381ce34a262f297
5
5
  SHA512:
6
- metadata.gz: 58a321a141ae059022e2303389d41bd8766a38c9d02e7aab5f2700766d1abd4d31438d831b99a06b85c9cbae5c74200d581e829fef0ec5109af45a4d0b0486d1
7
- data.tar.gz: 0ab555ce2babcc1a5787ea59e8fec0661c7681f971ed249696f398c42a44d154335adcaa7a303f2424d5a838ce587ed53284fbd8076f1160f8ed8b86c2d9f00a
6
+ metadata.gz: fe0647f14553910186ae4bd62937ef60d9768b2c0875aed66a921696c786f3f867057f7016d2551f622d5d43654979225a15a80efe64ef61a8c4fe2e93bb3d01
7
+ data.tar.gz: 95ff3b9a514096cd59de2f4f48238646beaec3ab8da2cbd712aed8fe26db654ce70506636bef85142c926794c6db1ff327c5e121cdf9f4e2e1fe0bdf5d9d9702
@@ -2,11 +2,16 @@
2
2
 
3
3
  module MinatoLogger
4
4
  class Configuration
5
- attr_reader :middleware, :route_blacklist
5
+ attr_reader :middleware, :route_blacklist, :sensitive_headers
6
6
 
7
7
  def initialize
8
8
  @middleware = MinatoLogger::Middleware.new
9
9
  @route_blacklist = %w[/health/alive /health/ready]
10
+ @sensitive_headers = %w[
11
+ AUTHORIZATION PROXY_AUTHORIZATION X_CSRF_TOKEN
12
+ HTTP_AUTHORIZATION HTTP_PROXY_AUTHORIZATION
13
+ HTTP_X_CSRF_TOKEN COOKIE HTTP_COOKIE
14
+ ]
10
15
 
11
16
  yield(self) if block_given?
12
17
  end
@@ -6,22 +6,18 @@ require 'rails/rack/logger'
6
6
  module MinatoLogger
7
7
  module Middlewares
8
8
  class RequestResponseLogger < Rails::Rack::Logger
9
- SENSITIVE_HEADERS = %w[
10
- AUTHORIZATION PROXY_AUTHORIZATION X_CSRF_TOKEN
11
- HTTP_AUTHORIZATION HTTP_PROXY_AUTHORIZATION
12
- HTTP_X_CSRF_TOKEN COOKIE HTTP_COOKIE
13
- ].freeze
14
-
15
9
  private
16
10
 
17
11
  def call_app(request, env)
18
12
  should_log = should_log?(request)
19
- log_request(request, env) if should_log
13
+ ctx = { request: { start_time: current_time, headers: extract_request_headers(request),
14
+ body: extract_request_body(request) } }
15
+ log_request(request, env, ctx) if should_log
20
16
  handle = dispatch_request_event(request)
21
- data = process_request(env)
22
- log_response(request, env, data) if should_log
17
+ ctx[:response] = process_request(env)
18
+ log_response(request, env, ctx) if should_log
23
19
  finish_request_instrumentation(handle, env['rails.rack_logger_tag_count'])
24
- [data[:status], data[:headers], [data[:body]]]
20
+ [ctx.dig(:response, :status), ctx.dig(:response, :headers), [ctx.dig(:response, :body)]]
25
21
  end
26
22
 
27
23
  def compute_tags(request)
@@ -44,11 +40,10 @@ module MinatoLogger
44
40
  end
45
41
 
46
42
  def process_request(env)
47
- data = { body: '', status: 500, start_time: current_time, headers: {} }
48
-
49
- data[:status], data[:headers], response = @app.call(env)
50
- data[:body] = extract_body_safely(response || [])
51
- data
43
+ res = { body: '', status: 500, headers: {} }
44
+ res[:status], res[:headers], response = @app.call(env)
45
+ res[:body] = extract_body_safely(response || [])
46
+ res
52
47
  end
53
48
 
54
49
  def dispatch_request_event(request)
@@ -58,41 +53,36 @@ module MinatoLogger
58
53
  handle
59
54
  end
60
55
 
61
- def log_request(request, env)
62
- payload = default_log_data(request, env).merge({
63
- type: 'REQUEST',
64
- message: "Receiving request #{request.method} #{request.fullpath}"
65
- })
66
- payload[:request].merge!({ headers: extract_request_headers(request), body: extract_request_body(request) })
56
+ def log_request(request, env, ctx)
57
+ payload = { type: 'REQUEST', message: "Receiving request #{request.method} #{request.fullpath}" }
58
+ payload.merge!(default_log_data(request, env, ctx))
67
59
  log(payload)
68
60
  end
69
61
 
70
- def log_response(request, env, data)
71
- data[:duration] = ((current_time - data[:start_time]) * 1000).round(2)
72
- payload = build_response_payload(request, env, data)
73
- log_level = data[:status] >= 500 ? :error : :info
62
+ def log_response(request, env, ctx)
63
+ ctx[:response][:duration] = ((current_time - ctx.dig(:request, :start_time)) * 1000).round(2)
64
+ payload = build_response_payload(request, env, ctx)
65
+ log_level = ctx.dig(:response, :status) >= 500 ? :error : :info
74
66
  log(payload, log_level)
75
67
  end
76
68
 
77
- def default_log_data(request, env)
69
+ def default_log_data(request, env, ctx)
78
70
  route = env['action_dispatch.route_uri_pattern'] || request.path
79
71
  request = { id: request.request_id, method: request.method,
80
72
  time: Time.zone.now.iso8601, remote_ip: request.remote_ip, route: route, ip: request.ip,
81
- path: request.fullpath, params: filter_params(request) }
73
+ path: request.fullpath, params: filter_params(request), headers: ctx.dig(:request, :headers),
74
+ body: ctx.dig(:request, :body) }
82
75
  { rails_version: Rails.version, request: request }
83
76
  end
84
77
 
85
- def build_response_payload(request, env, data)
86
- payload = default_log_data(request, env).merge({
87
- type: 'RESPONSE', duration: data[:duration], status: data[:status],
88
- message: "Responding #{request.method} #{request.fullpath} " \
89
- "- #{data[:status]} (#{data[:duration]}ms)"
90
- })
91
- payload[:response] = {
92
- body: safe_parse_json(data[:body]), headers: filter_sensitive_headers(data[:headers]),
93
- status: data[:status], duration: data[:duration]
94
- }
95
- payload
78
+ def build_response_payload(request, env, ctx)
79
+ response = { body: safe_parse_json(ctx.dig(:response, :body)),
80
+ headers: filter_sensitive_headers(ctx.dig(:response, :headers)),
81
+ status: ctx.dig(:response, :status), duration: ctx.dig(:response, :duration) }
82
+ payload = { type: 'RESPONSE', response: response,
83
+ message: "Responding #{request.method} #{request.fullpath} " \
84
+ "- #{ctx.dig(:response, :status)} (#{ctx.dig(:response, :duration)}ms)" }
85
+ default_log_data(request, env, ctx).merge(payload)
96
86
  end
97
87
 
98
88
  def log(payload, level = :info)
@@ -154,7 +144,7 @@ module MinatoLogger
154
144
 
155
145
  headers.reject do |k, _|
156
146
  key = k.to_s.upcase.tr('-', '_')
157
- SENSITIVE_HEADERS.any? { |sensitive| key.include?(sensitive) }
147
+ Rails.application.config.minato_logger.sensitive_headers.any? { |sensitive| key.include?(sensitive) }
158
148
  end
159
149
  end
160
150
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MinatoLogger
4
- VERSION = '0.2.15'
4
+ VERSION = '0.2.16'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: minato_logger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.15
4
+ version: 0.2.16
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ferreri