Module: PrometheusAspect

Included in:
ServerBase
Defined in:
lib/macaw_framework/aspects/prometheus_aspect.rb

Overview

Aspect that provides application metrics using prometheus.

Instance Method Summary collapse

Instance Method Details

#call_endpoint(prometheus_middleware, *args, **kwargs) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/macaw_framework/aspects/prometheus_aspect.rb', line 6

def call_endpoint(prometheus_middleware, *args, **kwargs)
  return super(*args, **kwargs) if prometheus_middleware.nil?

  start_time = Time.now

  begin
    response = super(*args)
  ensure
    duration = (Time.now - start_time) * 1_000

    endpoint_name = args[2].split('.').join('/')

    prometheus_middleware.request_duration_milliseconds.with_labels(endpoint: endpoint_name).observe(duration)
    prometheus_middleware.request_count.with_labels(endpoint: endpoint_name).increment
    if response
      prometheus_middleware.response_count.with_labels(endpoint: endpoint_name,
                                                       status: response[1]).increment
    end
  end

  response
end