Class: Wavefront::Query

Inherits:
Base
  • Object
show all
Defined in:
lib/wavefront-sdk/query.rb

Overview

Query Wavefront metrics.

Instance Attribute Summary

Attributes inherited from Base

#conn, #debug, #logger, #net, #noop, #opts, #update_keys, #verbose

Instance Method Summary collapse

Methods inherited from Base

#api_delete, #api_get, #api_post, #api_put, #hash_for_update, #initialize, #log, #mk_conn, #respond, #time_to_ms

Methods included from Mixins

#parse_time

Methods included from Validators

#wf_alert_id?, #wf_alert_severity?, #wf_cloudintegration_id?, #wf_dashboard_id?, #wf_epoch?, #wf_event_id?, #wf_granularity?, #wf_link_id?, #wf_link_template?, #wf_maintenance_window_id?, #wf_message_id?, #wf_metric_name?, #wf_ms_ts?, #wf_name?, #wf_point?, #wf_point_tags?, #wf_proxy_id?, #wf_savedsearch_entity?, #wf_savedsearch_id?, #wf_source_id?, #wf_string?, #wf_tag?, #wf_ts?, #wf_user_id?, #wf_value?, #wf_version?, #wf_webhook_id?

Constructor Details

This class inherits a constructor from Wavefront::Base

Instance Method Details

#api_baseObject



8
9
10
# File 'lib/wavefront-sdk/query.rb', line 8

def api_base
  'chart'
end

#query(query, granularity = nil, t_start = nil, t_end = nil, options = {}) ⇒ Wavefront::Response

GET /api/v2/chart/api Perform a charting query against Wavefront servers that returns the appropriate points in the specified time window and granularity. Any options can be pased through in the options hash. This means the SDK does not have to closely track the API, but also means the burden of data validation is down to the user.

Parameters:

  • query (String)

    Wavefront query to run

  • granularity (String) (defaults to: nil)

    the required granularity for the reported data

  • t_start (Time, Integer) (defaults to: nil)

    The start of the query window. May be a Ruby Time object, or epoch milliseconds

  • t_end (Time, Integer) (defaults to: nil)

    The end of the query window. May be a Ruby Time object, or epoch milliseconds.

  • options (Hash) (defaults to: {})

    any other options defined in the API

Returns:

Raises:

  • (ArgumentError)

    if query is not a string



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/wavefront-sdk/query.rb', line 31

def query(query, granularity = nil, t_start = nil, t_end = nil,
           options = {})

  raise ArgumentError unless query.is_a?(String)
  wf_granularity?(granularity)
  raise Wavefront::Exception::InvalidTimestamp if t_start.nil?

  options[:q] = query
  options[:g] = granularity
  options[:s] = parse_time(t_start, true)
  options[:e] = parse_time(t_end, true) if t_end

  options.delete_if do |k, v|
    v == false && k != :i
  end

  api_get('api', options)
end

#raw(metric, source = nil, t_start = nil, t_end = nil) ⇒ Object

GET /api/v2/chart/raw Perform a raw data query against Wavefront servers that returns second granularity points grouped by tags

Parameters:

  • metric (String)

    metric to query ingested points for (cannot contain wildcards)

  • source (String) (defaults to: nil)

    source to query ingested points for (cannot contain wildcards).

  • t_start (Time, Integer) (defaults to: nil)

    start time of window: defaults to one hour before t_end

  • t_end (Time, Integer) (defaults to: nil)

    end time of window: defaults to now

Raises:

  • (ArgumentError)


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/wavefront-sdk/query.rb', line 63

def raw(metric, source = nil, t_start = nil, t_end = nil)
  raise ArgumentError unless metric.is_a?(String)

  options = { metric: metric, }

  if source
    wf_source_id?(source)
    options[:source] = source
  end

  options[:startTime] = parse_time(t_start, true) if t_start
  options[:endTime] = parse_time(t_end, true) if t_end

  api_get('raw', options)
end

#response_shim(body, status) ⇒ Object

Fake a response which looks like we get from all the other paths. The default response is a single array.



82
83
84
85
86
87
88
# File 'lib/wavefront-sdk/query.rb', line 82

def response_shim(body, status)
  { response: JSON.parse(body),
    status:   { result:  status == 200 ? 'OK' : 'ERROR',
                message: '',
                code:    status },
  }.to_json
end