class Dogapi::Client

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.

Public Class Methods

new(api_key, application_key=nil, host=nil, device=nil, silent=true, timeout=nil) click to toggle source

Create a new Client optionally specifying a default host and device

# File lib/dogapi/facade.rb, line 14
def initialize(api_key, application_key=nil, host=nil, device=nil, silent=true, timeout=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 = 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)
  @event_svc = Dogapi::V1::EventService.new(@api_key, @application_key, silent, timeout)
  @tag_svc = Dogapi::V1::TagService.new(@api_key, @application_key, silent, timeout)
  @comment_svc = Dogapi::V1::CommentService.new(@api_key, @application_key, silent, timeout)
  @search_svc = Dogapi::V1::SearchService.new(@api_key, @application_key, silent, timeout)
  @dash_service = Dogapi::V1::DashService.new(@api_key, @application_key, silent, timeout)
  @alert_svc = Dogapi::V1::AlertService.new(@api_key, @application_key, silent, timeout)
  @user_svc = Dogapi::V1::UserService.new(@api_key, @application_key, silent, timeout)
  @snapshot_svc = Dogapi::V1::SnapshotService.new(@api_key, @application_key, silent, timeout)
  @embed_svc = Dogapi::V1::EmbedService.new(@api_key, @application_key, silent, timeout)
  @screenboard_svc = Dogapi::V1::ScreenboardService.new(@api_key, @application_key, silent, timeout)
  @monitor_svc = Dogapi::V1::MonitorService.new(@api_key, @application_key, silent, timeout)
  @service_check_svc = Dogapi::V1::ServiceCheckService.new(@api_key, @application_key, silent, timeout)
  @legacy_event_svc = Dogapi::EventService.new(@datadog_host)
end

Public Instance Methods

add_tags(host_id, tags, source = nil) click to toggle source

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

# File lib/dogapi/facade.rb, line 212
def add_tags(host_id, tags, source = nil)
  @tag_svc.add(host_id, tags, source)
end
alert(query, options = {}) click to toggle source

ALERTS

# File lib/dogapi/facade.rb, line 271
def alert(query, options = {})
  @alert_svc.alert(query, options)
end
all_tags(source = nil) click to toggle source

Get all tags and their associated hosts at your org

# File lib/dogapi/facade.rb, line 196
def all_tags(source = nil)
  @tag_svc.get_all(source)
end
batch_metrics() { || ... } click to toggle source
# File lib/dogapi/facade.rb, line 104
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) click to toggle source
# File lib/dogapi/facade.rb, line 439
def cancel_downtime(downtime_id)
  @monitor_svc.cancel_downtime(downtime_id)
end
comment(message, options = {}) click to toggle source

Post a comment

# File lib/dogapi/facade.rb, line 168
def comment(message, options = {})
  @comment_svc.comment(message, options)
end
create_dashboard(title, description, graphs, template_variables = nil) click to toggle source

Create a dashboard.

# File lib/dogapi/facade.rb, line 243
def create_dashboard(title, description, graphs, template_variables = nil)
  @dash_service.create_dashboard(title, description, graphs, template_variables)
end
create_embed(graph_json, description= {}) click to toggle source
# File lib/dogapi/facade.rb, line 340
def create_embed(graph_json, description= {})
  @embed_svc.create_embed(graph_json, description)
end
create_screenboard(description) click to toggle source

SCREENBOARD

# File lib/dogapi/facade.rb, line 355
def create_screenboard(description)
  @screenboard_svc.create_screenboard(description)
end
create_user(description = {}) click to toggle source
# File lib/dogapi/facade.rb, line 304
def create_user(description = {})
  @user_svc.create_user(description)
end
delete_alert(alert_id) click to toggle source
# File lib/dogapi/facade.rb, line 283
def delete_alert(alert_id)
  @alert_svc.delete_alert(alert_id)
end
delete_comment(comment_id) click to toggle source
# File lib/dogapi/facade.rb, line 177
def delete_comment(comment_id)
  @comment_svc.delete_comment(comment_id)
end
delete_dashboard(dash_id) click to toggle source

Delete the given dashboard.

# File lib/dogapi/facade.rb, line 263
def delete_dashboard(dash_id)
  @dash_service.delete_dashboard(dash_id)
end
delete_monitor(monitor_id) click to toggle source
# File lib/dogapi/facade.rb, line 399
def delete_monitor(monitor_id)
  @monitor_svc.delete_monitor(monitor_id)
end
delete_screenboard(board_id) click to toggle source
# File lib/dogapi/facade.rb, line 371
def delete_screenboard(board_id)
  @screenboard_svc.delete_screenboard(board_id)
end
detach_tags(host_id, source = nil) click to toggle source

Remove all tags from the given host

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

# File lib/dogapi/facade.rb, line 234
def detach_tags(host_id, source = nil)
  @tag_svc.detach(host_id, source)
end
detatch_tags(host_id) click to toggle source

DEPRECATED: Spelling mistake temporarily preserved as an alias.

# File lib/dogapi/facade.rb, line 226
def detatch_tags(host_id)
  warn "[DEPRECATION] Dogapi::Client.detatch() is deprecated. Use `detach` instead."
  detach_tags(host_id)
end
disable_user(handle) click to toggle source
# File lib/dogapi/facade.rb, line 320
def disable_user(handle)
  @user_svc.disable_user(handle)
end
emit_event(event, options = {}) click to toggle source

Record an event

Optional arguments:

:host        => String
:device      => String
# File lib/dogapi/facade.rb, line 122
def emit_event(event, options = {})
  scope = override_scope options

  @event_svc.post(event, scope)
end
emit_point(metric, value, options = {}) click to toggle source

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

# File lib/dogapi/facade.rb, line 60
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 = {}) click to toggle source

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

# File lib/dogapi/facade.rb, line 82
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) click to toggle source
# File lib/dogapi/facade.rb, line 344
def enable_embed(embed_id)
  @embed_svc.enable_embed(embed_id)
end
get_alert(alert_id) click to toggle source
# File lib/dogapi/facade.rb, line 279
def get_alert(alert_id)
  @alert_svc.get_alert(alert_id)
end
get_all_alerts() click to toggle source
# File lib/dogapi/facade.rb, line 287
def get_all_alerts()
  @alert_svc.get_all_alerts()
end
get_all_downtimes(options = {}) click to toggle source
# File lib/dogapi/facade.rb, line 443
def get_all_downtimes(options = {})
  @monitor_svc.get_all_downtimes(options)
end
get_all_embeds() click to toggle source

EMBEDS

# File lib/dogapi/facade.rb, line 332
def get_all_embeds()
  @embed_svc.get_all_embeds()
end
get_all_monitors(options = {}) click to toggle source
# File lib/dogapi/facade.rb, line 403
def get_all_monitors(options = {})
  @monitor_svc.get_all_monitors(options)
end
get_all_screenboards() click to toggle source
# File lib/dogapi/facade.rb, line 367
def get_all_screenboards()
  @screenboard_svc.get_all_screenboards()
end
get_all_users() click to toggle source
# File lib/dogapi/facade.rb, line 308
def get_all_users()
  @user_svc.get_all_users()
end
get_dashboard(dash_id) click to toggle source

Fetch the given dashboard.

# File lib/dogapi/facade.rb, line 253
def get_dashboard(dash_id)
  @dash_service.get_dashboard(dash_id)
end
get_dashboards() click to toggle source

Fetch all of the dashboards.

# File lib/dogapi/facade.rb, line 258
def get_dashboards
  @dash_service.get_dashboards
end
get_downtime(downtime_id) click to toggle source
# File lib/dogapi/facade.rb, line 435
def get_downtime(downtime_id)
  @monitor_svc.get_downtime(downtime_id)
end
get_embed(embed_id, description= {}) click to toggle source
# File lib/dogapi/facade.rb, line 336
def get_embed(embed_id, description= {})
  @embed_svc.get_embed(embed_id, description)
end
get_event(id) click to toggle source

Get the details of an event

id of the event to get

# File lib/dogapi/facade.rb, line 131
def get_event(id)
  @event_svc.get(id)
end
get_monitor(monitor_id, options = {}) click to toggle source
# File lib/dogapi/facade.rb, line 395
def get_monitor(monitor_id, options = {})
  @monitor_svc.get_monitor(monitor_id, options)
end
get_points(query, from, to) click to toggle source

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]

# File lib/dogapi/facade.rb, line 100
def get_points(query, from, to)
  @metric_svc.get(query, from, to)
end
get_screenboard(board_id) click to toggle source
# File lib/dogapi/facade.rb, line 363
def get_screenboard(board_id)
  @screenboard_svc.get_screenboard(board_id)
end
get_user(handle) click to toggle source
# File lib/dogapi/facade.rb, line 312
def get_user(handle)
  @user_svc.get_user(handle)
end
graph_snapshot(metric_query, start_ts, end_ts, event_query = nil) click to toggle source

Graph snapshot

# File lib/dogapi/facade.rb, line 325
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) click to toggle source

Get all tags for the given host

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

# File lib/dogapi/facade.rb, line 203
def host_tags(host_id, source = nil, by_source = false)
  @tag_svc.get(host_id, source, by_source)
end
invite(emails, options = {}) click to toggle source

User invite

# File lib/dogapi/facade.rb, line 300
def invite(emails, options = {})
  @user_svc.invite(emails, options)
end
monitor(type, query, options = {}) click to toggle source

MONITORS

# File lib/dogapi/facade.rb, line 387
def monitor(type, query, options = {})
  @monitor_svc.monitor(type, query, options)
end
mute_alerts() click to toggle source
# File lib/dogapi/facade.rb, line 291
def mute_alerts()
  @alert_svc.mute_alerts()
end
mute_host(hostname, options = {}) click to toggle source

HOST MUTING

# File lib/dogapi/facade.rb, line 451
def mute_host(hostname, options = {})
  @monitor_svc.mute_host(hostname, options)
end
mute_monitor(monitor_id, options = {}) click to toggle source
# File lib/dogapi/facade.rb, line 415
def mute_monitor(monitor_id, options = {})
  @monitor_svc.mute_monitor(monitor_id, options)
end
mute_monitors() click to toggle source
# File lib/dogapi/facade.rb, line 407
def mute_monitors()
  @monitor_svc.mute_monitors()
end
revoke_embed(embed_id) click to toggle source
# File lib/dogapi/facade.rb, line 348
def revoke_embed(embed_id)
  @embed_svc.revoke_embed(embed_id)
end
revoke_screenboard(board_id) click to toggle source
# File lib/dogapi/facade.rb, line 379
def revoke_screenboard(board_id)
  @screenboard_svc.revoke_screenboard(board_id)
end
schedule_downtime(scope, options = {}) click to toggle source

MONITOR DOWNTIME

# File lib/dogapi/facade.rb, line 427
def schedule_downtime(scope, options = {})
  @monitor_svc.schedule_downtime(scope, options)
end
service_check(check, host, status, options = {}) click to toggle source

SERVICE CHECKS

# File lib/dogapi/facade.rb, line 463
def service_check(check, host, status, options = {})
  @service_check_svc.service_check(check, host, status, options)
end
share_screenboard(board_id) click to toggle source
# File lib/dogapi/facade.rb, line 375
def share_screenboard(board_id)
  @screenboard_svc.share_screenboard(board_id)
end
start_event(event, options = {}) { || ... } click to toggle source

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

# File lib/dogapi/facade.rb, line 151
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 = {}) click to toggle source

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
# File lib/dogapi/facade.rb, line 145
def stream(start, stop, options = {})
  @event_svc.stream(start, stop, options)
end
unmute_alerts() click to toggle source
# File lib/dogapi/facade.rb, line 295
def unmute_alerts()
  @alert_svc.unmute_alerts()
end
unmute_host(hostname) click to toggle source
# File lib/dogapi/facade.rb, line 455
def unmute_host(hostname)
  @monitor_svc.unmute_host(hostname)
end
unmute_monitor(monitor_id, options = {}) click to toggle source
# File lib/dogapi/facade.rb, line 419
def unmute_monitor(monitor_id, options = {})
  @monitor_svc.unmute_monitor(monitor_id, options)
end
unmute_monitors() click to toggle source
# File lib/dogapi/facade.rb, line 411
def unmute_monitors()
  @monitor_svc.unmute_monitors()
end
update_alert(alert_id, query, options = {}) click to toggle source
# File lib/dogapi/facade.rb, line 275
def update_alert(alert_id, query, options = {})
  @alert_svc.update_alert(alert_id, query, options)
end
update_comment(comment_id, options = {}) click to toggle source

Post a comment

# File lib/dogapi/facade.rb, line 173
def update_comment(comment_id, options = {})
  @comment_svc.update_comment(comment_id, options)
end
update_dashboard(dash_id, title, description, graphs, template_variables = nil) click to toggle source

Update a dashboard.

# File lib/dogapi/facade.rb, line 248
def update_dashboard(dash_id, title, description, graphs, template_variables = nil)
  @dash_service.update_dashboard(dash_id, title, description, graphs, template_variables)
end
update_downtime(downtime_id, options = {}) click to toggle source
# File lib/dogapi/facade.rb, line 431
def update_downtime(downtime_id, options = {})
  @monitor_svc.update_downtime(downtime_id, options)
end
update_monitor(monitor_id, query, options = {}) click to toggle source
# File lib/dogapi/facade.rb, line 391
def update_monitor(monitor_id, query, options = {})
  @monitor_svc.update_monitor(monitor_id, query, options)
end
update_screenboard(board_id, description) click to toggle source
# File lib/dogapi/facade.rb, line 359
def update_screenboard(board_id, description)
  @screenboard_svc.update_screenboard(board_id, description)
end
update_tags(host_id, tags, source = nil) click to toggle source

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

# File lib/dogapi/facade.rb, line 221
def update_tags(host_id, tags, source = nil)
  @tag_svc.update(host_id, tags, source)
end
update_user(handle, description = {}) click to toggle source
# File lib/dogapi/facade.rb, line 316
def update_user(handle, description = {})
  @user_svc.update_user(handle, description)
end

Private Instance Methods

override_scope(options= {}) click to toggle source
# File lib/dogapi/facade.rb, line 469
def override_scope(options= {})
  defaults = { :host => @host, :device => @device }
  options = defaults.merge(options)
  Scope.new(options[:host], options[:device])
end