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 +4 -4
- data/.gitignore +1 -0
- data/Gemfile.lock +2 -2
- data/README.md +25 -4
- data/lib/akita/har_logger.rb +43 -12
- data/lib/akita/har_logger/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8337c88eacb3b48aa2190bb12b2bc406d1140b6e7d7c9c5ed921b5e92fe557c3
|
4
|
+
data.tar.gz: d6621fcf152e604396634a0614cb9fd66205e95be39be8e46142da90283af8a6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9efe412f404f5e56abac9af2e79b7d186ab895ceccf40cf369841563f86f5b78eb9ee7fe0cb561b20b7de93a9284343ffa50a355894fe62f6cf3e5d4193ea7d0
|
7
|
+
data.tar.gz: b251114f5be749a02d8d198e2dbce71ff98845085cdb48324c66e1791d3d6acd7252205cce6f5358f4e3a25fbabfecbba392094e21c56a88e8b0d89528beb408
|
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
/pkg/
|
data/Gemfile.lock
CHANGED
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.
|
65
|
-
|
66
|
-
|
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
|
|
data/lib/akita/har_logger.rb
CHANGED
@@ -29,14 +29,10 @@ module Akita
|
|
29
29
|
@app = app
|
30
30
|
|
31
31
|
if out_file_name == nil then
|
32
|
-
out_file_name =
|
32
|
+
out_file_name = HarLogger.default_file_name
|
33
33
|
end
|
34
34
|
|
35
|
-
|
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 =
|
57
|
+
out_file_name = HarLogger.default_file_name
|
62
58
|
end
|
63
59
|
|
64
|
-
|
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
|
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.
|
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-
|
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
|