Class: DatadogExporter::DatadogApiRequests::Monitors

Inherits:
Object
  • Object
show all
Defined in:
lib/datadog_exporter/datadog_api_requests/monitors.rb

Overview

The class that contains all the Datadog API client requests for Monitors

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config: DatadogExporter::Client::Config.new, client: DatadogExporter::Client.new(config:), monitors_api: DatadogAPIClient::V1::MonitorsAPI.new(client.datadog_client)) ⇒ Monitors

Returns a new instance of Monitors.



7
8
9
10
11
12
13
14
# File 'lib/datadog_exporter/datadog_api_requests/monitors.rb', line 7

def initialize(
  config: DatadogExporter::Client::Config.new,
  client: DatadogExporter::Client.new(config:),
  monitors_api: DatadogAPIClient::V1::MonitorsAPI.new(client.datadog_client)
)
  @client = client
  @monitors_api = monitors_api
end

Instance Attribute Details

#monitors_apiObject (readonly)

Returns the value of attribute monitors_api.



5
6
7
# File 'lib/datadog_exporter/datadog_api_requests/monitors.rb', line 5

def monitors_api
  @monitors_api
end

Instance Method Details

#create(monitor_hash) ⇒ Integer

Creates a monitor in Datadog

Parameters:

  • monitor_hash (Hash)

    The monitor configuration

Returns:

  • (Integer)

    The monitor ID



49
50
51
52
53
# File 'lib/datadog_exporter/datadog_api_requests/monitors.rb', line 49

def create(monitor_hash)
  monitor = @monitors_api.create_monitor(monitor_hash)

  monitor.id
end

#find(monitor_id) ⇒ Hash

Returns a Datadog monitor configuration.

Parameters:

  • monitor_id (Integer)

    The monitor ID in Datadog

Returns:

  • (Hash)

    The monitor configuration



38
39
40
41
# File 'lib/datadog_exporter/datadog_api_requests/monitors.rb', line 38

def find(monitor_id)
  monitor = @monitors_api.get_monitor(monitor_id)
  monitor.to_hash
end

#list_monitors(tag: nil) ⇒ Array<Hash>

Returns a list of Hashes containing Datadog monitor configuration. If no tag is provided, it returns all the monitors.

Parameters:

  • tag (String) (defaults to: nil)

    (optional) A tag defined in the Datadog monitors

Returns:

  • (Array<Hash>)

    A list of monitor configurations



23
24
25
26
27
28
29
30
# File 'lib/datadog_exporter/datadog_api_requests/monitors.rb', line 23

def list_monitors(tag: nil)
  monitors =
    @monitors_api
      .list_monitors # NOTE: Unfortunately, the filter provided by the API is not working https://github.com/DataDog/datadog-api-client-ruby/issues/2063
      .select { |monitor| tag.nil? || monitor.tags.include?(tag) }

  monitors.map(&:to_hash)
end