Class: DatadogSync

Inherits:
Object
  • Object
show all
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

Overview

Tool to import/export Datadog dashboards.

  • Backup in case if someone accidentally deletes dashboard and ability to recreate it with single command

  • Ability to keep dashboards in git repo, make modifications using text editor, suggest Pull Requests

  • YAML format providing simpler way to describe dashboards without repeating a lot of things in chatty JSON. Example: github.com/xeron/datadog-sync/blob/master/examples/dashboard_example.yml

  • Automation purposes (for example creating new dashboard or adding graphs when you deploy new version of application)

Constant Summary collapse

DoesNotExist =

Raised when something doesn’t exist

Class.new(RuntimeError)
AlreadyExists =

Raised when something already exists

Class.new(RuntimeError)
VERSION =

Version of library

'0.0.3'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, app_key, options = {}) ⇒ DatadogSync

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



17
18
19
20
21
22
23
24
25
26
27
# 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

Instance Attribute Details

#dd_clientObject (readonly)

:nodoc:



7
8
9
# File 'lib/datadog_sync/base.rb', line 7

def dd_client
  @dd_client
end

#loggerObject (readonly)

:nodoc:



7
8
9
# File 'lib/datadog_sync/base.rb', line 7

def logger
  @logger
end

Instance Method Details

#save_dashboards(dashboards_path, title_pattern = "") ⇒ Object

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)



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# 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 = "") ⇒ Object

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)



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 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