Sha256: 3da5392ca1ac4994c0a5f4a992fc8c700c5889ee2c5fab24e8e80bc1f7f4f169

Contents?: true

Size: 1.23 KB

Versions: 2

Compression:

Stored size: 1.23 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"], process_type: "web", queue_time: queue_time(env))

      url.query = query_params

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

      begin
        Net::HTTP.start(url.host, url.port, use_ssl: true) do |http|
          http.request(post)
        end
      rescue => e
        puts "Exception in sending post request to exporter from Rails process: #{e.message}"
      end

      @app.call(env)
    end

    private

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

      env["HTTP_X_REQUEST_START"].to_i
    end

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

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

      queue_time = (now - started_at(env)).round
      queue_time -= network_time(env)

      queue_time.positive? ? queue_time : 0
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
neetodeploy-autoscale-1.0.2 lib/neetodeploy/autoscale/middleware.rb
neetodeploy-autoscale-1.0.1 lib/neetodeploy/autoscale/middleware.rb