# frozen_string_literal: true require "net/http" require "time" module Neetodeploy class SidekiqMiddleware def call(worker, job, queue) url = URI.parse("https://nd-queue-time-exporter.neetodeployapp.com/metrics") queues_by_name = ::Sidekiq::Queue.all.each_with_object({}) do |queue_name, obj| obj[queue_name.name] = queue_name end queues = queues_by_name.keys queues.each do |queue_obj| queue = queues_by_name.fetch(queue_obj) { |name| ::Sidekiq::Queue.new(name) } query_params = URI.encode_www_form(app_name: ENV.to_h["NEETODEPLOY_APP_NAME"], process_type: "worker", queue_obj: queue_obj, queue_time: queue_time(queue)) 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 Sidekiq process: #{e.message}" end end yield end private def queues ::Sidekiq::Queue.all end def queue_time(queue_obj) queue = ::Sidekiq::Queue.new(queue_obj.name) (queue.latency * 1000).ceil end end end