Class: Dogapi::V1::MetricService

Inherits:
APIService show all
Defined in:
lib/dogapi/v1/metric.rb

Overview

Event-specific client affording more granular control than the simple Dogapi::Client

Constant Summary collapse

API_VERSION =
'v1'

Instance Attribute Summary

Attributes inherited from APIService

#api_key, #application_key

Instance Method Summary collapse

Methods inherited from APIService

#connect, #handle_redirect, #handle_response, #initialize, #prepare_params, #prepare_request, #request, #should_set_api_and_app_keys_in_params?, #suppress_error_if_silent

Constructor Details

This class inherits a constructor from Dogapi::APIService

Instance Method Details

#flush_bufferObject



40
41
42
43
44
# File 'lib/dogapi/v1/metric.rb', line 40

def flush_buffer()
  payload = @buffer
  @buffer = nil
  self.upload(payload)
end

#get(query, from, to) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/dogapi/v1/metric.rb', line 13

def get(query, from, to)
  extra_params = {
    from: from.to_i,
    to: to.to_i,
    query: query
  }
  request(Net::HTTP::Get, '/api/' + API_VERSION + '/query', extra_params, nil, false)
end

#get_active_metrics(from) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/dogapi/v1/metric.rb', line 89

def get_active_metrics(from)
  params = {
    from: from.to_i
  }

  request(Net::HTTP::Get, '/api/' + API_VERSION + '/metrics', params, nil, false)
end

#make_metric_payload(metric, points, scope, options) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/dogapi/v1/metric.rb', line 62

def make_metric_payload(metric, points, scope, options)
  begin
    typ = options[:type] || 'gauge'

    if typ != 'gauge' && typ != 'counter' && typ != 'count' && typ != 'rate'
      raise ArgumentError, 'metric type must be gauge or counter or count or rate'
    end

    metric_payload = {
      :metric => metric,
      :points => points,
      :type => typ,
      :host => scope.host,
      :device => scope.device
    }

    # Add tags if there are any
    if not options[:tags].nil?
      metric_payload[:tags] = options[:tags]
    end

    return metric_payload
  rescue Exception => e
    suppress_error_if_silent e
  end
end

#submit(*args) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/dogapi/v1/metric.rb', line 46

def submit(*args)
  if @buffer
    submit_to_buffer(*args)
  else
    submit_to_api(*args)
  end
end

#submit_to_api(metric, points, scope, options = {}) ⇒ Object



29
30
31
32
# File 'lib/dogapi/v1/metric.rb', line 29

def submit_to_api(metric, points, scope, options= {})
  payload = self.make_metric_payload(metric, points, scope, options)
  self.upload([payload])
end

#submit_to_buffer(metric, points, scope, options = {}) ⇒ Object



34
35
36
37
38
# File 'lib/dogapi/v1/metric.rb', line 34

def submit_to_buffer(metric, points, scope, options= {})
  payload = self.make_metric_payload(metric, points, scope, options)
  @buffer << payload
  return 200, {}
end

#switch_to_batchedObject



54
55
56
# File 'lib/dogapi/v1/metric.rb', line 54

def switch_to_batched()
  @buffer = Array.new
end

#switch_to_singleObject



58
59
60
# File 'lib/dogapi/v1/metric.rb', line 58

def switch_to_single()
  @buffer = nil
end

#upload(metrics) ⇒ Object



22
23
24
25
26
27
# File 'lib/dogapi/v1/metric.rb', line 22

def upload(metrics)
  body = {
    :series => metrics
  }
  request(Net::HTTP::Post, '/api/' + API_VERSION + '/series', nil, body, true, false)
end