From 306eb6e9c90295dcdff2a0094066542a46a8e634 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Wed, 18 Jan 2017 23:44:29 +0100 Subject: [PATCH] Add optional StatsD performance tracking --- Gemfile | 1 + Gemfile.lock | 2 ++ app/lib/statsd_monitor.rb | 11 +++++++++++ config/application.rb | 2 ++ config/environments/production.rb | 4 ++++ config/initializers/inflections.rb | 7 +++---- config/initializers/statsd.rb | 20 ++++++++++++++++++++ 7 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 app/lib/statsd_monitor.rb create mode 100644 config/initializers/statsd.rb diff --git a/Gemfile b/Gemfile index 6b85193690..1341e45de8 100644 --- a/Gemfile +++ b/Gemfile @@ -47,6 +47,7 @@ gem 'sidekiq' gem 'rails-settings-cached' gem 'pg_search' gem 'simple-navigation' +gem 'statsd-instrument' gem 'react-rails' gem 'browserify-rails' diff --git a/Gemfile.lock b/Gemfile.lock index 2c009955eb..7214d21eb6 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -370,6 +370,7 @@ GEM actionpack (>= 4.0) activesupport (>= 4.0) sprockets (>= 3.0.0) + statsd-instrument (2.1.2) temple (0.7.7) term-ansicolor (1.4.0) tins (~> 1.0) @@ -463,6 +464,7 @@ DEPENDENCIES simple-navigation simple_form simplecov + statsd-instrument uglifier (>= 1.3.0) webmock will_paginate diff --git a/app/lib/statsd_monitor.rb b/app/lib/statsd_monitor.rb new file mode 100644 index 0000000000..e48ce65410 --- /dev/null +++ b/app/lib/statsd_monitor.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +class StatsDMonitor + def initialize(app) + @app = app + end + + def call(env) + @app.call(env) + end +end diff --git a/config/application.rb b/config/application.rb index e561d04738..e97fb165ba 100644 --- a/config/application.rb +++ b/config/application.rb @@ -30,6 +30,8 @@ module Mastodon config.active_job.queue_adapter = :sidekiq + config.middleware.insert(0, 'StatsDMonitor') + config.middleware.insert_before 0, Rack::Cors do allow do origins '*' diff --git a/config/environments/production.rb b/config/environments/production.rb index 8b8d974b31..30170e8105 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -104,4 +104,8 @@ Rails.application.configure do config.react.variant = :production config.active_record.logger = nil + + config.to_prepare do + StatsD.backend = StatsD::Instrument::Backends::NullBackend if ENV['STATSD_ADDR'].blank? + end end diff --git a/config/initializers/inflections.rb b/config/initializers/inflections.rb index ac033bf9dc..8fd1ae72c8 100644 --- a/config/initializers/inflections.rb +++ b/config/initializers/inflections.rb @@ -10,7 +10,6 @@ # inflect.uncountable %w( fish sheep ) # end -# These inflection rules are supported but not enabled by default: -# ActiveSupport::Inflector.inflections(:en) do |inflect| -# inflect.acronym 'RESTful' -# end +ActiveSupport::Inflector.inflections(:en) do |inflect| + inflect.acronym 'StatsD' +end diff --git a/config/initializers/statsd.rb b/config/initializers/statsd.rb new file mode 100644 index 0000000000..c9c754e7ff --- /dev/null +++ b/config/initializers/statsd.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +StatsD.prefix = 'mastodon' +StatsD.default_sample_rate = 1 + +StatsDMonitor.extend(StatsD::Instrument) +StatsDMonitor.statsd_measure(:call, 'request.duration') + +STATSD_REQUEST_METRICS = { + 'request.status.success' => 200, + 'request.status.not_found' => 404, + 'request.status.too_many_requests' => 429, + 'request.status.internal_server_error' => 500, +}.freeze + +STATSD_REQUEST_METRICS.each do |name, code| + StatsDMonitor.statsd_count_if(:call, name) do |status, _env, _body| + status.to_i == code + end +end