Class: Dogapi::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/dogapi/facade.rb

Overview

A simple DogAPI client

See Dogapi::V1 for the thick underlying clients

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.

Instance Method Summary collapse

Constructor Details

#initialize(api_key, application_key = nil, host = nil, device = nil, silent = true, timeout = nil, endpoint = nil) ⇒ Client

Returns a new instance of Client.


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/dogapi/facade.rb', line 16

def initialize(api_key, application_key=nil, host=nil, device=nil, silent=true, timeout=nil, endpoint=nil)

  if api_key
    @api_key = api_key
  else
    raise 'Please provide an API key to submit your data'
  end

  @application_key = application_key
  @datadog_host = endpoint || Dogapi.find_datadog_host()
  @host = host || Dogapi.find_localhost()
  @device = device

  # FIXME: refactor to avoid all this code duplication
  @metric_svc = Dogapi::V1::MetricService.new(@api_key, @application_key, silent, timeout, @datadog_host)
  @event_svc = Dogapi::V1::EventService.new(@api_key, @application_key, silent, timeout, @datadog_host)
  @tag_svc = Dogapi::V1::TagService.new(@api_key, @application_key, silent, timeout, @datadog_host)
  @comment_svc = Dogapi::V1::CommentService.new(@api_key, @application_key, silent, timeout, @datadog_host)
  @search_svc = Dogapi::V1::SearchService.new(@api_key, @application_key, silent, timeout, @datadog_host)
  @dash_service = Dogapi::V1::DashService.new(@api_key, @application_key, silent, timeout, @datadog_host)
  @dashboard_list_service = Dogapi::V1::DashboardListService.new(
    @api_key, @application_key, silent, timeout, @datadog_host
  )
  @alert_svc = Dogapi::V1::AlertService.new(@api_key, @application_key, silent, timeout, @datadog_host)
  @user_svc = Dogapi::V1::UserService.new(@api_key, @application_key, silent, timeout, @datadog_host)
  @snapshot_svc = Dogapi::V1::SnapshotService.new(@api_key, @application_key, silent, timeout, @datadog_host)
  @embed_svc = Dogapi::V1::EmbedService.new(@api_key, @application_key, silent, timeout, @datadog_host)
  @screenboard_svc = Dogapi::V1::ScreenboardService.new(@api_key, @application_key, silent, timeout, @datadog_host)
  @monitor_svc = Dogapi::V1::MonitorService.new(@api_key, @application_key, silent, timeout, @datadog_host)
  @service_check_svc = Dogapi::V1::ServiceCheckService.new(@api_key, @application_key, silent, timeout, @datadog_host)
  @metadata_svc = Dogapi::V1::MetadataService.new(@api_key, @application_key, silent, timeout, @datadog_host)
  @legacy_event_svc = Dogapi::EventService.new(@datadog_host)
end

Instance Method Details

#add_items_to_dashboard_list(dashboard_list_id, dashboards) ⇒ Object


301
302
303
# File 'lib/dogapi/facade.rb', line 301

def add_items_to_dashboard_list(dashboard_list_id, dashboards)
  @dashboard_list_service.add_items(dashboard_list_id, dashboards)
end

#add_tags(host_id, tags, source = nil) ⇒ Object

Add the tags to the given host

host_id can be the host's numeric id or string name

tags is and Array of Strings


222
223
224
# File 'lib/dogapi/facade.rb', line 222

def add_tags(host_id, tags, source=nil)
  @tag_svc.add(host_id, tags, source)
end

#alert(query, options = {}) ⇒ Object

ALERTS


321
322
323
# File 'lib/dogapi/facade.rb', line 321

def alert(query, options= {})
  @alert_svc.alert(query, options)
end

#all_tags(source = nil) ⇒ Object

Get all tags and their associated hosts at your org


206
207
208
# File 'lib/dogapi/facade.rb', line 206

def all_tags(source=nil)
  @tag_svc.get_all(source)
end

#batch_metricsObject


107
108
109
110
111
112
113
114
115
# File 'lib/dogapi/facade.rb', line 107

def batch_metrics()
  @metric_svc.switch_to_batched
  begin
    yield
    @metric_svc.flush_buffer # flush_buffer should returns the response from last API call
  ensure
    @metric_svc.switch_to_single
  end
end

#cancel_downtime(downtime_id) ⇒ Object


493
494
495
# File 'lib/dogapi/facade.rb', line 493

def cancel_downtime(downtime_id)
  @monitor_svc.cancel_downtime(downtime_id)
end

#cancel_downtime_by_scope(scope) ⇒ Object


497
498
499
# File 'lib/dogapi/facade.rb', line 497

def cancel_downtime_by_scope(scope)
  @monitor_svc.cancel_downtime_by_scope(scope)
end

#comment(message, options = {}) ⇒ Object

Post a comment


178
179
180
# File 'lib/dogapi/facade.rb', line 178

def comment(message, options= {})
  @comment_svc.comment(message, options)
end

#create_dashboard(title, description, graphs, template_variables = nil) ⇒ Object

Create a dashboard.


253
254
255
# File 'lib/dogapi/facade.rb', line 253

def create_dashboard(title, description, graphs, template_variables=nil)
  @dash_service.create_dashboard(title, description, graphs, template_variables)
end

#create_dashboard_list(name) ⇒ Object

DASHBOARD LISTS


281
282
283
# File 'lib/dogapi/facade.rb', line 281

def create_dashboard_list(name)
  @dashboard_list_service.create(name)
end

#create_embed(graph_json, description = {}) ⇒ Object


390
391
392
# File 'lib/dogapi/facade.rb', line 390

def create_embed(graph_json, description= {})
  @embed_svc.create_embed(graph_json, description)
end

#create_screenboard(description) ⇒ Object

SCREENBOARD


405
406
407
# File 'lib/dogapi/facade.rb', line 405

def create_screenboard(description)
  @screenboard_svc.create_screenboard(description)
end

#create_user(description = {}) ⇒ Object


354
355
356
# File 'lib/dogapi/facade.rb', line 354

def create_user(description= {})
  @user_svc.create_user(description)
end

#delete_alert(alert_id) ⇒ Object


333
334
335
# File 'lib/dogapi/facade.rb', line 333

def delete_alert(alert_id)
  @alert_svc.delete_alert(alert_id)
end

#delete_comment(comment_id) ⇒ Object


187
188
189
# File 'lib/dogapi/facade.rb', line 187

def delete_comment(comment_id)
  @comment_svc.delete_comment(comment_id)
end

#delete_dashboard(dash_id) ⇒ Object

Delete the given dashboard.


273
274
275
# File 'lib/dogapi/facade.rb', line 273

def delete_dashboard(dash_id)
  @dash_service.delete_dashboard(dash_id)
end

#delete_dashboard_list(dashboard_list_id) ⇒ Object


297
298
299
# File 'lib/dogapi/facade.rb', line 297

def delete_dashboard_list(dashboard_list_id)
  @dashboard_list_service.delete(dashboard_list_id)
end

#delete_event(id) ⇒ Object

Delete an event

id of the event to delete


141
142
143
# File 'lib/dogapi/facade.rb', line 141

def delete_event(id)
  @event_svc.delete(id)
end

#delete_items_from_dashboard_list(dashboard_list_id, dashboards) ⇒ Object


309
310
311
# File 'lib/dogapi/facade.rb', line 309

def delete_items_from_dashboard_list(dashboard_list_id, dashboards)
  @dashboard_list_service.delete_items(dashboard_list_id, dashboards)
end

#delete_monitor(monitor_id) ⇒ Object


449
450
451
# File 'lib/dogapi/facade.rb', line 449

def delete_monitor(monitor_id)
  @monitor_svc.delete_monitor(monitor_id)
end

#delete_screenboard(board_id) ⇒ Object


421
422
423
# File 'lib/dogapi/facade.rb', line 421

def delete_screenboard(board_id)
  @screenboard_svc.delete_screenboard(board_id)
end

#detach_tags(host_id, source = nil) ⇒ Object

Remove all tags from the given host

host_id can be the host's numeric id or string name


244
245
246
# File 'lib/dogapi/facade.rb', line 244

def detach_tags(host_id, source=nil)
  @tag_svc.detach(host_id, source)
end

#detatch_tags(host_id) ⇒ Object

DEPRECATED: Spelling mistake temporarily preserved as an alias.


236
237
238
239
# File 'lib/dogapi/facade.rb', line 236

def detatch_tags(host_id)
  warn '[DEPRECATION] Dogapi::Client.detatch() is deprecated. Use `detach` instead.'
  detach_tags(host_id)
end

#disable_user(handle) ⇒ Object


370
371
372
# File 'lib/dogapi/facade.rb', line 370

def disable_user(handle)
  @user_svc.disable_user(handle)
end

#emit_event(event, options = {}) ⇒ Object

Record an event

Optional arguments:

:host        => String
:device      => String

125
126
127
128
129
# File 'lib/dogapi/facade.rb', line 125

def emit_event(event, options= {})
  scope = override_scope options

  @event_svc.post(event, scope)
end

#emit_point(metric, value, options = {}) ⇒ Object

Record a single point of metric data

Optional arguments:

:timestamp => Ruby stdlib Time
:host      => String
:device    => String
:options   => Map

options = “counter” to specify a counter metric options = [“tag1”, “tag2”] to tag the point


63
64
65
66
67
68
69
70
71
72
# File 'lib/dogapi/facade.rb', line 63

def emit_point(metric, value, options= {})
  defaults = { :timestamp => Time.now }
  options = defaults.merge(options)

  self.emit_points(
    metric,
    [[options[:timestamp], value]],
    options
  )
end

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

Record a set of points of metric data

points is an array of [Time, value] doubles

Optional arguments:

:host   => String
:device => String
:options   => Map

options = “counter” to specify a counter metric options = [“tag1”, “tag2”] to tag the point


85
86
87
88
89
90
91
92
93
94
95
# File 'lib/dogapi/facade.rb', line 85

def emit_points(metric, points, options= {})
  scope = override_scope options

  points.each do |p|
    p[0].kind_of? Time or raise 'Not a Time'
    p[0] = p[0].to_i
    p[1] = p[1].to_f # TODO: stupid to_f will never raise an exception
  end

  @metric_svc.submit(metric, points, scope, options)
end

#enable_embed(embed_id) ⇒ Object


394
395
396
# File 'lib/dogapi/facade.rb', line 394

def enable_embed(embed_id)
  @embed_svc.enable_embed(embed_id)
end

#get_alert(alert_id) ⇒ Object


329
330
331
# File 'lib/dogapi/facade.rb', line 329

def get_alert(alert_id)
  @alert_svc.get_alert(alert_id)
end

#get_all_alertsObject


337
338
339
# File 'lib/dogapi/facade.rb', line 337

def get_all_alerts()
  @alert_svc.get_all_alerts()
end

#get_all_dashboard_listsObject


293
294
295
# File 'lib/dogapi/facade.rb', line 293

def get_all_dashboard_lists()
  @dashboard_list_service.all()
end

#get_all_downtimes(options = {}) ⇒ Object


501
502
503
# File 'lib/dogapi/facade.rb', line 501

def get_all_downtimes(options= {})
  @monitor_svc.get_all_downtimes(options)
end

#get_all_embedsObject

EMBEDS


382
383
384
# File 'lib/dogapi/facade.rb', line 382

def get_all_embeds()
  @embed_svc.get_all_embeds()
end

#get_all_monitors(options = {}) ⇒ Object


453
454
455
# File 'lib/dogapi/facade.rb', line 453

def get_all_monitors(options= {})
  @monitor_svc.get_all_monitors(options)
end

#get_all_screenboardsObject


417
418
419
# File 'lib/dogapi/facade.rb', line 417

def get_all_screenboards()
  @screenboard_svc.get_all_screenboards()
end

#get_all_usersObject


358
359
360
# File 'lib/dogapi/facade.rb', line 358

def get_all_users()
  @user_svc.get_all_users()
end

#get_dashboard(dash_id) ⇒ Object

Fetch the given dashboard.


263
264
265
# File 'lib/dogapi/facade.rb', line 263

def get_dashboard(dash_id)
  @dash_service.get_dashboard(dash_id)
end

#get_dashboard_list(dashboard_list_id) ⇒ Object


289
290
291
# File 'lib/dogapi/facade.rb', line 289

def get_dashboard_list(dashboard_list_id)
  @dashboard_list_service.get(dashboard_list_id)
end

#get_dashboardsObject

Fetch all of the dashboards.


268
269
270
# File 'lib/dogapi/facade.rb', line 268

def get_dashboards
  @dash_service.get_dashboards
end

#get_downtime(downtime_id) ⇒ Object


489
490
491
# File 'lib/dogapi/facade.rb', line 489

def get_downtime(downtime_id)
  @monitor_svc.get_downtime(downtime_id)
end

#get_embed(embed_id, description = {}) ⇒ Object


386
387
388
# File 'lib/dogapi/facade.rb', line 386

def get_embed(embed_id, description= {})
  @embed_svc.get_embed(embed_id, description)
end

#get_event(id) ⇒ Object

Get the details of an event

id of the event to get


134
135
136
# File 'lib/dogapi/facade.rb', line 134

def get_event(id)
  @event_svc.get(id)
end

#get_items_of_dashboard_list(dashboard_list_id) ⇒ Object


313
314
315
# File 'lib/dogapi/facade.rb', line 313

def get_items_of_dashboard_list(dashboard_list_id)
  @dashboard_list_service.get_items(dashboard_list_id)
end

#get_metadata(metric) ⇒ Object

Get metadata information on an existing Datadog metric


530
531
532
# File 'lib/dogapi/facade.rb', line 530

def (metric)
  @metadata_svc.get(metric)
end

#get_monitor(monitor_id, options = {}) ⇒ Object


445
446
447
# File 'lib/dogapi/facade.rb', line 445

def get_monitor(monitor_id, options= {})
  @monitor_svc.get_monitor(monitor_id, options)
end

#get_points(query, from, to) ⇒ Object

Get a set of points by query between from and to

from The seconds since the unix epoch [Time, Integer] to The seconds since the unix epoch [Time, Integer] query The query string [String]


103
104
105
# File 'lib/dogapi/facade.rb', line 103

def get_points(query, from, to)
  @metric_svc.get(query, from, to)
end

#get_screenboard(board_id) ⇒ Object


413
414
415
# File 'lib/dogapi/facade.rb', line 413

def get_screenboard(board_id)
  @screenboard_svc.get_screenboard(board_id)
end

#get_user(handle) ⇒ Object


362
363
364
# File 'lib/dogapi/facade.rb', line 362

def get_user(handle)
  @user_svc.get_user(handle)
end

#graph_snapshot(metric_query, start_ts, end_ts, event_query = nil) ⇒ Object

Graph snapshot


375
376
377
# File 'lib/dogapi/facade.rb', line 375

def graph_snapshot(metric_query, start_ts, end_ts, event_query=nil)
  @snapshot_svc.snapshot(metric_query, start_ts, end_ts, event_query)
end

#host_tags(host_id, source = nil, by_source = false) ⇒ Object

Get all tags for the given host

host_id can be the host's numeric id or string name


213
214
215
# File 'lib/dogapi/facade.rb', line 213

def host_tags(host_id, source=nil, by_source=false)
  @tag_svc.get(host_id, source, by_source)
end

#invite(emails, options = {}) ⇒ Object

User invite


350
351
352
# File 'lib/dogapi/facade.rb', line 350

def invite(emails, options= {})
  @user_svc.invite(emails, options)
end

#monitor(type, query, options = {}) ⇒ Object

MONITORS


437
438
439
# File 'lib/dogapi/facade.rb', line 437

def monitor(type, query, options= {})
  @monitor_svc.monitor(type, query, options)
end

#mute_alertsObject


341
342
343
# File 'lib/dogapi/facade.rb', line 341

def mute_alerts()
  @alert_svc.mute_alerts()
end

#mute_host(hostname, options = {}) ⇒ Object

HOST MUTING


509
510
511
# File 'lib/dogapi/facade.rb', line 509

def mute_host(hostname, options= {})
  @monitor_svc.mute_host(hostname, options)
end

#mute_monitor(monitor_id, options = {}) ⇒ Object


469
470
471
# File 'lib/dogapi/facade.rb', line 469

def mute_monitor(monitor_id, options= {})
  @monitor_svc.mute_monitor(monitor_id, options)
end

#mute_monitorsObject


461
462
463
# File 'lib/dogapi/facade.rb', line 461

def mute_monitors()
  @monitor_svc.mute_monitors()
end

#revoke_embed(embed_id) ⇒ Object


398
399
400
# File 'lib/dogapi/facade.rb', line 398

def revoke_embed(embed_id)
  @embed_svc.revoke_embed(embed_id)
end

#revoke_screenboard(board_id) ⇒ Object


429
430
431
# File 'lib/dogapi/facade.rb', line 429

def revoke_screenboard(board_id)
  @screenboard_svc.revoke_screenboard(board_id)
end

#schedule_downtime(scope, options = {}) ⇒ Object

MONITOR DOWNTIME


481
482
483
# File 'lib/dogapi/facade.rb', line 481

def schedule_downtime(scope, options= {})
  @monitor_svc.schedule_downtime(scope, options)
end

#search(query) ⇒ Object

Run the given search query.


196
197
198
# File 'lib/dogapi/facade.rb', line 196

def search(query)
  @search_svc.search query
end

#service_check(check, host, status, options = {}) ⇒ Object

SERVICE CHECKS


521
522
523
# File 'lib/dogapi/facade.rb', line 521

def service_check(check, host, status, options= {})
  @service_check_svc.service_check(check, host, status, options)
end

#share_screenboard(board_id) ⇒ Object


425
426
427
# File 'lib/dogapi/facade.rb', line 425

def share_screenboard(board_id)
  @screenboard_svc.share_screenboard(board_id)
end

#start_event(event, options = {}) ⇒ Object

DEPRECATED: Recording events with a duration has been deprecated. The functionality will be removed in a later release.


161
162
163
164
165
166
167
168
169
170
171
# File 'lib/dogapi/facade.rb', line 161

def start_event(event, options= {})
  warn '[DEPRECATION] Dogapi::Client.start_event() is deprecated. Use `emit_event` instead.'
  defaults = { :source_type => nil }
  options = defaults.merge(options)

  scope = override_scope options

  @legacy_event_svc.start(@api_key, event, scope, options[:source_type]) do
    yield
  end
end

#stream(start, stop, options = {}) ⇒ Object

Get an optionally filtered event stream

start is a Time object for when to start the stream

stop is a Time object for when to end the stream

Optional arguments:

:priority   => "normal" or "low"
:sources    => String, see https://github.com/DataDog/dogapi/wiki/Event for a current list of sources
:tags       => Array of Strings

155
156
157
# File 'lib/dogapi/facade.rb', line 155

def stream(start, stop, options= {})
  @event_svc.stream(start, stop, options)
end

#unmute_alertsObject


345
346
347
# File 'lib/dogapi/facade.rb', line 345

def unmute_alerts()
  @alert_svc.unmute_alerts()
end

#unmute_host(hostname) ⇒ Object


513
514
515
# File 'lib/dogapi/facade.rb', line 513

def unmute_host(hostname)
  @monitor_svc.unmute_host(hostname)
end

#unmute_monitor(monitor_id, options = {}) ⇒ Object


473
474
475
# File 'lib/dogapi/facade.rb', line 473

def unmute_monitor(monitor_id, options= {})
  @monitor_svc.unmute_monitor(monitor_id, options)
end

#unmute_monitorsObject


465
466
467
# File 'lib/dogapi/facade.rb', line 465

def unmute_monitors()
  @monitor_svc.unmute_monitors()
end

#update_alert(alert_id, query, options = {}) ⇒ Object


325
326
327
# File 'lib/dogapi/facade.rb', line 325

def update_alert(alert_id, query, options= {})
  @alert_svc.update_alert(alert_id, query, options)
end

#update_comment(comment_id, options = {}) ⇒ Object

Post a comment


183
184
185
# File 'lib/dogapi/facade.rb', line 183

def update_comment(comment_id, options= {})
  @comment_svc.update_comment(comment_id, options)
end

#update_dashboard(dash_id, title, description, graphs, template_variables = nil) ⇒ Object

Update a dashboard.


258
259
260
# File 'lib/dogapi/facade.rb', line 258

def update_dashboard(dash_id, title, description, graphs, template_variables=nil)
  @dash_service.update_dashboard(dash_id, title, description, graphs, template_variables)
end

#update_dashboard_list(dashboard_list_id, name) ⇒ Object


285
286
287
# File 'lib/dogapi/facade.rb', line 285

def update_dashboard_list(dashboard_list_id, name)
  @dashboard_list_service.update(dashboard_list_id, name)
end

#update_downtime(downtime_id, options = {}) ⇒ Object


485
486
487
# File 'lib/dogapi/facade.rb', line 485

def update_downtime(downtime_id, options= {})
  @monitor_svc.update_downtime(downtime_id, options)
end

#update_items_of_dashboard_list(dashboard_list_id, dashboards) ⇒ Object


305
306
307
# File 'lib/dogapi/facade.rb', line 305

def update_items_of_dashboard_list(dashboard_list_id, dashboards)
  @dashboard_list_service.update_items(dashboard_list_id, dashboards)
end

#update_metadata(metric, options = {}) ⇒ Object

Update metadata fields for an existing Datadog metric. If the metadata does not exist for the metric it is created by the update. Optional arguments:

:type             => String, type of the metric (ex. "gauge", "rate", etc.)
                     see http://docs.datadoghq.com/metrictypes/
:description      => String, description of the metric
:short_name       => String, short name of the metric
:unit             => String, unit type associated with the metric (ex. "byte", "operation")
                     see http://docs.datadoghq.com/units/ for full list
:per_unit         => String, per unit type (ex. "second" as in "queries per second")
                     see http://docs.datadoghq.com/units/ for full
:statsd_interval  => Integer, statsd flush interval for metric in seconds (if applicable)

547
548
549
# File 'lib/dogapi/facade.rb', line 547

def (metric, options= {})
  @metadata_svc.update(metric, options)
end

#update_monitor(monitor_id, query, options = {}) ⇒ Object


441
442
443
# File 'lib/dogapi/facade.rb', line 441

def update_monitor(monitor_id, query, options= {})
  @monitor_svc.update_monitor(monitor_id, query, options)
end

#update_screenboard(board_id, description) ⇒ Object


409
410
411
# File 'lib/dogapi/facade.rb', line 409

def update_screenboard(board_id, description)
  @screenboard_svc.update_screenboard(board_id, description)
end

#update_tags(host_id, tags, source = nil) ⇒ Object

Replace the tags on the given host

host_id can be the host's numeric id or string name

tags is and Array of Strings


231
232
233
# File 'lib/dogapi/facade.rb', line 231

def update_tags(host_id, tags, source=nil)
  @tag_svc.update(host_id, tags, source)
end

#update_user(handle, description = {}) ⇒ Object


366
367
368
# File 'lib/dogapi/facade.rb', line 366

def update_user(handle, description= {})
  @user_svc.update_user(handle, description)
end

#validate_monitor(type, query, options = {}) ⇒ Object


457
458
459
# File 'lib/dogapi/facade.rb', line 457

def validate_monitor(type, query, options= {})
  @monitor_svc.validate_monitor(type, query, options)
end