class DatadogSync

Tool to import/export Datadog dashboards.

Constants

AlreadyExists

Raised when something already exists

DoesNotExist

Raised when something doesn’t exist

VERSION

Version of library

Public Class Methods

new(api_key, app_key, options={}) click to toggle source

Create DatadogSync instance.

Attributes

  • api_key - Datadog API key (String)

  • app_key - Datadog APP key (String)

  • options - Options (Hash)

    • log_level - :debug < :info < :warn < :error < :fatal < :unknown

    • log_target - log file path, STDOUT by default

# File lib/datadog_sync/base.rb, line 17
def initialize(api_key, app_key, options={})
  default_options = {
    log_level: :info,
    log_target: STDOUT
  }
  final_options = default_options.merge(options)

  @dd_client = Dogapi::Client.new(api_key, app_key)

  set_logger(final_options)
end

Public Instance Methods

save_dashboards(dashboards_path, title_pattern="") click to toggle source

Save dashboards from Datadog to the filesystem (export from DD).

Attributes

  • dashboards_path - local path where to save json files with dashboard data (String)

  • title_pattern - pattern to filter dashboards by name using regex, empty by default (String)

Returns

  • IDs of saved dashboards (Array)

# File lib/datadog_sync/save_dashboards.rb, line 9
def save_dashboards(dashboards_path, title_pattern="")
  regex = Regexp.new(title_pattern)
  base_path = File.expand_path(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"].to_i }

  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

  return filtered_dashes_ids
end
update_dashboards(dashboards_path, title_pattern="") click to toggle source

Update dashboards in Datadog from the local filesystem (import to DD).

Attributes

  • dashboards_path - local path where to load json files from (String)

  • title_pattern - pattern to filter dashboards by name using regex, empty by default (String)

Returns

  • IDs of updated dashboards (Array)

# File lib/datadog_sync/update_dashboards.rb, line 9
def update_dashboards(dashboards_path, title_pattern="")
  regex = Regexp.new(title_pattern)
  base_path = File.expand_path(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 = []
  filtered_dashes_ids = []
  all_dash_files.each do |file|
    data = JSON.parse(File.read(file))
    if data["title"] =~ regex
      filtered_dashes << data
      filtered_dashes_ids << data["id"]
    end
  end

  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

  return filtered_dashes_ids
end

Private Instance Methods

sanitize_filename(filename) click to toggle source
# File lib/datadog_sync/base.rb, line 31
def sanitize_filename(filename)
  filename.gsub(/[\?\*\/\\]/, "_")
end
set_logger(config) click to toggle source
# File lib/datadog_sync/base.rb, line 35
def set_logger(config)
  @logger = Logger.new(config[:log_target])
  @logger.level = config[:log_level]
  @logger.datetime_format = "%Y-%m-%d %H:%M:%S"
end