class RorVsWild::Agent

Experimental RBS support (using type sampling data from the type_fusion project).

# sig/rorvswild/agent.rbs

class RorVsWild::Agent
  def add_section: (RorVsWild::Section section) -> (false | Array[RorVsWild::Section])
  def current_data: () -> Hash
  def error_context: () -> nil
  def ignored_request?: (String name) -> false
end

def self.default_config

def self.default_config
  {
    api_url: "https://www.rorvswild.com/api/v1",
    ignore_exceptions: default_ignored_exceptions,
    ignore_requests: [],
    ignore_plugins: [],
    ignore_jobs: [],
  }
end

def self.default_ignored_exceptions

def self.default_ignored_exceptions
  if defined?(Rails)
    ActionDispatch::ExceptionWrapper.rescue_responses.keys
  else
    []
  end
end

def add_section(section)

Experimental RBS support (using type sampling data from the type_fusion project).

def add_section: (RorVsWild::Section section) -> (false | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section)

This signature was generated using 8 samples from 1 application.

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

def catch_error(context = nil, &block)

def catch_error(context = nil, &block)
  begin
    block.call
  rescue Exception => ex
    record_error(ex, context)
    ex
  end
end

def cleanup_data

def cleanup_data
  result = Thread.current[:rorvswild_data]
  Thread.current[:rorvswild_data] = nil
  result
end

def current_data

Experimental RBS support (using type sampling data from the type_fusion project).

type RorVsWild__Agent_current_data_return_value = sections | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | section_stack | RorVsWild::Section | environment | os | String | user | String | host | String | ruby | String | rails | String | pid | Integer | cwd | String | revision | String | started_at | Integer | path | String | name | String | sections | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | section_stack | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | environment | os | String | user | String | host | String | ruby | String | rails | String | pid | Integer | cwd | String | revision | String | started_at | Integer | path | String | name | String | sections |  | section_stack | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | environment | os | String | user | String | host | String | ruby | String | rails | String | pid | Integer | cwd | String | revision | String | started_at | Integer | path | String | name | String | sections | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | section_stack | RorVsWild::Section | environment | os | String | user | String | host | String | ruby | String | rails | String | pid | Integer | cwd | String | revision | String | started_at | Integer | path | String | name | String | sections |  | section_stack | RorVsWild::Section | environment | os | String | user | String | host | String | ruby | String | rails | String | pid | Integer | cwd | String | revision | String | started_at | Integer | path | String | name | String | sections | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | section_stack | RorVsWild::Section | RorVsWild::Section | environment | os | String | user | String | host | String | ruby | String | rails | String | pid | Integer | cwd | String | revision | String | started_at | Integer | path | String | name | String | sections | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | section_stack | RorVsWild::Section | RorVsWild::Section | environment | os | String | user | String | host | String | ruby | String | rails | String | pid | Integer | cwd | String | revision | String | started_at | Integer | path | String | name | String | sections | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | section_stack | RorVsWild::Section | environment | os | String | user | String | host | String | ruby | String | rails | String | pid | Integer | cwd | String | revision | String | started_at | Integer | path | String | name | String | sections | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | section_stack | RorVsWild::Section | environment | os | String | user | String | host | String | ruby | String | rails | String | pid | Integer | cwd | String | revision | String | started_at | Integer | path | String | name | String | sections | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | section_stack | RorVsWild::Section | environment | os | String | user | String | host | String | ruby | String | rails | String | pid | Integer | cwd | String | revision | String | started_at | Integer | path | String | name | String | sections | RorVsWild::Section | RorVsWild::Section | section_stack | RorVsWild::Section | RorVsWild::Section | environment | os | String | user | String | host | String | ruby | String | rails | String | pid | Integer | cwd | String | revision | String | started_at | Integer | path | String | name | String | sections | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | section_stack | RorVsWild::Section | RorVsWild::Section | environment | os | String | user | String | host | String | ruby | String | rails | String | pid | Integer | cwd | String | revision | String | started_at | Integer | path | String | name | String | sections | RorVsWild::Section | section_stack |  | environment | os | String | user | String | host | String | ruby | String | rails | String | pid | Integer | cwd | String | revision | String | started_at | Integer | path | String | name | String | sections |  | section_stack |  | environment | os | String | user | String | host | String | ruby | String | rails | String | pid | Integer | cwd | String | revision | String | started_at | Integer | path | String | name | String | sections | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | section_stack | RorVsWild::Section | environment | os | String | user | String | host | String | ruby | String | rails | String | pid | Integer | cwd | String | revision | String | started_at | Integer | path | String | name | String | sections | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | section_stack | RorVsWild::Section | environment | os | String | user | String | host | String | ruby | String | rails | String | pid | Integer | cwd | String | revision | String | started_at | Integer | path | String | name | String | sections | RorVsWild::Section | section_stack | RorVsWild::Section | environment | os | String | user | String | host | String | ruby | String | rails | String | pid | Integer | cwd | String | revision | String | started_at | Integer | path | String | name | String | sections | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | section_stack | RorVsWild::Section | environment | os | String | user | String | host | String | ruby | String | rails | String | pid | Integer | cwd | String | revision | String | started_at | Integer | path | String | name | String | sections | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | section_stack | RorVsWild::Section | RorVsWild::Section | environment | os | String | user | String | host | String | ruby | String | rails | String | pid | Integer | cwd | String | revision | String | started_at | Integer | path | String | name | String | sections | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | section_stack | RorVsWild::Section | environment | os | String | user | String | host | String | ruby | String | rails | String | pid | Integer | cwd | String | revision | String | started_at | Integer | path | String | name | String | sections | RorVsWild::Section | RorVsWild::Section | section_stack | RorVsWild::Section | environment | os | String | user | String | host | String | ruby | String | rails | String | pid | Integer | cwd | String | revision | String | started_at | Integer | path | String | name | String | sections | RorVsWild::Section | RorVsWild::Section | section_stack |  | environment | os | String | user | String | host | String | ruby | String | rails | String | pid | Integer | cwd | String | revision | String | started_at | Integer | path | String | name | String | sections | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | section_stack | RorVsWild::Section | environment | os | String | user | String | host | String | ruby | String | rails | String | pid | Integer | cwd | String | revision | String | started_at | Integer | path | String | name | String | error | line | Integer | file | String | message | String | backtrace | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | exception | String | context | NilClass | environment | os | String | user | String | host | String | ruby | String | rails | String | pid | Integer | cwd | String | revision | String | session | session_id | String | _csrf_token | String | parameters | name | String | controller | String | action | String | request | headers | Host | String | Sec-Fetch-Site | String | Accept-Encoding | String | Connection | String | Sec-Fetch-Mode | String | Accept | String | User-Agent | String | Referer | String | Sec-Fetch-Dest | String | Accept-Language | String | Version | String | name | String | method | String | url | String | sections | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | RorVsWild::Section | section_stack | RorVsWild::Section | RorVsWild::Section | environment | os | String | user | String | host | String | ruby | String | rails | String | pid | Integer | cwd | String | revision | String | started_at | Integer | path | String | name | String | error | line | Integer | file | String | message | String | backtrace | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | String | exception | String | context | NilClass | environment | os | String | user | String | host | String | ruby | String | rails | String | pid | Integer | cwd | String | revision | String | session | session_id | String | _csrf_token | String | parameters | name | String | controller | String | action | String | request | headers | Host | String | Sec-Fetch-Site | String | Accept-Encoding | String | Connection | String | Sec-Fetch-Mode | String | Accept | String | User-Agent | String | Referer | String | Sec-Fetch-Dest | String | Accept-Language | String | Version | String | name | String | method | String | url | String

def current_data: () -> RorVsWild__Agent_current_data_return_value

This signature was generated using 94 samples from 1 application.

def current_data
  Thread.current[:rorvswild_data]
end

def error_context

Experimental RBS support (using type sampling data from the type_fusion project).

def error_context: () -> nil

This signature was generated using 1 sample from 1 application.

def error_context
  current_data[:error_context] if current_data
end

def error_context=(hash)

def error_context=(hash)
  current_data[:error_context] = hash if current_data
end

def exception_to_hash(exception, context = nil)

def exception_to_hash(exception, context = nil)
  file, line = locator.find_most_relevant_file_and_line_from_exception(exception)
  context = context ? error_context.merge(context) : error_context if error_context
  {
    line: line.to_i,
    file: locator.relative_path(file),
    message: exception.message,
    backtrace: exception.backtrace || ["No backtrace"],
    exception: exception.class.to_s,
    context: context,
    environment: Host.to_h,
  }
end

def ignored_exception?(exception)

def ignored_exception?(exception)
  return false unless config[:ignore_exceptions]
  config[:ignore_exceptions].any? { |str_or_regex| str_or_regex === exception.class.to_s }
end

def ignored_job?(name)

def ignored_job?(name)
  config[:ignore_jobs].any? { |str_or_regex| str_or_regex === name }
end

def ignored_request?(name)

Experimental RBS support (using type sampling data from the type_fusion project).

def ignored_request?: (String name) -> false

This signature was generated using 1 sample from 1 application.

def ignored_request?(name)
  config[:ignore_requests].any? { |str_or_regex| str_or_regex === name }
end

def initialize(config)

def initialize(config)
  @config = self.class.default_config.merge(config)
  load_features
  @client = Client.new(@config)
  @queue = config[:queue] || Queue.new(client)
  @locator = RorVsWild::Locator.new
  Host.load_config(config)
  Deployment.load_config(config)
  RorVsWild.logger.debug("Start RorVsWild #{RorVsWild::VERSION}")
  setup_plugins
  cleanup_data
  send_deployment
end

def initialize_data

def initialize_data
  Thread.current[:rorvswild_data] = {
    sections: [],
    section_stack: [],
    environment: Host.to_h,
    started_at: RorVsWild.clock_milliseconds,
  }
end

def load_features

def load_features
  features = config[:features] || []
  RorVsWild.logger.info("Server metrics are now monitored enabled by default") if features.include?("server_metrics")
end

def measure_block(name = nil, kind = "code".freeze, &block)

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

def measure_code(code)

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

def measure_job(name, parameters: nil, &block)

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]
    queue_job
  end
end

def measure_section(name, kind: "code", appendable_command: false, &block)

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

def merge_error_context(hash)

def merge_error_context(hash)
  self.error_context = error_context ? error_context.merge(hash) : hash
end

def push_exception(exception, options = nil)

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

def queue_job

def queue_job
  queue.push_job(cleanup_data)
end

def queue_request

def queue_request
  (data = cleanup_data) && data[:name] && queue.push_request(data)
end

def record_error(exception, context = nil)

def record_error(exception, context = nil)
  send_error(exception_to_hash(exception, context)) if !ignored_exception?(exception)
end

def send_deployment

def send_deployment
  client.post("/deployments", deployment: Deployment.to_h)
end

def send_error(hash)

def send_error(hash)
  client.post_async("/errors".freeze, error: hash)
end

def setup_plugins

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

def start_request

def start_request
  current_data || initialize_data
end

def stop_request

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