class DatadogSync
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)
Constants
- AlreadyExists
Raised when something already exists
- DoesNotExist
Raised when something doesn’t exist
- VERSION
Version of library
Public Class Methods
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 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 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
# File lib/datadog_sync/base.rb, line 31 def sanitize_filename(filename) filename.gsub(/[\?\*\/\\]/, "_") end
# 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