Class: PirateMetrics::Agent
- Inherits:
-
Object
- Object
- PirateMetrics::Agent
- Defined in:
- lib/pirate_metrics/agent.rb
Constant Summary collapse
- BACKOFF =
2.0
- MAX_RECONNECT_DELAY =
15
- MAX_BUFFER =
5000
- REPLY_TIMEOUT =
10
- CONNECT_TIMEOUT =
20
- EXIT_FLUSH_TIMEOUT =
5
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#enabled ⇒ Object
readonly
Returns the value of attribute enabled.
-
#host ⇒ Object
Returns the value of attribute host.
-
#port ⇒ Object
Returns the value of attribute port.
-
#queue ⇒ Object
Returns the value of attribute queue.
-
#synchronous ⇒ Object
Returns the value of attribute synchronous.
Class Method Summary collapse
Instance Method Summary collapse
-
#cleanup ⇒ Object
Called when a process is exiting to give it some extra time to push events to the service.
- #enabled? ⇒ Boolean
-
#flush(allow_reconnect = false) ⇒ Object
Synchronously flush all pending metrics out to the server By default will not try to reconnect to the server if a connection failure happens during the flush, though you may optionally override this behavior by passing true.
-
#initialize(api_key, options = {}) ⇒ Agent
constructor
Sets up a connection to the collector.
- #logger ⇒ Object
- #logger=(logger) ⇒ Object
-
#stop ⇒ Object
Stopping the agent will immediately stop all communication to PirateMetrics.
Constructor Details
#initialize(api_key, options = {}) ⇒ Agent
Sets up a connection to the collector.
PirateMetrics::Agent.new(API_KEY)
PirateMetrics::Agent.new(API_KEY, :collector => 'hostname:port')
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/pirate_metrics/agent.rb', line 37 def initialize(api_key, = {}) # symbolize options keys .replace( .inject({}) { |m, (k, v)| m[(k.to_sym rescue k) || k] = v; m } ) # defaults # host: piratemetrics.com # port: 80 # enabled: true # synchronous: false @api_key = api_key @host, @port = [:collector].to_s.split(':') @host = [:host] || 'https://piratemetrics.com' @port = ([:port] || 443).to_i @enabled = .has_key?(:enabled) ? !![:enabled] : true @synchronous = !![:synchronous] @pid = Process.pid @allow_reconnect = true setup_cleanup_at_exit if @enabled end |
Instance Attribute Details
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
19 20 21 |
# File 'lib/pirate_metrics/agent.rb', line 19 def connection @connection end |
#enabled ⇒ Object (readonly)
Returns the value of attribute enabled.
19 20 21 |
# File 'lib/pirate_metrics/agent.rb', line 19 def enabled @enabled end |
#host ⇒ Object
Returns the value of attribute host.
18 19 20 |
# File 'lib/pirate_metrics/agent.rb', line 18 def host @host end |
#port ⇒ Object
Returns the value of attribute port.
18 19 20 |
# File 'lib/pirate_metrics/agent.rb', line 18 def port @port end |
#queue ⇒ Object
Returns the value of attribute queue.
18 19 20 |
# File 'lib/pirate_metrics/agent.rb', line 18 def queue @queue end |
#synchronous ⇒ Object
Returns the value of attribute synchronous.
18 19 20 |
# File 'lib/pirate_metrics/agent.rb', line 18 def synchronous @synchronous end |
Class Method Details
.logger ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/pirate_metrics/agent.rb', line 25 def self.logger if !@logger @logger = Logger.new(STDERR) @logger.level = Logger::WARN end @logger end |
.logger=(l) ⇒ Object
21 22 23 |
# File 'lib/pirate_metrics/agent.rb', line 21 def self.logger=(l) @logger = l end |
Instance Method Details
#cleanup ⇒ Object
Called when a process is exiting to give it some extra time to push events to the service. An at_exit handler is automatically registered for this method, but can be called manually in cases where at_exit is bypassed like Resque workers.
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/pirate_metrics/agent.rb', line 131 def cleanup if running? logger.info "Cleaning up agent, queue size: #{@queue.size}, thread running: #{@thread.alive?}" @allow_reconnect = false queue_metric('exit') begin with_timeout(EXIT_FLUSH_TIMEOUT) { @thread.join } rescue Timeout::Error if @queue.size > 0 logger.error "Timed out working agent thread on exit, dropping #{@queue.size} metrics" else logger.error "Timed out PirateMetrics Agent, exiting" end end end @thread = nil end |
#enabled? ⇒ Boolean
99 100 101 |
# File 'lib/pirate_metrics/agent.rb', line 99 def enabled? @enabled end |
#flush(allow_reconnect = false) ⇒ Object
Synchronously flush all pending metrics out to the server By default will not try to reconnect to the server if a connection failure happens during the flush, though you may optionally override this behavior by passing true.
agent.flush
92 93 94 95 96 97 |
# File 'lib/pirate_metrics/agent.rb', line 92 def flush(allow_reconnect = false) queue_metric('flush', nil, { :synchronous => true, :allow_reconnect => allow_reconnect }) if running? end |
#logger ⇒ Object
107 108 109 |
# File 'lib/pirate_metrics/agent.rb', line 107 def logger @logger || self.class.logger end |
#logger=(logger) ⇒ Object
103 104 105 |
# File 'lib/pirate_metrics/agent.rb', line 103 def logger=(logger) @logger = logger end |
#stop ⇒ Object
Stopping the agent will immediately stop all communication to PirateMetrics. If you call this and submit another metric, the agent will start again.
Calling stop will cause all metrics waiting to be sent to be discarded. Don’t call it unless you are expecting this behavior.
agent.stop
120 121 122 123 124 125 |
# File 'lib/pirate_metrics/agent.rb', line 120 def stop if @thread @thread.kill @thread = nil end end |