Class: DogapiDemo::V1::MetricService

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

Overview

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

Constant Summary collapse

API_VERSION =
"v1"

Instance Method Summary collapse

Methods inherited from APIService

#connect, #initialize, #request, #suppress_error_if_silent

Constructor Details

This class inherits a constructor from DogapiDemo::APIService

Instance Method Details

#flush_bufferObject



62
63
64
65
66
# File 'lib/dogapi-demo/v1/metric.rb', line 62

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

#get(query, from, to) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/dogapi-demo/v1/metric.rb', line 11

def get(query, from, to)
  begin
    params = {
      :api_key => @api_key,
      :application_key => @application_key,

      from: from.to_i,
      to: to.to_i,
      query: query
    }
    request(Net::HTTP::Get, '/api/' + API_VERSION + '/query', params, nil, false)
  rescue Exception => e
    if @silent
      warn e
      return -1, {}
    else
      raise e
    end
  end
end

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



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/dogapi-demo/v1/metric.rb', line 84

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

    if typ != "gauge" && typ != "counter"
      raise ArgumentError, "metric type must be gauge or counter"
    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
    if @silent
      warn e
      return -1, {}
    else
      raise e
    end
  end
end

#submit(*args) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/dogapi-demo/v1/metric.rb', line 68

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

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



51
52
53
54
# File 'lib/dogapi-demo/v1/metric.rb', line 51

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



56
57
58
59
60
# File 'lib/dogapi-demo/v1/metric.rb', line 56

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



76
77
78
# File 'lib/dogapi-demo/v1/metric.rb', line 76

def switch_to_batched()
  @buffer = Array.new
end

#switch_to_singleObject



80
81
82
# File 'lib/dogapi-demo/v1/metric.rb', line 80

def switch_to_single()
  @buffer = nil
end

#upload(metrics) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/dogapi-demo/v1/metric.rb', line 32

def upload(metrics)
  begin
    params = {
      :api_key => @api_key
    }
    body = {
      :series => metrics
    }
    request(Net::HTTP::Post, '/api/' + API_VERSION + '/series', params, body, true)
  rescue Exception => e
    if @silent
      warn e
      return -1, {}
    else
      raise e
    end
  end
end