Sha256: 5e779f2ab30b074047fd681acda58b2af0d9e085137d52010e6d7fa59837ed87

Contents?: true

Size: 1.46 KB

Versions: 31

Compression:

Stored size: 1.46 KB

Contents

# frozen_string_literal: true

class LHC::Prometheus < LHC::Interceptor
  include ActiveSupport::Configurable

  config_accessor :client, :namespace

  REQUEST_COUNTER_KEY = :lhc_requests
  REQUEST_HISTOGRAM_KEY = :lhc_request_seconds

  class << self
    attr_accessor :registered
  end

  def initialize(request)
    super(request)
    return if LHC::Prometheus.registered || LHC::Prometheus.client.blank?

    begin
      LHC::Prometheus.client.registry.counter(LHC::Prometheus::REQUEST_COUNTER_KEY, 'Counter of all LHC requests.')
      LHC::Prometheus.client.registry.histogram(LHC::Prometheus::REQUEST_HISTOGRAM_KEY, 'Request timings for all LHC requests in seconds.')
    rescue Prometheus::Client::Registry::AlreadyRegisteredError => e
      Rails.logger.error(e) if defined?(Rails)
    ensure
      LHC::Prometheus.registered = true
    end
  end

  def after_response
    return if !LHC::Prometheus.registered || LHC::Prometheus.client.blank?

    host = URI.parse(request.url).host

    LHC::Prometheus.client.registry
      .get(LHC::Prometheus::REQUEST_COUNTER_KEY)
      .increment(
        code: response.code,
        success: response.success?,
        timeout: response.timeout?,
        host: host,
        app: LHC::Prometheus.namespace
      )

    LHC::Prometheus.client.registry
      .get(LHC::Prometheus::REQUEST_HISTOGRAM_KEY)
      .observe({
                 host: host,
                 app: LHC::Prometheus.namespace
               }, response.time)
  end
end

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
lhc-15.2.1 lib/lhc/interceptors/prometheus.rb
lhc-15.2.0 lib/lhc/interceptors/prometheus.rb
lhc-15.1.3 lib/lhc/interceptors/prometheus.rb
lhc-15.1.2 lib/lhc/interceptors/prometheus.rb
lhc-15.1.1 lib/lhc/interceptors/prometheus.rb
lhc-15.1.0 lib/lhc/interceptors/prometheus.rb
lhc-16.0.0.pre.pro2162.2 lib/lhc/interceptors/prometheus.rb
lhc-16.0.0.pre.pro2162 lib/lhc/interceptors/prometheus.rb
lhc-15.0.1 lib/lhc/interceptors/prometheus.rb
lhc-15.0.0 lib/lhc/interceptors/prometheus.rb
lhc-14.0.0 lib/lhc/interceptors/prometheus.rb
lhc-13.4.0.pre.pro1766.1 lib/lhc/interceptors/prometheus.rb
lhc-13.2.0 lib/lhc/interceptors/prometheus.rb
lhc-13.1.0 lib/lhc/interceptors/prometheus.rb
lhc-13.0.0 lib/lhc/interceptors/prometheus.rb
lhc-12.3.0 lib/lhc/interceptors/prometheus.rb
lhc-12.2.1 lib/lhc/interceptors/prometheus.rb
lhc-12.2.0 lib/lhc/interceptors/prometheus.rb
lhc-12.1.3 lib/lhc/interceptors/prometheus.rb
lhc-12.1.2 lib/lhc/interceptors/prometheus.rb