datadog-exporter 0.2.3 → 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 179182c9b8d56a269d1f5c27dd4b1f8017123d9f392147c7abc06fe5e707400d
4
- data.tar.gz: ddc8dee960a3770b6b5a90122b5aef17bbc08befa0702599b34b4d0b9cdb05a7
3
+ metadata.gz: b142d610fdbc5f67a849a2d5e81f3cbb19c8f33e2803e5255a76928150985264
4
+ data.tar.gz: 109da7c30bddba8f4cc5af9a041d2b0af610806cf92ebc3d4af3854bd36f8a85
5
5
  SHA512:
6
- metadata.gz: 935ec9316bb8658244ab27f6531a4c9ab9f1d0e59f841bb0eff1a0d7f023517574eefbbcc2247af9ba3bbbec89f9f2746420d3253db873c84e193205e98638df
7
- data.tar.gz: 9c13205263070078039b50a0c3996f7babef73b5b8292416b0f9852c60499f4a60c262b9cabdde3ff968790e9016ac86c6bd8a0f0d5248d89f13d1dd7b01b44b
6
+ metadata.gz: ebeb609535f2c6728369fcde5be278fef901d4caaa3a5bb5e2f5a0bec8c5a838e6e38a76abf8252fb9141324586e158a655b55e92969d24895e85cfca3b59507
7
+ data.tar.gz: addb52c745f314d88dd4dafec06d068b92cde6ae6a6bec0a05ecad8771018e910af31e5dc00b861e040464d89d2a10a86fc51677e324d20f3569a90310c885f7
@@ -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,6 +31,7 @@ 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.
@@ -32,17 +39,38 @@ module DatadogExporter
32
39
  # @param [Symbol] to The environment (defined in your organizations_config_filename) where the monitors will be imported
33
40
  # @param [String] tag (optional) A tag defined in the Datadog monitors
34
41
  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
+ monitors = []
43
+
44
+ list(tag: tag) do |monitor|
45
+ template = @template_manager.create_template(monitor)
46
+ monitor = @template_manager.create_monitor(template, environment: to)
47
+
48
+ if exists?(monitor)
49
+ puts "Monitor `#{monitor[:name]}` already exists on #{to}"
50
+ elsif accepted?(monitor, to)
51
+ monitors << @request.create(monitor)
41
52
  end
53
+ end
54
+
55
+ puts "monitors created: #{monitors.count}"
42
56
  end
43
57
 
44
58
  private
45
59
 
60
+ def exists?(monitor_to_import)
61
+ existent_monitor =
62
+ existent_monitors.find { |monitor| monitor[:name] == monitor_to_import[:name] }
63
+
64
+ existent_monitor.inspect == monitor_to_import.inspect
65
+ end
66
+
67
+ def existent_monitors
68
+ @existent_monitors ||=
69
+ @existent_monitors_service.list.map do |monitor|
70
+ @template_manager.filter_by_template_keys(monitor)
71
+ end
72
+ end
73
+
46
74
  def monitors
47
75
  @monitors_base_path
48
76
  .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.4".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.4
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: 2024-12-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: datadog_api_client