class ElasticAPM::Config

rubocop:disable Metrics/ClassLength @api private

Constants

DEFAULTS
DEPRECATED_OPTIONS
DURATION_DEFAULT_UNITS
DURATION_KEYS
ENV_TO_KEY
SIZE_DEFAULT_UNITS
SIZE_KEYS

Attributes

active[RW]
active?[RW]
alert_logger[W]
api_buffer_size[RW]
api_request_size[RW]
api_request_time[RW]
capture_body[R]
capture_body?[R]
capture_env[RW]
capture_env?[RW]
capture_headers[RW]
capture_headers?[RW]
config_file[RW]
current_user_email_method[RW]
current_user_id_method[RW]
current_user_method[RW]
current_user_username_method[RW]
custom_key_filters[R]
default_tags[RW]
disable_send[RW]
disable_send?[RW]
disable_start_message[RW]
disable_start_message?[RW]
disabled_spies[RW]
environment[RW]
filter_exception_types[RW]
framework_name[RW]
framework_version[RW]
hostname[RW]
http_compression[RW]
http_compression?[RW]
ignore_url_patterns[R]
instrument[RW]
instrument?[RW]
instrumented_rake_tasks[RW]
log_level[RW]
log_path[RW]
logger[RW]
metrics_interval[RW]
pool_size[RW]
proxy_address[RW]
proxy_headers[RW]
proxy_password[RW]
proxy_port[RW]
proxy_username[RW]
root_path[RW]
secret_token[RW]
server_ca_cert[RW]
server_url[RW]
service_name[RW]
service_version[RW]
source_lines_error_app_frames[RW]
source_lines_error_library_frames[RW]
source_lines_span_app_frames[RW]
source_lines_span_library_frames[RW]
span_frames_min_duration[R]
span_frames_min_duration_us[R]
stack_trace_limit[RW]
transaction_max_spans[RW]
transaction_sample_rate[RW]
verify_server_cert[RW]
verify_server_cert?[RW]
view_paths[RW]

Public Class Methods

new(options = {}) { |self| ... } click to toggle source
# File lib/elastic_apm/config.rb, line 126
def initialize(options = {})
  set_defaults

  set_from_args(options)
  set_from_config_file
  set_from_env

  normalize_durations
  normalize_sizes

  yield self if block_given?

  build_logger if logger.nil?
end

Public Instance Methods

alert_logger() click to toggle source
# File lib/elastic_apm/config.rb, line 211
def alert_logger
  @alert_logger ||= PrefixedLogger.new($stdout, prefix: Logging::PREFIX)
end
app=(app) click to toggle source
# File lib/elastic_apm/config.rb, line 215
def app=(app)
  case app_type?(app)
  when :sinatra
    set_sinatra(app)
  when :rails
    set_rails(app)
  else
    self.service_name = 'ruby'
  end
end
app_type?(app) click to toggle source
# File lib/elastic_apm/config.rb, line 226
def app_type?(app)
  if defined?(Rails::Application) && app.is_a?(Rails::Application)
    return :rails
  end

  if app.is_a?(Class) && app.superclass.to_s == 'Sinatra::Base'
    return :sinatra
  end

  nil
end
available_spies() click to toggle source

rubocop:disable Metrics/MethodLength

# File lib/elastic_apm/config.rb, line 251
def available_spies
  %w[
    delayed_job
    elasticsearch
    faraday
    http
    json
    mongo
    net_http
    redis
    sequel
    sidekiq
    sinatra
    tilt
    rake
  ]
end
capture_body=(value) click to toggle source

rubocop:disable Metrics/MethodLength

# File lib/elastic_apm/config.rb, line 319
def capture_body=(value)
  if value =~ /(all|transactions|errors|off)/
    @capture_body = value
    return
  end

  case value
  when true
    alert_logger.warn "Boolean value for option `capture_body' has " \
      "been deprecated. Setting to 'all'"
    @capture_body = 'all'
  when false
    alert_logger.warn "Boolean value for option `capture_body' has " \
      "been deprecated. Setting to 'off'"
    @capture_body = 'off'
  else
    default = DEFAULTS[:capture_body]
    alert_logger.warn "Unknown value `#{value}' for option "\
      "`capture_body'. Defaulting to `#{default}'"
    @capture_body = default
  end
end
collect_metrics?() click to toggle source
# File lib/elastic_apm/config.rb, line 314
def collect_metrics?
  metrics_interval > 0
end
custom_key_filters=(filters) click to toggle source
# File lib/elastic_apm/config.rb, line 242
def custom_key_filters=(filters)
  @custom_key_filters = Array(filters).map(&Regexp.method(:new))
end
enabled_spies() click to toggle source

rubocop:enable Metrics/MethodLength

# File lib/elastic_apm/config.rb, line 270
def enabled_spies
  available_spies - disabled_spies
end
ignore_url_patterns=(strings) click to toggle source
# File lib/elastic_apm/config.rb, line 246
def ignore_url_patterns=(strings)
  @ignore_url_patterns = Array(strings).map(&Regexp.method(:new))
end
method_missing(name, *args) click to toggle source
Calls superclass method
# File lib/elastic_apm/config.rb, line 301
def method_missing(name, *args)
  if DEPRECATED_OPTIONS.include?(name)
    alert_logger.warn "The option `#{name}' has been removed."
    return
  end

  if name.to_s.end_with?('=')
    raise ConfigError, "No such option `#{name.to_s.delete('=')}'"
  end

  super
end
respond_to_missing?(name) click to toggle source
# File lib/elastic_apm/config.rb, line 295
def respond_to_missing?(name)
  return true if DEPRECATED_OPTIONS.include? name
  return true if name.to_s.end_with?('=')
  false
end
span_frames_min_duration=(duration) click to toggle source
# File lib/elastic_apm/config.rb, line 274
def span_frames_min_duration=(duration)
  @span_frames_min_duration = duration
  @span_frames_min_duration_us = duration * 1_000_000
end
span_frames_min_duration?() click to toggle source
# File lib/elastic_apm/config.rb, line 279
def span_frames_min_duration?
  span_frames_min_duration != 0
end
use_ssl?() click to toggle source
# File lib/elastic_apm/config.rb, line 238
def use_ssl?
  server_url.start_with?('https')
end

Private Instance Methods

assign(options) click to toggle source

rubocop:enable Metrics/MethodLength

# File lib/elastic_apm/config.rb, line 345
def assign(options)
  options.each do |key, value|
    send("#{key}=", value)
  end
end
build_logger() click to toggle source
# File lib/elastic_apm/config.rb, line 423
def build_logger
  logger = Logger.new(log_path == '-' ? STDOUT : log_path)
  logger.level = log_level

  self.logger = logger
end
format_name(str) click to toggle source
# File lib/elastic_apm/config.rb, line 430
def format_name(str)
  str && str.gsub('::', '_')
end
normalize_durations() click to toggle source
# File lib/elastic_apm/config.rb, line 434
def normalize_durations
  DURATION_KEYS.each do |key|
    value = send(key).to_s
    default_unit = DURATION_DEFAULT_UNITS.fetch(key, 's')
    duration = Duration.parse(value, default_unit: default_unit)
    send("#{key}=", duration.seconds)
  end
end
normalize_sizes() click to toggle source
# File lib/elastic_apm/config.rb, line 443
def normalize_sizes
  SIZE_KEYS.each do |key|
    value = send(key).to_s
    default_unit = SIZE_DEFAULT_UNITS.fetch(key, 'b')
    size = Size.parse(value, default_unit: default_unit)
    send("#{key}=", size.bytes)
  end
end
rails_app_name(app) click to toggle source
# File lib/elastic_apm/config.rb, line 415
def rails_app_name(app)
  if Rails::VERSION::MAJOR >= 6
    app.class.module_parent_name
  else
    app.class.parent_name
  end
end
set_defaults() click to toggle source
# File lib/elastic_apm/config.rb, line 351
def set_defaults
  assign(DEFAULTS)
end
set_from_args(options) click to toggle source

rubocop:enable Metrics/AbcSize rubocop:enable Metrics/MethodLength, Metrics/CyclomaticComplexity

# File lib/elastic_apm/config.rb, line 379
def set_from_args(options)
  assign(options)
rescue ConfigError => e
  alert_logger.warn format(
    'Failed to configure from arguments: %s',
    e.message
  )
end
set_from_config_file() click to toggle source
# File lib/elastic_apm/config.rb, line 388
def set_from_config_file
  return unless File.exist?(config_file)
  assign(YAML.safe_load(ERB.new(File.read(config_file)).result) || {})
rescue ConfigError => e
  alert_logger.warn format(
    'Failed to configure from config file: %s',
    e.message
  )
end
set_from_env() click to toggle source

rubocop:disable Metrics/MethodLength, Metrics/CyclomaticComplexity rubocop:disable Metrics/AbcSize

# File lib/elastic_apm/config.rb, line 357
def set_from_env
  ENV_TO_KEY.each do |env_key, key|
    next unless (value = ENV[env_key])

    type, key = key if key.is_a? Array

    value =
      case type
      when :int then value.to_i
      when :float then value.to_f
      when :bool then !%w[0 false].include?(value.strip.downcase)
      when :list then value.split(/[ ,]/)
      when :dict then Hash[value.split(/[&,]/).map { |kv| kv.split('=') }]
      else value
      end

    send("#{key}=", value)
  end
end
set_rails(app) click to toggle source
# File lib/elastic_apm/config.rb, line 405
def set_rails(app) # rubocop:disable Metrics/AbcSize
  self.service_name ||= format_name(service_name || rails_app_name(app))
  self.framework_name ||= 'Ruby on Rails'
  self.framework_version ||= Rails::VERSION::STRING
  self.logger ||= Rails.logger

  self.root_path = Rails.root.to_s
  self.view_paths = app.config.paths['app/views'].existent
end
set_sinatra(app) click to toggle source
# File lib/elastic_apm/config.rb, line 398
def set_sinatra(app)
  self.service_name = format_name(service_name || app.to_s)
  self.framework_name = framework_name || 'Sinatra'
  self.framework_version = framework_version || Sinatra::VERSION
  self.root_path = Dir.pwd
end