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
DebugLogging.warn("Cannot connect to the logging agent (#{@server_address}); " +
"retrying in #{@reconnect_timeout} second(s).")
@next_reconnect_time = current_time + @reconnect_timeout
end
return Log.new
end
end
end