akita-har_logger 0.2.0 → 0.2.1

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: 3da46a0fcf41a0f5f1f1d88261e7345713fe9487d7a7c4485d85e0ee808c8717
4
- data.tar.gz: 8857a59375d849bf4c94b47a131c90a950f5bc06da6979c75815d3a6f1c0c4d9
3
+ metadata.gz: 8337c88eacb3b48aa2190bb12b2bc406d1140b6e7d7c9c5ed921b5e92fe557c3
4
+ data.tar.gz: d6621fcf152e604396634a0614cb9fd66205e95be39be8e46142da90283af8a6
5
5
  SHA512:
6
- metadata.gz: a2a17be7f105d38c3f75b70bd42dd8982c189a129895ca887333fc3609d2e3f1c362fddffd7a77f97bd43358bc907982ea6b718f96515297cf9b65c437073a0d
7
- data.tar.gz: 1919850a9c0be28cd22610699a17caec16bafa2b8bf9485ab4b0f7646fbf77a2b4c3294f676033ab6c14258bf52a7ffa08dd9fe3c7e0fe729c90fa228d2acfa2
6
+ metadata.gz: 9efe412f404f5e56abac9af2e79b7d186ab895ceccf40cf369841563f86f5b78eb9ee7fe0cb561b20b7de93a9284343ffa50a355894fe62f6cf3e5d4193ea7d0
7
+ data.tar.gz: b251114f5be749a02d8d198e2dbce71ff98845085cdb48324c66e1791d3d6acd7252205cce6f5358f4e3a25fbabfecbba392094e21c56a88e8b0d89528beb408
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ /pkg/
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- akita-har_logger (0.2.0)
4
+ akita-har_logger (0.2.1)
5
5
  json (~> 2.3)
6
6
 
7
7
  GEM
@@ -33,4 +33,4 @@ DEPENDENCIES
33
33
  rspec (~> 3.10)
34
34
 
35
35
  BUNDLED WITH
36
- 2.2.15
36
+ 2.2.16
data/README.md CHANGED
@@ -48,6 +48,8 @@ Here is a sample configuration for a test environment that just adds the
48
48
  instrumentation.
49
49
 
50
50
  ```ruby
51
+ # config/environments/test.rb
52
+
51
53
  Rails.application.configure.do
52
54
  # Other configuration for the Rails application...
53
55
 
@@ -61,11 +63,30 @@ end
61
63
  ### `ActionController` filter
62
64
 
63
65
  To instrument with a filter, add an instance of `Akita::HarLogger::Filter` as
64
- an `around_action` filter to your `ActionController` implementation. Here is an
65
- example of a bare-bones `app/controllers/application_controller.rb` with this
66
- instrumentation.
66
+ an `around_action` filter to your `ActionController` implementation.
67
+
68
+ For convenience, you can call `Akita::HarLogger::Filter.install` to do this for
69
+ all `ActionController`s in your application. We recommend adding this call to a
70
+ configuration initializer. For example, this initializer adds the filter only
71
+ in the test environment:
67
72
 
68
73
  ```ruby
74
+ # config/initializers/har_logging.rb
75
+
76
+ # Add the HAR logger as an `around_action` filter to all `ActionControllers`
77
+ # that are loaded by the application in the test environment. Optionally give
78
+ # an output HAR file to save your trace. If not specified, this defaults to
79
+ # `akita_trace_{timestamp}.har`.
80
+ Akita::HarLogger::Filter.install("akita_trace.har") if Rails.env.test?
81
+ ```
82
+
83
+ You can also selectively instrument your `ActionController` implementations by
84
+ adding the filter manually. Here is a bare-bones `ActionController`
85
+ implementation that adds the filter only in the test environment.
86
+
87
+ ```ruby
88
+ # app/controllers/application_controller.rb
89
+
69
90
  class ApplicationController < ActionController::API
70
91
  include Response
71
92
  include ExceptionHandler
@@ -73,7 +94,7 @@ class ApplicationController < ActionController::API
73
94
  # Add the HAR logger as an `around_action` filter. Optionally give an output
74
95
  # HAR file to save your trace. If not specified, this defaults to
75
96
  # `akita_trace_{timestamp}.har`.
76
- around_action Akita::HarLogger::Filter.new("akita_trace.har")
97
+ around_action Akita::HarLogger::Filter.new("akita_trace.har") if Rails.env.test?
77
98
  end
78
99
  ```
79
100
 
@@ -29,14 +29,10 @@ module Akita
29
29
  @app = app
30
30
 
31
31
  if out_file_name == nil then
32
- out_file_name = "akita_trace_#{Time.now.to_i}.har"
32
+ out_file_name = HarLogger.default_file_name
33
33
  end
34
34
 
35
- # This queue is used to ensure that event logging is thread-safe. The
36
- # main thread will enqueue HarEntry objects. The HAR writer thread
37
- # below dequeues these objects and writes them to the output file.
38
- @entry_queue = Queue.new
39
- WriterThread.new out_file_name, @entry_queue
35
+ @entry_queue = HarLogger.get_queue(out_file_name)
40
36
  end
41
37
 
42
38
  def call(env)
@@ -58,16 +54,21 @@ module Akita
58
54
  class Filter
59
55
  def initialize(out_file_name = nil)
60
56
  if out_file_name == nil then
61
- out_file_name = "akita_trace_#{Time.now.to_i}.har"
57
+ out_file_name = HarLogger.default_file_name
62
58
  end
63
59
 
64
- # This queue is used to ensure that event logging is thread-safe. The
65
- # main thread will enqueue HarEntry objects. The HAR writer thread
66
- # below dequeues these objects and writes them to the output file.
67
- @entry_queue = Queue.new
68
- WriterThread.new out_file_name, @entry_queue
60
+ @entry_queue = HarLogger.get_queue(out_file_name)
69
61
  end
70
62
 
63
+ # Registers an `on_load` initializer to add a logging filter to any
64
+ # ActionController that is created.
65
+ def self.install(out_file_name = nil, hook_name = :action_controller)
66
+ ActiveSupport.on_load(hook_name) do
67
+ around_action Filter.new(out_file_name)
68
+ end
69
+ end
70
+
71
+ # Implements the actual `around` filter.
71
72
  def around(controller)
72
73
  start_time = Time.now
73
74
 
@@ -84,5 +85,35 @@ module Akita
84
85
  [response.body])
85
86
  end
86
87
  end
88
+
89
+ @@default_file_name = "akita_trace_#{Time.now.to_i}.har"
90
+ def self.default_file_name
91
+ @@default_file_name
92
+ end
93
+
94
+ # Maps the name of each output file to a queue of entries to be logged to
95
+ # that file. The queue is used to ensure that event logging is thread-safe.
96
+ # The main thread will enqueue HarEntry objects. A HAR writer thread
97
+ # dequeues these objects and writes them to the output file.
98
+ @@entry_queues = {}
99
+ @@entry_queues_mutex = Mutex.new
100
+
101
+ # Returns the entry queue for the given file. If an entry queue doesn't
102
+ # already exist, one is created and a HAR writer thread is started for the
103
+ # queue.
104
+ def self.get_queue(out_file_name)
105
+ queue = nil
106
+ @@entry_queues_mutex.synchronize {
107
+ if @@entry_queues.has_key?(out_file_name) then
108
+ return @@entry_queues[out_file_name]
109
+ end
110
+
111
+ queue = Queue.new
112
+ @@entry_queues[out_file_name] = queue
113
+ }
114
+
115
+ WriterThread.new out_file_name, queue
116
+ return queue
117
+ end
87
118
  end
88
119
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Akita
4
4
  module HarLogger
5
- VERSION = "0.2.0"
5
+ VERSION = "0.2.1"
6
6
  end
7
7
  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.0
4
+ version: 0.2.1
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-04-17 00:00:00.000000000 Z
11
+ date: 2021-04-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
@@ -46,6 +46,7 @@ executables: []
46
46
  extensions: []
47
47
  extra_rdoc_files: []
48
48
  files:
49
+ - ".gitignore"
49
50
  - Gemfile
50
51
  - Gemfile.lock
51
52
  - LICENSE