meta_request 0.8.5 → 0.8.6
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 +4 -4
- data/README.md +39 -12
- data/lib/meta_request/app_notifications.rb +88 -0
- data/lib/meta_request/app_request.rb +20 -0
- data/lib/meta_request/config.rb +34 -0
- data/lib/meta_request/event.rb +91 -0
- data/lib/meta_request/log_interceptor.rb +47 -0
- data/lib/meta_request/middlewares/app_request_handler.rb +33 -0
- data/lib/meta_request/middlewares/headers.rb +38 -0
- data/lib/meta_request/middlewares/meta_request_handler.rb +27 -0
- data/lib/meta_request/middlewares.rb +9 -0
- data/lib/meta_request/railtie.rb +27 -0
- data/lib/meta_request/storage.rb +45 -0
- data/lib/meta_request/utils.rb +38 -0
- data/lib/meta_request/version.rb +5 -0
- data/lib/meta_request.rb +29 -0
- metadata +17 -6
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 42d633478998c2f18fc5239e8b17f74338cb9a6252d77faf5f37cb5a05c02e8b
|
|
4
|
+
data.tar.gz: d68155e564411724fb788235da075aa19480dca3bf64cc862fe05d8bdc530cf9
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: b1997459c524ed7de84ba6c5da17b35e88402a72e1c34b121649e64bae5d8f4654481021356e746ee692f4a760f7b986c8a0d0b6a2723f62a6c89eff4f7e1f23
|
|
7
|
+
data.tar.gz: 55c8bfca434ae490505f30041abb3889361b53447d81b54a5e41ffec98444d24e1a9f67cf99cacfa38ce42cf13442db3c1e35fd649bd49f34b355c212fe9efcb
|
data/README.md
CHANGED
|
@@ -1,13 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
===========
|
|
1
|
+
# MetaRequest
|
|
3
2
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
<img width="640" alt="rails_panel" src="https://github.com/dejan/rails_panel/assets/4494/f41b18b9-444c-419f-93fb-163481f53f82">
|
|
3
|
+
Supporting gem for [Rails Panel (Google Chrome extension for Rails development)](https://github.com/dejan/rails_panel).
|
|
7
4
|
|
|
8
5
|
## Installation
|
|
9
6
|
|
|
10
|
-
|
|
7
|
+
Add meta_request gem to development group in Gemfile:
|
|
11
8
|
|
|
12
9
|
```ruby
|
|
13
10
|
group :development do
|
|
@@ -15,16 +12,46 @@ group :development do
|
|
|
15
12
|
end
|
|
16
13
|
```
|
|
17
14
|
|
|
18
|
-
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
See [Rails Panel extension](https://github.com/dejan/rails_panel).
|
|
18
|
+
|
|
19
|
+
## Compatibility Warnings
|
|
20
|
+
|
|
21
|
+
If you're using [LiveReload](http://livereload.com/) or
|
|
22
|
+
[Rack::LiveReload](https://github.com/johnbintz/rack-livereload) make sure to
|
|
23
|
+
exclude watching your tmp/ folder because meta_request writes a lot of data there
|
|
24
|
+
and your browser will refresh like a madman.
|
|
25
|
+
|
|
26
|
+
## Configuration
|
|
27
|
+
|
|
28
|
+
Gem can be configured using block:
|
|
29
|
+
|
|
30
|
+
```ruby
|
|
31
|
+
MetaRequest.configure do |config|
|
|
32
|
+
config.storage_pool_size = 30
|
|
33
|
+
end
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
List of available attributes and defaults can be found in [lib/meta_request/config.rb](lib/meta_request/config.rb).
|
|
19
37
|
|
|
20
|
-
##
|
|
38
|
+
## Docker
|
|
39
|
+
|
|
40
|
+
Apps running in Docker container will have filepaths of the container so links to editor would not work. To fix this, you need to propagate working directory through environment variable `SOURCE_PATH`. With docker-compose it can be done like this:
|
|
41
|
+
|
|
42
|
+
```yaml
|
|
43
|
+
services:
|
|
44
|
+
app:
|
|
45
|
+
environment:
|
|
46
|
+
- SOURCE_PATH=$PWD
|
|
47
|
+
# ...
|
|
48
|
+
```
|
|
21
49
|
|
|
22
|
-
|
|
50
|
+
## Development
|
|
23
51
|
|
|
24
|
-
|
|
52
|
+
Run all tests:
|
|
25
53
|
|
|
26
|
-
|
|
27
|
-
* Ruby 3
|
|
54
|
+
docker-compose up
|
|
28
55
|
|
|
29
56
|
## Licence
|
|
30
57
|
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module MetaRequest
|
|
4
|
+
class AppNotifications
|
|
5
|
+
# these are the specific keys in the cache payload that we display in the
|
|
6
|
+
# panel view
|
|
7
|
+
CACHE_KEY_COLUMNS = %i[key hit options type].freeze
|
|
8
|
+
|
|
9
|
+
# define this here so we can pass it in to all of our cache subscribe calls
|
|
10
|
+
CACHE_BLOCK = proc { |*args|
|
|
11
|
+
name, start, ending, transaction_id, payload = args
|
|
12
|
+
|
|
13
|
+
# from http://edgeguides.rubyonrails.org/active_support_instrumentation.html#cache-fetch-hit-active-support
|
|
14
|
+
#
|
|
15
|
+
# :super_operation :fetch is added when a read is used with #fetch
|
|
16
|
+
#
|
|
17
|
+
# so if :super_operation is present, we'll use it for the type. otherwise
|
|
18
|
+
# strip (say) 'cache_delete.active_support' down to 'delete'
|
|
19
|
+
payload[:type] = payload.delete(:super_operation) || name.sub(/cache_(.*?)\..*$/, '\1')
|
|
20
|
+
|
|
21
|
+
# anything that isn't in CACHE_KEY_COLUMNS gets shoved into :options
|
|
22
|
+
# instead
|
|
23
|
+
payload[:options] = {}
|
|
24
|
+
payload.keys.each do |k|
|
|
25
|
+
payload[:options][k] = payload.delete(k) unless k.in? CACHE_KEY_COLUMNS
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
callsite = Utils.dev_callsite(caller)
|
|
29
|
+
payload.merge!(callsite) if callsite
|
|
30
|
+
|
|
31
|
+
Event.new(name, start, ending, transaction_id, payload)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
# sql processing block - used for sql.active_record and sql.sequel
|
|
35
|
+
|
|
36
|
+
# HACK: we hardcode the event name to 'sql.active_record' so that the ui will
|
|
37
|
+
# display sequel events without modification. otherwise the ui would need to
|
|
38
|
+
# be modified to support a sequel tab (or to change the display name on the
|
|
39
|
+
# active_record tab when necessary - which maybe makes more sense?)
|
|
40
|
+
SQL_EVENT_NAME = 'sql.active_record'
|
|
41
|
+
|
|
42
|
+
SQL_BLOCK = proc { |*args|
|
|
43
|
+
_name, start, ending, transaction_id, payload = args
|
|
44
|
+
callsite = Utils.dev_callsite(caller)
|
|
45
|
+
payload.merge!(callsite) if callsite
|
|
46
|
+
|
|
47
|
+
Event.new(SQL_EVENT_NAME, start, ending, transaction_id, payload)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
VIEW_BLOCK = proc { |*args|
|
|
51
|
+
name, start, ending, transaction_id, payload = args
|
|
52
|
+
payload[:identifier] = MetaRequest::Utils.sub_source_path(payload[:identifier])
|
|
53
|
+
|
|
54
|
+
Event.new(name, start, ending, transaction_id, payload)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
# Subscribe to all events relevant to RailsPanel
|
|
58
|
+
#
|
|
59
|
+
def self.subscribe
|
|
60
|
+
new
|
|
61
|
+
.subscribe('meta_request.log')
|
|
62
|
+
.subscribe('sql.active_record', &SQL_BLOCK)
|
|
63
|
+
.subscribe('sql.sequel', &SQL_BLOCK)
|
|
64
|
+
.subscribe('render_partial.action_view', &VIEW_BLOCK)
|
|
65
|
+
.subscribe('render_template.action_view', &VIEW_BLOCK)
|
|
66
|
+
.subscribe('process_action.action_controller.exception')
|
|
67
|
+
.subscribe('process_action.action_controller') do |*args|
|
|
68
|
+
name, start, ending, transaction_id, payload = args
|
|
69
|
+
payload[:status] = '500' if payload[:exception]
|
|
70
|
+
Event.new(name, start, ending, transaction_id, payload)
|
|
71
|
+
end
|
|
72
|
+
.subscribe('cache_read.active_support', &CACHE_BLOCK)
|
|
73
|
+
.subscribe('cache_generate.active_support', &CACHE_BLOCK)
|
|
74
|
+
.subscribe('cache_fetch_hit.active_support', &CACHE_BLOCK)
|
|
75
|
+
.subscribe('cache_write.active_support', &CACHE_BLOCK)
|
|
76
|
+
.subscribe('cache_delete.active_support', &CACHE_BLOCK)
|
|
77
|
+
.subscribe('cache_exist?.active_support', &CACHE_BLOCK)
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def subscribe(event_name)
|
|
81
|
+
ActiveSupport::Notifications.subscribe(event_name) do |*args|
|
|
82
|
+
event = block_given? ? yield(*args) : Event.new(*args)
|
|
83
|
+
AppRequest.current.events << event if AppRequest.current
|
|
84
|
+
end
|
|
85
|
+
self
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module MetaRequest
|
|
4
|
+
class AppRequest
|
|
5
|
+
attr_reader :id, :events
|
|
6
|
+
|
|
7
|
+
def initialize(id)
|
|
8
|
+
@id = id
|
|
9
|
+
@events = []
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def self.current
|
|
13
|
+
Thread.current[:meta_request_id]
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def current!
|
|
17
|
+
Thread.current[:meta_request_id] = self
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module MetaRequest
|
|
4
|
+
class Config
|
|
5
|
+
attr_writer :logger, :storage_pool_size, :source_path
|
|
6
|
+
|
|
7
|
+
# logger used for reporting gem's fatal errors
|
|
8
|
+
def logger
|
|
9
|
+
@logger ||= Logger.new(File.join(Rails.root, 'log', 'meta_request.log'))
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
# Number of files kept in storage.
|
|
13
|
+
# Increase when using an application loading many simultaneous requests.
|
|
14
|
+
def storage_pool_size
|
|
15
|
+
@storage_pool_size ||= 20
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def source_path
|
|
19
|
+
@source_path ||= ENV['SOURCE_PATH'] || Rails.root.to_s
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# List of relative paths inside Rails app. Used in Location calculation.
|
|
23
|
+
def ignored_paths
|
|
24
|
+
self.ignored_paths = %w[bin vendor] unless @ignored_paths
|
|
25
|
+
@ignored_paths
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def ignored_paths=(paths)
|
|
29
|
+
@ignored_paths = paths.map do |path|
|
|
30
|
+
Rails.root.join(path).to_s.freeze
|
|
31
|
+
end.freeze
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'active_support'
|
|
4
|
+
require 'active_support/cache'
|
|
5
|
+
require 'active_support/json'
|
|
6
|
+
require 'active_support/core_ext'
|
|
7
|
+
|
|
8
|
+
module MetaRequest
|
|
9
|
+
# Subclass of ActiveSupport Event that is JSON encodable
|
|
10
|
+
#
|
|
11
|
+
class Event < ActiveSupport::Notifications::Event
|
|
12
|
+
NOT_JSON_ENCODABLE = 'Not JSON Encodable'
|
|
13
|
+
|
|
14
|
+
attr_reader :duration
|
|
15
|
+
|
|
16
|
+
def initialize(name, start, ending, transaction_id, payload)
|
|
17
|
+
@name = name
|
|
18
|
+
super(name, start, ending, transaction_id, json_encodable(payload))
|
|
19
|
+
@duration = 1000.0 * (ending - start)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def self.events_for_exception(exception_wrapper)
|
|
23
|
+
if defined?(ActionDispatch::ExceptionWrapper)
|
|
24
|
+
exception = exception_wrapper.exception
|
|
25
|
+
trace = exception_wrapper.application_trace
|
|
26
|
+
trace = exception_wrapper.framework_trace if trace.empty?
|
|
27
|
+
else
|
|
28
|
+
exception = exception_wrapper
|
|
29
|
+
trace = exception.backtrace
|
|
30
|
+
end
|
|
31
|
+
trace.unshift "#{exception.class} (#{exception.message})"
|
|
32
|
+
trace.map do |call|
|
|
33
|
+
Event.new('process_action.action_controller.exception', 0, 0, nil, call: call)
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
private
|
|
38
|
+
|
|
39
|
+
def json_encodable(payload)
|
|
40
|
+
return {} unless payload.is_a?(Hash)
|
|
41
|
+
|
|
42
|
+
transform_hash(sanitize_hash(payload), deep: true) do |hash, key, value|
|
|
43
|
+
if value.class.to_s == 'ActionDispatch::Http::Headers'
|
|
44
|
+
value = value.to_h.select { |k, _| k.upcase == k }
|
|
45
|
+
elsif not_encodable?(value)
|
|
46
|
+
value = NOT_JSON_ENCODABLE
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
begin
|
|
50
|
+
value.to_json(methods: [:duration])
|
|
51
|
+
new_value = value
|
|
52
|
+
rescue StandardError
|
|
53
|
+
new_value = NOT_JSON_ENCODABLE
|
|
54
|
+
end
|
|
55
|
+
hash[key] = new_value
|
|
56
|
+
end.with_indifferent_access
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def sanitize_hash(payload)
|
|
60
|
+
if @name =~ /\Acache_\w+\.active_support\z/
|
|
61
|
+
payload[:key] = ActiveSupport::Cache::Store.new.send(:normalize_key, payload[:key])
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
payload.except(:locals)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def not_encodable?(value)
|
|
68
|
+
return true if defined?(ActiveRecord) && value.is_a?(ActiveRecord::ConnectionAdapters::AbstractAdapter)
|
|
69
|
+
return true if defined?(ActiveRecord) && defined?(ActiveRecord::Transaction) && value.is_a?(ActiveRecord::Transaction)
|
|
70
|
+
return true if defined?(ActionDispatch) && (value.is_a?(ActionDispatch::Request) || value.is_a?(ActionDispatch::Response))
|
|
71
|
+
return true if defined?(ActionView) && value.is_a?(ActionView::Helpers::FormBuilder)
|
|
72
|
+
|
|
73
|
+
false
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# https://gist.github.com/dbenhur/1070399
|
|
77
|
+
def transform_hash(original, options = {}, &block)
|
|
78
|
+
options[:safe_descent] ||= {}
|
|
79
|
+
new_hash = {}
|
|
80
|
+
options[:safe_descent][original.object_id] = new_hash
|
|
81
|
+
original.each_with_object(new_hash) do |(key, value), result|
|
|
82
|
+
if options[:deep] && Hash === value
|
|
83
|
+
value = options[:safe_descent].fetch(value.object_id) do
|
|
84
|
+
transform_hash(value, options, &block)
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
block.call(result, key, value)
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
end
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module MetaRequest
|
|
4
|
+
module LogInterceptor
|
|
5
|
+
def debug(message = nil, *args)
|
|
6
|
+
push_event(:debug, message)
|
|
7
|
+
super
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def info(message = nil, *args)
|
|
11
|
+
push_event(:info, message)
|
|
12
|
+
super
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def warn(message = nil, *args)
|
|
16
|
+
push_event(:warn, message)
|
|
17
|
+
super
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def error(message = nil, *args)
|
|
21
|
+
push_event(:error, message)
|
|
22
|
+
super
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def fatal(message = nil, *args)
|
|
26
|
+
push_event(:fatal, message)
|
|
27
|
+
super
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def unknown(message = nil, *args)
|
|
31
|
+
push_event(:unknown, message)
|
|
32
|
+
super
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
private
|
|
36
|
+
|
|
37
|
+
def push_event(level, message)
|
|
38
|
+
callsite = AppRequest.current && Utils.dev_callsite(caller.drop(1))
|
|
39
|
+
if callsite
|
|
40
|
+
payload = callsite.merge(message: message, level: level)
|
|
41
|
+
AppRequest.current.events << Event.new('meta_request.log', 0, 0, 0, payload)
|
|
42
|
+
end
|
|
43
|
+
rescue StandardError => e
|
|
44
|
+
MetaRequest.config.logger.fatal(e.message + "\n " + e.backtrace.join("\n "))
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'json'
|
|
4
|
+
|
|
5
|
+
module MetaRequest
|
|
6
|
+
module Middlewares
|
|
7
|
+
class AppRequestHandler
|
|
8
|
+
def initialize(app)
|
|
9
|
+
@app = app
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def call(env)
|
|
13
|
+
app_request = AppRequest.new env['action_dispatch.request_id']
|
|
14
|
+
app_request.current!
|
|
15
|
+
@app.call(env)
|
|
16
|
+
rescue StandardError => e
|
|
17
|
+
if defined?(ActionDispatch::ExceptionWrapper)
|
|
18
|
+
wrapper = if ActionDispatch::ExceptionWrapper.method_defined? :env
|
|
19
|
+
ActionDispatch::ExceptionWrapper.new(env, e)
|
|
20
|
+
else
|
|
21
|
+
ActionDispatch::ExceptionWrapper.new(env['action_dispatch.backtrace_cleaner'], e)
|
|
22
|
+
end
|
|
23
|
+
app_request.events.push(*Event.events_for_exception(wrapper))
|
|
24
|
+
else
|
|
25
|
+
app_request.events.push(*Event.events_for_exception(e))
|
|
26
|
+
end
|
|
27
|
+
raise
|
|
28
|
+
ensure
|
|
29
|
+
Storage.new(app_request.id).write(app_request.events.to_json) unless app_request.events.empty?
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'rack/contrib/response_headers'
|
|
4
|
+
|
|
5
|
+
module MetaRequest
|
|
6
|
+
module Middlewares
|
|
7
|
+
class Headers
|
|
8
|
+
def initialize(app, app_config)
|
|
9
|
+
@app = app
|
|
10
|
+
@app_config = app_config
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def call(env)
|
|
14
|
+
request_path = env['PATH_INFO']
|
|
15
|
+
middleware = Rack::ResponseHeaders.new(@app) do |headers|
|
|
16
|
+
headers['X-Meta-Request-Version'] = MetaRequest::VERSION unless skip?(request_path)
|
|
17
|
+
end
|
|
18
|
+
middleware.call(env)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
private
|
|
22
|
+
|
|
23
|
+
# returns true if path should be ignored (not handled by RailsPanel extension)
|
|
24
|
+
#
|
|
25
|
+
def skip?(path)
|
|
26
|
+
asset?(path) || path.start_with?('/__better_errors/')
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def asset?(path)
|
|
30
|
+
@app_config.respond_to?(:assets) && path.start_with?(assets_prefix)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def assets_prefix
|
|
34
|
+
"/#{@app_config.assets.prefix[%r{\A/?(.*?)/?\z}, 1]}/"
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module MetaRequest
|
|
4
|
+
module Middlewares
|
|
5
|
+
class MetaRequestHandler
|
|
6
|
+
def initialize(app)
|
|
7
|
+
@app = app
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def call(env)
|
|
11
|
+
request_id = env['PATH_INFO'][%r{/__meta_request/(.+)\.json$}, 1]
|
|
12
|
+
if request_id
|
|
13
|
+
events_json(request_id)
|
|
14
|
+
else
|
|
15
|
+
@app.call(env)
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
private
|
|
20
|
+
|
|
21
|
+
def events_json(request_id)
|
|
22
|
+
events_json = Storage.new(request_id).read
|
|
23
|
+
[200, { 'Content-Type' => 'text/plain; charset=utf-8' }, [events_json]]
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module MetaRequest
|
|
4
|
+
module Middlewares
|
|
5
|
+
autoload :Headers, 'meta_request/middlewares/headers'
|
|
6
|
+
autoload :AppRequestHandler, 'meta_request/middlewares/app_request_handler'
|
|
7
|
+
autoload :MetaRequestHandler, 'meta_request/middlewares/meta_request_handler'
|
|
8
|
+
end
|
|
9
|
+
end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'rails/railtie'
|
|
4
|
+
|
|
5
|
+
module MetaRequest
|
|
6
|
+
class Railtie < ::Rails::Railtie
|
|
7
|
+
initializer 'meta_request.inject_middlewares' do |app|
|
|
8
|
+
app.middleware.use Middlewares::MetaRequestHandler
|
|
9
|
+
|
|
10
|
+
if defined? ActionDispatch::DebugExceptions
|
|
11
|
+
app.middleware.insert_before ActionDispatch::DebugExceptions, Middlewares::Headers, app.config
|
|
12
|
+
else
|
|
13
|
+
app.middleware.use Middlewares::Headers, app.config
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
app.middleware.use Middlewares::AppRequestHandler
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
initializer 'meta_request.log_interceptor' do
|
|
20
|
+
Rails.logger&.extend(LogInterceptor)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
initializer 'meta_request.subscribe_to_notifications' do
|
|
24
|
+
AppNotifications.subscribe
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module MetaRequest
|
|
4
|
+
class Storage
|
|
5
|
+
attr_reader :key
|
|
6
|
+
|
|
7
|
+
def initialize(key)
|
|
8
|
+
@key = key
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def write(value)
|
|
12
|
+
FileUtils.mkdir_p dir_path
|
|
13
|
+
File.open(json_file, 'wb') { |file| file.write(value) }
|
|
14
|
+
maintain_file_pool(MetaRequest.config.storage_pool_size)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def read
|
|
18
|
+
File.read(json_file)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
private
|
|
22
|
+
|
|
23
|
+
def maintain_file_pool(size)
|
|
24
|
+
files = Dir["#{dir_path}/*.json"]
|
|
25
|
+
files = files.sort_by { |f| -file_ctime(f) }
|
|
26
|
+
(files[size..-1] || []).each do |file|
|
|
27
|
+
FileUtils.rm_f(file)
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def file_ctime(file)
|
|
32
|
+
File.stat(file).ctime.to_i
|
|
33
|
+
rescue Errno::ENOENT
|
|
34
|
+
0
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def json_file
|
|
38
|
+
File.join(dir_path, "#{key}.json")
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def dir_path
|
|
42
|
+
File.join(Rails.root, 'tmp', 'data', 'meta_request')
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module MetaRequest
|
|
4
|
+
module Utils
|
|
5
|
+
module_function
|
|
6
|
+
|
|
7
|
+
def dev_callsite(caller)
|
|
8
|
+
app_line = caller.detect { |c| valid_application_path? c }
|
|
9
|
+
return nil unless app_line
|
|
10
|
+
|
|
11
|
+
_, filename, _, line, _, method = app_line.split(/^(.*?)(:(\d+))(:in `(.*)')?$/)
|
|
12
|
+
|
|
13
|
+
{
|
|
14
|
+
filename: sub_source_path(filename),
|
|
15
|
+
line: line.to_i,
|
|
16
|
+
method: method
|
|
17
|
+
}
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def sub_source_path(path)
|
|
21
|
+
rails_root = MetaRequest.rails_root
|
|
22
|
+
source_path = MetaRequest.config.source_path
|
|
23
|
+
return path if rails_root == source_path
|
|
24
|
+
|
|
25
|
+
path.sub(rails_root, source_path)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def valid_application_path?(path)
|
|
29
|
+
path.start_with?(MetaRequest.rails_root) && !ignored_path?(path)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def ignored_path?(path)
|
|
33
|
+
MetaRequest.config.ignored_paths.any? do |ignored_path|
|
|
34
|
+
path.start_with?(ignored_path.to_s)
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
data/lib/meta_request.rb
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module MetaRequest
|
|
4
|
+
autoload :VERSION, 'meta_request/version'
|
|
5
|
+
autoload :Config, 'meta_request/config'
|
|
6
|
+
autoload :Event, 'meta_request/event'
|
|
7
|
+
autoload :AppRequest, 'meta_request/app_request'
|
|
8
|
+
autoload :Storage, 'meta_request/storage'
|
|
9
|
+
autoload :Middlewares, 'meta_request/middlewares'
|
|
10
|
+
autoload :LogInterceptor, 'meta_request/log_interceptor'
|
|
11
|
+
autoload :AppNotifications, 'meta_request/app_notifications'
|
|
12
|
+
autoload :Utils, 'meta_request/utils'
|
|
13
|
+
|
|
14
|
+
def self.config
|
|
15
|
+
@config ||= Config.new
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def self.configure
|
|
19
|
+
yield config
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# stash a frozen copy away so we're not allocating a new string over and over
|
|
23
|
+
# again in AppNotifications and LogInterceptor
|
|
24
|
+
def self.rails_root
|
|
25
|
+
@rails_root ||= Rails.root.to_s.freeze
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
require 'meta_request/railtie'
|
metadata
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: meta_request
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.8.
|
|
4
|
+
version: 0.8.6
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Dejan Simic
|
|
8
|
-
autorequire:
|
|
9
8
|
bindir: bin
|
|
10
9
|
cert_chain: []
|
|
11
|
-
date:
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
12
11
|
dependencies:
|
|
13
12
|
- !ruby/object:Gem::Dependency
|
|
14
13
|
name: rack-contrib
|
|
@@ -85,11 +84,24 @@ extensions: []
|
|
|
85
84
|
extra_rdoc_files: []
|
|
86
85
|
files:
|
|
87
86
|
- README.md
|
|
87
|
+
- lib/meta_request.rb
|
|
88
|
+
- lib/meta_request/app_notifications.rb
|
|
89
|
+
- lib/meta_request/app_request.rb
|
|
90
|
+
- lib/meta_request/config.rb
|
|
91
|
+
- lib/meta_request/event.rb
|
|
92
|
+
- lib/meta_request/log_interceptor.rb
|
|
93
|
+
- lib/meta_request/middlewares.rb
|
|
94
|
+
- lib/meta_request/middlewares/app_request_handler.rb
|
|
95
|
+
- lib/meta_request/middlewares/headers.rb
|
|
96
|
+
- lib/meta_request/middlewares/meta_request_handler.rb
|
|
97
|
+
- lib/meta_request/railtie.rb
|
|
98
|
+
- lib/meta_request/storage.rb
|
|
99
|
+
- lib/meta_request/utils.rb
|
|
100
|
+
- lib/meta_request/version.rb
|
|
88
101
|
homepage: https://github.com/dejan/rails_panel/tree/master/meta_request
|
|
89
102
|
licenses:
|
|
90
103
|
- MIT
|
|
91
104
|
metadata: {}
|
|
92
|
-
post_install_message:
|
|
93
105
|
rdoc_options: []
|
|
94
106
|
require_paths:
|
|
95
107
|
- lib
|
|
@@ -104,8 +116,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
104
116
|
- !ruby/object:Gem::Version
|
|
105
117
|
version: '0'
|
|
106
118
|
requirements: []
|
|
107
|
-
rubygems_version:
|
|
108
|
-
signing_key:
|
|
119
|
+
rubygems_version: 4.0.12
|
|
109
120
|
specification_version: 4
|
|
110
121
|
summary: Request your Rails request
|
|
111
122
|
test_files: []
|