Class: PhusionPassenger::AnalyticsLogger

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

Defined Under Namespace

Classes: Log, SharedData

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.



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/phusion_passenger/analytics_logger.rb', line 145

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")
	@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

#max_connect_triesObject

Returns the value of attribute max_connect_tries.



142
143
144
# File 'lib/phusion_passenger/analytics_logger.rb', line 142

def max_connect_tries
  @max_connect_tries
end

#reconnect_timeoutObject

Returns the value of attribute reconnect_timeout.



143
144
145
# File 'lib/phusion_passenger/analytics_logger.rb', line 143

def reconnect_timeout
  @reconnect_timeout
end

Class Method Details

.new_from_options(options) ⇒ Object



131
132
133
134
135
136
137
138
139
140
# File 'lib/phusion_passenger/analytics_logger.rb', line 131

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



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

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

#closeObject



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

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

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



220
221
222
223
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
# File 'lib/phusion_passenger/analytics_logger.rb', line 220

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
	
	@shared_data.synchronize do
		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)
					sleep RETRY_SLEEP if try_count < @max_connect_tries
				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

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



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/phusion_passenger/analytics_logger.rb', line 181

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)}"
	@shared_data.synchronize do
		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)
					sleep RETRY_SLEEP if try_count < @max_connect_tries
				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