Class: RorVsWild::Agent

Inherits:
Object
  • Object
show all
Includes:
Location
Defined in:
lib/rorvswild/agent.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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_rootObject (readonly)

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_regexObject (readonly)

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

#clientObject (readonly)

Returns the value of attribute client.



23
24
25
# File 'lib/rorvswild/agent.rb', line 23

def client
  @client
end

#configObject (readonly)

Returns the value of attribute config.



23
24
25
# File 'lib/rorvswild/agent.rb', line 23

def config
  @config
end

#queueObject (readonly)

Returns the value of attribute queue.



23
24
25
# File 'lib/rorvswild/agent.rb', line 23

def queue
  @queue
end

Class Method Details

.default_configObject



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_exceptionsObject



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(extra_details = nil, &block)
  begin
    block.call
  rescue Exception => ex
    record_error(ex, extra_details) if !ignored_exception?(ex)
    ex
  end
end

#dataObject



125
126
127
# File 'lib/rorvswild/agent.rb', line 125

def data
  Thread.current[:rorvswild_data] ||= {}
end

#ignored_action?(name) ⇒ Boolean

Returns:

  • (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] # Prevent from recursive jobs
  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, extra_details = nil)
  post_error(exception_to_hash(exception, extra_details))
end

#setup_pluginsObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rorvswild/agent.rb', line 39

def setup_plugins
  Plugin::NetHttp.setup

  Plugin::Redis.setup
  Plugin::Mongo.setup
  Plugin::Elasticsearch.setup

  Plugin::Resque.setup
  Plugin::Sidekiq.setup
  Plugin::ActiveJob.setup
  Plugin::DelayedJob.setup

  Plugin::ActionView.setup
  Plugin::ActiveRecord.setup
  Plugin::ActionMailer.setup
  Plugin::ActionController.setup
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_requestObject



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