dogapi 1.1.0 → 1.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,20 @@
1
- = Ruby client for Datadog API v1.1.0
1
+ = Ruby client for Datadog API v1.2.1
2
2
 
3
3
  The Ruby client is a library suitable for inclusion in existing Ruby projects or for development of standalone scripts. It provides an abstraction on top of Datadog's raw HTTP interface for reporting events and metrics.
4
4
 
5
+ = What's new?
6
+
7
+ == v1.2.x
8
+
9
+ * You can now manage host tags
10
+ * You can now get event details and query the stream in addition to posting events
11
+ * Functionality relating to events with a duration has been deprecated
12
+ * The underlying clients have been updated to use Datadog's new public HTTP API[https://github.com/DataDog/dogapi/wiki]
13
+
14
+ == v1.1.x
15
+
16
+ * You do not need to use environment variables anymore to use the client.
17
+
5
18
  = Installation
6
19
 
7
20
  == From Source
@@ -20,7 +33,7 @@ Gem page: https://rubygems.org/gems/dogapi
20
33
 
21
34
  = Usage
22
35
 
23
- == How to find your API Key
36
+ == How to find your API and application keys
24
37
 
25
38
  Go to your setup page[https://app.datadoghq.com/account/settings].
26
39
 
@@ -34,62 +47,66 @@ on a host, simply specify the device when calling emit functions.
34
47
 
35
48
  == Submit an event to Datadog
36
49
 
37
- === If the event has no duration
38
50
 
39
51
  require 'rubygems'
40
52
  require 'dogapi'
41
53
 
42
54
  api_key = "abcdef123456"
43
55
 
56
+ # submitting events doesn't require an application_key, so we don't bother setting it
44
57
  dog = Dogapi::Client.new(api_key)
45
58
 
46
- dog.emit_event(Dogapi::Event.new('Testing done, FTW'), host => "my_host")
59
+ dog.emit_event(Dogapi::Event.new('Testing done, FTW'), :host => "my_host")
60
+
61
+
62
+ == Tag a host in Datadog
47
63
 
48
- === If the event has a duration
49
64
 
50
65
  require 'rubygems'
51
66
  require 'dogapi'
52
67
 
53
68
  api_key = "abcdef123456"
69
+ application_key = "fedcba654321"
54
70
 
55
- dog = Dogapi::Client.new(api_key)
71
+ dog = Dogapi::Client.new(api_key, application_key)
72
+
73
+ dog.add_tags("my_host", ["tagA", "tagB"])
56
74
 
57
- dog.start_event(Dogapi::Event.new('My event with a duration'), host => "my_host") do
58
- # do your work here...
59
- # e.g. sleep 1
60
- end
61
- # stop_event will be sent automatically
62
75
 
63
76
  == Submit a metric to Datadog
64
77
 
65
- You want to track a new metric called +some.metric.name+ and have just sampled it from +my_device+ on +my_host+.
78
+ You want to track a new metric called +some+.+metric+.+name+ and have just sampled it from +my_device+ on +my_host+.
66
79
  Its value is 50. Here is how you submit the value to Datadog.
67
80
 
81
+
68
82
  require 'rubygems'
69
83
  require 'dogapi'
70
84
 
71
85
  api_key = "abcdef123456"
72
86
 
87
+ # submitting metrics doesn't require an application_key, so we don't bother setting it
73
88
  dog = Dogapi::Client.new(api_key)
74
89
 
75
- dog.emit_point 'some.metric.name', 50.0, host => "my_host", device => "my_device"
90
+ dog.emit_point('some.metric.name', 50.0, :host => "my_host", :device => "my_device")
91
+
76
92
 
77
93
  Let us now assume that you have sampled the metric multiple times and you would like to submit the results.
78
94
  You can use the +emit_points+ method (instead of +emit_point+). Since you are submitting more than one
79
95
  data point you will need to pass a list of +Time+, +float+ pairs, instead of a simple +float+ value.
80
96
 
97
+
81
98
  require 'rubygems'
82
99
  require 'dogapi'
83
100
 
84
- = Actual sampling takes place
101
+ # Actual sampling takes place
85
102
  t1 = Time.now
86
103
  val1 = 50.0
87
104
 
88
- = some time elapses
105
+ # some time elapses
89
106
  t2 = Time.now
90
107
  val2 = 51.0
91
108
 
92
- = some more time elapses
109
+ # some more time elapses
93
110
  t3 = Time.now
94
111
  val3 = -60.0
95
112
 
@@ -97,4 +114,4 @@ data point you will need to pass a list of +Time+, +float+ pairs, instead of a s
97
114
 
98
115
  dog = Dogapi::Client.new(api_key)
99
116
 
100
- dog.emit_points 'some.metric.name', [[t1, val1], [t2, val2], [t3, val3]], host => "my_host", device => "my_device"
117
+ dog.emit_points('some.metric.name', [[t1, val1], [t2, val2], [t3, val3]], :host => "my_host", :device => "my_device")
@@ -18,15 +18,17 @@ module Dogapi
18
18
  end
19
19
  end
20
20
 
21
- # Superclass that deals with the details of communicating with the DataDog API
21
+ # <b>DEPRECATED:</b> Going forward, use the newer APIService.
22
22
  class Service
23
+ # <b>DEPRECATED:</b> Going forward, use the newer APIService.
23
24
  def initialize(api_key, api_host=Dogapi.find_datadog_host)
24
25
  @api_key = api_key
25
26
  @host = api_host
26
27
  end
27
28
 
28
- # Manages the HTTP connection
29
+ # <b>DEPRECATED:</b> Going forward, use the newer APIService.
29
30
  def connect
31
+ warn "[DEPRECATION] Dogapi::Service has been deprecated in favor of the newer V1 services"
30
32
  uri = URI.parse(@host)
31
33
  session = Net::HTTP.new(uri.host, uri.port)
32
34
  if 'https' == uri.scheme
@@ -41,12 +43,9 @@ module Dogapi
41
43
  end
42
44
  end
43
45
 
44
- # Prepares the request and handles the response
45
- #
46
- # +method+ is an implementation of Net::HTTP::Request (e.g. Net::HTTP::Post)
47
- #
48
- # +params+ is a Hash that will be converted to request parameters
46
+ # <b>DEPRECATED:</b> Going forward, use the newer APIService.
49
47
  def request(method, url, params)
48
+ warn "[DEPRECATION] Dogapi::Service has been deprecated in favor of the newer V1 services"
50
49
  if !params.has_key? :api_key
51
50
  params[:api_key] = @api_key
52
51
  end
@@ -72,6 +71,69 @@ module Dogapi
72
71
  end
73
72
  end
74
73
 
74
+ # Superclass that deals with the details of communicating with the DataDog API
75
+ class APIService
76
+ def initialize(api_key, application_key)
77
+ @api_key = api_key
78
+ @application_key = application_key
79
+ @api_host = Dogapi.find_datadog_host()
80
+ end
81
+
82
+ # Manages the HTTP connection
83
+ def connect
84
+ uri = URI.parse(@api_host)
85
+ session = Net::HTTP.new(uri.host, uri.port)
86
+ if 'https' == uri.scheme
87
+ session.use_ssl = true
88
+ # FIXME - turn off SSL verification for now until we can spend
89
+ # some time figuring out how to find certs across platforms.
90
+ # - matt 10/06/2011
91
+ session.verify_mode = OpenSSL::SSL::VERIFY_NONE
92
+ end
93
+ session.start do |conn|
94
+ yield(conn)
95
+ end
96
+ end
97
+
98
+ # Prepares the request and handles the response
99
+ #
100
+ # +method+ is an implementation of Net::HTTP::Request (e.g. Net::HTTP::Post)
101
+ #
102
+ # +params+ is a Hash that will be converted to request parameters
103
+ def request(method, url, params, body, send_json)
104
+ resp_obj = nil
105
+ resp = nil
106
+ connect do |conn|
107
+ if params and params.size > 0
108
+ qs_params = params.map { |k,v| k.to_s + "=" + v.to_s }
109
+ qs = "?" + qs_params.join("&")
110
+ url = url + qs
111
+ end
112
+
113
+ req = method.new(url)
114
+
115
+ if send_json
116
+ req.content_type = 'application/json'
117
+ req.body = JSON.generate(body)
118
+ end
119
+
120
+ resp = conn.request(req)
121
+ resp_str = resp.body
122
+
123
+ if resp.code != 204 and resp.body != '' and resp.body != 'null' and resp.body != nil
124
+ begin
125
+ resp_obj = JSON.parse(resp.body)
126
+ rescue
127
+ raise 'Invalid JSON Response: ' + resp.body
128
+ end
129
+ else
130
+ resp_obj = {}
131
+ end
132
+ end
133
+ return resp.code, resp_obj
134
+ end
135
+ end
136
+
75
137
  def Dogapi.find_datadog_host
76
138
  # allow env-based overriding, useful for tests
77
139
  ENV["DATADOG_HOST"] || "https://app.datadoghq.com"
@@ -15,6 +15,9 @@ module Dogapi
15
15
  :event_object,
16
16
  :msg_title,
17
17
  :msg_text,
18
+ :priority,
19
+ :parent,
20
+ :tags,
18
21
  :json_payload
19
22
 
20
23
  # Optional arguments:
@@ -25,6 +28,9 @@ module Dogapi
25
28
  # :event_type => String
26
29
  # :event_object => String
27
30
  # :msg_title => String
31
+ # :priority => String
32
+ # :parent => event ID (integer)
33
+ # :tags => array of Strings
28
34
  # :json_payload => String
29
35
  def initialize(msg_text, options={})
30
36
  defaults = {
@@ -35,6 +41,9 @@ module Dogapi
35
41
  :event_type => '',
36
42
  :event_object => '',
37
43
  :msg_title => '',
44
+ :priority => "normal",
45
+ :parent => nil,
46
+ :tags => [],
38
47
  :json_payload => ''
39
48
  }
40
49
  options = defaults.merge(options)
@@ -47,17 +56,23 @@ module Dogapi
47
56
  @event_type = options[:event_type]
48
57
  @event_object = options[:event_object]
49
58
  @msg_title = options[:msg_title]
59
+ @priority = options[:priority]
60
+ @parent = options[:parent]
61
+ @tags = options[:tags]
50
62
  @json_payload = options[:json_payload]
51
63
  end
52
64
  end
53
65
 
54
- # Event-specific client affording more granular control than the simple Dogapi::Client
66
+ # <b>DEPRECATED:</b> Going forward, use the V1 services. This legacy service will be
67
+ # removed in an upcoming release.
55
68
  class EventService < Dogapi::Service
56
69
 
57
70
  API_VERSION = "1.0.0"
58
71
 
59
- # Records an Event with no duration
72
+ # <b>DEPRECATED:</b> Going forward, use the V1 services. This legacy service will be
73
+ # removed in an upcoming release.
60
74
  def submit(api_key, event, scope=nil, source_type=nil)
75
+ warn "[DEPRECATION] Dogapi::EventService.submit() has been deprecated in favor of the newer V1 services"
61
76
  scope = scope || Dogapi::Scope.new()
62
77
  params = {
63
78
  :api_key => api_key,
@@ -84,12 +99,10 @@ module Dogapi
84
99
  request Net::HTTP::Post, '/event/submit', params
85
100
  end
86
101
 
87
- # Manages recording an event with a duration
88
- #
89
- # 0. The start time is recorded immediately
90
- # 0. The given block is executed with access to the response of the start request
91
- # 0. The end time is recorded once the block completes execution
102
+ # <b>DEPRECATED:</b> Going forward, use the V1 services. This legacy service will be
103
+ # removed in an upcoming release.
92
104
  def start(api_key, event, scope, source_type=nil)
105
+ warn "[DEPRECATION] Dogapi::EventService.start() has been deprecated in favor of the newer V1 services"
93
106
  response = submit api_key, event, scope, source_type
94
107
  success = nil
95
108
 
@@ -1,15 +1,17 @@
1
1
  require 'time'
2
- require 'dogapi/metric'
2
+ require 'dogapi/v1'
3
3
 
4
4
  module Dogapi
5
5
 
6
6
  # A simple DogAPI client
7
7
  #
8
- # See Dogapi::EventService and Dogapi::MetricService for the thick underlying clients
8
+ # See Dogapi::V1 for the thick underlying clients
9
+ #
10
+ # Class methods return a tuple of (+response_code+, +response_body+). Unless otherwise noted, the response body is deserialized JSON. Up-to-date information about the JSON object structure is available in the HTTP API documentation, here[https://github.com/DataDog/dogapi/wiki].
9
11
  class Client
10
12
 
11
13
  # Create a new Client optionally specifying a default host and device
12
- def initialize(api_key, host=nil, device=nil)
14
+ def initialize(api_key, application_key=nil, host=nil, device=nil)
13
15
 
14
16
  if api_key
15
17
  @api_key = api_key
@@ -17,15 +19,23 @@ module Dogapi
17
19
  raise 'Please provide an API key to submit your data'
18
20
  end
19
21
 
22
+ @application_key = application_key
23
+
20
24
  @datadog_host = Dogapi.find_datadog_host()
21
25
 
22
26
  @host = host
23
27
  @device = device
24
28
 
25
- @metric_svc = Dogapi::MetricService.new(@datadog_host)
26
- @event_svc = Dogapi::EventService.new(@datadog_host)
29
+ @metric_svc = Dogapi::V1::MetricService.new(@api_key, @application_key)
30
+ @event_svc = Dogapi::V1::EventService.new(@api_key, @application_key)
31
+ @tag_svc = Dogapi::V1::TagService.new(@api_key, @application_key)
32
+
33
+ @legacy_event_svc = Dogapi::EventService.new(@datadog_host)
27
34
  end
28
35
 
36
+ #
37
+ # METRICS
38
+
29
39
  # Record a single point of metric data
30
40
  #
31
41
  # Optional arguments:
@@ -36,10 +46,10 @@ module Dogapi
36
46
  defaults = {:timestamp => Time.now, :host => nil, :device => nil}
37
47
  options = defaults.merge(options)
38
48
 
39
- self.emit_points metric,
49
+ self.emit_points(metric,
40
50
  [[options[:timestamp], value]],
41
51
  :host => options[:host],
42
- :device => options[:device]
52
+ :device => options[:device])
43
53
  end
44
54
 
45
55
  # Record a set of points of metric data
@@ -57,52 +67,105 @@ module Dogapi
57
67
 
58
68
  points.each do |p|
59
69
  p[0].kind_of? Time or raise "Not a Time"
60
- p[1].to_f # TODO: stupid to_f will never raise and exception
70
+ p[0] = p[0].to_i
71
+ p[1] = p[1].to_f # TODO: stupid to_f will never raise an exception
61
72
  end
62
73
 
63
- @metric_svc.submit @api_key, scope, metric, points
74
+ @metric_svc.submit(metric, points, scope)
64
75
  end
65
76
 
66
- # Record an event with no duration
67
77
  #
68
- # If <tt>:source_type</tt> is unset, a generic "system" event will be reported
78
+ # EVENTS
79
+
80
+ # Record an event
69
81
  #
70
82
  # Optional arguments:
71
83
  # :host => String
72
84
  # :device => String
73
- # :source_type => String
74
85
  def emit_event(event, options={})
75
- defaults = {:host => nil, :device => nil, :source_type => nil}
86
+ defaults = {:host => nil, :device => nil}
76
87
  options = defaults.merge(options)
77
88
 
78
- scope = override_scope options[:host], options[:device]
89
+ scope = override_scope(options[:host], options[:device])
90
+
91
+ @event_svc.post(event, scope)
92
+ end
79
93
 
80
- @event_svc.submit(@api_key, event, scope, options[:source_type])
94
+ # Get the details of an event
95
+ #
96
+ # +id+ of the event to get
97
+ def get_event(id)
98
+ @event_svc.get(id)
81
99
  end
82
100
 
83
- # Record an event with a duration
101
+ # Get an optionally filtered event stream
84
102
  #
85
- # 0. The start time is recorded immediately
86
- # 0. The given block is executed
87
- # 0. The end time is recorded once the block completes execution
103
+ # +start+ is a Time object for when to start the stream
88
104
  #
89
- # If <tt>:source_type</tt> is unset, a generic "system" event will be reported
105
+ # +stop+ is a Time object for when to end the stream
90
106
  #
91
107
  # Optional arguments:
92
- # :host => String
93
- # :device => String
94
- # :source_type => String
108
+ # :priority => "normal" or "low"
109
+ # :sources => String, see https://github.com/DataDog/dogapi/wiki/Event for a current list of sources
110
+ # :tags => Array of Strings
111
+ def stream(start, stop, options={})
112
+ @event_svc.stream(start, stop, options)
113
+ end
114
+
115
+ # <b>DEPRECATED:</b> Recording events with a duration has been deprecated.
116
+ # The functionality will be removed in a later release.
95
117
  def start_event(event, options={})
118
+ warn "[DEPRECATION] Dogapi::Client.start_event() is deprecated. Use `emit_event` instead."
96
119
  defaults = {:host => nil, :device => nil, :source_type => nil}
97
120
  options = defaults.merge(options)
98
121
 
99
122
  scope = override_scope options[:host], options[:device]
100
123
 
101
- @event_svc.start(@api_key, event, scope, options[:source_type]) do
124
+ @legacy_event_svc.start(@api_key, event, scope, options[:source_type]) do
102
125
  yield
103
126
  end
104
127
  end
105
128
 
129
+ #
130
+ # TAGS
131
+
132
+ # Get all tags and their associated hosts at your org
133
+ def all_tags()
134
+ @tag_svc.get_all()
135
+ end
136
+
137
+ # Get all tags for the given host
138
+ #
139
+ # +host_id+ can be the host's numeric id or string name
140
+ def host_tags(host_id)
141
+ @tag_svc.get(host_id)
142
+ end
143
+
144
+ # Add the tags to the given host
145
+ #
146
+ # +host_id+ can be the host's numeric id or string name
147
+ #
148
+ # +tags+ is and Array of Strings
149
+ def add_tags(host_id, tags)
150
+ @tag_svc.add(host_id, tags)
151
+ end
152
+
153
+ # Replace the tags on the given host
154
+ #
155
+ # +host_id+ can be the host's numeric id or string name
156
+ #
157
+ # +tags+ is and Array of Strings
158
+ def update_tags(host_id, tags)
159
+ @tag_svc.update(host_id, tags)
160
+ end
161
+
162
+ # Remove all tags from the given host
163
+ #
164
+ # +host_id+ can be the host's numeric id or string name
165
+ def detatch_tags(host_id)
166
+ @tag_svc.detatch(host_id)
167
+ end
168
+
106
169
  private
107
170
 
108
171
  def override_scope(host, device)
@@ -5,12 +5,16 @@ require 'json'
5
5
 
6
6
  module Dogapi
7
7
 
8
+ # <b>DEPRECATED:</b> Going forward, use the V1 services. This legacy service will be
9
+ # removed in an upcoming release.
8
10
  class MetricService < Dogapi::Service
9
11
 
10
12
  API_VERSION = "1.0.0"
11
13
 
12
- # Records some number of points for a given metric
14
+ # <b>DEPRECATED:</b> Going forward, use the V1 services. This legacy service will be
15
+ # removed in an upcoming release.
13
16
  def submit(api_key, scope, metric, points)
17
+ warn "[DEPRECATION] Dogapi::MetricService.submit() has been deprecated in favor of the newer V1 services"
14
18
  series = [{
15
19
  :host => scope.host,
16
20
  :device => scope.device,
@@ -0,0 +1,3 @@
1
+ require 'dogapi/v1/event'
2
+ require 'dogapi/v1/metric'
3
+ require 'dogapi/v1/tag'
@@ -0,0 +1,71 @@
1
+ require 'dogapi'
2
+
3
+ module Dogapi
4
+ class V1 # for namespacing
5
+
6
+ # Event-specific client affording more granular control than the simple Dogapi::Client
7
+ class EventService < Dogapi::APIService
8
+
9
+ API_VERSION = "v1"
10
+
11
+ # Records an Event with no duration
12
+ def post(event, scope=nil)
13
+ scope = scope || Dogapi::Scope.new()
14
+ params = {
15
+ :api_key => @api_key
16
+ }
17
+
18
+ body = {
19
+ :title => event.msg_title,
20
+ :text => event.msg_text,
21
+ :date_happened => event.date_happened.to_i,
22
+ :priority => event.priority,
23
+ :parent => event.parent,
24
+ :tags => event.tags,
25
+ }
26
+
27
+ request(Net::HTTP::Post, '/api/v1/events', params, body, true)
28
+ end
29
+
30
+ def get(id)
31
+ params = {
32
+ :api_key => @api_key,
33
+ :application_key => @application_key
34
+ }
35
+
36
+ request(Net::HTTP::Get, '/api/' + API_VERSION + '/events/' + id.to_s, params, nil, false)
37
+ end
38
+
39
+ def stream(start, stop, options={})
40
+ defaults = {
41
+ :priority => nil,
42
+ :sources => nil,
43
+ :tags => nil
44
+ }
45
+ options = defaults.merge(options)
46
+
47
+ params = {
48
+ :api_key => @api_key,
49
+ :application_key => @application_key,
50
+
51
+ :start => start.to_i,
52
+ :end => stop.to_i
53
+ }
54
+
55
+ if options[:priority]:
56
+ params[:priority] = options[:priority]
57
+ end
58
+ if options[:sources]:
59
+ params[:sources] = options[:sources]
60
+ end
61
+ if options[:tags]:
62
+ params[:tags] = options[:tags]
63
+ end
64
+
65
+ request(Net::HTTP::Get, '/api/' + API_VERSION + '/events', params, nil, false)
66
+ end
67
+
68
+ end
69
+
70
+ end
71
+ end
@@ -0,0 +1,33 @@
1
+ require 'dogapi'
2
+
3
+ module Dogapi
4
+ class V1 # for namespacing
5
+
6
+ # Event-specific client affording more granular control than the simple Dogapi::Client
7
+ class MetricService < Dogapi::APIService
8
+
9
+ API_VERSION = "v1"
10
+
11
+ # Records an Event with no duration
12
+ def submit(metric, points, scope)
13
+ params = {
14
+ :api_key => @api_key
15
+ }
16
+
17
+ body = { :series => [
18
+ {
19
+ :metric => metric,
20
+ :points => points,
21
+ :type => "gauge",
22
+ :host => scope.host,
23
+ :device => scope.device
24
+ }
25
+ ]
26
+ }
27
+
28
+ request(Net::HTTP::Post, '/api/' + API_VERSION + '/series', params, body, true)
29
+ end
30
+ end
31
+
32
+ end
33
+ end
@@ -0,0 +1,72 @@
1
+ require 'dogapi'
2
+
3
+ module Dogapi
4
+ class V1 # for namespacing
5
+
6
+ #
7
+ class TagService < Dogapi::APIService
8
+
9
+ API_VERSION = "v1"
10
+
11
+ # Gets all tags in your org and the hosts tagged with them
12
+ def get_all()
13
+ params = {
14
+ :api_key => @api_key,
15
+ :application_key => @application_key
16
+ }
17
+
18
+ request(Net::HTTP::Get, '/api/' + API_VERSION + '/tags/hosts', params, nil, false)
19
+ end
20
+
21
+ # Gets all tags for a given host
22
+ def get(host_id)
23
+ params = {
24
+ :api_key => @api_key,
25
+ :application_key => @application_key
26
+ }
27
+
28
+ request(Net::HTTP::Get, '/api/' + API_VERSION + '/tags/hosts/' + host_id.to_s, params, nil, false)
29
+ end
30
+
31
+ # Adds a list of tags to a host
32
+ def add(host_id, tags)
33
+ params = {
34
+ :api_key => @api_key,
35
+ :application_key => @application_key
36
+ }
37
+
38
+ body = {
39
+ :tags => tags
40
+ }
41
+
42
+ request(Net::HTTP::Post, '/api/' + API_VERSION + '/tags/hosts/' + host_id.to_s, params, body, true)
43
+ end
44
+
45
+ # Remove all tags from a host and replace them with a new list
46
+ def update(host_id, tags)
47
+ params = {
48
+ :api_key => @api_key,
49
+ :application_key => @application_key
50
+ }
51
+
52
+ body = {
53
+ :tags => tags
54
+ }
55
+
56
+ request(Net::HTTP::Put, '/api/' + API_VERSION + '/tags/hosts/' + host_id.to_s, params, body, true)
57
+ end
58
+
59
+ # Remove all tags from a host
60
+ def detatch(host_id)
61
+ params = {
62
+ :api_key => @api_key,
63
+ :application_key => @application_key
64
+ }
65
+
66
+ request(Net::HTTP::Delete, '/api/' + API_VERSION + '/tags/hosts/' + host_id.to_s, params, nil, false)
67
+ end
68
+
69
+ end
70
+
71
+ end
72
+ end
@@ -1,37 +1,103 @@
1
1
  require 'test/unit'
2
2
  require 'dogapi'
3
+ require 'time'
3
4
 
4
5
  class TestClient < Test::Unit::TestCase
5
6
 
6
7
  def config_client_test_env
7
- @api_key = ENV['DATADOG_KEY']
8
+ @api_key = ENV['DATADOG_API_KEY']
9
+ @app_key = ENV['DATADOG_APP_KEY']
10
+ if not @api_key or not @app_key
11
+ puts "\n"
12
+ abort "To run tests in your environment, set 'DATADOG_API_KEY' and 'DATADOG_APP_KEY' to appropriate values for your account. Be aware that the tests will submit data, some of which won't be removed at the end.\n\n"
13
+ end
8
14
  end
9
15
 
10
16
  def setup
11
- config_client_test_env
17
+ config_client_test_env()
12
18
  end
13
19
 
14
- def test_simple_client
15
- dog = Dogapi::Client.new(@api_key)
20
+ def test_tags
21
+ hostname = 'test.tag.host'
22
+ dog = Dogapi::Client.new(@api_key, @app_key)
16
23
 
17
- dog.emit_point 'test.dogclient.emit_point',
18
- 999,
19
- :host => 'test.dogclient.fake',
20
- :device => 'eth0'
24
+ # post a metric to make sure the test host context exists
25
+ dog.emit_point('test.tag.metric', 1, :host => hostname)
21
26
 
22
- dog.emit_points 'test.dogclient.emit_points',
23
- [[Time.now - 60, 10], [Time.now - 30, 20], [Time.now, 30]],
24
- :host => 'test.dogclient.fake',
25
- :device => 'eth0'
27
+ dog.all_tags()
26
28
 
27
- my_event = Dogapi::Event.new('Testing Event API')
28
- dog.emit_event my_event,
29
- :host => 'test.dogclient.fake',
30
- :device => 'eth0'
29
+ dog.detatch_tags(hostname)
30
+ code, resp = dog.host_tags(hostname)
31
+ assert resp["tags"].size == 0
31
32
 
32
- my_event = Dogapi::Event.new('Testing Event API with a duration')
33
- dog.start_event(my_event, :host =>'test.dogclient.fake', :device => 'eth0') do
34
- sleep 1
35
- end
33
+ dog.add_tags(hostname, ['test.tag.1', 'test.tag.2'])
34
+ code, resp = dog.host_tags(hostname)
35
+ new_tags = resp["tags"]
36
+ assert new_tags.size == 2
37
+ assert new_tags.include?('test.tag.1')
38
+ assert new_tags.include?('test.tag.2')
39
+
40
+ dog.add_tags(hostname, ['test.tag.3'])
41
+ code, resp = dog.host_tags(hostname)
42
+ new_tags = resp["tags"]
43
+ assert new_tags.size == 3
44
+ assert new_tags.include?('test.tag.1')
45
+ assert new_tags.include?('test.tag.2')
46
+ assert new_tags.include?('test.tag.3')
47
+
48
+ dog.update_tags(hostname, ['test.tag.4'])
49
+ code, resp = dog.host_tags(hostname)
50
+ new_tags = resp["tags"]
51
+ assert new_tags.size == 1
52
+ assert new_tags.include?('test.tag.4')
53
+
54
+ dog.detatch_tags(hostname)
55
+ code, resp = dog.host_tags(hostname)
56
+ assert resp["tags"].size == 0
36
57
  end
58
+
59
+ def test_events
60
+ now = Time.now()
61
+
62
+ now_ts = now
63
+ now_title = 'end test title ' + now_ts.to_i.to_s
64
+ now_message = 'test message ' + now_ts.to_i.to_s
65
+
66
+ before_ts = (now - 5*60)
67
+ before_title = 'start test title ' + before_ts.to_i.to_s
68
+ before_message = 'test message ' + before_ts.to_i.to_s
69
+
70
+ dog = Dogapi::Client.new(@api_key, @app_key)
71
+ dog_r = Dogapi::Client.new(@api_key)
72
+
73
+ code, resp = dog_r.emit_event(Dogapi::Event.new(now_message, :msg_title =>now_title, :date_happened => now_ts))
74
+ now_event_id = resp["event"]["id"]
75
+ sleep 1
76
+ code, resp = dog_r.emit_event(Dogapi::Event.new(before_message, :msg_title =>before_title, :date_happened => before_ts))
77
+ before_event_id = resp["event"]["id"]
78
+
79
+ code, resp = dog.stream(before_ts, now_ts + 1)
80
+ stream = resp["events"]
81
+
82
+ assert stream.last['title'] == before_title
83
+ assert stream.first['title'] == now_title
84
+
85
+ code, resp = dog.get_event(now_event_id)
86
+ now_event = resp['event']
87
+ code, resp = dog.get_event(before_event_id)
88
+ before_event = resp['event']
89
+
90
+ assert now_event['text'] == now_message
91
+ assert before_event['text'] == before_message
92
+ end
93
+
94
+ def test_metrics
95
+ # FIXME: actually verify this once there's a way to look at metrics through the api
96
+ dog = Dogapi::Client.new(@api_key, @app_key)
97
+ dog_r = Dogapi::Client.new(@api_key)
98
+
99
+ dog_r.emit_point('test.metric.metric', 10, :host => 'test.metric.host')
100
+ dog_r.emit_points('test.metric.metric', [[Time.now-5*60, 0]], :host => 'test.metric.host')
101
+ end
102
+
37
103
  end
@@ -1,6 +1,3 @@
1
1
  require 'test/unit'
2
2
 
3
- require 'tc_env'
4
3
  require 'tc_client'
5
- require 'tc_event'
6
- require 'tc_metric'
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dogapi
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
4
+ hash: 29
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
+ - 2
8
9
  - 1
9
- - 0
10
- version: 1.1.0
10
+ version: 1.2.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Datadog, Inc.
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-10-06 00:00:00 Z
18
+ date: 2011-10-27 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: json
@@ -46,11 +46,12 @@ files:
46
46
  - lib/dogapi/event.rb
47
47
  - lib/dogapi/facade.rb
48
48
  - lib/dogapi/metric.rb
49
+ - lib/dogapi/v1/event.rb
50
+ - lib/dogapi/v1/metric.rb
51
+ - lib/dogapi/v1/tag.rb
52
+ - lib/dogapi/v1.rb
49
53
  - lib/dogapi.rb
50
54
  - tests/tc_client.rb
51
- - tests/tc_env.rb
52
- - tests/tc_event.rb
53
- - tests/tc_metric.rb
54
55
  - tests/ts_dogapi.rb
55
56
  - README.rdoc
56
57
  homepage: http://datadoghq.com/
@@ -87,7 +88,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
87
88
  requirements: []
88
89
 
89
90
  rubyforge_project:
90
- rubygems_version: 1.8.6
91
+ rubygems_version: 1.8.11
91
92
  signing_key:
92
93
  specification_version: 3
93
94
  summary: Ruby bindings for Datadog's API
@@ -1,23 +0,0 @@
1
- require 'test/unit'
2
- require 'dogapi'
3
-
4
- class TestEnvironment < Test::Unit::TestCase
5
-
6
- def setup
7
- @host = ENV['DATADOG_HOST']
8
- end
9
-
10
- def teardown
11
- ENV['DATADOG_HOST'] = @host
12
- end
13
-
14
- def test_unset
15
- ENV['DATADOG_HOST'] = nil
16
- assert_equal "https://app.datadoghq.com", Dogapi.find_datadog_host
17
- end
18
-
19
- def test_set
20
- ENV['DATADOG_HOST'] = 'test.host'
21
- assert_equal 'test.host', Dogapi.find_datadog_host
22
- end
23
- end
@@ -1,35 +0,0 @@
1
- require 'test/unit'
2
- require 'dogapi'
3
-
4
- class TestEventClient < Test::Unit::TestCase
5
-
6
- def config_client_test_env
7
- @api_key = ENV['DATADOG_KEY']
8
- end
9
-
10
- def setup
11
- config_client_test_env
12
- end
13
-
14
- def submit_event(scope=nil, source_type=nil)
15
- event_service = Dogapi::EventService.new(@host)
16
- tok = '#' + (rand(10000)+1).to_s
17
- test_message = 'event_test_' + tok
18
- event = Dogapi::Event.new(test_message, :event_type => 'test-event-type')
19
- res = event_service.submit(@api_key, event, scope, source_type)
20
- assert_not_equal(res['id'], nil)
21
- end
22
-
23
- def test_submit_event
24
- submit_event
25
- end
26
-
27
- def test_submit_scoped_event
28
- submit_event(Dogapi::Scope.new('scoped-testhost', 'testdevice'))
29
- end
30
-
31
- def test_submit_typed_event
32
- submit_event(Dogapi::Scope.new('typed-testhost', 'testdevice'), 'GitHub')
33
- end
34
-
35
- end
@@ -1,29 +0,0 @@
1
- require 'test/unit'
2
- require 'time'
3
- require 'dogapi'
4
-
5
- class TestMetricClient < Test::Unit::TestCase
6
-
7
- def config_client_test_env
8
- @api_key = ENV['DATADOG_KEY']
9
- end
10
-
11
- def setup
12
- config_client_test_env
13
- end
14
-
15
- def test_submit_metric
16
- metric_service = Dogapi::MetricService.new(@host)
17
- scope = Dogapi::Scope.new('test.dogclient.fake', 'eth0')
18
- metric = 'test.dogclient.metric.submit_metric'
19
- points = [
20
- [Time.now-90, 1.0],
21
- [Time.now-60, 2.0],
22
- [Time.now-30, 4.0],
23
- [Time.now, 8.0]
24
- ]
25
- res = metric_service.submit(@api_key, scope, metric, points)
26
- assert_equal(res['status'], 'ok')
27
- end
28
-
29
- end