Class: DynoscaleRuby::Middleware

Inherits:
Object
  • Object
show all
Includes:
Logger
Defined in:
lib/dynoscale_ruby/middleware.rb

Constant Summary collapse

MEASUREMENT_TTL =

minutes

5

Instance Method Summary collapse

Methods included from Logger

logger

Constructor Details

#initialize(app) ⇒ Middleware

Returns a new instance of Middleware.



17
18
19
# File 'lib/dynoscale_ruby/middleware.rb', line 17

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/dynoscale_ruby/middleware.rb', line 21

def call(env)
  return @app.call(env) if ENV['SKIP_DYNOSCALE_AGENT']

  is_dev = ENV['DYNOSCALE_DEV'] == 'true'
  dyno = is_dev ? "dev.1" : ENV['DYNO']

  unless ENV['DYNOSCALE_URL']
    puts "Missing DYNOSCALE_URL environment variable"
    return @app.call(env)
  end
  return @app.call(env) unless is_dev || ENV['DYNO']&.split(".")&.last == "1"

  request_calculator = RequestCalculator.new(env)
  workers =  DynoscaleRuby::Worker.constants.select{|c| DynoscaleRuby::Worker.const_get(c).is_a? Class }.map{|c| DynoscaleRuby::Worker.const_get(c) }
  Recorder.record!(request_calculator, workers)

  api_wrapper = DynoscaleRuby::ApiWrapper.new(dyno, ENV['DYNOSCALE_URL'], ENV['HEROKU_APP_NAME'])
  Reporter.start!(Recorder, api_wrapper) unless Reporter.running?


  @app.call(env)
end