dogapi-demo 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +10 -0
  3. data/.rspec +2 -0
  4. data/.tailor +106 -0
  5. data/.travis.yml +8 -0
  6. data/CHANGELOG.md +157 -0
  7. data/Gemfile +16 -0
  8. data/LICENSE +25 -0
  9. data/README.rdoc +143 -0
  10. data/Rakefile +50 -0
  11. data/dogapi-demo.gemspec +32 -0
  12. data/examples/Capfile +19 -0
  13. data/examples/custom_event.rb +37 -0
  14. data/examples/custom_metric.rb +35 -0
  15. data/lib/capistrano/README.md +13 -0
  16. data/lib/capistrano/datadog.rb +125 -0
  17. data/lib/capistrano/datadog/v2.rb +74 -0
  18. data/lib/capistrano/datadog/v3.rb +64 -0
  19. data/lib/dogapi-demo.rb +5 -0
  20. data/lib/dogapi-demo/common.rb +168 -0
  21. data/lib/dogapi-demo/event.rb +129 -0
  22. data/lib/dogapi-demo/facade.rb +475 -0
  23. data/lib/dogapi-demo/metric.rb +34 -0
  24. data/lib/dogapi-demo/v1.rb +13 -0
  25. data/lib/dogapi-demo/v1/alert.rb +112 -0
  26. data/lib/dogapi-demo/v1/comment.rb +62 -0
  27. data/lib/dogapi-demo/v1/dash.rb +94 -0
  28. data/lib/dogapi-demo/v1/embed.rb +106 -0
  29. data/lib/dogapi-demo/v1/event.rb +101 -0
  30. data/lib/dogapi-demo/v1/metric.rb +118 -0
  31. data/lib/dogapi-demo/v1/monitor.rb +264 -0
  32. data/lib/dogapi-demo/v1/screenboard.rb +110 -0
  33. data/lib/dogapi-demo/v1/search.rb +27 -0
  34. data/lib/dogapi-demo/v1/service_check.rb +32 -0
  35. data/lib/dogapi-demo/v1/snapshot.rb +30 -0
  36. data/lib/dogapi-demo/v1/tag.rb +141 -0
  37. data/lib/dogapi-demo/v1/user.rb +113 -0
  38. data/lib/dogapi-demo/version.rb +3 -0
  39. data/spec/alerts_spec.rb +33 -0
  40. data/spec/common_spec.rb +37 -0
  41. data/spec/facade_spec.rb +166 -0
  42. data/spec/spec_helper.rb +30 -0
  43. data/spec/support/cassettes/Alerts/create/returns_HTTP_code_200.yml +114 -0
  44. data/spec/support/cassettes/Alerts/create/returns_a_valid_event_ID.yml +114 -0
  45. data/spec/support/cassettes/Alerts/create/returns_the_same_query_as_sent.yml +114 -0
  46. data/spec/support/cassettes/Facade/Events/emits_aggregate_events.yml +193 -0
  47. data/spec/support/cassettes/Facade/Events/emits_events_and_retrieves_them.yml +100 -0
  48. data/spec/support/cassettes/Facade/Events/emits_events_with_specified_priority.yml +98 -0
  49. data/spec/support/cassettes/Facade/Tags/adds_updates_and_detaches_tags.yml +442 -0
  50. data/tests/test_alerts.rb +38 -0
  51. data/tests/test_base.rb +30 -0
  52. data/tests/test_client.rb +23 -0
  53. data/tests/test_comments.rb +39 -0
  54. data/tests/test_dashes.rb +85 -0
  55. data/tests/test_embed.rb +194 -0
  56. data/tests/test_monitors.rb +192 -0
  57. data/tests/test_screenboard.rb +90 -0
  58. data/tests/test_search.rb +20 -0
  59. data/tests/test_snapshot.rb +28 -0
  60. data/tests/test_users.rb +65 -0
  61. metadata +178 -0
@@ -0,0 +1,27 @@
1
+ require 'dogapi-demo'
2
+
3
+ module DogapiDemo
4
+ class V1 # for namespacing
5
+
6
+ class SearchService < DogapiDemo::APIService
7
+
8
+ API_VERSION = "v1"
9
+
10
+ def search(query)
11
+ begin
12
+ params = {
13
+ :api_key => @api_key,
14
+ :application_key => @application_key,
15
+ :q => query
16
+ }
17
+
18
+ request(Net::HTTP::Get, "/api/#{API_VERSION}/search", params, nil, false)
19
+ rescue Exception => e
20
+ suppress_error_if_silent e
21
+ end
22
+ end
23
+
24
+ end
25
+
26
+ end
27
+ end
@@ -0,0 +1,32 @@
1
+ require 'dogapi-demo'
2
+
3
+ module DogapiDemo
4
+ class V1
5
+
6
+ class ServiceCheckService < DogapiDemo::APIService
7
+
8
+ API_VERSION = 'v1'
9
+
10
+ def service_check(check, host, status, options = {})
11
+ begin
12
+ params = {
13
+ :api_key => @api_key,
14
+ :application_key => @application_key
15
+ }
16
+
17
+ body = {
18
+ 'check' => check,
19
+ 'host_name' => host,
20
+ 'status' => status
21
+ }.merge options
22
+
23
+ request(Net::HTTP::Post, "/api/#{API_VERSION}/check_run", params, body, true)
24
+ rescue Exception => e
25
+ suppress_error_if_silent e
26
+ end
27
+ end
28
+
29
+ end
30
+
31
+ end
32
+ end
@@ -0,0 +1,30 @@
1
+ require 'dogapi-demo'
2
+
3
+ module DogapiDemo
4
+ class V1 # for namespacing
5
+
6
+ class SnapshotService < DogapiDemo::APIService
7
+
8
+ API_VERSION = "v1"
9
+
10
+ def snapshot(metric_query, start_ts, end_ts, event_query=nil)
11
+ begin
12
+ params = {
13
+ :api_key => @api_key,
14
+ :application_key => @application_key,
15
+ :metric_query => metric_query,
16
+ :start => start_ts,
17
+ :end => end_ts,
18
+ :event_query => event_query
19
+ }
20
+
21
+ request(Net::HTTP::Get, "/api/#{API_VERSION}/graph/snapshot", params, nil, false)
22
+ rescue Exception => e
23
+ suppress_error_if_silent e
24
+ end
25
+ end
26
+
27
+ end
28
+
29
+ end
30
+ end
@@ -0,0 +1,141 @@
1
+ require 'dogapi-demo'
2
+
3
+ module DogapiDemo
4
+ class V1 # for namespacing
5
+
6
+ #
7
+ class TagService < DogapiDemo::APIService
8
+
9
+ API_VERSION = "v1"
10
+
11
+ # Gets all tags in your org and the hosts tagged with them
12
+ def get_all(source=nil)
13
+ begin
14
+ params = {
15
+ :api_key => @api_key,
16
+ :application_key => @application_key
17
+ }
18
+ if source
19
+ params['source'] = source
20
+ end
21
+
22
+ request(Net::HTTP::Get, '/api/' + API_VERSION + '/tags/hosts', params, nil, false)
23
+ rescue Exception => e
24
+ if @silent
25
+ warn e
26
+ return -1, {}
27
+ else
28
+ raise e
29
+ end
30
+ end
31
+ end
32
+
33
+ # Gets all tags for a given host
34
+ def get(host_id, source=nil, by_source=false)
35
+ begin
36
+ params = {
37
+ :api_key => @api_key,
38
+ :application_key => @application_key
39
+ }
40
+ if source
41
+ params['source'] = source
42
+ end
43
+ if by_source
44
+ params['by_source'] = 'true'
45
+ end
46
+
47
+ request(Net::HTTP::Get, '/api/' + API_VERSION + '/tags/hosts/' + host_id.to_s, params, nil, false)
48
+ rescue Exception => e
49
+ if @silent
50
+ warn e
51
+ return -1, {}
52
+ else
53
+ raise e
54
+ end
55
+ end
56
+ end
57
+
58
+ # Adds a list of tags to a host
59
+ def add(host_id, tags, source=nil)
60
+ begin
61
+ params = {
62
+ :api_key => @api_key,
63
+ :application_key => @application_key
64
+ }
65
+ if source
66
+ params['source'] = source
67
+ end
68
+
69
+ body = {
70
+ :tags => tags
71
+ }
72
+
73
+ request(Net::HTTP::Post, '/api/' + API_VERSION + '/tags/hosts/' + host_id.to_s, params, body, true)
74
+ rescue Exception => e
75
+ if @silent
76
+ warn e
77
+ return -1, {}
78
+ else
79
+ raise e
80
+ end
81
+ end
82
+ end
83
+
84
+ # Remove all tags from a host and replace them with a new list
85
+ def update(host_id, tags, source=nil)
86
+ begin
87
+ params = {
88
+ :api_key => @api_key,
89
+ :application_key => @application_key
90
+ }
91
+ if source
92
+ params['source'] = source
93
+ end
94
+
95
+ body = {
96
+ :tags => tags
97
+ }
98
+
99
+ request(Net::HTTP::Put, '/api/' + API_VERSION + '/tags/hosts/' + host_id.to_s, params, body, true)
100
+ rescue Exception => e
101
+ if @silent
102
+ warn e
103
+ return -1, {}
104
+ else
105
+ raise e
106
+ end
107
+ end
108
+ end
109
+
110
+ # <b>DEPRECATED:</b> Spelling mistake temporarily preserved as an alias.
111
+ def detatch(host_id)
112
+ warn "[DEPRECATION] DogapiDemo::V1::TagService.detatch() is deprecated. Use `detach` instead."
113
+ detach(host_id)
114
+ end
115
+
116
+ # Remove all tags from a host
117
+ def detach(host_id, source=nil)
118
+ begin
119
+ params = {
120
+ :api_key => @api_key,
121
+ :application_key => @application_key
122
+ }
123
+ if source
124
+ params['source'] = source
125
+ end
126
+
127
+ request(Net::HTTP::Delete, '/api/' + API_VERSION + '/tags/hosts/' + host_id.to_s, params, nil, false)
128
+ rescue Exception => e
129
+ if @silent
130
+ warn e
131
+ return -1, {}
132
+ else
133
+ raise e
134
+ end
135
+ end
136
+ end
137
+
138
+ end
139
+
140
+ end
141
+ end
@@ -0,0 +1,113 @@
1
+ require 'dogapi-demo'
2
+
3
+ module DogapiDemo
4
+ class V1 # for namespacing
5
+
6
+ class UserService < DogapiDemo::APIService
7
+
8
+ API_VERSION = "v1"
9
+
10
+ # <b>DEPRECATED:</b> Going forward, we're using a new and more restful API,
11
+ # the new methods are get_user, create_user, update_user, disable_user
12
+ def invite(emails, options = {})
13
+ warn "[DEPRECATION] DogapiDemo::V1::UserService::invite has been deprecated in favor of DogapiDemo::V1::UserService::create_user"
14
+ begin
15
+ params = {
16
+ :api_key => @api_key,
17
+ :application_key => @application_key
18
+ }
19
+
20
+ body = {
21
+ 'emails' => emails,
22
+ }.merge options
23
+
24
+ request(Net::HTTP::Post, "/api/#{API_VERSION}/invite_users", params, body, true)
25
+ rescue Exception => e
26
+ suppress_error_if_silent e
27
+ end
28
+ end
29
+
30
+ # Create a user
31
+ #
32
+ # :description => Hash: user description containing 'handle' and 'name' properties
33
+ def create_user(description = {})
34
+ begin
35
+ params = {
36
+ :api_key => @api_key,
37
+ :application_key => @application_key
38
+ }
39
+
40
+ request(Net::HTTP::Post, "/api/#{API_VERSION}/user", params, description, true)
41
+ rescue Exception => e
42
+ suppress_error_if_silent e
43
+ end
44
+ end
45
+
46
+ # Retrieve user information
47
+ #
48
+ # :handle => String: user handle
49
+ def get_user(handle)
50
+ begin
51
+ params = {
52
+ :api_key => @api_key,
53
+ :application_key => @application_key
54
+ }
55
+
56
+ request(Net::HTTP::Get, "/api/#{API_VERSION}/user/#{handle}", params, nil, false)
57
+ rescue Exception => e
58
+ suppress_error_if_silent e
59
+ end
60
+ end
61
+
62
+ # Retrieve all users
63
+ def get_all_users()
64
+ begin
65
+ params = {
66
+ :api_key => @api_key,
67
+ :application_key => @application_key
68
+ }
69
+
70
+ request(Net::HTTP::Get, "/api/#{API_VERSION}/user", params, nil, false)
71
+ rescue Exception => e
72
+ suppress_error_if_silent e
73
+ end
74
+ end
75
+
76
+ # Update a user
77
+ #
78
+ # :handle => String: user handle
79
+ # :description => Hash: user description optionally containing 'name', 'email',
80
+ # 'is_admin', 'disabled' properties
81
+ def update_user(handle, description = {})
82
+ begin
83
+ params = {
84
+ :api_key => @api_key,
85
+ :application_key => @application_key
86
+ }
87
+
88
+ request(Net::HTTP::Put, "/api/#{API_VERSION}/user/#{handle}", params, description, true)
89
+ rescue Exception => e
90
+ suppress_error_if_silent e
91
+ end
92
+ end
93
+
94
+ # Disable a user
95
+ #
96
+ # :handle => String: user handle
97
+ def disable_user(handle)
98
+ begin
99
+ params = {
100
+ :api_key => @api_key,
101
+ :application_key => @application_key
102
+ }
103
+
104
+ request(Net::HTTP::Delete, "/api/#{API_VERSION}/user/#{handle}", params, nil, false)
105
+ rescue Exception => e
106
+ suppress_error_if_silent e
107
+ end
108
+ end
109
+
110
+ end
111
+
112
+ end
113
+ end
@@ -0,0 +1,3 @@
1
+ module DogapiDemo
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Alerts", :vcr => true do
4
+
5
+ before(:all) do
6
+ @api_key = ENV["DATADOG_API_KEY"]
7
+ @app_key = ENV["DATADOG_APP_KEY"]
8
+ @dog = Dogapi::Client.new(@api_key, @app_key)
9
+ @query = 'avg(last_10m):avg:test.metric.metric{host:test.metric.host} > 5'
10
+ end
11
+
12
+ context "create" do
13
+ before(:each) do
14
+ @new_alert = @dog.alert(@query)
15
+ end
16
+ after(:each) do
17
+ @dog.delete_alert(@new_alert[1]['id'])
18
+ end
19
+
20
+ it "returns HTTP code 200" do
21
+ expect(@new_alert[0]).to eq '200'
22
+ end
23
+
24
+ it "returns a valid event ID" do
25
+ expect(@new_alert[1]['id']).to be_a(Fixnum)
26
+ end
27
+
28
+ it "returns the same query as sent" do
29
+ expect(@new_alert[1]['query']).to eq @query
30
+ end
31
+ end
32
+
33
+ end
@@ -0,0 +1,37 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Common" do
4
+
5
+ context "Scope" do
6
+
7
+ it "validates the Scope class" do
8
+ obj = Dogapi::Scope.new("somehost", "somedevice")
9
+
10
+ expect(obj.host).to eq "somehost"
11
+ expect(obj.device).to eq "somedevice"
12
+ end
13
+
14
+ end # end Scope
15
+
16
+ context "HttpConnection" do
17
+
18
+ it "respects the proxy configuration" do
19
+ service = Dogapi::APIService.new("api_key", "app_key")
20
+
21
+ service.connect do |conn|
22
+ expect(conn.proxy_address).to be(nil)
23
+ expect(conn.proxy_port).to be(nil)
24
+ end
25
+
26
+ ENV["http_proxy"] = "https://www.proxy.com:443"
27
+
28
+ service.connect do |conn|
29
+ expect(conn.proxy_address).to eq "www.proxy.com"
30
+ expect(conn.proxy_port).to eq 443
31
+ end
32
+
33
+ ENV["http_proxy"] = nil
34
+ end
35
+ end
36
+
37
+ end # end Common
@@ -0,0 +1,166 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Facade", :vcr => true do
4
+
5
+ before(:all) do
6
+ @api_key = ENV["DATADOG_API_KEY"]
7
+ @app_key = ENV["DATADOG_APP_KEY"]
8
+ @job_number = ENV['TRAVIS_JOB_NUMBER'] || '1'
9
+ @dog = Dogapi::Client.new(@api_key, @app_key)
10
+ end
11
+
12
+ context "Client" do
13
+
14
+ before(:each) do
15
+ @dogmock = Dogapi::Client.new(@api_key, @app_key)
16
+ @service = @dogmock.instance_variable_get(:@metric_svc)
17
+ @service.instance_variable_set(:@uploaded, Array.new)
18
+ def @service.upload payload
19
+ @uploaded << payload
20
+ return 200, {}
21
+ end
22
+ end
23
+
24
+ it "emit_point passes data" do
25
+ @dogmock.emit_point("metric.name", 0, :host => "myhost")
26
+
27
+ uploaded = @service.instance_variable_get(:@uploaded)
28
+ expect(uploaded.length).to eq 1
29
+ series = uploaded.first
30
+ expect(series.class).to eq Array
31
+ expect(series[0][:metric]).to eq "metric.name"
32
+ expect(series[0][:points][0][1]).to eq 0
33
+ expect(series[0][:host]).to eq "myhost"
34
+ end
35
+
36
+ it "emit_point uses localhost default" do
37
+ @dogmock.emit_point("metric.name", 0)
38
+
39
+ uploaded = @service.instance_variable_get(:@uploaded)
40
+ series = uploaded.first
41
+ expect(series[0][:host]).to eq Dogapi.find_localhost
42
+ end
43
+
44
+ it "emit_point can pass nil host" do
45
+ @dogmock.emit_point("metric.name", 0, :host => nil)
46
+
47
+ uploaded = @service.instance_variable_get(:@uploaded)
48
+ series = uploaded.first
49
+ expect(series[0][:host]).to be_nil
50
+ end
51
+
52
+ it "emit_points can be batched" do
53
+ code, resp = @dogmock.batch_metrics do
54
+ @dogmock.emit_point("metric.name", 1, :type => 'counter')
55
+ @dogmock.emit_point("othermetric.name", 2, :type => 'counter')
56
+ end
57
+ expect(code).to eq 200
58
+ # Verify that we uploaded what we expected
59
+ uploaded = @service.instance_variable_get(:@uploaded)
60
+ expect(uploaded.length).to eq 1
61
+ series = uploaded.first
62
+ expect(series.class).to eq Array
63
+ expect(series[0][:metric]).to eq 'metric.name'
64
+ expect(series[0][:points][0][1]).to eq 1
65
+ expect(series[0][:type]).to eq 'counter'
66
+ expect(series[1][:metric]).to eq 'othermetric.name'
67
+ expect(series[1][:points][0][1]).to eq 2
68
+ expect(series[1][:type]).to eq 'counter'
69
+
70
+ # Verify that the buffer was correclty emptied
71
+ buffer = @service.instance_variable_get(:@buffer)
72
+ expect(buffer).to be nil
73
+ end
74
+
75
+ it "flushes the buffer even if an exception is raised" do
76
+ begin
77
+ @dogmock.batch_metrics do
78
+ @dogmock.emit_point("metric.name", 1, :type => 'counter')
79
+ raise "Oh no, something went wrong"
80
+ end
81
+ rescue
82
+ end
83
+ buffer = @service.instance_variable_get(:@buffer)
84
+ expect(buffer).to be nil
85
+ end
86
+
87
+ end
88
+
89
+ context "Events" do
90
+
91
+ it "emits events and retrieves them" do
92
+ now = Time.now()
93
+
94
+ # Tag the events with the build number, because Travis parallel testing
95
+ # can cause problems with the event stream
96
+ tags = ["test-run:#{@job_number}"]
97
+
98
+ now_ts = now
99
+ now_title = 'dogapi-rb end test title ' + now_ts.to_i.to_s
100
+ now_message = 'test message'
101
+
102
+
103
+ event = Dogapi::Event.new(now_message, :msg_title => now_title,
104
+ :date_happened => now_ts, :tags => tags)
105
+
106
+ code, resp = @dog.emit_event(event)
107
+ now_event_id = resp["event"]["id"]
108
+ sleep 8
109
+ code, resp = @dog.get_event(now_event_id)
110
+ expect(resp['event']).not_to be_nil
111
+ expect(resp['event']['text']).to eq(now_message)
112
+ end
113
+
114
+ it "emits events with specified priority" do
115
+ event = Dogapi::Event.new('test message', :msg_title => 'title', :date_happened => Time.now(), :priority => "low")
116
+ code, resp = @dog.emit_event(event)
117
+ low_event_id = resp["event"]["id"]
118
+ sleep 8
119
+ code, resp = @dog.get_event(low_event_id)
120
+ expect(resp['event']).not_to be_nil
121
+ low_event = resp['event']
122
+ expect(low_event['priority']).to eq("low")
123
+ end
124
+
125
+ it "emits aggregate events" do
126
+ now = Time.now()
127
+ code, resp = @dog.emit_event(Dogapi::Event.new("Testing Aggregation (first)", :aggregation_key => now.to_i))
128
+ first = resp["event"]["id"]
129
+ code, resp = @dog.emit_event(Dogapi::Event.new("Testing Aggregation (second)", :aggregation_key => now.to_i))
130
+ second = resp["event"]["id"]
131
+ sleep 8
132
+ code, resp = @dog.get_event(first)
133
+ expect(resp["event"]).not_to be_nil
134
+ code, resp = @dog.get_event(second)
135
+ expect(resp["event"]).not_to be_nil
136
+ end
137
+
138
+ end
139
+
140
+ context "Tags" do
141
+ it "adds, updates and detaches tags" do
142
+ hostname = "test.tag.host"
143
+
144
+ @dog.emit_point('test.tag.metric', 1, :host => hostname)
145
+ sleep 5
146
+ @dog.detach_tags(hostname)
147
+ code, resp = @dog.host_tags(hostname)
148
+ expect(resp["tags"]).to be_empty
149
+
150
+ @dog.add_tags(hostname, ['test.tag.1', 'test.tag.2'])
151
+ code, resp = @dog.host_tags(hostname)
152
+ new_tags = resp["tags"]
153
+ expect(new_tags).to match_array(['test.tag.1', 'test.tag.2'])
154
+
155
+ @dog.add_tags(hostname, ['test.tag.3'])
156
+ code, resp = @dog.host_tags(hostname)
157
+ new_tags = resp["tags"]
158
+ expect(new_tags).to match_array(['test.tag.1', 'test.tag.2', 'test.tag.3'])
159
+
160
+ @dog.detach_tags(hostname)
161
+ code, resp = @dog.host_tags(hostname)
162
+ expect(resp["tags"]).to be_empty
163
+ end
164
+ end
165
+
166
+ end