Skip to content
This repository has been archived by the owner on Dec 11, 2023. It is now read-only.

New configuration options: "use_middleware" and "use_subscribers" #55

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/librato/rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,14 @@ module Rails
mattr_accessor :token
mattr_accessor :flush_interval
mattr_accessor :source_pids
mattr_accessor :use_middleware
mattr_accessor :use_subscribers

# config defaults
self.flush_interval = 60 # seconds
self.source_pids = false # append process id to the source?
self.use_middleware = true
self.use_subscribers = true
# log_level (default :info)
# source (default: your machine's hostname)

Expand Down Expand Up @@ -100,7 +104,8 @@ def setup(app)
log :info, "starting up with #{app_server}..."
end
@pid = $$
app.middleware.use Librato::Rack::Middleware
app.middleware.use Librato::Rack::Middleware if self.use_middleware
enable_subscribers! if self.use_subscribers
start_worker unless forking_server?
end

Expand Down
4 changes: 2 additions & 2 deletions lib/librato/rails/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Librato
module Rails
module Configuration
CONFIG_SETTABLE = %w{user token flush_interval log_level prefix source source_pids}
CONFIG_SETTABLE = %w{user token flush_interval log_level prefix source source_pids use_middleware use_subscribers}

mattr_accessor :config_file
self.config_file = 'config/librato.yml'
Expand Down Expand Up @@ -49,4 +49,4 @@ def configure_with_environment

end
end
end
end
4 changes: 3 additions & 1 deletion lib/librato/rails/logging.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ def trace_settings
:qualified_source => qualified_source,
:log_level => log_level,
:prefix => prefix,
:flush_interval => self.flush_interval
:flush_interval => self.flush_interval,
:use_middleware => self.use_middleware,
:use_subscribers => self.use_subscribers
}
log :info, 'Settings: ' + settings.pretty_inspect
end
Expand Down
104 changes: 53 additions & 51 deletions lib/librato/rails/subscribers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,75 +3,77 @@ module Rails

# controllers

ActiveSupport::Notifications.subscribe /process_action.action_controller/ do |*args|
def self.enable_subscribers!

event = ActiveSupport::Notifications::Event.new(*args)
controller = event.payload[:controller]
action = event.payload[:action]
ActiveSupport::Notifications.subscribe /process_action.action_controller/ do |*args|

format = event.payload[:format] || "all"
format = "all" if format == "*/*"
status = event.payload[:status]
exception = event.payload[:exception]
# page_key = "request.#{controller}.#{action}_#{format}."
event = ActiveSupport::Notifications::Event.new(*args)
controller = event.payload[:controller]
action = event.payload[:action]

group "rails.request" do |r|
format = event.payload[:format] || "all"
format = "all" if format == "*/*"
status = event.payload[:status]
exception = event.payload[:exception]
# page_key = "request.#{controller}.#{action}_#{format}."

r.increment 'total'
r.timing 'time', event.duration
group "rails.request" do |r|

if exception
r.increment 'exceptions'
else
r.timing 'time.db', event.payload[:db_runtime] || 0
r.timing 'time.view', event.payload[:view_runtime] || 0
end
r.increment 'total'
r.timing 'time', event.duration

unless status.blank?
r.group 'status' do |s|
s.increment status
s.increment "#{status.to_s[0]}xx"
s.timing "#{status}.time", event.duration
s.timing "#{status.to_s[0]}xx.time", event.duration
if exception
r.increment 'exceptions'
else
r.timing 'time.db', event.payload[:db_runtime] || 0
r.timing 'time.view', event.payload[:view_runtime] || 0
end
end

r.increment 'slow' if event.duration > 200.0
end # end group
unless status.blank?
r.group 'status' do |s|
s.increment status
s.increment "#{status.to_s[0]}xx"
s.timing "#{status}.time", event.duration
s.timing "#{status.to_s[0]}xx.time", event.duration
end
end

end # end subscribe
r.increment 'slow' if event.duration > 200.0
end # end group

# SQL
end # end subscribe

ActiveSupport::Notifications.subscribe 'sql.active_record' do |*args|
payload = args.last
# SQL

group "rails.sql" do |s|
# puts (event.payload[:name] || 'nil') + ":" + event.payload[:sql] + "\n"
s.increment 'queries'
ActiveSupport::Notifications.subscribe 'sql.active_record' do |*args|
payload = args.last

sql = payload[:sql].strip
s.increment 'selects' if sql.starts_with?('SELECT')
s.increment 'inserts' if sql.starts_with?('INSERT')
s.increment 'updates' if sql.starts_with?('UPDATE')
s.increment 'deletes' if sql.starts_with?('DELETE')
group "rails.sql" do |s|
# puts (event.payload[:name] || 'nil') + ":" + event.payload[:sql] + "\n"
s.increment 'queries'

sql = payload[:sql].strip
s.increment 'selects' if sql.starts_with?('SELECT')
s.increment 'inserts' if sql.starts_with?('INSERT')
s.increment 'updates' if sql.starts_with?('UPDATE')
s.increment 'deletes' if sql.starts_with?('DELETE')
end
end
end

# ActionMailer
# ActionMailer

ActiveSupport::Notifications.subscribe 'deliver.action_mailer' do |*args|
# payload[:mailer] => 'UserMailer'
group "rails.mail" do |m|
m.increment 'sent'
ActiveSupport::Notifications.subscribe 'deliver.action_mailer' do |*args|
# payload[:mailer] => 'UserMailer'
group "rails.mail" do |m|
m.increment 'sent'
end
end
end

ActiveSupport::Notifications.subscribe 'receive.action_mailer' do |*args|
group "rails.mail" do |m|
m.increment 'received'
ActiveSupport::Notifications.subscribe 'receive.action_mailer' do |*args|
group "rails.mail" do |m|
m.increment 'received'
end
end
end

end
end
end
4 changes: 3 additions & 1 deletion test/support/integration_case.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
class ActiveSupport::IntegrationCase < ActiveSupport::TestCase
include Capybara::DSL
include Rails.application.routes.url_helpers


Librato::Rails.enable_subscribers!

setup do
# remove any accumulated metrics
Librato::Rails.delete_all
Expand Down