Class: Wavefront::Query

Inherits:
CoreApi show all
Defined in:
lib/wavefront-sdk/query.rb

Overview

Query Wavefront metrics.

Instance Attribute Summary

Attributes inherited from CoreApi

#api, #creds, #logger, #opts, #update_keys

Instance Method Summary collapse

Methods inherited from CoreApi

#api_path, #hash_for_update, #initialize, #setup_api, #time_to_ms

Methods included from Mixins

#log, #parse_relative_time, #parse_time, #relative_time, #time_multiplier, #valid_relative_time?

Methods included from Validators

#uuid?, #wf_alert_id?, #wf_alert_severity?, #wf_apitoken_id?, #wf_cloudintegration_id?, #wf_dashboard_id?, #wf_derivedmetric_id?, #wf_distribution?, #wf_distribution_count?, #wf_distribution_interval?, #wf_distribution_values?, #wf_epoch?, #wf_event_id?, #wf_granularity?, #wf_integration_id?, #wf_link_id?, #wf_link_template?, #wf_maintenance_window_id?, #wf_message_id?, #wf_metric_name?, #wf_ms_ts?, #wf_name?, #wf_notificant_id?, #wf_point?, #wf_point_tag?, #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_usergroup_id?, #wf_value?, #wf_version?, #wf_webhook_id?

Constructor Details

This class inherits a constructor from Wavefront::CoreApi

Instance Method Details

#api_baseObject



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

def api_base
  'chart'
end

#extract_error_message(body) ⇒ Object

There ought to be a message= block in the response, but sometimes there isn’t. So far it seems that in this second case, the message is on its own line.



110
111
112
113
114
# File 'lib/wavefront-sdk/query.rb', line 110

def extract_error_message(body)
  body.match(/message='([^']+)'/).captures.first
rescue StandardError
  body.lines.last.strip
end

#parsed_response(body) ⇒ Array

A bad query doesn’t send back a JSON object. It sends back a string with an embedded message.



100
101
102
103
104
# File 'lib/wavefront-sdk/query.rb', line 100

def parsed_response(body)
  [JSON.parse(body), '']
rescue JSON::ParserError
  ['', extract_error_message(body)]
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.

Raises:

  • (ArgumentError)

    if query is not a string



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# 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 { |k, v| v == false && k != :i }
  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

Raises:

  • (ArgumentError)


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

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.

I don’t know if something has changed in the API, but sending a complete nonsense query like ‘st(“some.series”)’ returns an error message, but with a 200 code. So we fudge a 400 if we see a message.



84
85
86
87
88
89
90
91
92
93
# File 'lib/wavefront-sdk/query.rb', line 84

def response_shim(body, status)
  resp, err_msg = parsed_response(body)

  status = 400 if status == 200 && !err_msg.empty?

  { response: resp,
    status:   { result:  status == 200 ? 'OK' : 'ERROR',
                message: err_msg,
                code:    status } }.to_json
end