Class: PhusionPassenger::AnalyticsLogger

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/phusion_passenger/analytics_logger.rb

Defined Under Namespace

Classes: Connection, Lock, Log

Constant Summary collapse

RETRY_SLEEP =
0.2
NETWORK_ERRORS =
[Errno::EPIPE, Errno::ECONNREFUSED, Errno::ECONNRESET,
Errno::EHOSTUNREACH, Errno::ENETDOWN, Errno::ENETUNREACH, Errno::ETIMEDOUT]

Constants included from Utils

Utils::NULL

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils

#connect_to_server, #generate_random_id, #get_socket_address_type, #global_backtrace_report, included, #install_options_as_ivars, #local_socket_address?, mktmpdir, #print_exception, #process_is_alive?, #require_option, #split_by_null_into_hash

Constructor Details

#initialize(logging_agent_address, username, password, node_name) ⇒ AnalyticsLogger

Returns a new instance of AnalyticsLogger.



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/phusion_passenger/analytics_logger.rb', line 186

def initialize(logging_agent_address, username, password, node_name)
	@server_address = logging_agent_address
	@username = username
	@password = password
	if node_name && !node_name.empty?
		@node_name = node_name
	else
		@node_name = `hostname`.strip
	end
	@random_dev = File.open("/dev/urandom")
	
	# This mutex protects the following instance variables, but
	# not the contents of @connection.
	@mutex = Mutex.new
	
	@connection = Connection.new(nil)
	if @server_address && local_socket_address?(@server_address)
		@max_connect_tries = 10
	else
		@max_connect_tries = 1
	end
	@reconnect_timeout = 1
	@next_reconnect_time = Time.utc(1980, 1, 1)
end

Instance Attribute Details

#max_connect_triesObject

Returns the value of attribute max_connect_tries.



183
184
185
# File 'lib/phusion_passenger/analytics_logger.rb', line 183

def max_connect_tries
  @max_connect_tries
end

#reconnect_timeoutObject

Returns the value of attribute reconnect_timeout.



184
185
186
# File 'lib/phusion_passenger/analytics_logger.rb', line 184

def reconnect_timeout
  @reconnect_timeout
end

Class Method Details

.new_from_options(options) ⇒ Object



172
173
174
175
176
177
178
179
180
181
# File 'lib/phusion_passenger/analytics_logger.rb', line 172

def self.new_from_options(options)
	if options["analytics"] && options["logging_agent_address"]
		return new(options["logging_agent_address"],
			options["logging_agent_username"],
			options["logging_agent_password"],
			options["node_name"])
	else
		return nil
	end
end

Instance Method Details

#clear_connectionObject



211
212
213
214
215
216
217
218
219
# File 'lib/phusion_passenger/analytics_logger.rb', line 211

def clear_connection
	@mutex.synchronize do
		@connection.synchronize do
			@random_dev = File.open("/dev/urandom") if @random_dev.closed?
			@connection.unref
			@connection = Connection.new(nil)
		end
	end
end

#closeObject



221
222
223
224
225
226
227
228
229
# File 'lib/phusion_passenger/analytics_logger.rb', line 221

def close
	@mutex.synchronize do
		@connection.synchronize do
			@random_dev.close
			@connection.unref
			@connection = nil
		end
	end
end

#continue_transaction(txn_id, group_name, category = :requests, union_station_key = "-") ⇒ Object



290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'lib/phusion_passenger/analytics_logger.rb', line 290

def continue_transaction(txn_id, group_name, category = :requests, union_station_key = "-")
	if !@server_address
		return Log.new
	elsif !txn_id || txn_id.empty?
		raise ArgumentError, "Transaction ID may not be empty"
	end
	
	Lock.new(@mutex).synchronize do |lock|
		if current_time < @next_reconnect_time
			return Log.new
		end
		
		Lock.new(@connection.mutex).synchronize do |connection_lock|
			if !@connection.connected?
				begin
					connect
					connection_lock.reset(@connection.mutex)
				rescue SystemCallError, IOError
					@connection.disconnect
					DebugLogging.warn("Cannot connect to the logging agent at #{@server_address}; " +
						"retrying in #{@reconnect_timeout} second(s).")
					@next_reconnect_time = current_time + @reconnect_timeout
					return Log.new
				rescue Exception => e
					@connection.disconnect
					raise e
				end
			end
			
			begin
				@connection.channel.write("openTransaction",
					txn_id, group_name, "", category,
					AnalyticsLogger.timestamp_string,
					union_station_key,
					true)
				return Log.new(@connection, txn_id)
			rescue SystemCallError, IOError
				@connection.disconnect
				DebugLogging.warn("The logging agent at #{@server_address}" <<
					" closed the connection; will reconnect in " <<
					"#{@reconnect_timeout} second(s).")
				@next_reconnect_time = current_time + @reconnect_timeout
				return Log.new
			rescue Exception => e
				@connection.disconnect
				raise e
			end
		end
	end
end

#new_transaction(group_name, category = :requests, union_station_key = "-") ⇒ Object



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/phusion_passenger/analytics_logger.rb', line 231

def new_transaction(group_name, category = :requests, union_station_key = "-")
	if !@server_address
		return Log.new
	elsif !group_name || group_name.empty?
		raise ArgumentError, "Group name may not be empty"
	end
	
	txn_id = (AnalyticsLogger.current_time.to_i / 60).to_s(36)
	txn_id << "-#{random_token(11)}"
	
	Lock.new(@mutex).synchronize do |lock|
		if current_time < @next_reconnect_time
			return Log.new
		end
		
		Lock.new(@connection.mutex).synchronize do |connection_lock|
			if !@connection.connected?
				begin
					connect
					connection_lock.reset(@connection.mutex)
				rescue SystemCallError, IOError
					@connection.disconnect
					DebugLogging.warn("Cannot connect to the logging agent at #{@server_address}; " +
						"retrying in #{@reconnect_timeout} second(s).")
					@next_reconnect_time = current_time + @reconnect_timeout
					return Log.new
				rescue Exception => e
					@connection.disconnect
					raise e
				end
			end
			
			begin
				@connection.channel.write("openTransaction",
					txn_id, group_name, "", category,
					AnalyticsLogger.timestamp_string,
					union_station_key,
					true,
					true)
				result = @connection.channel.read
				if result != ["ok"]
					raise "Expected logging server to respond with 'ok', but got #{result.inspect} instead"
				end
				return Log.new(@connection, txn_id)
			rescue SystemCallError, IOError
				@connection.disconnect
				DebugLogging.warn("The logging agent at #{@server_address}" <<
					" closed the connection; will reconnect in " <<
					"#{@reconnect_timeout} second(s).")
				@next_reconnect_time = current_time + @reconnect_timeout
				return Log.new
			rescue Exception => e
				@connection.disconnect
				raise e
			end
		end
	end
end