Sha256: 835d65e345890a04b8027cfac545c78c63bbd7c11fe98887574416f5046e8af2

Contents?: true

Size: 1.23 KB

Versions: 1

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

1 entries across 1 versions & 1 rubygems

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