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]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils

process_is_alive?

Constructor Details

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

Returns a new instance of AnalyticsLogger.



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/phusion_passenger/analytics_logger.rb', line 179

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.



176
177
178
# File 'lib/phusion_passenger/analytics_logger.rb', line 176

def max_connect_tries
  @max_connect_tries
end

#reconnect_timeoutObject

Returns the value of attribute reconnect_timeout.



177
178
179
# File 'lib/phusion_passenger/analytics_logger.rb', line 177

def reconnect_timeout
  @reconnect_timeout
end

Class Method Details

.new_from_options(options) ⇒ Object



165
166
167
168
169
170
171
172
173
174
# File 'lib/phusion_passenger/analytics_logger.rb', line 165

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_base64"].unpack('m').first,
			options["node_name"])
	else
		return nil
	end
end

Instance Method Details

#clear_connectionObject



204
205
206
207
208
209
210
211
212
# File 'lib/phusion_passenger/analytics_logger.rb', line 204

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



214
215
216
217
218
219
220
221
222
# File 'lib/phusion_passenger/analytics_logger.rb', line 214

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 = nil) ⇒ Object



283
284
285
286
287
288
289
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
# File 'lib/phusion_passenger/analytics_logger.rb', line 283

def continue_transaction(txn_id, group_name, category = :requests, union_station_key = nil)
	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 = nil) ⇒ Object



224
225
226
227
228
229
230
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
# File 'lib/phusion_passenger/analytics_logger.rb', line 224

def new_transaction(group_name, category = :requests, union_station_key = nil)
	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