Class: Wavefront::Client

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/wavefront/client.rb,
lib/wavefront/client/version.rb

Constant Summary collapse

DEFAULT_PATH =
'/chart/api'
VERSION =
"3.4.0"

Constants included from Constants

Wavefront::Constants::ALERT_FORMATS, Wavefront::Constants::DEFAULT_ALERT_FORMAT, Wavefront::Constants::DEFAULT_FORMAT, Wavefront::Constants::DEFAULT_HOST, Wavefront::Constants::DEFAULT_INFILE_FORMAT, Wavefront::Constants::DEFAULT_OBSOLETE_METRICS, Wavefront::Constants::DEFAULT_PERIOD_SECONDS, Wavefront::Constants::DEFAULT_PREFIX_LENGTH, Wavefront::Constants::DEFAULT_PROXY, Wavefront::Constants::DEFAULT_PROXY_PORT, Wavefront::Constants::DEFAULT_STRICT, Wavefront::Constants::EVENT_LEVELS, Wavefront::Constants::EVENT_STATE_DIR, Wavefront::Constants::FORMATS, Wavefront::Constants::GRANULARITIES

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token, host = DEFAULT_HOST, debug = false) ⇒ Client

Returns a new instance of Client.



33
34
35
36
37
# File 'lib/wavefront/client.rb', line 33

def initialize(token, host=DEFAULT_HOST, debug=false)
  @headers = {'X-AUTH-TOKEN' => token}
  @base_uri = URI::HTTPS.build(:host => host, :path => DEFAULT_PATH)
  debug(debug)
end

Instance Attribute Details

#base_uriObject (readonly)

Returns the value of attribute base_uri.



31
32
33
# File 'lib/wavefront/client.rb', line 31

def base_uri
  @base_uri
end

#headersObject (readonly)

Returns the value of attribute headers.



31
32
33
# File 'lib/wavefront/client.rb', line 31

def headers
  @headers
end

Instance Method Details

#query(query, granularity = 'm', options = {}) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/wavefront/client.rb', line 39

def query(query, granularity='m', options={})

  options[:end_time] ||= Time.now.utc
  options[:start_time] ||= options[:end_time] - DEFAULT_PERIOD_SECONDS
  options[:response_format] ||= DEFAULT_FORMAT
  options[:prefix_length] ||= DEFAULT_PREFIX_LENGTH
  options[:strict] = DEFAULT_STRICT unless options.keys.include?(:strict)
  options[:includeObsoleteMetrics] = DEFAULT_OBSOLETE_METRICS unless options.keys.include?(:includeObsoleteMetrics)

  [ options[:start_time], options[:end_time] ].each { |o| raise Wavefront::Exception::InvalidTimeFormat unless o.is_a?(Time) }
  raise Wavefront::Exception::InvalidGranularity unless GRANULARITIES.include?(granularity)
  raise Wavefront::Exception::InvalidResponseFormat unless FORMATS.include?(options[:response_format])
  raise InvalidPrefixLength unless options[:prefix_length].is_a?(Fixnum)

  args = {:params =>
          {:q => query, :g => granularity, :n => 'Unknown',
           :s => options[:start_time].to_i,
           :e => options[:end_time].to_i,
           :strict => options[:strict],
           :includeObsoleteMetrics => options[:includeObsoleteMetrics]
          }}.merge(@headers)

  if options[:passthru]
    args[:params].merge!(options[:passthru])
  end

  response = RestClient.get @base_uri.to_s, args

  klass = Object.const_get('Wavefront').const_get('Response').const_get(options[:response_format].to_s.capitalize)
  return klass.new(response, options)

end