Class: RorVsWild::Agent
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
-
#add_section(section) ⇒ Object
-
#catch_error(extra_details = nil, &block) ⇒ Object
-
#data ⇒ Object
-
#ignored_action?(name) ⇒ Boolean
-
#initialize(config) ⇒ Agent
constructor
-
#measure_block(name, kind = "code".freeze, &block) ⇒ Object
-
#measure_code(code) ⇒ Object
-
#measure_job(name, parameters: nil, &block) ⇒ Object
-
#measure_section(name, kind: "code", appendable_command: false, &block) ⇒ Object
-
#push_exception(exception, options = nil) ⇒ Object
-
#record_error(exception, extra_details = nil) ⇒ Object
-
#setup_plugins ⇒ Object
-
#start_request(payload) ⇒ Object
-
#stop_request ⇒ Object
Methods included from Location
#extract_most_relevant_file_and_line, #extract_most_relevant_file_and_line_from_array_of_strings, #extract_most_relevant_file_and_line_from_exception, #find_most_relevant_location, #gem_home, #gem_home_regex, #guess_gem_home, #relative_path
Constructor Details
#initialize(config) ⇒ Agent
Returns a new instance of Agent.
25
26
27
28
29
30
31
32
33
34
35
36
37
|
# File 'lib/rorvswild/agent.rb', line 25
def initialize(config)
@config = self.class.default_config.merge(config)
@client = Client.new(@config)
@queue = config[:queue] || Queue.new(client)
@app_root = config[:app_root]
@app_root ||= Rails.root.to_s if defined?(Rails)
@app_root_regex = app_root ? /\A#{app_root}/ : nil
RorVsWild.logger.info("Start RorVsWild #{RorVsWild::VERSION} from #{app_root}")
setup_plugins
cleanup_data
end
|
Instance Attribute Details
#app_root ⇒ Object
Returns the value of attribute app_root.
23
24
25
|
# File 'lib/rorvswild/agent.rb', line 23
def app_root
@app_root
end
|
#app_root_regex ⇒ Object
Returns the value of attribute app_root_regex.
23
24
25
|
# File 'lib/rorvswild/agent.rb', line 23
def app_root_regex
@app_root_regex
end
|
#client ⇒ Object
Returns the value of attribute client.
23
24
25
|
# File 'lib/rorvswild/agent.rb', line 23
def client
@client
end
|
#config ⇒ Object
Returns the value of attribute config.
23
24
25
|
# File 'lib/rorvswild/agent.rb', line 23
def config
@config
end
|
#queue ⇒ Object
Returns the value of attribute queue.
23
24
25
|
# File 'lib/rorvswild/agent.rb', line 23
def queue
@queue
end
|
Class Method Details
.default_config ⇒ Object
7
8
9
10
11
12
13
|
# File 'lib/rorvswild/agent.rb', line 7
def self.default_config
{
api_url: "https://www.rorvswild.com/api/v1",
ignore_exceptions: default_ignored_exceptions,
ignore_actions: [],
}
end
|
.default_ignored_exceptions ⇒ Object
15
16
17
18
19
20
21
|
# File 'lib/rorvswild/agent.rb', line 15
def self.default_ignored_exceptions
if defined?(Rails)
%w[ActionController::RoutingError] + Rails.application.config.action_dispatch.rescue_responses.map { |(key,value)| key }
else
[]
end
end
|
Instance Method Details
#add_section(section) ⇒ Object
129
130
131
132
133
134
135
136
|
# File 'lib/rorvswild/agent.rb', line 129
def add_section(section)
return unless data[:sections]
if sibling = data[:sections].find { |s| s.sibling?(section) }
sibling.merge(section)
else
data[:sections] << section
end
end
|
#catch_error(extra_details = nil, &block) ⇒ Object
105
106
107
108
109
110
111
112
|
# File 'lib/rorvswild/agent.rb', line 105
def catch_error( = nil, &block)
begin
block.call
rescue Exception => ex
record_error(ex, ) if !ignored_exception?(ex)
ex
end
end
|
#data ⇒ Object
125
126
127
|
# File 'lib/rorvswild/agent.rb', line 125
def data
Thread.current[:rorvswild_data] ||= {}
end
|
#ignored_action?(name) ⇒ Boolean
138
139
140
|
# File 'lib/rorvswild/agent.rb', line 138
def ignored_action?(name)
config[:ignore_actions].include?(name)
end
|
#measure_block(name, kind = "code".freeze, &block) ⇒ Object
61
62
63
|
# File 'lib/rorvswild/agent.rb', line 61
def measure_block(name, kind = "code".freeze, &block)
data[:name] ? measure_section(name, kind: kind, &block) : measure_job(name, &block)
end
|
#measure_code(code) ⇒ Object
57
58
59
|
# File 'lib/rorvswild/agent.rb', line 57
def measure_code(code)
measure_block(code) { eval(code) }
end
|
#measure_job(name, parameters: nil, &block) ⇒ Object
79
80
81
82
83
84
85
86
87
88
89
90
91
|
# File 'lib/rorvswild/agent.rb', line 79
def measure_job(name, parameters: nil, &block)
return block.call if data[:name] initialize_data(name)
begin
block.call
rescue Exception => ex
push_exception(ex, parameters: parameters)
raise
ensure
data[:runtime] = RorVsWild.clock_milliseconds - data[:started_at]
post_job
end
end
|
#measure_section(name, kind: "code", appendable_command: false, &block) ⇒ Object
65
66
67
68
69
70
71
72
73
74
75
76
77
|
# File 'lib/rorvswild/agent.rb', line 65
def measure_section(name, kind: "code", appendable_command: false, &block)
return block.call unless data[:name]
begin
RorVsWild::Section.start do |section|
section.appendable_command = appendable_command
section.command = name
section.kind = kind
end
block.call
ensure
RorVsWild::Section.stop
end
end
|
#push_exception(exception, options = nil) ⇒ Object
118
119
120
121
122
123
|
# File 'lib/rorvswild/agent.rb', line 118
def push_exception(exception, options = nil)
return if ignored_exception?(exception)
data[:error] = exception_to_hash(exception)
data[:error].merge!(options) if options
data[:error]
end
|
#record_error(exception, extra_details = nil) ⇒ Object
114
115
116
|
# File 'lib/rorvswild/agent.rb', line 114
def record_error(exception, = nil)
post_error(exception_to_hash(exception, ))
end
|
#start_request(payload) ⇒ Object
93
94
95
96
97
|
# File 'lib/rorvswild/agent.rb', line 93
def start_request(payload)
return if data[:name]
initialize_data(payload[:name])
data[:path] = payload[:path]
end
|
#stop_request ⇒ Object
99
100
101
102
103
|
# File 'lib/rorvswild/agent.rb', line 99
def stop_request
return unless data[:name]
data[:runtime] = RorVsWild.clock_milliseconds - data[:started_at]
post_request
end
|