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_account_id?, #wf_alert_id?, #wf_alert_severity?, #wf_apitoken_id?, #wf_aws_external_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_ingestionpolicy_id?, #wf_integration_id?, #wf_link_id?, #wf_link_template?, #wf_maintenance_window_id?, #wf_message_id?, #wf_metric_name?, #wf_metricspolicy_id?, #wf_monitoredapplication_id?, #wf_monitoredcluster_id?, #wf_ms_ts?, #wf_name?, #wf_notificant_id?, #wf_permission?, #wf_point?, #wf_point_tag?, #wf_point_tags?, #wf_proxy_id?, #wf_role_id?, #wf_sampling_value?, #wf_savedsearch_entity?, #wf_savedsearch_id?, #wf_serviceaccount_id?, #wf_source_id?, #wf_spansamplingpolicy_id?, #wf_string?, #wf_tag?, #wf_trace?, #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



10
11
12
# File 'lib/wavefront-sdk/query.rb', line 10

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.



114
115
116
117
118
# File 'lib/wavefront-sdk/query.rb', line 114

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.

Returns:

  • (Array)

    [parsed body of response, error_message]. One or the other



104
105
106
107
108
# File 'lib/wavefront-sdk/query.rb', line 104

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.

rubocop:disable Metrics/ParameterLists

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



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

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

  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)


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

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.



88
89
90
91
92
93
94
95
96
97
# File 'lib/wavefront-sdk/query.rb', line 88

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