akita-har_logger 0.2.9 → 0.2.13

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c1366b8824f9cd610742238604c0d162845132a67dd67e9763542ae271cf0b7d
4
- data.tar.gz: 2ca5e09f1f65045405ce549b194fa4a51573663f43e1f3ecdde5c73d930538c2
3
+ metadata.gz: a3b611831d6f7988fe3b4bdbd3b1f23cf3e7943b64d982359efe3479e5ccae89
4
+ data.tar.gz: 5f13ca181485730ee97ce59135189f42b6239ca1dfd7ac5149dfc0b77fbccd16
5
5
  SHA512:
6
- metadata.gz: 82b07817b21e2e05c0f5c5139370c7bdd9b712dcd02e134b9d52be1d1ce95a00e4ff200067c0797a9be1a1f2b56a0518a2fac0526696bb3de3700f8f58c9bd44
7
- data.tar.gz: 45ea9d1c479cb47f481c1b3fb7eba1dc8032dc692dc69d478e8cba98b2abb14d94e685efff5d5591785a871ddecc787166c99f70ceb2d99fc5f6961803f27a49
6
+ metadata.gz: '00192467492c3d4faeec67d204f87c19f898004d202d52c40ea56e44b4eca30b6f3f22718000e592fdec5f4b74673c13aecf23f13a668ec87fd02e0e15041948'
7
+ data.tar.gz: fb3e2004d5b971cbd081e690afebdffb904491de368f44d703635ba7b5c2aeeddae32fb7612fe4bbdef804dee930bd596005002ae2a2d5a1abdbf5195588b8cc
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- akita-har_logger (0.2.9)
4
+ akita-har_logger (0.2.13)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -33,22 +33,60 @@ module Akita
33
33
  end
34
34
 
35
35
  @entry_queue = HarLogger.get_queue(out_file_name)
36
+ rescue => e
37
+ HarLogger.logException("initializing middleware", e)
38
+ raise
36
39
  end
37
40
 
38
41
  def call(env)
39
42
  start_time = Time.now
43
+
44
+ # Read the request body here, in case there is non-Rack-compliant
45
+ # middleware in the stack that closes the request-body stream on us.
46
+ request_body = env['rack.input'].read
47
+ env['rack.input'].rewind # Be kind.
48
+
40
49
  status, headers, body = @app.call(env)
41
50
  end_time = Time.now
42
51
 
43
52
  wait_time_ms = ((end_time.to_f - start_time.to_f) * 1000).round
44
53
 
54
+ # Patch env with our saved request body.
55
+ saved_input = env['rack.input']
56
+ env['rack.input'] = StringIO.new request_body
57
+
58
+ # Buffer the response body in case it's not a rewindable stream.
59
+ body = HarLogger.bufferBody(body)
60
+
45
61
  @entry_queue << (HarEntry.new start_time, wait_time_ms, env, status,
46
62
  headers, body)
47
63
 
64
+ # Be kind and restore the original request-body stream.
65
+ env['rack.input'] = saved_input
66
+
48
67
  [ status, headers, body ]
68
+ rescue => e
69
+ HarLogger.logException("handling request", e)
70
+ raise
49
71
  end
50
72
  end
51
73
 
74
+ # Logs the given exception.
75
+ def self.logException(context, e)
76
+ Rails.logger.debug "AKITA: Exception while #{context}: #{e.message} "\
77
+ "(#{e.class.name})"
78
+ e.backtrace.each { |frame|
79
+ Rails.logger.debug "AKITA: #{frame}"
80
+ }
81
+ end
82
+
83
+ # Reads the given body into an array and returns the result.
84
+ def self.bufferBody(body)
85
+ result = []
86
+ body.each { |part| result << part }
87
+ result
88
+ end
89
+
52
90
  # Logging filter for `ActionController`s.
53
91
  # TODO: Some amount of code duplication here. Should refactor.
54
92
  class Filter
@@ -58,6 +96,9 @@ module Akita
58
96
  end
59
97
 
60
98
  @entry_queue = HarLogger.get_queue(out_file_name)
99
+ rescue => e
100
+ HarLogger.logException("initializing filter", e)
101
+ raise
61
102
  end
62
103
 
63
104
  # Registers an `on_load` initializer to add a logging filter to any
@@ -72,6 +113,11 @@ module Akita
72
113
  def around(controller)
73
114
  start_time = Time.now
74
115
 
116
+ # Read the request body here, in case there is non-Rack-compliant
117
+ # middleware in the stack that closes the request-body stream on us.
118
+ request_body = controller.response.request.env['rack.input'].read
119
+ controller.response.request.env['rack.input'].rewind # Be kind.
120
+
75
121
  yield
76
122
 
77
123
  end_time = Time.now
@@ -80,9 +126,19 @@ module Akita
80
126
  response = controller.response
81
127
  request = response.request
82
128
 
129
+ # Patch env with our saved request body.
130
+ saved_input = request.env['rack.input']
131
+ request.env['rack.input'] = StringIO.new request_body
132
+
83
133
  @entry_queue << (HarEntry.new start_time, wait_time_ms, request.env,
84
134
  response.status, response.headers,
85
135
  [response.body])
136
+
137
+ # Be kind and restore the original request-body stream.
138
+ request.env['rack.input'] = saved_input
139
+ rescue => e
140
+ HarLogger.logException("handling request", e)
141
+ raise
86
142
  end
87
143
  end
88
144
 
@@ -103,13 +159,23 @@ module Akita
103
159
  # queue.
104
160
  def self.get_queue(out_file_name)
105
161
  queue = nil
162
+ Rails.logger.debug "AKITA: About to acquire entry-queue mutex "\
163
+ "#{@@entry_queues_mutex} in #{Thread.current}. "\
164
+ "Self-owned? #{@@entry_queues_mutex.owned?}"
106
165
  @@entry_queues_mutex.synchronize {
107
- if @@entry_queues.has_key?(out_file_name) then
108
- return @@entry_queues[out_file_name]
166
+ begin
167
+ Rails.logger.debug "AKITA: Acquired entry-queue mutex "\
168
+ "#{@@entry_queues_mutex} in #{Thread.current}."
169
+ if @@entry_queues.has_key?(out_file_name) then
170
+ return @@entry_queues[out_file_name]
171
+ end
172
+
173
+ queue = Queue.new
174
+ @@entry_queues[out_file_name] = queue
175
+ ensure
176
+ Rails.logger.debug "AKITA: About to release entry-queue mutex "\
177
+ "#{@@entry_queues_mutex} in #{Thread.current}."
109
178
  end
110
-
111
- queue = Queue.new
112
- @@entry_queues[out_file_name] = queue
113
179
  }
114
180
 
115
181
  WriterThread.new out_file_name, queue
@@ -122,7 +122,7 @@ module Akita
122
122
  # encoding and characters that have no UTF-8 representation by
123
123
  # replacing with '?'. Log a warning when this happens.
124
124
  sourceCharset = getPostDataCharSet(env)
125
- source = String.new(req.body.string).force_encoding(sourceCharset)
125
+ source = String.new(req.body.read).force_encoding(sourceCharset)
126
126
  utf8EncodingSuccessful = false
127
127
  if source.valid_encoding? then
128
128
  begin
@@ -159,7 +159,9 @@ module Akita
159
159
 
160
160
  def getBodySize(body)
161
161
  length = 0
162
- body.each { |part| length += part.bytesize }
162
+ # Convert each body part into a string in case we're dealing with
163
+ # non-Rack-compliant components.
164
+ body.each { |part| length += part.to_s.bytesize }
163
165
  length
164
166
  end
165
167
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Akita
4
4
  module HarLogger
5
- VERSION = "0.2.9"
5
+ VERSION = "0.2.13"
6
6
  end
7
7
  end
@@ -15,42 +15,57 @@ module Akita
15
15
  shutdown_mutex = Mutex.new
16
16
 
17
17
  Thread.new do
18
+ Rails.logger.debug "AKITA: About to acquire shutdown mutex "\
19
+ "#{shutdown_mutex} for file #{out_file_name} "\
20
+ "in writer thread #{Thread.current}. "\
21
+ "Self-owned? #{shutdown_mutex.owned?}"
18
22
  shutdown_mutex.synchronize {
19
- File.open(out_file_name, 'w') { |f|
20
- # Produce a preamble.
21
- f.write <<~EOF.chomp
22
- {
23
- "log": {
24
- "version": "1.2",
25
- "creator": {
26
- "name": "Akita HAR logger for Ruby",
27
- "version": "1.0.0"
28
- },
29
- "entries": [
30
- EOF
23
+ begin
24
+ Rails.logger.debug "AKITA: Acquired shutdown mutex "\
25
+ "#{shutdown_mutex} for file "\
26
+ "#{out_file_name} in writer thread "\
27
+ "#{Thread.current}."
28
+ File.open(out_file_name, 'w') { |f|
29
+ # Produce a preamble.
30
+ f.write <<~EOF.chomp
31
+ {
32
+ "log": {
33
+ "version": "1.2",
34
+ "creator": {
35
+ "name": "Akita HAR logger for Ruby",
36
+ "version": "1.0.0"
37
+ },
38
+ "entries": [
39
+ EOF
31
40
 
32
- first_entry = true
41
+ first_entry = true
33
42
 
34
- loop do
35
- entry = entry_queue.pop
36
- if entry == nil then break end
43
+ loop do
44
+ entry = entry_queue.pop
45
+ if entry == nil then break end
37
46
 
38
- # Emit comma separator if needed.
39
- f.puts (first_entry ? '' : ',')
40
- first_entry = false
47
+ # Emit comma separator if needed.
48
+ f.puts (first_entry ? '' : ',')
49
+ first_entry = false
41
50
 
42
- # Emit the dequeued entry.
43
- f.write JSON.generate(entry)
44
- end
51
+ # Emit the dequeued entry.
52
+ f.write JSON.generate(entry)
53
+ end
45
54
 
46
- # Produce the epilogue.
47
- f.write <<~EOF
55
+ # Produce the epilogue.
56
+ f.write <<~EOF
48
57
 
49
- ]
58
+ ]
59
+ }
50
60
  }
51
- }
52
- EOF
53
- }
61
+ EOF
62
+ }
63
+ ensure
64
+ Rails.logger.debug "AKITA: About to release shutdown mutex "\
65
+ "#{shutdown_mutex} for file "\
66
+ "#{out_file_name} in writer thread "\
67
+ "#{Thread.current}."
68
+ end
54
69
  }
55
70
  end
56
71
 
@@ -59,7 +74,18 @@ module Akita
59
74
  # Signal to the consumer that this is the end of the entry stream and
60
75
  # wait for the consumer to terminate.
61
76
  entry_queue << nil
62
- shutdown_mutex.synchronize {}
77
+ Rails.logger.debug "AKITA: About to acquire shutdown mutex "\
78
+ "#{shutdown_mutex} for file #{out_file_name} "\
79
+ "while shutting down in #{Thread.current}. "\
80
+ "Self-owned? #{shutdown_mutex.owned?}"
81
+ shutdown_mutex.synchronize {
82
+ Rails.logger.debug "AKITA: Acquired shutdown mutex "\
83
+ "#{shutdown_mutex} for file #{out_file_name} "\
84
+ "while shutting down in #{Thread.current}."
85
+ Rails.logger.debug "AKITA: About to release shutdown mutex "\
86
+ "#{shutdown_mutex} for file #{out_file_name} "\
87
+ "while shutting down in #{Thread.current}."
88
+ }
63
89
  end
64
90
  end
65
91
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: akita-har_logger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.9
4
+ version: 0.2.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jed Liu
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-07-19 00:00:00.000000000 Z
11
+ date: 2021-07-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec