Last active
March 29, 2019 18:52
Revisions
-
pseudomuto renamed this gist
Sep 4, 2015 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
pseudomuto revised this gist
Sep 4, 2015 . 2 changed files with 17 additions and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,10 @@ # app/middleware/statsd_monitor.rb class Middleware::StatsDMonitor def initialize(app) @app = app end def call(env) @app.call(env) end end 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 charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ # app/middleware/statsd_monitor.rb class Middleware::StatsDMonitor def initialize(app) @app = app @@ -9,6 +9,12 @@ def call(env) end end # config/initializers/inflections.rb ActiveSupport::Inflector.inflections(:en) do |inflect| # allows Middleware::StatsDMonitor to be loaded from middleware/statsd_monitor.rb inflect.acronym "StatsD" end # config/initializers/statsd.rb STATSD_REQUEST_METRICS = { "request.success" => 200, -
pseudomuto revised this gist
Sep 4, 2015 . 7 changed files with 94 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,8 @@ # config/application.rb module DemoApp class Application < Rails::Application ... ... config.middleware.insert(0, "Middleware::StatsDMonitor") end end 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,5 @@ # config/initializers/inflections.rb ActiveSupport::Inflector.inflections(:en) do |inflect| # allows Middleware::StatsDMonitor to be loaded from middleware/statsd_monitor.rb inflect.acronym "StatsD" end 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,3 @@ # config/initializers/statsd.rb StatsD.prefix = "DemoApp" StatsD.default_sample_rate = 1 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,8 @@ # config/initializers/statsd.rb ... ... # send the environment as a tag STATSD_TAGS = ["env:#{Rails.env}"].freeze Middleware::StatsDMonitor.extend(StatsD::Instrument) Middleware::StatsDMonitor.statsd_measure(:call, "request.duration", tags: STATSD_TAGS) 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,16 @@ # config/initializers/statsd.rb STATSD_REQUEST_METRICS = { "request.success" => 200, "request.redirect" => 302, "request.bad_request" => 400, "request.not_found" => 404, "request.too_many_requests" => 429, "request.internal_server_error" => 500, "request.bad_gateway" => 502 }.freeze STATSD_REQUEST_METRICS.each do |name, code| Middleware::StatsDMonitor.statsd_count_if(:call, name, tags: STATSD_TAGS) do |status, _env, _body| status.to_i == code end end 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,42 @@ # test/integration/statsd_request_monitoring_test.rb class StatsDRequestMonitoringTest < ActionDispatch::IntegrationTest ... ... test "successful requests are counted" do assert_increment("request.success", 200) end test "redirects are counted" do assert_increment("request.redirect", 302) end test "bad requests are counted" do assert_increment("request.bad_request", 400) end test "not found requests are counted" do assert_increment("request.not_found", 404) end test "dropped requests are counted" do assert_increment("request.too_many_requests", 429) end test "internal server errors are counted" do assert_increment("request.internal_server_error", 500) end test "bad gateway requests are counted" do assert_increment("request.bad_gateway", 502) end private def assert_increment(name, status) assert_statsd_increment("#{StatsD.prefix}.#{name}", tags: STATSD_TAGS) do # skip the processing and just return the supplied status Rack::Runtime.any_instance.stubs(call: [status, {}, ""]) get "/" end end end 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,12 @@ # test/integration/statsd_request_monitoring_test.rb require "test_helper" class StatsDRequestMonitoringTest < ActionDispatch::IntegrationTest include StatsD::Instrument::Assertions test "request duration is measured" do assert_statsd_measure("#{StatsD.prefix}.request.duration", tags: STATSD_TAGS) do get "/" end end end -
pseudomuto revised this gist
Sep 4, 2015 . 3 changed files with 36 additions and 34 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,10 +0,0 @@ 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 charactersOriginal file line number Diff line number Diff line change @@ -1,24 +0,0 @@ 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 charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,39 @@ # app/middleware/statsd_monitor class Middleware::StatsDMonitor def initialize(app) @app = app end def call(env) @app.call(env) end end # config/initializers/statsd.rb STATSD_REQUEST_METRICS = { "request.success" => 200, "request.redirect" => 302, "request.bad_request" => 400, "request.not_found" => 404, "request.too_many_requests" => 429, "request.internal_server_error" => 500, "request.bad_gateway" => 502 }.freeze STATSD_TAGS = ["env:#{Rails.env}"].freeze StatsD.prefix = "DemoApp" StatsD.default_sample_rate = 1 Middleware::StatsDMonitor.extend(StatsD::Instrument) Middleware::StatsDMonitor.statsd_measure(:call, "request.duration", tags: STATSD_TAGS) STATSD_REQUEST_METRICS.each do |name, code| Middleware::StatsDMonitor.statsd_count_if(:call, name, tags: STATSD_TAGS) do |status, _env, _body| status.to_i == code end end # test/integration/statsd_request_monitoring_test.rb require "test_helper" -
pseudomuto revised this gist
Sep 4, 2015 . 1 changed file with 10 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,10 @@ # app/middleware/statsd_monitor class Middleware::StatsDMonitor def initialize(app) @app = app end def call(env) @app.call(env) end end -
pseudomuto revised this gist
Sep 4, 2015 . 1 changed file with 4 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,4 @@ source "https://rubygems.org/" ... ... gem "statsd-instrument" -
pseudomuto revised this gist
Sep 4, 2015 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,4 @@ # test/integration/statsd_request_monitoring_test.rb require "test_helper" class StatsDRequestMonitoringTest < ActionDispatch::IntegrationTest -
pseudomuto revised this gist
Sep 4, 2015 . 1 changed file with 48 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,48 @@ require "test_helper" class StatsDRequestMonitoringTest < ActionDispatch::IntegrationTest include StatsD::Instrument::Assertions test "request duration is measured" do assert_statsd_measure("#{StatsD.prefix}.request.duration", tags: STATSD_TAGS) do get "/" end end test "successful requests are counted" do assert_increment("request.success", 200) end test "redirects are counted" do assert_increment("request.redirect", 302) end test "bad requests are counted" do assert_increment("request.bad_request", 400) end test "not found requests are counted" do assert_increment("request.not_found", 404) end test "dropped requests are counted" do assert_increment("request.too_many_requests", 429) end test "internal server errors are counted" do assert_increment("request.internal_server_error", 500) end test "bad gateway requests are counted" do assert_increment("request.bad_gateway", 502) end private def assert_increment(name, status) assert_statsd_increment("#{StatsD.prefix}.#{name}", tags: STATSD_TAGS) do Rack::Runtime.any_instance.stubs(call: [status, {}, ""]) get "/" end end end -
pseudomuto revised this gist
Sep 4, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -18,7 +18,7 @@ Middleware::StatsDMonitor.statsd_measure(:call, "request.duration", tags: STATSD_TAGS) STATSD_REQUEST_METRICS.each do |name, code| Middleware::StatsDMonitor.statsd_count_if(:call, name, tags: STATSD_TAGS) do |status, _env, _body| status.to_i == code end end -
pseudomuto created this gist
Sep 4, 2015 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,24 @@ # config/initializers/statsd.rb STATSD_REQUEST_METRICS = { "request.success" => 200, "request.redirect" => 302, "request.bad_request" => 400, "request.not_found" => 404, "request.too_many_requests" => 429, "request.internal_server_error" => 500, "request.bad_gateway" => 502 }.freeze STATSD_TAGS = ["env:#{Rails.env}"].freeze StatsD.prefix = "DemoApp" StatsD.default_sample_rate = 1 Middleware::StatsDMonitor.extend(StatsD::Instrument) Middleware::StatsDMonitor.statsd_measure(:call, "request.duration", tags: STATSD_TAGS) STATSD_REQUEST_METRICS.each do |name, code| Middleware::StatsDMonitor.statsd_count_if(:call, name, tags: DATADOG_TAGS) do |status, _env, _body| status.to_i == code end end