Class: DatadogExporter::Monitors::Import

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

Overview

This class provide import tools for Datadog Monitors

Whether they are templates or original monitors, it imports them from the base_path/monitors folder.

Instance Method Summary collapse

Constructor Details

#initialize(config: DatadogExporter::Client::Config.new, client: DatadogExporter::Client.new(config:), request: DatadogExporter::DatadogApiRequests::Monitors.new(config:, client:), template_manager: Utilities::TemplateManager.new(config:), file_class: File, existent_monitors_service: DatadogExporter::Monitors::Export.new) ⇒ Import

Returns a new instance of Import.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/datadog_exporter/monitors/import.rb', line 8

def initialize(
  config: DatadogExporter::Client::Config.new,
  client: DatadogExporter::Client.new(config:),
  request: DatadogExporter::DatadogApiRequests::Monitors.new(config:, client:),
  template_manager: Utilities::TemplateManager.new(config:),
  file_class: File,
  existent_monitors_service: DatadogExporter::Monitors::Export.new
)
  @config = config
  @request = request
  @monitors_base_path = config.base_path.join(DatadogExporter::Monitors::EXPORT_FOLDER)
  @template_manager = template_manager
  @file_class = file_class
  @existent_monitors_service = existent_monitors_service
end

Instance Method Details

#import(to:, tag: nil) ⇒ Object

Imports Datadog monitors configuration from YAML files.

* Loops all exported monitors in the base_path/monitors folder.
* Transforms the monitor into a template with placeholders.
* Replace the placeholders with the environment values
* Checks if the monitor already exists in the Datadog environment.
* Creates the monitor in the Datadog environment.

If a tag is provided, it imports only the monitors with that tag.

Parameters:

  • to (Symbol)

    The environment (defined in your organizations_config_filename) where the monitors will be imported

  • tag (String) (defaults to: nil)

    (optional) A tag defined in the Datadog monitors



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/datadog_exporter/monitors/import.rb', line 41

def import(to:, tag: nil)
  monitors = []

  list(tag: tag) do |monitor|
    template = @template_manager.create_template(monitor)
    monitor = @template_manager.create_monitor(template, environment: to)

    if exists?(monitor)
      puts "Monitor `#{monitor[:name]}` already exists on #{to}"
    elsif accepted?(monitor, to)
      monitors << @request.create(monitor)
    end
  end

  puts "monitors created: #{monitors.count}"
end

#list(tag: nil) ⇒ Object



24
25
26
# File 'lib/datadog_exporter/monitors/import.rb', line 24

def list(tag: nil, &)
  monitors.select { |monitor| tag.nil? || monitor[:tags].include?(tag) }.each(&)
end