about summary refs log tree commit diff
path: root/config
diff options
context:
space:
mode:
authorEugen Rochko <eugen@zeonfederated.com>2017-01-18 23:44:29 +0100
committerEugen Rochko <eugen@zeonfederated.com>2017-01-18 23:44:29 +0100
commit306eb6e9c90295dcdff2a0094066542a46a8e634 (patch)
tree10851d9b6eb2d81f5249769dc35bd2c1fb7d61d1 /config
parent7cfd5b680a64e2dcdc375274d9619e8ab2e2bb9f (diff)
Add optional StatsD performance tracking
Diffstat (limited to 'config')
-rw-r--r--config/application.rb2
-rw-r--r--config/environments/production.rb4
-rw-r--r--config/initializers/inflections.rb7
-rw-r--r--config/initializers/statsd.rb20
4 files changed, 29 insertions, 4 deletions
diff --git a/config/application.rb b/config/application.rb
index e561d0473..e97fb165b 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 8b8d974b3..30170e810 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 ac033bf9d..8fd1ae72c 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 000000000..c9c754e7f
--- /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