From 35a8cafa35c12d33f9f761bacab189397b34045f Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Fri, 29 Sep 2017 03:16:44 +0200 Subject: [PATCH] Replace self-rolled statsd instrumention with localshred/nsa (#5118) --- Gemfile | 2 +- Gemfile.lock | 9 +++++++-- config/environments/production.rb | 5 ----- config/initializers/statsd.rb | 25 +++++++++++-------------- 4 files changed, 19 insertions(+), 22 deletions(-) diff --git a/Gemfile b/Gemfile index 09b3b8effd7..82ef492fce3 100644 --- a/Gemfile +++ b/Gemfile @@ -42,6 +42,7 @@ gem 'kaminari', '~> 1.0' gem 'link_header', '~> 0.0' gem 'mime-types', '~> 3.1' gem 'nokogiri', '~> 1.7' +gem 'nsa', '~> 0.2' gem 'oj', '~> 3.0' gem 'ostatus2', '~> 2.0' gem 'ox', '~> 2.5' @@ -64,7 +65,6 @@ gem 'sidekiq-bulk', '~>0.1.1' gem 'simple-navigation', '~> 4.0' gem 'simple_form', '~> 3.4' gem 'sprockets-rails', '~> 3.2', require: 'sprockets/railtie' -gem 'statsd-instrument', '~> 2.1' gem 'twitter-text', '~> 1.14' gem 'tzinfo-data', '~> 1.2017' gem 'webpacker', '~> 3.0' diff --git a/Gemfile.lock b/Gemfile.lock index 73419fd28a4..b95e52b37dc 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -289,6 +289,11 @@ GEM mini_portile2 (~> 2.2.0) nokogumbo (1.4.13) nokogiri + nsa (0.2.4) + activesupport (>= 4.2, < 6) + concurrent-ruby (~> 1.0.0) + sidekiq (>= 3.5.0) + statsd-ruby (~> 1.2.0) oj (3.3.5) openssl (2.0.5) orm_adapter (0.5.0) @@ -483,7 +488,7 @@ GEM sshkit (1.14.0) net-scp (>= 1.1.2) net-ssh (>= 2.8.0) - statsd-instrument (2.1.4) + statsd-ruby (1.2.1) strong_migrations (0.1.9) activerecord (>= 3.2.0) temple (0.8.0) @@ -578,6 +583,7 @@ DEPENDENCIES microformats (~> 4.0) mime-types (~> 3.1) nokogiri (~> 1.7) + nsa (~> 0.2) oj (~> 3.0) ostatus2 (~> 2.0) ox (~> 2.5) @@ -617,7 +623,6 @@ DEPENDENCIES simple_form (~> 3.4) simplecov (~> 0.14) sprockets-rails (~> 3.2) - statsd-instrument (~> 2.1) strong_migrations twitter-text (~> 1.14) tzinfo-data (~> 1.2017) diff --git a/config/environments/production.rb b/config/environments/production.rb index 397ea48dac7..5705ffcfe9e 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -90,11 +90,6 @@ Rails.application.configure do config.action_mailer.delivery_method = ENV.fetch('SMTP_DELIVERY_METHOD', 'smtp').to_sym - config.to_prepare do - StatsD.backend = StatsD::Instrument::Backends::NullBackend.new if ENV['STATSD_ADDR'].blank? - Sidekiq::Logging.logger.level = Logger::WARN - end - config.action_dispatch.default_headers = { 'Server' => 'Mastodon', 'X-Frame-Options' => 'DENY', diff --git a/config/initializers/statsd.rb b/config/initializers/statsd.rb index f00b1d4015c..17a17617424 100644 --- a/config/initializers/statsd.rb +++ b/config/initializers/statsd.rb @@ -1,18 +1,15 @@ # frozen_string_literal: true -RESERVED_CHARACTERS_REGEX = /[\:\|\@]/ -StatsD.prefix = 'mastodon' -StatsD.default_sample_rate = 1 +if ENV['STATSD_ADDR'].present? + host, port = ENV['STATSD_ADDR'].split(':') -def clean_name(str) - str.gsub('::', '.').gsub(RESERVED_CHARACTERS_REGEX, '_') -end - -ActiveSupport::Notifications.subscribe(/performance/) do |name, _start, _finish, _id, payload| - action = payload[:action] || :increment - measurement = payload[:measurement] - value = payload[:value] - key_name = clean_name("#{name}.#{measurement}") - - StatsD.send(action.to_s, key_name, (value || 1)) + statsd = ::Statsd.new(host, port) + statsd.namespace = ['Mastodon', Rails.env].join('.') + + ::NSA.inform_statsd(statsd) do |informant| + informant.collect(:action_controller, :web) + informant.collect(:active_record, :db) + informant.collect(:cache, :cache) + informant.collect(:sidekiq, :sidekiq) + end end