Skip to content
Merged
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ To enable reporting of Puppet runs to your Datadog timeline, enable the report p

5. (Optional) Enable tagging of reports with facts:

You can add tags to reports that are sent to Datadog as events. These tags can be sourced from Puppet facts for the given node the report is regarding. These should be 1:1 and not involve structured facts (hashes, arrays, etc.) to ensure readability. To enable tagging, set the parameter `datadog_agent::reports::report_fact_tags` to the array value of facts—for example `["virtual","trusted.extensions.pp_role","operatingsystem"]` results in three separate tags per report event.
You can add tags to reports that are sent to Datadog as events. These tags can be sourced from Puppet facts for the given node the report is regarding. These should be 1:1 and not involve structured facts (hashes, arrays, etc.) to ensure readability. To enable regular fact tagging, set the parameter `datadog_agent::reports::report_fact_tags` to the array value of facts—for example `["virtual","operatingsystem"]`. To enable trustd fact tagging, set the parameter `datadog_agent::reports::report_trusted_fact_tags` to the array value of facts—for example `["trusted.certname","trusted.extensions.pp_role","trusted hostname"]`.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
You can add tags to reports that are sent to Datadog as events. These tags can be sourced from Puppet facts for the given node the report is regarding. These should be 1:1 and not involve structured facts (hashes, arrays, etc.) to ensure readability. To enable regular fact tagging, set the parameter `datadog_agent::reports::report_fact_tags` to the array value of facts—for example `["virtual","operatingsystem"]`. To enable trustd fact tagging, set the parameter `datadog_agent::reports::report_trusted_fact_tags` to the array value of facts—for example `["trusted.certname","trusted.extensions.pp_role","trusted hostname"]`.
You can add tags to reports that are sent to Datadog as events. These tags can be sourced from Puppet facts for the given node the report is regarding. These should be 1:1 and not involve structured facts (hashes, arrays, etc.) to ensure readability.
To enable regular fact tagging, set the parameter `datadog_agent::reports::report_fact_tags` to the array value of facts—for example `["virtual","operatingsystem"]`.
To enable trusted fact tagging, set the parameter `datadog_agent::reports::report_trusted_fact_tags` to the array value of facts—for example `["trusted.certname","trusted.extensions.pp_role","trusted.hostname"]`.


NOTE: Changing these settings requires a restart of pe-puppetserver (or puppetserver) to re-read the report processor. Ensure the changes are deployed prior to restarting the service(s).

Expand Down
10 changes: 8 additions & 2 deletions lib/puppet/reports/datadog_reports.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
API_KEY = config[:datadog_api_key]
API_URL = config[:api_url]
REPORT_FACT_TAGS = config[:report_fact_tags] || []
REPORT_TRUSTED_FACT_TAGS = config[:report_trusted_fact_tags] || []

if ENV['DD_PROXY_HTTP'].nil?
ENV['DD_PROXY_HTTP'] = config[:proxy_http]
Expand Down Expand Up @@ -135,9 +136,13 @@ def process
end

facts = Puppet::Node::Facts.indirection.find(host).values
dog_tags = REPORT_FACT_TAGS.map { |name| "#{name}:#{facts.dig(*name.split('.'))}" }
facts_tags = REPORT_FACT_TAGS.map { |name| "#{name}:#{facts.dig(*name.split('.'))}" }
trusted_facts = (Puppet.lookup(:trusted_information) { Hash.new }).to_h
trusted_facts = { "trusted" => trusted_facts.to_h }
Copy link
Copy Markdown
Contributor

@albertvaka albertvaka Nov 6, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To make sure report_trusted_fact_tags is consistent with trusted_facts_to_tags. When accessing the $trusted variable from puppet code, do you have to use the trusted.* prefix? (eg: $trusted['trusted. certname']). Since trusted_facts_to_tags just digs in $trusted.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the examples section in Puppet docs [1], it looks like the trusted prefix doesn't have to be used when accessing $trusted, so I'm going to remove it from this PR and merge it.

[1] https://puppet.com/docs/puppet/5.5/lang_facts_and_builtin_vars.html#examples

trusted_fact_tags = REPORT_TRUSTED_FACT_TAGS.map { |name| "#{name}:#{trusted_facts.dig(*name.split('.'))}" }
dog_tags = facts_tags + trusted_fact_tags

Puppet.debug "Sending events for #{@msg_host} to Datadog"
Puppet.debug "Sending events for #{@msg_host} to Datadog. Trusted facts #{trusted_facts.to_s}. Trusted facts tags #{@trusted_fact_tags.to_s}."
@dog.emit_event(Dogapi::Event.new(event_data,
msg_title: event_title,
event_type: 'config_management.run',
Expand All @@ -147,5 +152,6 @@ def process
source_type_name: 'puppet',
tags: dog_tags),
host: @msg_host)
Puppet.info "Event sent for #{@msg_host} to Datadog"
end
end
10 changes: 8 additions & 2 deletions manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@
# $hiera_tags
# Boolean to grab tags from hiera to allow merging
# $facts_to_tags
# Optional array of facts' names that you can use to define tags following
# the scheme: "fact_name:fact_value".
# Optional array of facts' names that you can use to define tags following the scheme: "fact_name:fact_value".
# See also trusted_facts_to_tags.
# Note: this is not for Puppet Report Tags. See report_fact_tags
# $trusted_facts_to_tags
# Optional array of trusted facts' names that you can use to define tags following
# the scheme: "fact_name:fact_value".
# Note: this is not for Puppet Report Tags. See report_trusted_fact_tags
# $puppet_run_reports
# Will send results from your puppet agent runs back to the datadog service.
# $manage_dogapi_gem
Expand Down Expand Up @@ -67,6 +69,8 @@
# Set value of the 'dogstatsd_port' variable. Defaultis 8125.
# $report_fact_tags
# Sets tags for report events sent to Datadog from specified facts
# $report_trusted_fact_tags
# Sets tags for report events sent to Datadog from specified trusted facts
# $statsd_forward_host
# Set the value of the statsd_forward_host varable. Used to forward all
# statsd metrics to another host.
Expand Down Expand Up @@ -264,6 +268,7 @@
$dogstatsd_port = 8125,
$dogstatsd_socket = '',
Array $report_fact_tags = [],
Array $report_trusted_fact_tags = [],
String $statsd_forward_host = '',
$statsd_forward_port = '',
String $statsd_histogram_percentiles = '0.95',
Expand Down Expand Up @@ -785,6 +790,7 @@
proxy_http => $proxy_http,
proxy_https => $proxy_https,
report_fact_tags => $report_fact_tags,
report_trusted_fact_tags => $report_trusted_fact_tags,
}
}

Expand Down
1 change: 1 addition & 0 deletions manifests/reports.pp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
$proxy_http = undef,
$proxy_https = undef,
$report_fact_tags = [],
$report_trusted_fact_tags = [],
$datadog_site = 'datadoghq.com',
$puppet_gem_provider = $datadog_agent::params::gem_provider,
) inherits datadog_agent::params {
Expand Down
6 changes: 6 additions & 0 deletions templates/datadog-reports.yaml.erb
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,9 @@
- <%= tag %>
<%- end -%>
<% end -%>
<% if @report_trusted_fact_tags -%>
:report_trusted_fact_tags:
<%- @report_trusted_fact_tags.each do |tag| -%>
- <%= tag %>
<%- end -%>
<% end -%>