Class: RorVsWild::Agent

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Agent

Returns a new instance of Agent.


27
28
29
30
31
32
33
34
35
36
# File 'lib/rorvswild/agent.rb', line 27

def initialize(config)
  @config = self.class.default_config.merge(config)
  @client = Client.new(@config)
  @queue = config[:queue] || Queue.new(client)
  @locator = RorVsWild::Locator.new

  RorVsWild.logger.debug("Start RorVsWild #{RorVsWild::VERSION}")
  setup_plugins
  cleanup_data
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client


25
26
27
# File 'lib/rorvswild/agent.rb', line 25

def client
  @client
end

#configObject (readonly)

Returns the value of attribute config


25
26
27
# File 'lib/rorvswild/agent.rb', line 25

def config
  @config
end

#locatorObject (readonly)

Returns the value of attribute locator


25
26
27
# File 'lib/rorvswild/agent.rb', line 25

def locator
  @locator
end

#queueObject (readonly)

Returns the value of attribute queue


25
26
27
# File 'lib/rorvswild/agent.rb', line 25

def queue
  @queue
end

Class Method Details

.default_configObject


7
8
9
10
11
12
13
14
15
# 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_requests: [],
    ignore_plugins: [],
    ignore_jobs: [],
  }
end

.default_ignored_exceptionsObject


17
18
19
20
21
22
23
# File 'lib/rorvswild/agent.rb', line 17

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


120
121
122
123
124
125
126
127
# File 'lib/rorvswild/agent.rb', line 120

def add_section(section)
  return unless current_data[:sections]
  if sibling = current_data[:sections].find { |s| s.sibling?(section) }
    sibling.merge(section)
  else
    current_data[:sections] << section
  end
end

#catch_error(extra_details = nil, &block) ⇒ Object


95
96
97
98
99
100
101
102
# File 'lib/rorvswild/agent.rb', line 95

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

#current_dataObject


116
117
118
# File 'lib/rorvswild/agent.rb', line 116

def current_data
  Thread.current[:rorvswild_data]
end

#ignored_job?(name) ⇒ Boolean

Returns:

  • (Boolean)

133
134
135
# File 'lib/rorvswild/agent.rb', line 133

def ignored_job?(name)
  config[:ignore_jobs].include?(name)
end

#ignored_request?(name) ⇒ Boolean

Returns:

  • (Boolean)

129
130
131
# File 'lib/rorvswild/agent.rb', line 129

def ignored_request?(name)
  (config[:ignore_actions] || config[:ignore_requests]).include?(name)
end

#measure_block(name, kind = "code".freeze, &block) ⇒ Object


52
53
54
# File 'lib/rorvswild/agent.rb', line 52

def measure_block(name, kind = "code".freeze, &block)
  current_data ? measure_section(name, kind: kind, &block) : measure_job(name, &block)
end

#measure_code(code) ⇒ Object


48
49
50
# File 'lib/rorvswild/agent.rb', line 48

def measure_code(code)
  measure_block(code) { eval(code) }
end

#measure_job(name, parameters: nil, &block) ⇒ Object


70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/rorvswild/agent.rb', line 70

def measure_job(name, parameters: nil, &block)
  return measure_section(name, &block) if current_data # For recursive jobs
  return block.call if ignored_job?(name)
  initialize_data[:name] = name
  begin
    block.call
  rescue Exception => ex
    push_exception(ex, parameters: parameters, job: {name: name})
    raise
  ensure
    current_data[:runtime] = RorVsWild.clock_milliseconds - current_data[:started_at]
    post_job
  end
end

#measure_section(name, kind: "code", appendable_command: false, &block) ⇒ Object


56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/rorvswild/agent.rb', line 56

def measure_section(name, kind: "code", appendable_command: false, &block)
  return block.call unless current_data
  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


108
109
110
111
112
113
114
# File 'lib/rorvswild/agent.rb', line 108

def push_exception(exception, options = nil)
  return if ignored_exception?(exception)
  return unless current_data
  current_data[:error] = exception_to_hash(exception)
  current_data[:error].merge!(options) if options
  current_data[:error]
end

#record_error(exception, extra_details = nil) ⇒ Object


104
105
106
# File 'lib/rorvswild/agent.rb', line 104

def record_error(exception, extra_details = nil)
  post_error(exception_to_hash(exception, extra_details))
end

#setup_pluginsObject


38
39
40
41
42
43
44
45
46
# File 'lib/rorvswild/agent.rb', line 38

def setup_plugins
  for name in RorVsWild::Plugin.constants
    next if config[:ignore_plugins] && config[:ignore_plugins].include?(name.to_s)
    if (plugin = RorVsWild::Plugin.const_get(name)).respond_to?(:setup)
      RorVsWild.logger.debug("Setup RorVsWild::Plugin::#{name}")
      plugin.setup
    end
  end
end

#start_requestObject


85
86
87
# File 'lib/rorvswild/agent.rb', line 85

def start_request
  current_data || initialize_data
end

#stop_requestObject


89
90
91
92
93
# File 'lib/rorvswild/agent.rb', line 89

def stop_request
  return unless current_data
  current_data[:runtime] = RorVsWild.clock_milliseconds - current_data[:started_at]
  post_request
end