Sha256: a9bde0c62af41f5e9363b9113d1c8bfe9bfb538f2a9f3c68b2f3fda917fcff1c

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.at(env["HTTP_X_REQUEST_START"] / 1000)
    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.3.0 lib/neetodeploy/autoscale/middleware.rb