Class: Amigo::Autoscaler::Checkers::WebLatency::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/amigo/autoscaler/checkers/web_latency.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, redis:, threshold: 0.08, namespace: NAMESPACE) ⇒ Middleware

Returns a new instance of Middleware.

Parameters:

  • threshold (Float) (defaults to: 0.08)

    Do not record the latency of requests faster than this. These are usually just things like healthchecks, files, or other very fast requests which do not represent the overall system slowness.



69
70
71
72
73
74
# File 'lib/amigo/autoscaler/checkers/web_latency.rb', line 69

def initialize(app, redis:, threshold: 0.08, namespace: NAMESPACE)
  @app = app
  @redis = redis
  @threshold = threshold
  @namespace = namespace
end

Instance Method Details

#call(env) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/amigo/autoscaler/checkers/web_latency.rb', line 76

def call(env)
  start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  status, headers, body = @app.call(env)
  duration = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start
  if duration > @threshold
    begin
      WebLatency.set_latency(
        redis: @redis,
        namespace: @namespace,
        at: Time.now,
        duration:,
      )
    rescue StandardError => e
      Amigo.log(nil, :error, "web_latency_error", exception: e)
    end
  end
  [status, headers, body]
end