Last active
November 11, 2021 03:51
-
-
Save brenogazzola/c4939c8c9fe700ffe0389a5dac427448 to your computer and use it in GitHub Desktop.
Datadog
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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