Sha256: 273754b88d20ecde12c2c52f5e6d1ca0e373be194ced9a218bcb2eae7d5b9157

Contents?: true

Size: 1.41 KB

Versions: 11

Compression:

Stored size: 1.41 KB

Contents

class LHC::Monitoring < LHC::Interceptor

  # Options forwarded to the monitoring
  FORWARDED_OPTIONS = {
    monitoring_key: :key
  }

  include ActiveSupport::Configurable

  config_accessor :statsd, :env

  def before_request
    return unless statsd
    LHC::Monitoring.statsd.count("#{key(request)}.before_request", 1)
  end

  def after_request
    return unless statsd
    LHC::Monitoring.statsd.count("#{key(request)}.count", 1)
    LHC::Monitoring.statsd.count("#{key(request)}.after_request", 1)
  end

  def after_response
    return unless statsd
    key = key(response)
    LHC::Monitoring.statsd.timing("#{key}.time", response.time) if response.success?
    key += response.timeout? ? '.timeout' : ".#{response.code}"
    LHC::Monitoring.statsd.count(key, 1)
  end

  private

  def key(target)
    request = target.is_a?(LHC::Request) ? target : target.request
    key = options(request.options)[:key]
    return key if key.present?

    url = sanitize_url(request.url)
    key = [
      'lhc',
      Rails.application.class.parent_name.underscore,
      LHC::Monitoring.env || Rails.env,
      URI.parse(url).host.gsub(/\./, '_'),
      request.method
    ]
    key.join('.')
  end

  def sanitize_url(url)
    return url if url.match(%r{https?://})
    "http://#{url}"
  end

  def options(input = {})
    options = {}
    FORWARDED_OPTIONS.each do |k, v|
      options[v] = input[k] if input.key?(k)
    end
    options
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
lhc-10.1.2 lib/lhc/interceptors/monitoring.rb
lhc-10.1.1 lib/lhc/interceptors/monitoring.rb
lhc-10.1.0 lib/lhc/interceptors/monitoring.rb
lhc-10.0.2 lib/lhc/interceptors/monitoring.rb
lhc-9.4.4 lib/lhc/interceptors/monitoring.rb
lhc-10.0.1 lib/lhc/interceptors/monitoring.rb
lhc-10.0.0 lib/lhc/interceptors/monitoring.rb
lhc-9.4.3 lib/lhc/interceptors/monitoring.rb
lhc-9.4.2 lib/lhc/interceptors/monitoring.rb
lhc-9.4.1 lib/lhc/interceptors/monitoring.rb
lhc-9.4.0 lib/lhc/interceptors/monitoring.rb