Class: LHC::Prometheus

Inherits:
Interceptor show all
Includes:
ActiveSupport::Configurable
Defined in:
lib/lhc/interceptors/prometheus.rb

Class Attribute Summary collapse

Attributes inherited from Interceptor

#request

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Interceptor

#after_request, #before_raw_request, #before_request, #before_response, dup, #response

Constructor Details

#initialize(request) ⇒ Prometheus

Returns a new instance of Prometheus.



18
19
20
21
22
23
24
# File 'lib/lhc/interceptors/prometheus.rb', line 18

def initialize(request)
  super(request)
  return if LHC::Prometheus.registered || LHC::Prometheus.client.blank?
  LHC::Prometheus.client.registry.counter(LHC::Prometheus.request_key, 'Counter of all LHC requests.')
  LHC::Prometheus.client.registry.histogram(LHC::Prometheus.times_key, 'Times for all LHC requests.')
  LHC::Prometheus.registered = true
end

Class Attribute Details

.registeredObject

Returns the value of attribute registered.



7
8
9
# File 'lib/lhc/interceptors/prometheus.rb', line 7

def registered
  @registered
end

Class Method Details

.request_keyObject



10
11
12
# File 'lib/lhc/interceptors/prometheus.rb', line 10

def self.request_key
  [LHC::Prometheus.namespace, 'lhc_requests'].join('_').to_sym
end

.times_keyObject



14
15
16
# File 'lib/lhc/interceptors/prometheus.rb', line 14

def self.times_key
  [LHC::Prometheus.namespace, 'lhc_times'].join('_').to_sym
end

Instance Method Details

#after_responseObject



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/lhc/interceptors/prometheus.rb', line 26

def after_response
  return if !LHC::Prometheus.registered || LHC::Prometheus.client.blank?
  LHC::Prometheus.client.registry
    .get(LHC::Prometheus.request_key)
    .increment(
      code: response.code,
      success: response.success?,
      timeout: response.timeout?
    )
  LHC::Prometheus.client.registry
    .get(LHC::Prometheus.times_key)
    .observe({}, response.time_ms)
end