#
# Cookbook:: irrigate-datadog
# Recipe:: logs
#

# logs should be an array of hashes, like this (json):
#
# "logs": [
#   {
#     "type": "file",
#     "path": "/path/to/service_name_error.log",
#     "source": "php",
#     "sourcecategory": "error",
#     "service": "prod-oa", <-- should match the APM name
#     "tags": [
#       "service_name:oa",
#       "environment:prod"
#     ]
#   },
#

log_files = node['datadog']['logs']

unless log_files.empty?
  # Enable logs agent if log files are specified
  node.default['datadog']['enable_logs_agent'] = true

  # Make sure datadog agent can read each specified log file.
  log_files.each do |log_file|
    file log_file['path'] do
      group node['datadog']['group']
      only_if { ::File.exist?(log_file['path']) }
    end
  end
end

datadog_monitor 'logs' do
  logs log_files
  use_integration_template true
  action :add
  notifies :restart, 'service[datadog-agent]' if node['datadog']['agent_start']
end
