Skip to content

Instantly share code, notes, and snippets.

@brenogazzola
Last active November 11, 2021 03:51
Show Gist options
  • Save brenogazzola/c4939c8c9fe700ffe0389a5dac427448 to your computer and use it in GitHub Desktop.
Save brenogazzola/c4939c8c9fe700ffe0389a5dac427448 to your computer and use it in GitHub Desktop.
Datadog
# frozen_string_literal: true
require "datadog/statsd"
class QueueTime
def initialize(app)
@app = app
end
def call(env)
start = env["HTTP_X_REQUEST_START"].to_f * 1000
wait = env["puma.request_body_wait"]
current = Time.now.to_f * 1000
DogStatsD.statsd.histogram("puma.queue_time", (current - wait - start).to_i, tags: ["environment:production"]) unless start == 0
@status, @headers, @response = @app.call(env)
[@status, @headers, self]
end
def each(&block)
@response.each(&block)
end
end
class DogStatsD
def self.statsd
@statsd ||= ::Datadog::Statsd.new("localhost", 8125)
end
def self.forked
@statsd = nil
end
def self.close
@statsd&.flush
@statsd&.close
@statsd = nil
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment