dogapi 1.3.2 → 1.3.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -1,9 +1,18 @@
1
- = Ruby client for Datadog API v1.3.0
1
+ = Ruby client for Datadog API v1.3.2
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
5
  = What's new?
6
6
 
7
+ == v1.3.2
8
+
9
+ * Support an aggregation key to aggregate events together
10
+
11
+ == v1.3.1
12
+
13
+ * Metrics can be counters, rather than just gauges (thanks to @treeder)
14
+ * Metrics can be tagged.
15
+
7
16
  == v1.3.0
8
17
 
9
18
  * Capistrano integration. See https://github.com/DataDog/dogapi-rb/tree/master/lib/capistrano
@@ -35,6 +44,15 @@ Gem page: https://rubygems.org/gems/dogapi
35
44
 
36
45
  $ gem install dogapi
37
46
 
47
+ If you get a permission error, you might need to run the install process with sudo:
48
+
49
+ $ sudo gem install dogapi
50
+
51
+ If you get a LoadError, missing mkmf, you need to install the development packages for ruby.
52
+
53
+ $ # on ubuntu e.g.
54
+ $ sudo apt-get install ruby-dev
55
+
38
56
  = Usage
39
57
 
40
58
  == How to find your API and application keys
data/lib/dogapi/common.rb CHANGED
@@ -73,10 +73,11 @@ module Dogapi
73
73
 
74
74
  # Superclass that deals with the details of communicating with the DataDog API
75
75
  class APIService
76
- def initialize(api_key, application_key)
76
+ def initialize(api_key, application_key, silent=true)
77
77
  @api_key = api_key
78
78
  @application_key = application_key
79
79
  @api_host = Dogapi.find_datadog_host()
80
+ @silent = silent
80
81
  end
81
82
 
82
83
  # Manages the HTTP connection
data/lib/dogapi/event.rb CHANGED
@@ -52,9 +52,9 @@ module Dogapi
52
52
  # Copy and pasted from the internets
53
53
  # http://stackoverflow.com/a/5031637/25276
54
54
  def to_hash
55
- Hash[instance_variables.map { |var|
56
- [var[1..-1].to_sym, instance_variable_get(var)]
57
- }]
55
+ hash = {}
56
+ instance_variables.each {|var| hash[var[1..-1].to_sym] = instance_variable_get(var) }
57
+ hash
58
58
  end
59
59
  end
60
60
 
data/lib/dogapi/facade.rb CHANGED
@@ -11,7 +11,7 @@ module Dogapi
11
11
  class Client
12
12
 
13
13
  # Create a new Client optionally specifying a default host and device
14
- def initialize(api_key, application_key=nil, host=nil, device=nil)
14
+ def initialize(api_key, application_key=nil, host=nil, device=nil, silent=true)
15
15
 
16
16
  if api_key
17
17
  @api_key = api_key
@@ -26,9 +26,9 @@ module Dogapi
26
26
  @host = host
27
27
  @device = device
28
28
 
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)
29
+ @metric_svc = Dogapi::V1::MetricService.new(@api_key, @application_key, silent)
30
+ @event_svc = Dogapi::V1::EventService.new(@api_key, @application_key, silent)
31
+ @tag_svc = Dogapi::V1::TagService.new(@api_key, @application_key, silent)
32
32
 
33
33
  @legacy_event_svc = Dogapi::EventService.new(@datadog_host)
34
34
  end
@@ -10,59 +10,86 @@ module Dogapi
10
10
 
11
11
  # Records an Event with no duration
12
12
  def post(event, scope=nil)
13
- scope = scope || Dogapi::Scope.new()
14
- params = {
15
- :api_key => @api_key
16
- }
17
-
18
- body = event.to_hash.merge({
19
- :title => event.msg_title,
20
- :text => event.msg_text,
21
- :date_happened => event.date_happened.to_i,
22
- :host => scope.host,
23
- :device => scope.device,
24
- :aggregation_key => event.aggregation_key.to_s
25
- })
13
+ begin
14
+ scope = scope || Dogapi::Scope.new()
15
+ params = {
16
+ :api_key => @api_key
17
+ }
26
18
 
27
- request(Net::HTTP::Post, '/api/v1/events', params, body, true)
19
+ body = event.to_hash.merge({
20
+ :title => event.msg_title,
21
+ :text => event.msg_text,
22
+ :date_happened => event.date_happened.to_i,
23
+ :host => scope.host,
24
+ :device => scope.device,
25
+ :aggregation_key => event.aggregation_key.to_s
26
+ })
27
+
28
+ request(Net::HTTP::Post, '/api/v1/events', params, body, true)
29
+ rescue Exception => e
30
+ if @silent
31
+ warn e
32
+ return -1, {}
33
+ else
34
+ raise e
35
+ end
36
+ end
28
37
  end
29
38
 
30
39
  def get(id)
31
- params = {
32
- :api_key => @api_key,
33
- :application_key => @application_key
34
- }
40
+ begin
41
+ params = {
42
+ :api_key => @api_key,
43
+ :application_key => @application_key
44
+ }
35
45
 
36
- request(Net::HTTP::Get, '/api/' + API_VERSION + '/events/' + id.to_s, params, nil, false)
46
+ request(Net::HTTP::Get, '/api/' + API_VERSION + '/events/' + id.to_s, params, nil, false)
47
+ rescue Exception => e
48
+ if @silent
49
+ warn e
50
+ return -1, {}
51
+ else
52
+ raise e
53
+ end
54
+ end
37
55
  end
38
56
 
39
57
  def stream(start, stop, options={})
40
- defaults = {
41
- :priority => nil,
42
- :sources => nil,
43
- :tags => nil
44
- }
45
- options = defaults.merge(options)
58
+ begin
59
+ defaults = {
60
+ :priority => nil,
61
+ :sources => nil,
62
+ :tags => nil
63
+ }
64
+ options = defaults.merge(options)
46
65
 
47
- params = {
48
- :api_key => @api_key,
49
- :application_key => @application_key,
66
+ params = {
67
+ :api_key => @api_key,
68
+ :application_key => @application_key,
50
69
 
51
- :start => start.to_i,
52
- :end => stop.to_i
53
- }
70
+ :start => start.to_i,
71
+ :end => stop.to_i
72
+ }
54
73
 
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
74
+ if options[:priority]
75
+ params[:priority] = options[:priority]
76
+ end
77
+ if options[:sources]
78
+ params[:sources] = options[:sources]
79
+ end
80
+ if options[:tags]
81
+ params[:tags] = options[:tags]
82
+ end
64
83
 
65
- request(Net::HTTP::Get, '/api/' + API_VERSION + '/events', params, nil, false)
84
+ request(Net::HTTP::Get, '/api/' + API_VERSION + '/events', params, nil, false)
85
+ rescue Exception => e
86
+ if @silent
87
+ warn e
88
+ return -1, {}
89
+ else
90
+ raise e
91
+ end
92
+ end
66
93
  end
67
94
 
68
95
  end
@@ -10,33 +10,42 @@ module Dogapi
10
10
 
11
11
  # Records an Event with no duration
12
12
  def submit(metric, points, scope, options={})
13
- params = {
14
- :api_key => @api_key
15
- }
16
- typ = options[:type] || "gauge"
17
-
18
- if typ != "gauge" && typ == "counter"
19
- raise ArgumentError, "metric type must be gauge or counter"
20
- end
21
-
22
- body = { :series => [
23
- {
24
- :metric => metric,
25
- :points => points,
26
- :type => typ,
27
- :host => scope.host,
28
- :device => scope.device
29
- }
30
- ]
31
- }
32
-
33
-
34
- # Add tags if there are any
35
- if not options[:tags].nil?
36
- body[:series][0][:tags] = options[:tags]
13
+ begin
14
+ params = {
15
+ :api_key => @api_key
16
+ }
17
+ typ = options[:type] || "gauge"
18
+
19
+ if typ != "gauge" && typ != "counter"
20
+ raise ArgumentError, "metric type must be gauge or counter"
21
+ end
22
+
23
+ body = { :series => [
24
+ {
25
+ :metric => metric,
26
+ :points => points,
27
+ :type => typ,
28
+ :host => scope.host,
29
+ :device => scope.device
30
+ }
31
+ ]
32
+ }
33
+
34
+
35
+ # Add tags if there are any
36
+ if not options[:tags].nil?
37
+ body[:series][0][:tags] = options[:tags]
38
+ end
39
+
40
+ request(Net::HTTP::Post, '/api/' + API_VERSION + '/series', params, body, true)
41
+ rescue Exception => e
42
+ if @silent
43
+ warn e
44
+ return -1, {}
45
+ else
46
+ raise e
47
+ end
37
48
  end
38
-
39
- request(Net::HTTP::Post, '/api/' + API_VERSION + '/series', params, body, true)
40
49
  end
41
50
  end
42
51
 
data/lib/dogapi/v1/tag.rb CHANGED
@@ -10,50 +10,86 @@ module Dogapi
10
10
 
11
11
  # Gets all tags in your org and the hosts tagged with them
12
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)
13
+ begin
14
+ params = {
15
+ :api_key => @api_key,
16
+ :application_key => @application_key
17
+ }
18
+
19
+ request(Net::HTTP::Get, '/api/' + API_VERSION + '/tags/hosts', params, nil, false)
20
+ rescue Exception => e
21
+ if @silent
22
+ warn e
23
+ return -1, {}
24
+ else
25
+ raise e
26
+ end
27
+ end
19
28
  end
20
29
 
21
30
  # Gets all tags for a given host
22
31
  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)
32
+ begin
33
+ params = {
34
+ :api_key => @api_key,
35
+ :application_key => @application_key
36
+ }
37
+
38
+ request(Net::HTTP::Get, '/api/' + API_VERSION + '/tags/hosts/' + host_id.to_s, params, nil, false)
39
+ rescue Exception => e
40
+ if @silent
41
+ warn e
42
+ return -1, {}
43
+ else
44
+ raise e
45
+ end
46
+ end
29
47
  end
30
48
 
31
49
  # Adds a list of tags to a host
32
50
  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)
51
+ begin
52
+ params = {
53
+ :api_key => @api_key,
54
+ :application_key => @application_key
55
+ }
56
+
57
+ body = {
58
+ :tags => tags
59
+ }
60
+
61
+ request(Net::HTTP::Post, '/api/' + API_VERSION + '/tags/hosts/' + host_id.to_s, params, body, true)
62
+ rescue Exception => e
63
+ if @silent
64
+ warn e
65
+ return -1, {}
66
+ else
67
+ raise e
68
+ end
69
+ end
43
70
  end
44
71
 
45
72
  # Remove all tags from a host and replace them with a new list
46
73
  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)
74
+ begin
75
+ params = {
76
+ :api_key => @api_key,
77
+ :application_key => @application_key
78
+ }
79
+
80
+ body = {
81
+ :tags => tags
82
+ }
83
+
84
+ request(Net::HTTP::Put, '/api/' + API_VERSION + '/tags/hosts/' + host_id.to_s, params, body, true)
85
+ rescue Exception => e
86
+ if @silent
87
+ warn e
88
+ return -1, {}
89
+ else
90
+ raise e
91
+ end
92
+ end
57
93
  end
58
94
 
59
95
  # <b>DEPRECATED:</b> Spelling mistake temporarily preserved as an alias.
@@ -64,12 +100,21 @@ module Dogapi
64
100
 
65
101
  # Remove all tags from a host
66
102
  def detach(host_id)
67
- params = {
68
- :api_key => @api_key,
69
- :application_key => @application_key
70
- }
71
-
72
- request(Net::HTTP::Delete, '/api/' + API_VERSION + '/tags/hosts/' + host_id.to_s, params, nil, false)
103
+ begin
104
+ params = {
105
+ :api_key => @api_key,
106
+ :application_key => @application_key
107
+ }
108
+
109
+ request(Net::HTTP::Delete, '/api/' + API_VERSION + '/tags/hosts/' + host_id.to_s, params, nil, false)
110
+ rescue Exception => e
111
+ if @silent
112
+ warn e
113
+ return -1, {}
114
+ else
115
+ raise e
116
+ end
117
+ end
73
118
  end
74
119
 
75
120
  end
data/tests/test_client.rb CHANGED
@@ -97,9 +97,11 @@ class TestClient < Test::Unit::TestCase
97
97
  # Testing priorities
98
98
  code, resp = dog_r.emit_event(Dogapi::Event.new(now_message, :msg_title =>now_title, :date_happened => now_ts, :priority => "low"))
99
99
  low_event_id = resp["event"]["id"]
100
+
101
+ sleep 3
102
+
100
103
  code, resp = dog.get_event(low_event_id)
101
104
  low_event = resp['event']
102
- puts low_event
103
105
  assert low_event['priority'] == "low"
104
106
 
105
107
  # Testing aggregates
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 1
7
7
  - 3
8
- - 2
9
- version: 1.3.2
8
+ - 3
9
+ version: 1.3.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - Datadog, Inc.
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2012-06-04 00:00:00 -04:00
17
+ date: 2012-07-05 00:00:00 -04:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency