Class: DatadogSync
- Inherits:
-
Object
- Object
- DatadogSync
- Defined in:
- lib/datadog_sync/base.rb,
lib/datadog_sync/errors.rb,
lib/datadog_sync/version.rb,
lib/datadog_sync/save_dashboards.rb,
lib/datadog_sync/update_dashboards.rb
Constant Summary collapse
- DoesNotExist =
Class.new(RuntimeError)
- AlreadyExists =
Class.new(RuntimeError)
- VERSION =
'0.0.2'
Instance Attribute Summary collapse
-
#dd_client ⇒ Object
readonly
Returns the value of attribute dd_client.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
Instance Method Summary collapse
-
#initialize(api_key, app_key, options = {}) ⇒ DatadogSync
constructor
A new instance of DatadogSync.
- #save_dashboards(dashboards_path, title_pattern = "") ⇒ Object
- #update_dashboards(dashboards_path, title_pattern = "") ⇒ Object
Constructor Details
#initialize(api_key, app_key, options = {}) ⇒ DatadogSync
Returns a new instance of DatadogSync.
4 5 6 7 8 9 10 11 12 13 14 |
# File 'lib/datadog_sync/base.rb', line 4 def initialize(api_key, app_key, ={}) = { log_level: :info, log_target: STDOUT } = .merge() @dd_client = Dogapi::Client.new(api_key, app_key) set_logger() end |
Instance Attribute Details
#dd_client ⇒ Object (readonly)
Returns the value of attribute dd_client.
2 3 4 |
# File 'lib/datadog_sync/base.rb', line 2 def dd_client @dd_client end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
2 3 4 |
# File 'lib/datadog_sync/base.rb', line 2 def logger @logger end |
Instance Method Details
#save_dashboards(dashboards_path, title_pattern = "") ⇒ Object
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/datadog_sync/save_dashboards.rb', line 2 def save_dashboards(dashboards_path, title_pattern="") regex = Regexp.new(title_pattern) base_path = File.(dashboards_path) if File.file?(base_path) logger.error "Provided gashboards path already exists and it's not a directory." raise AlreadyExists elsif !File.directory?(base_path) logger.info "Creating directory for dashboards: '#{base_path}'" FileUtils.mkdir_p(base_path) end all_dashes = dd_client.get_dashboards[1]["dashes"] logger.info "Found #{all_dashes.count} dashboards" filtered_dashes = all_dashes.select { |dash| dash["title"] =~ regex } filtered_dashes_ids = filtered_dashes.collect { |dash| dash["id"] } logger.info "Saving #{filtered_dashes.count} dashboards with pattern /#{title_pattern}/ into '#{base_path}'" filtered_dashes_ids.each do |dash_id| dash_data = dd_client.get_dashboard(dash_id)[1]["dash"] filename = sanitize_filename(dash_data["title"]) filepath = File.join(base_path, "#{filename}.json") File.open(filepath, "wb") do |f| f.puts JSON.pretty_generate(dash_data) end end end |
#update_dashboards(dashboards_path, title_pattern = "") ⇒ Object
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/datadog_sync/update_dashboards.rb', line 2 def update_dashboards(dashboards_path, title_pattern="") regex = Regexp.new(title_pattern) base_path = File.(dashboards_path) unless File.directory?(base_path) logger.error "Provided gashboards path does not exist." raise DoesNotExist end all_dash_files = Dir.glob(File.join(base_path, "*")) logger.info "Found #{all_dash_files.count} local dashboards" filtered_dashes = [] all_dash_files.each do |file| data = JSON.parse(File.read(file)) filtered_dashes << data if data["title"] =~ regex end # filtered_dashes_ids = filtered_dashes.collect { |dash| dash["id"] } logger.info "Updating #{filtered_dashes.count} dashboards with pattern /#{title_pattern}/ from '#{base_path}'" filtered_dashes.each do |dash| dd_client.update_dashboard(dash["id"], dash["title"], dash["description"], dash["graphs"], dash["template_variables"]) end end |