datadog-exporter 0.2.3 → 0.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 179182c9b8d56a269d1f5c27dd4b1f8017123d9f392147c7abc06fe5e707400d
4
- data.tar.gz: ddc8dee960a3770b6b5a90122b5aef17bbc08befa0702599b34b4d0b9cdb05a7
3
+ metadata.gz: 6382559bfdfc5253fef3781c39450805a266aa5ef2912b16bd4e4eaba4ea94fc
4
+ data.tar.gz: 869c9670567a052c6310c126cad2feabc779a1d3197bca5f053458859eed3d3f
5
5
  SHA512:
6
- metadata.gz: 935ec9316bb8658244ab27f6531a4c9ab9f1d0e59f841bb0eff1a0d7f023517574eefbbcc2247af9ba3bbbec89f9f2746420d3253db873c84e193205e98638df
7
- data.tar.gz: 9c13205263070078039b50a0c3996f7babef73b5b8292416b0f9852c60499f4a60c262b9cabdde3ff968790e9016ac86c6bd8a0f0d5248d89f13d1dd7b01b44b
6
+ metadata.gz: f2631aa925d8577f653a180687c5560296e1087c99fc8899aa5c1f6aa8ab843a8621395d6eb4d09fa71b5e3c4372d7084efeef03a84da9e7215e0b7840379219
7
+ data.tar.gz: a0970ff4712a47fe56b5f4df0e37f1be7d8164aa1ee006c6aa45dc61445901f2cb658104bd8a3d0f95b72187a00b5f0e0f218a6008c5b4383741165c90bcc419
@@ -21,6 +21,10 @@ module DatadogExporter
21
21
  @file_class = file_class
22
22
  end
23
23
 
24
+ def list(tag: nil, &)
25
+ monitors(tag:).each(&)
26
+ end
27
+
24
28
  ##
25
29
  # Exports Datadog monitors configuration in YAML files.
26
30
  # If no tag is provided, it exports all the monitors.
@@ -31,7 +35,7 @@ module DatadogExporter
31
35
  def export(tag: nil)
32
36
  reset_monitors_dir
33
37
 
34
- monitors(tag:).each { |exported_datadog_hash| save_original(exported_datadog_hash) }
38
+ list(tag:) { |exported_datadog_hash| save_original(exported_datadog_hash) }
35
39
 
36
40
  config.logger.info("Exported #{@monitors.count} monitors to #{@monitors_base_path}")
37
41
  end
@@ -48,7 +52,7 @@ module DatadogExporter
48
52
  def export_as_template(tag: nil)
49
53
  reset_monitors_dir
50
54
 
51
- monitors(tag:).each { |exported_datadog_hash| save_template(exported_datadog_hash) }
55
+ list(tag:) { |exported_datadog_hash| save_template(exported_datadog_hash) }
52
56
 
53
57
  config.logger.info(
54
58
  "Exported #{@monitors.count} monitor templates to #{@monitors_base_path}",
@@ -10,13 +10,19 @@ module DatadogExporter
10
10
  client: DatadogExporter::Client.new(config:),
11
11
  request: DatadogExporter::DatadogApiRequests::Monitors.new(config:, client:),
12
12
  template_manager: Utilities::TemplateManager.new(config:),
13
- file_class: File
13
+ file_class: File,
14
+ existent_monitors_service: DatadogExporter::Monitors::Export.new
14
15
  )
15
16
  @config = config
16
17
  @request = request
17
18
  @monitors_base_path = config.base_path.join(DatadogExporter::Monitors::EXPORT_FOLDER)
18
19
  @template_manager = template_manager
19
20
  @file_class = file_class
21
+ @existent_monitors_service = existent_monitors_service
22
+ end
23
+
24
+ def list(tag: nil, &)
25
+ monitors.select { |monitor| tag.nil? || monitor[:tags].include?(tag) }.each(&)
20
26
  end
21
27
 
22
28
  ##
@@ -25,24 +31,47 @@ module DatadogExporter
25
31
  # * Loops all exported monitors in the base_path/monitors folder.
26
32
  # * Transforms the monitor into a template with placeholders.
27
33
  # * Replace the placeholders with the environment values
34
+ # * Checks if the monitor already exists in the Datadog environment.
28
35
  # * Creates the monitor in the Datadog environment.
29
36
  #
30
37
  # If a tag is provided, it imports only the monitors with that tag.
31
38
  #
32
39
  # @param [Symbol] to The environment (defined in your organizations_config_filename) where the monitors will be imported
40
+ # @param [Symbol] from (optional) The environment (defined in your organizations_config_filename) where the placeholders are defined
33
41
  # @param [String] tag (optional) A tag defined in the Datadog monitors
34
- def import(to:, tag: nil)
35
- monitors
36
- .select { |monitor| tag.nil? || monitor[:tags].include?(tag) }
37
- .each do |monitor|
38
- template = @template_manager.create_template(monitor)
39
- target_monitor = @template_manager.create_monitor(template, environment: to)
40
- @request.create(target_monitor) if accepted?(target_monitor, to)
42
+ def import(to:, from: :base, tag: nil)
43
+ monitors = []
44
+
45
+ list(tag: tag) do |monitor|
46
+ template = @template_manager.create_template(monitor, environment: from)
47
+ monitor = @template_manager.create_monitor(template, environment: to)
48
+
49
+ if exists?(monitor)
50
+ puts "Monitor `#{monitor[:name]}` already exists on #{to}"
51
+ elsif accepted?(monitor, to)
52
+ monitors << @request.create(monitor)
41
53
  end
54
+ end
55
+
56
+ puts "monitors created: #{monitors.count}"
42
57
  end
43
58
 
44
59
  private
45
60
 
61
+ def exists?(monitor_to_import)
62
+ existent_monitor =
63
+ existent_monitors.find { |monitor| monitor[:name] == monitor_to_import[:name] }
64
+
65
+ existent_monitor.inspect == monitor_to_import.inspect
66
+ end
67
+
68
+ def existent_monitors
69
+ @existent_monitors ||=
70
+ @existent_monitors_service.list.map do |monitor|
71
+ @template_manager.filter_by_template_keys(monitor)
72
+ end
73
+ end
74
+
46
75
  def monitors
47
76
  @monitors_base_path
48
77
  .glob("*.yml")
@@ -18,6 +18,8 @@ module DatadogExporter
18
18
  replaced_string = original_string.dup
19
19
 
20
20
  placeholders.each do |placeholder_name, matching_text|
21
+ raise "Unknown placeholder value for #{placeholder_name}" if matching_text == ""
22
+
21
23
  placeholder = "#{placeholder_name}_placeholder"
22
24
  original_string.include?(matching_text) &&
23
25
  replaced_string.gsub!(matching_text, placeholder)
@@ -75,14 +77,14 @@ module DatadogExporter
75
77
  replace_placeholders_with_values(placeholders, filter_by_template_keys(template))
76
78
  end
77
79
 
78
- private
79
-
80
80
  def filter_by_template_keys(datadog_hash)
81
81
  return datadog_hash if template_keys.empty?
82
82
 
83
83
  datadog_hash.slice(*template_keys)
84
84
  end
85
85
 
86
+ private
87
+
86
88
  def replace_values_with_placeholders(hash_data, placeholders)
87
89
  return hash_data if placeholders.nil?
88
90
 
@@ -1,3 +1,3 @@
1
1
  module DatadogExporter
2
- VERSION = "0.2.3".freeze
2
+ VERSION = "0.2.5".freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: datadog-exporter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fran Martinez
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-12-30 00:00:00.000000000 Z
11
+ date: 2025-01-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: datadog_api_client