11. Why?
● NewRelic is really great...but
○ "Database call response times"
■ Standard package = $24/month/server
○ "Transaction tracing"
■ Pro package = $149/month/server
○ basically unavailable for freelancers and small
projects
● Proof-of-concept
○ 2 hours of hacking
○ 2 hours of waiting for Google Analytics
12. How?
● Google Analytics Site Speed User Timing
_gaq.push([‘_trackTiming’, category, variable, time, opt_label, opt_sample]);
● Google Analytics Custom Variables
_gaq.push([`_setCustomVar’, index, name, value, opt_scope]);
● ActiveSupport::Notifications
ActiveSupport::Notifications.subscribe('process_action.action_controller') do |_, from, to, _, payload|
Garelic::Metrics.report(:action, :db_runtime, payload[:db_runtime] || 0)
Garelic::Metrics.report(:action, :view_runtime, payload[:view_runtime] || 0)
Garelic::Metrics.report(:action, :total_runtime, (to - from) * 1000)
Garelic::Metrics.action_identifier = "#{payload[:controller]}##{payload[:action]}"
end
ActiveSupport::Notifications.subscribe('sql.active_record') do |_, start, finish, _, payload|
type = payload[:name] || 'Other SQL'
Garelic::Metrics.report(:active_record, type, (finish - start) * 1000) if type != 'CACHE'
end
13. How?
● Ruby Monkey Patching
module ActionController
class Metal < AbstractController::Base
alias :dispatch_without_garelic :dispatch
def dispatch(*args)
Garelic::Metrics.reset!
response = dispatch_without_garelic(*args)
timing_data = Garelic.report_user_timing_from_metrics(Garelic::Metrics)
_, _, chunks = response
chunks.each do |chunk|
chunk.gsub!(Garelic::Timing, timing_data)
end
response
end
end
end
14. Limitations
● Cannot measure redirect, AJAX, async jobs
● Everything is visible in source code
○ Reporting stacktrace & raw SQL considered harmful
● Page caching breaks reports
● It's a hack & GA has its own limitations
15. Ideas for future development
● Tracking errors via GA events on error 500
pages
● Tracking only slow responses (thresholding)
● Share Google Analytics dashboards &
custom reports
● Pull requests welcome! https://github.
com/jsuchal/garelic
16. Related resources
● All Presentation Software is Broken http://www.
igvita.com/2012/05/14/all-presentation-software-is-broken/
● GA User Timing Method https://developers.google.
com/analytics/devguides/collection/gajs/methods/gaJSApiUserTiming
● GA Custom Variables https://developers.google.
com/analytics/devguides/collection/gajs/gaTrackingCustomVariables#setu
p