Sha256: a3e2570529ab9467413fe999f9de0490e9a5b394f477f962c96324cda9155f6a

Contents?: true

Size: 1.08 KB

Versions: 1

Compression:

Stored size: 1.08 KB

Contents

# frozen_string_literal: true

require "net/http"
require "time"

module Neetodeploy
  class Middleware
    def initialize(app)
      @app = app
    end

    def call(env)
      url = URI.parse("https://nd-queue-time-exporter.neetodeployapp.com/metrics")

      query_params = URI.encode_www_form(app_name: ENV.to_h["NEETODEPLOY_APP_NAME"], queue_time: queue_time(env))

      url.query = query_params

      post = Net::HTTP::Post.new(url)
      post["AuthToken"] = "K0An3O3MSyEEMTCnRd1IHgGjdGQkzy"

      Net::HTTP.start(url.host, url.port, use_ssl: true) do |http|
        http.request(post)
      end

      @app.call(env)
    end

    private

    def started_at(env)
      return nil unless env["HTTP_X_REQUEST_START"].present?

      Time.parse(env["HTTP_X_REQUEST_START"])
    end

    def network_time(env)
      env["puma.request_body_wait"].to_i
    end

    def queue_time(now = Time.now, env)
      return if started_at(env).nil?

      queue_time = ((now - started_at(env)) * 1000).to_i
      queue_time -= network_time(env)

      queue_time.positive? ? queue_time : 0
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
neetodeploy-autoscale-0.1.0 lib/neetodeploy/autoscale/middleware.rb