Improve StatsD instrumentation

This commit is contained in:
Eugen Rochko 2017-01-26 19:08:05 +01:00
parent 7329fbd8a4
commit d567f21d4f
4 changed files with 24 additions and 27 deletions

View File

@ -3,7 +3,6 @@ require_relative 'boot'
require 'rails/all' require 'rails/all'
require_relative '../app/lib/exceptions' require_relative '../app/lib/exceptions'
require_relative '../lib/statsd_monitor'
# Require the gems listed in Gemfile, including any gems # Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production. # you've limited to :test, :development, or :production.
@ -31,8 +30,6 @@ module Mastodon
config.active_job.queue_adapter = :sidekiq config.active_job.queue_adapter = :sidekiq
config.middleware.insert(0, ::StatsDMonitor)
config.middleware.insert_before 0, Rack::Cors do config.middleware.insert_before 0, Rack::Cors do
allow do allow do
origins '*' origins '*'

View File

@ -0,0 +1,18 @@
# frozen_string_literal: true
instrumentation_hostname = ENV.fetch('INSTRUMENTATION_HOSTNAME') { 'localhost' }
ActiveSupport::Notifications.subscribe(/process_action.action_controller/) do |*args|
event = ActiveSupport::Notifications::Event.new(*args)
controller = event.payload[:controller]
action = event.payload[:action]
format = event.payload[:format] || 'all'
format = 'all' if format == '*/*'
status = event.payload[:status]
key = "#{controller}.#{action}.#{format}.#{instrumentation_hostname}"
ActiveSupport::Notifications.instrument :performance, action: :measure, measurement: "#{key}.total_duration", value: event.duration
ActiveSupport::Notifications.instrument :performance, action: :measure, measurement: "#{key}.db_time", value: event.payload[:db_runtime]
ActiveSupport::Notifications.instrument :performance, action: :measure, measurement: "#{key}.view_time", value: event.payload[:view_runtime]
ActiveSupport::Notifications.instrument :performance, measurement: "#{key}.status.#{status}"
end

View File

@ -3,18 +3,11 @@
StatsD.prefix = 'mastodon' StatsD.prefix = 'mastodon'
StatsD.default_sample_rate = 1 StatsD.default_sample_rate = 1
StatsDMonitor.extend(StatsD::Instrument) ActiveSupport::Notifications.subscribe(/performance/) do |name, _start, _finish, _id, payload|
StatsDMonitor.statsd_measure(:call, 'request.duration') action = payload[:action] || :increment
measurement = payload[:measurement]
value = payload[:value]
key_name = "#{name}.#{measurement}"
STATSD_REQUEST_METRICS = { StatsD.send(action.to_s, key_name, (value || 1))
'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 end

View File

@ -1,11 +0,0 @@
# frozen_string_literal: true
class StatsDMonitor
def initialize(app)
@app = app
end
def call(env)
@app.call(env)
end
end