Skip to content

Instantly share code, notes, and snippets.

@pseudomuto
Last active March 29, 2019 18:52

Revisions

  1. pseudomuto renamed this gist Sep 4, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. pseudomuto revised this gist Sep 4, 2015. 2 changed files with 17 additions and 1 deletion.
    10 changes: 10 additions & 0 deletions app_middleware_statsd_monitor.rb
    Original 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
    8 changes: 7 additions & 1 deletion final.rb
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    # app/middleware/statsd_monitor
    # 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,
  3. pseudomuto revised this gist Sep 4, 2015. 7 changed files with 94 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions application.rb
    Original 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
    5 changes: 5 additions & 0 deletions inflections.rb
    Original 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
    3 changes: 3 additions & 0 deletions statds_initializer_01.rb
    Original 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
    8 changes: 8 additions & 0 deletions statsd_initializer_02.rb
    Original 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)
    16 changes: 16 additions & 0 deletions statsd_initializer_03.rb
    Original 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
    42 changes: 42 additions & 0 deletions test_counters.rb
    Original 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
    12 changes: 12 additions & 0 deletions test_duration.rb
    Original 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
  4. pseudomuto revised this gist Sep 4, 2015. 3 changed files with 36 additions and 34 deletions.
    10 changes: 0 additions & 10 deletions app_middleware_statsd_monitor.rb
    Original file line number Diff line number Diff line change
    @@ -1,10 +0,0 @@
    # app/middleware/statsd_monitor
    class Middleware::StatsDMonitor
    def initialize(app)
    @app = app
    end

    def call(env)
    @app.call(env)
    end
    end
    24 changes: 0 additions & 24 deletions config_initializers_statsd_final.rb
    Original file line number Diff line number Diff line change
    @@ -1,24 +0,0 @@
    # 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
    36 changes: 36 additions & 0 deletions ...n_statsd_request_monitoring_test_final.rb → final.rb
    Original 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"

  5. pseudomuto revised this gist Sep 4, 2015. 1 changed file with 10 additions and 0 deletions.
    10 changes: 10 additions & 0 deletions app_middleware_statsd_monitor.rb
    Original 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
  6. pseudomuto revised this gist Sep 4, 2015. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions Gemfile
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    source "https://rubygems.org/"
    ...
    ...
    gem "statsd-instrument"
  7. pseudomuto revised this gist Sep 4, 2015. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions test_integration_statsd_request_monitoring_test_final.rb
    Original 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
  8. pseudomuto revised this gist Sep 4, 2015. 1 changed file with 48 additions and 0 deletions.
    48 changes: 48 additions & 0 deletions test_integration_statsd_request_monitoring_test_final.rb
    Original 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
  9. pseudomuto revised this gist Sep 4, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion config_initializers_statsd_final.rb
    Original 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: DATADOG_TAGS) do |status, _env, _body|
    Middleware::StatsDMonitor.statsd_count_if(:call, name, tags: STATSD_TAGS) do |status, _env, _body|
    status.to_i == code
    end
    end
  10. pseudomuto created this gist Sep 4, 2015.
    24 changes: 24 additions & 0 deletions config_initializers_statsd_final.rb
    Original 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