Sha256: b1fe416849a80f98db7af8dd2069c7ab03b1f2613a2357bc2e8f08b775dc963b

Contents?: true

Size: 1.23 KB

Versions: 2

Compression:

Stored size: 1.23 KB

Contents

class LHC::Monitoring < LHC::Interceptor

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

  include ActiveSupport::Configurable

  config_accessor :statsd

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

  def after_response(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,
      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

2 entries across 2 versions & 1 rubygems

Version Path
lhc-core-interceptors-2.1.1 lib/lhc-core-interceptors/monitoring.rb
lhc-core-interceptors-2.1.0 lib/lhc-core-interceptors/monitoring.rb