Class: PhusionPassenger::AnalyticsLogger

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

Defined Under Namespace

Classes: Lock, Log, SharedData

Constant Summary

RETRY_SLEEP =
0.2
NETWORK_ERRORS =
[Errno::EPIPE, Errno::ECONNREFUSED, Errno::ECONNRESET,
Errno::EHOSTUNREACH, Errno::ENETDOWN, Errno::ENETUNREACH, Errno::ETIMEDOUT]
RANDOM_CHARS =
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9']

Constants included from Utils

Utils::FileSystemWatcher, Utils::NULL

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from Utils

#split_by_null_into_hash

Constructor Details

- (AnalyticsLogger) initialize(logging_agent_address, username, password, node_name)

Returns a new instance of AnalyticsLogger



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/phusion_passenger/analytics_logger.rb', line 150

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 @shared_data.
	@mutex = Mutex.new
	
	@shared_data = SharedData.new
	if @server_address && local_socket_address?(@server_address)
		@max_connect_tries = 10
	else
		@max_connect_tries = 1
	end
	@reconnect_timeout = 60
	@next_reconnect_time = Time.utc(1980, 1, 1)
end

Instance Attribute Details

- (Object) max_connect_tries

Returns the value of attribute max_connect_tries



147
148
149
# File 'lib/phusion_passenger/analytics_logger.rb', line 147

def max_connect_tries
  @max_connect_tries
end

- (Object) reconnect_timeout

Returns the value of attribute reconnect_timeout



148
149
150
# File 'lib/phusion_passenger/analytics_logger.rb', line 148

def reconnect_timeout
  @reconnect_timeout
end

Class Method Details

+ (Object) new_from_options(options)



136
137
138
139
140
141
142
143
144
145
# File 'lib/phusion_passenger/analytics_logger.rb', line 136

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

- (Object) clear_connection



175
176
177
178
179
180
181
182
183
# File 'lib/phusion_passenger/analytics_logger.rb', line 175

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

- (Object) close



185
186
187
188
189
190
191
192
193
# File 'lib/phusion_passenger/analytics_logger.rb', line 185

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

- (Object) continue_transaction(txn_id, group_name, category = :requests, union_station_key = nil)



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
# File 'lib/phusion_passenger/analytics_logger.rb', line 245

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|
	Lock.new(@shared_data.mutex).synchronize do |shared_data_lock|
		try_count = 0
		if current_time >= @next_reconnect_time
			while try_count < @max_connect_tries
				begin
					connect if !connected?
					@shared_data.client.write("openTransaction",
						txn_id, group_name, "", category,
						AnalyticsLogger.timestamp_string,
						union_station_key,
						true)
					return Log.new(@shared_data, txn_id)
				rescue Errno::ENOENT, *NETWORK_ERRORS
					try_count += 1
					disconnect(true)
					shared_data_lock.reset(@shared_data.mutex, false)
					lock.unlock
					sleep RETRY_SLEEP if try_count < @max_connect_tries
					lock.lock
					shared_data_lock.lock
				rescue Exception => e
					disconnect
					raise e
				end
			end
			# Failed to connect.
			DebugLogging.warn("Cannot connect to the logging agent (#{@server_address}); " +
				"retrying in #{@reconnect_timeout} seconds.")
			@next_reconnect_time = current_time + @reconnect_timeout
		end
		return Log.new
	end
	end
end

- (Object) new_transaction(group_name, category = :requests, union_station_key = nil)



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/phusion_passenger/analytics_logger.rb', line 195

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|
	Lock.new(@shared_data.mutex).synchronize do |shared_data_lock|
		try_count = 0
		if current_time >= @next_reconnect_time
			while try_count < @max_connect_tries
				begin
					connect if !connected?
					@shared_data.client.write("openTransaction",
						txn_id, group_name, "", category,
						AnalyticsLogger.timestamp_string,
						union_station_key,
						true,
						true)
					result = @shared_data.client.read
					if result != ["ok"]
						raise "Expected logging server to respond with 'ok', but got #{result.inspect} instead"
					end
					return Log.new(@shared_data, txn_id)
				rescue Errno::ENOENT, *NETWORK_ERRORS
					try_count += 1
					disconnect(true)
					shared_data_lock.reset(@shared_data.mutex, false)
					lock.unlock
					sleep RETRY_SLEEP if try_count < @max_connect_tries
					lock.lock
					shared_data_lock.lock
				rescue Exception => e
					disconnect
					raise e
				end
			end
			# Failed to connect.
			DebugLogging.warn("Cannot connect to the logging agent (#{@server_address}); " +
				"retrying in #{@reconnect_timeout} seconds.")
			@next_reconnect_time = current_time + @reconnect_timeout
		end
		return Log.new
	end
	end
end