Module: Rack::Metrics
- Defined in:
- lib/rack/metrics.rb,
lib/rack/metrics.rb,
lib/rack/metrics/config.rb,
lib/rack/metrics/current.rb,
lib/rack/metrics/metrics.rb,
lib/rack/metrics/railtie.rb,
lib/rack/metrics/version.rb,
lib/rack/metrics/middleware.rb,
lib/rack/metrics/instrumenter.rb,
lib/generators/rack/metrics/install/install_generator.rb
Defined Under Namespace
Classes: Config, Current, Event, InstallGenerator, Instrumenter, Middleware, Railtie, Stack
Constant Summary
collapse
- VERSION =
"0.4.0"
Class Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Class Attribute Details
.config ⇒ Object
9
10
11
|
# File 'lib/rack/metrics/metrics.rb', line 9
def config
@config ||= Rack::Metrics::Config.new
end
|
.start_processing ⇒ Object
Returns the value of attribute start_processing.
7
8
9
|
# File 'lib/rack/metrics/metrics.rb', line 7
def start_processing
@start_processing
end
|
Class Method Details
.current ⇒ Object
13
14
15
|
# File 'lib/rack/metrics/metrics.rb', line 13
def current
Thread.current[:rack_metrics]
end
|
.current=(c) ⇒ Object
17
18
19
|
# File 'lib/rack/metrics/metrics.rb', line 17
def current=(c)
Thread.current[:rack_metrics] = c
end
|
.log(message) ⇒ Object
33
34
35
36
37
38
39
|
# File 'lib/rack/metrics/metrics.rb', line 33
def log(message)
begin
Rails.logger.info message
rescue
puts message
end
end
|
.parse_stack(stack) ⇒ Object
11
12
13
14
15
16
17
18
19
20
|
# File 'lib/rack/metrics.rb', line 11
def self.parse_stack(stack)
parsed = []
stack.each do |line|
line.gsub!("#{Rails.root}/", '')
if /^((?:app|config|lib|test).+?):(\d+)(?::in `(.*)')?/ =~ line
parsed<< line
end
end
parsed
end
|
.push_data(data) ⇒ Object
21
22
23
24
25
26
27
28
29
30
31
|
# File 'lib/rack/metrics/metrics.rb', line 21
def push_data(data)
return if ENV['RACK_METRICS_API_KEY'].blank?
log("[Rack Metrics] => Pushing metrics data")
begin
uri = URI(::File.join(Rack::Metrics.config.endpoint, 'api/v1'))
res = Net::HTTP.post_form(uri, 'api_key' => ENV['RACK_METRICS_API_KEY'], 'api_version' => '1.1.0', 'data' => data)
rescue => e
log "[Rack Metrics] => Error while pushing metrics data: #{e.message}"
end
Metrics.current = nil
end
|
.subscribe ⇒ Object
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
# File 'lib/rack/metrics.rb', line 22
def self.subscribe
env = Rails.env || ENV['rack_env'] || ENV['RAILS_ENV']
return unless Rack::Metrics.config.environments.include?(env.to_sym)
ActiveSupport::Notifications.subscribe "start_processing.action_controller" do |*args|
Metrics.current = Event.new *args
Metrics.current.stack = Stack.new
Metrics.current.template = Event.new *args
ActiveSupport::Notifications.subscribe "render_template.action_view" do |name, time, finished, transaction_id, payload|
Metrics.current.template.name = name
Metrics.current.template.time = time
Metrics.current.template.end = finished
Metrics.current.template.transaction_id = transaction_id
Metrics.current.template.payload = payload
Metrics.current.template.payload[:identifier] = Metrics.current.template.payload[:identifier].gsub("#{Rails.root}/", '') unless Metrics.current.template.payload[:identifier].nil?
end
end
ActiveSupport::Notifications.subscribe "render_template.action_view" do |name, time, finished, transaction_id, payload|
begin
Metrics.current.template.name = name
Metrics.current.template.time = time
Metrics.current.template.end = finished
Metrics.current.template.transaction_id = transaction_id
Metrics.current.template.payload = payload
Metrics.current.template.payload[:identifier] = Metrics.current.template.payload[:identifier].gsub("#{Rails.root}/", '') unless Metrics.current.template.payload[:identifier].nil?
rescue Exception => e
Metrics.log("[Rack-Metrics] exception raised: #{e.inspect}")
end
end
ActiveSupport::Notifications.subscribe "start_render_partial.action_view" do |*args|
render_partial = Event.new *args
Metrics.current.stack<< render_partial
end
ActiveSupport::Notifications.subscribe "render_partial.action_view" do |*args|
render_partial = Metrics.current.stack.pop
render_partial.init *args
render_partial.payload[:identifier] = render_partial.payload[:identifier].gsub("#{Rails.root}/", '') unless render_partial.payload[:identifier].nil?
if Metrics.current.stack.empty?
Metrics.current.template.partials<< render_partial
else
Metrics.current.stack.peek.partials<< render_partial
end
end
ActiveSupport::Notifications.subscribe "process_action.action_controller" do |name, time, finished, transaction_id, payload|
Metrics.current.name = name
Metrics.current.time = time
Metrics.current.end = finished
Metrics.current.transaction_id = transaction_id
Metrics.current.payload = payload
Metrics.current.payload['env'] = env
memory = `ps -o rss -p #{Process::pid}`.chomp.split("\n").last.strip.to_i / 1024
Metrics.current.payload['memory'] = memory.to_i
begin
data = Metrics.current.to_json
thread = Thread.new do
Metrics.push_data(data)
end
rescue Exception => e
Metrics.log("[Rack-Metrics] exception raised: #{e.inspect}")
end
end
ActiveSupport::Notifications.subscribe "sql.active_record" do |*args|
sql = Event.new *args
if Metrics.current.is_a?(Rack::Metrics::Event)
unless sql.payload[:name] == 'SCHEMA'
sql.payload['stacktrace'] = Metrics.parse_stack(caller(2)).join("\r\n")
if Metrics.current.stack.empty?
Metrics.current.template.queries<< sql
else
Metrics.current.stack.peek.queries<< sql
end
end
end
end
ActiveSupport::Notifications.subscribe "query.moped" do |*args|
query = Event.new *args
if Metrics.current.is_a?(Rack::Metrics::Event)
query.payload['stacktrace'] = Metrics.parse_stack(caller(2)).join("\r\n")
query.payload['sql'] = query.payload[:ops][0].log_inspect unless query.payload[:ops].nil? or !query.payload[:ops][0].respond_to?(:log_inspect)
if Metrics.current.stack.empty?
Metrics.current.template.queries<< query
else
Metrics.current.stack.peek.queries<< query
end
end
end
end
|
Instance Method Details
#current ⇒ Object
42
43
44
|
# File 'lib/rack/metrics/metrics.rb', line 42
def current
Metrics.current
end
|
#current=(c) ⇒ Object
46
47
48
|
# File 'lib/rack/metrics/metrics.rb', line 46
def current=(c)
Metrics.current = c
end
|