Sha256: b094e383571fb5cbce8bfe495418fa7a5ca0545e2fa4efb1169bf84ace36c71b

Contents?: true

Size: 1.49 KB

Versions: 3

Compression:

Stored size: 1.49 KB

Contents

module Heroku::Scalr::Metric

  # @param [Symbol] type the metric type
  # @param [Heroku::Scalr::App] app the application
  # @return [Heroku::Scalr::Metric::Abstract] a metric instance
  def self.new(type, app)
    case type
    when :wait, "wait"
      Wait.new(app)
    else
      Ping.new(app)
    end
  end

  class Abstract

    # @param [Heroku::Scalr::App] app the application
    def initialize(app)
      @app = app
    end

    # @return [Integer] number of dynos to adjust by
    def by
      0
    end

    protected

      def compare(ms, low, high)
        ms <= low ? -1 : (ms >= high ? 1 : 0)
      end

      def log(*args)
        @app.log(*args)
      end

  end

  class Ping < Abstract

    # @see Heroku::Scalr::Metric::Abstract#by
    def by
      status = nil

      real = Benchmark.realtime do
        status = @app.http.get.status
      end

      unless status == 200
        log :warn, "unable to ping, server responded with #{status}"
        return 0
      end

      ms = (real * 1000).floor
      log :debug, "current ping time: #{ms}ms"

      compare(ms, @app.ping_low, @app.ping_high)
    end

  end

  class Wait < Abstract

    # @see Heroku::Scalr::Metric::Abstract#by
    def by
      ms = @app.http.get.headers["X-Heroku-Queue-Wait"]
      unless ms
        log :warn, "unable to determine queue wait time"
        return 0
      end

      ms = ms.to_i
      log :debug, "current queue wait time: #{ms}ms"

      compare(ms, @app.wait_low, @app.wait_high)
    end

  end


end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
heroku-scalr-0.2.2 lib/heroku/scalr/metric.rb
heroku-scalr-0.2.1 lib/heroku/scalr/metric.rb
heroku-scalr-0.2.0 lib/heroku/scalr/metric.rb