Class: PhusionPassenger::ActiveSupport3Extensions::AnalyticsLogging

Inherits:
ActiveSupport::LogSubscriber
  • Object
show all
Defined in:
lib/phusion_passenger/active_support3_extensions/init.rb

Defined Under Namespace

Modules: ACExtension, ASBenchmarkableExtension Classes: ExceptionLogger

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ AnalyticsLogging

Returns a new instance of AnalyticsLogging.



93
94
95
# File 'lib/phusion_passenger/active_support3_extensions/init.rb', line 93

def initialize(options)
	install_event_preprocessor(options[:event_preprocessor]) if options[:event_preprocessor]
end

Class Method Details

.install!(options, user_options) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/phusion_passenger/active_support3_extensions/init.rb', line 41

def self.install!(options, user_options)
	analytics_logger = options["analytics_logger"]
	app_group_name = options["app_group_name"]
	return false if !analytics_logger || !options["analytics"]
	
	# If the Ruby interpreter supports GC statistics then turn it on
	# so that the info can be logged.
	GC.enable_stats if GC.respond_to?(:enable_stats)
	
	subscriber = self.new(user_options)
	AnalyticsLogging.attach_to(:action_controller, subscriber)
	AnalyticsLogging.attach_to(:active_record, subscriber)
	if defined?(ActiveSupport::Cache::Store)
		ActiveSupport::Cache::Store.instrument = true
		AnalyticsLogging.attach_to(:active_support, subscriber)
	end
	PhusionPassenger.on_event(:starting_request_handler_thread) do
		if defined?(ActiveSupport::Cache::Store)
			# This flag is thread-local.
			ActiveSupport::Cache::Store.instrument = true
		end
	end
	
	if defined?(ActionDispatch::DebugExceptions)
		exceptions_middleware = ActionDispatch::DebugExceptions
	elsif defined?(ActionDispatch::ShowExceptions)
		exceptions_middleware = ActionDispatch::ShowExceptions
	end
	if exceptions_middleware
		if defined?(Rails)
			Rails.application.middleware.insert_after(
				exceptions_middleware,
				ExceptionLogger, analytics_logger, app_group_name)
		end
	end
	
	if defined?(ActionController::Base)
		ActionController::Base.class_eval do
			include ACExtension
		end
	end
	
	if defined?(ActiveSupport::Benchmarkable)
		ActiveSupport::Benchmarkable.class_eval do
			include ASBenchmarkableExtension
			alias_method_chain :benchmark, :passenger
		end
	end
	
	return true
end

Instance Method Details

#cache_fetch_hit(event) ⇒ Object



123
124
125
# File 'lib/phusion_passenger/active_support3_extensions/init.rb', line 123

def cache_fetch_hit(event)
	PhusionPassenger.log_cache_hit(nil, event.payload[:key])
end

#cache_generate(event) ⇒ Object



127
128
129
130
# File 'lib/phusion_passenger/active_support3_extensions/init.rb', line 127

def cache_generate(event)
	PhusionPassenger.log_cache_miss(nil, event.payload[:key],
		event.duration * 1000)
end

#cache_read(event) ⇒ Object



115
116
117
118
119
120
121
# File 'lib/phusion_passenger/active_support3_extensions/init.rb', line 115

def cache_read(event)
	if event.payload[:hit]
		PhusionPassenger.log_cache_hit(nil, event.payload[:key])
	else
		PhusionPassenger.log_cache_miss(nil, event.payload[:key])
	end
end

#process_action(event) ⇒ Object



97
98
99
100
101
102
103
# File 'lib/phusion_passenger/active_support3_extensions/init.rb', line 97

def process_action(event)
	log = Thread.current[PASSENGER_ANALYTICS_WEB_LOG]
	if log
		view_runtime = event.payload[:view_runtime]
		log.message("View rendering time: #{(view_runtime * 1000).to_i}") if view_runtime
	end
end

#sql(event) ⇒ Object



105
106
107
108
109
110
111
112
113
# File 'lib/phusion_passenger/active_support3_extensions/init.rb', line 105

def sql(event)
	if log = Thread.current[PASSENGER_ANALYTICS_WEB_LOG]
		name = event.payload[:name] || "SQL"
		sql = event.payload[:sql]
		digest = Digest::MD5.hexdigest("#{name}\0#{sql}\0#{rand}")
		log.measured_time_points("DB BENCHMARK: #{digest}",
			event.time, event.end, "#{name}\n#{sql}")
	end
end