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) ⇒ Import

Returns a new instance of Import.



8
9
10
11
12
13
14
15
16
17
18
19
20
# 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
)
  @config = config
  @request = request
  @monitors_base_path = config.base_path.join(DatadogExporter::Monitors::EXPORT_FOLDER)
  @template_manager = template_manager
  @file_class = file_class
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
* 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



34
35
36
37
38
39
40
41
42
# File 'lib/datadog_exporter/monitors/import.rb', line 34

def import(to:, tag: nil)
  monitors
    .select { |monitor| tag.nil? || monitor[:tags].include?(tag) }
    .each do |monitor|
      template = @template_manager.create_template(monitor)
      target_monitor = @template_manager.create_monitor(template, environment: to)
      @request.create(target_monitor) if accepted?(target_monitor, to)
    end
end