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


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

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


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

def registered
  @registered
end

Class Method Details

.request_keyObject


12
13
14
# File 'lib/lhc/interceptors/prometheus.rb', line 12

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

.times_keyObject


16
17
18
# File 'lib/lhc/interceptors/prometheus.rb', line 16

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

Instance Method Details

#after_responseObject


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

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