Class: HGAPI::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/hg_api/client.rb

Constant Summary collapse

SUPPORTED_TRANSPORTS =
[:udp, :tcp, :http]
HTTP_URI =
"https://hostedgraphite.com/api/v1/sink"
HOST =
'carbon.hostedgraphite.com'
PORT =
2003

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



13
14
15
16
# File 'lib/hg_api/client.rb', line 13

def initialize(options = {})
  @settings = build_settings(options)
  @disabled = @settings[:api_key].nil?
end

Instance Attribute Details

#loggerObject

Returns the value of attribute logger.



6
7
8
# File 'lib/hg_api/client.rb', line 6

def logger
  @logger
end

Instance Method Details

#metric(key, value, options = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/hg_api/client.rb', line 18

def metric(key, value, options = {})
  return if @disabled

  begin
    send_metric(key, value, check_transport!(options[:via]) || @settings[:default_transport])
  rescue SocketError => e
    logger.error "[HG API] #{e.class} #{e.message} while sending metric #{key}" if logger
  end
end

#time(key, options = {}) ⇒ Object



28
29
30
31
32
33
# File 'lib/hg_api/client.rb', line 28

def time(key, options = {})
  start = Time.now
  result = yield
  metric(key, ((Time.now - start) * 1000).round, options)
  result
end