Class
PhusionPassenger::AnalyticsLogger

In: | lib/phusion_passenger/analytics_logger.rb
Parent: | Object

Methods

- clear_connection
- close
- continue_transaction
- new
- new_from_options
- new_transaction

Included Modules

- Utils

Classes and Modules
Class PhusionPassenger::AnalyticsLogger::Log
Class PhusionPassenger::AnalyticsLogger::SharedData
Constants

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']

Attributes

[RW] | max_connect_tries |
[RW] | reconnect_timeout |

Public Class methods
new(logging_agent_address, username, password, node_name)

[ show source ]

         # File lib/phusion_passenger/analytics_logger.rb, line 145
    145:         def initialize(logging_agent_address, username, password, node_name)
    146:                 @server_address = logging_agent_address
    147:                 @username = username
    148:                 @password = password
    149:                 if node_name && !node_name.empty?
    150:                         @node_name = node_name
    151:                 else
    152:                         @node_name = `hostname`.strip
    153:                 end
    154:                 @random_dev = File.open("/dev/urandom")
    155:                 @shared_data = SharedData.new
    156:                 if @server_address && local_socket_address?(@server_address)
    157:                         @max_connect_tries = 10
    158:                 else
    159:                         @max_connect_tries = 1
    160:                 end
    161:                 @reconnect_timeout = 60
    162:                 @next_reconnect_time = Time.utc(1980, 1, 1)
    163:         end

new_from_options(options)

[ show source ]

         # File lib/phusion_passenger/analytics_logger.rb, line 131
    131:         def self.new_from_options(options)
    132:                 if options["analytics"] && options["logging_agent_address"]
    133:                         return new(options["logging_agent_address"],
    134:                                 options["logging_agent_username"],
    135:                                 options["logging_agent_password_base64"].unpack('m').first,
    136:                                 options["node_name"])
    137:                 else
    138:                         return nil
    139:                 end
    140:         end

Public Instance methods
clear_connection()

[ show source ]

         # File lib/phusion_passenger/analytics_logger.rb, line 165
    165:         def clear_connection
    166:                 @shared_data.synchronize do
    167:                         @random_dev = File.open("/dev/urandom") if @random_dev.closed?
    168:                         @shared_data.unref
    169:                         @shared_data = SharedData.new
    170:                 end
    171:         end

close()

[ show source ]

         # File lib/phusion_passenger/analytics_logger.rb, line 173
    173:         def close
    174:                 @shared_data.synchronize do
    175:                         @random_dev.close
    176:                         @shared_data.unref
    177:                         @shared_data = nil
    178:                 end
    179:         end

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

[ show source ]

         # File lib/phusion_passenger/analytics_logger.rb, line 220
    220:         def continue_transaction(txn_id, group_name, category = :requests, union_station_key = nil)
    221:                 if !@server_address
    222:                         return Log.new
    223:                 elsif !txn_id || txn_id.empty?
    224:                         raise ArgumentError, "Transaction ID may not be empty"
    225:                 end
    226:
    227:                 @shared_data.synchronize do
    228:                         try_count = 0
    229:                         if current_time >= @next_reconnect_time
    230:                                 while try_count < @max_connect_tries
    231:                                         begin
    232:                                                 connect if !connected?
    233:                                                 @shared_data.client.write("openTransaction",
    234:                                                         txn_id, group_name, "", category,
    235:                                                         AnalyticsLogger.timestamp_string,
    236:                                                         union_station_key,
    237:                                                         true)
    238:                                                 return Log.new(@shared_data, txn_id)
    239:                                         rescue Errno::ENOENT, *NETWORK_ERRORS
    240:                                                 try_count += 1
    241:                                                 disconnect(true)
    242:                                                 sleep RETRY_SLEEP if try_count < @max_connect_tries
    243:                                         rescue Exception => e
    244:                                                 disconnect
    245:                                                 raise e
    246:                                         end
    247:                                 end
    248:                                 # Failed to connect.
    249:                                 DebugLogging.warn("Cannot connect to the logging agent (#{@server_address}); " +
    250:                                         "retrying in #{@reconnect_timeout} seconds.")
    251:                                 @next_reconnect_time = current_time + @reconnect_timeout
    252:                         end
    253:                         return Log.new
    254:                 end
    255:         end

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

[ show source ]

         # File lib/phusion_passenger/analytics_logger.rb, line 181
    181:         def new_transaction(group_name, category = :requests, union_station_key = nil)
    182:                 if !@server_address
    183:                         return Log.new
    184:                 elsif !group_name || group_name.empty?
    185:                         raise ArgumentError, "Group name may not be empty"
    186:                 end
    187:
    188:                 txn_id = (AnalyticsLogger.current_time.to_i / 60).to_s(36)
    189:                 txn_id << "-#{random_token(11)}"
    190:                 @shared_data.synchronize do
    191:                         try_count = 0
    192:                         if current_time >= @next_reconnect_time
    193:                                 while try_count < @max_connect_tries
    194:                                         begin
    195:                                                 connect if !connected?
    196:                                                 @shared_data.client.write("openTransaction",
    197:                                                         txn_id, group_name, "", category,
    198:                                                         AnalyticsLogger.timestamp_string,
    199:                                                         union_station_key,
    200:                                                         true)
    201:                                                 return Log.new(@shared_data, txn_id)
    202:                                         rescue Errno::ENOENT, *NETWORK_ERRORS
    203:                                                 try_count += 1
    204:                                                 disconnect(true)
    205:                                                 sleep RETRY_SLEEP if try_count < @max_connect_tries
    206:                                         rescue Exception => e
    207:                                                 disconnect
    208:                                                 raise e
    209:                                         end
    210:                                 end
    211:                                 # Failed to connect.
    212:                                 DebugLogging.warn("Cannot connect to the logging agent (#{@server_address}); " +
    213:                                         "retrying in #{@reconnect_timeout} seconds.")
    214:                                 @next_reconnect_time = current_time + @reconnect_timeout
    215:                         end
    216:                         return Log.new
    217:                 end
    218:         end