Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Standardize DD_TAGS format #162

Merged
merged 27 commits into from
Jul 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
3466a11
parse DD_TAGS using DD_TAGS_SEP as delimiter, defaults to comma
NouemanKHAL Apr 13, 2023
d611a15
update compile & supply scripts
NouemanKHAL Apr 14, 2023
a8ba46b
add ruby script to update the datadog.yaml config tags and dogstatsd_…
NouemanKHAL Apr 14, 2023
352173d
update get_tags.py script to export tags comma-separated
NouemanKHAL Apr 14, 2023
49744c5
unset DD_TAGS when starting agents + update DD_TAGS export logic
NouemanKHAL Apr 14, 2023
5666364
fix typo in DD_NODE_AGENT_TAGS name + cleanup
NouemanKHAL Apr 14, 2023
90949a0
update README
NouemanKHAL Apr 14, 2023
43b3dd5
update bin/compile to match bin/supply
NouemanKHAL Apr 17, 2023
a306ed3
Apply suggestions from code review
NouemanKHAL Apr 18, 2023
2dff705
small cleanup: remove LEGACY_TAGS_FORMAT
NouemanKHAL Apr 26, 2023
20e11b4
apply suggestion
NouemanKHAL Apr 26, 2023
859be6b
Update README.md
NouemanKHAL Apr 26, 2023
037f502
Add `buildpack_version` tag to logs, traces and metrics (#164)
NouemanKHAL May 25, 2023
e1b3995
Revert "Add `buildpack_version` tag to logs, traces and metrics (#164)"
NouemanKHAL Jul 12, 2023
cd4656b
Merge branch 'master' into noueman/standardize-dd-tags-format
NouemanKHAL Jul 12, 2023
fafbd81
remove extra line
NouemanKHAL Jul 12, 2023
60cd53a
cleanup merge
NouemanKHAL Jul 12, 2023
3d3bbdb
keep the tags separator logic backward-compatible
NouemanKHAL Jul 12, 2023
4870178
fix parse_tags method
NouemanKHAL Jul 12, 2023
bcc889b
writing ruby array via .to_yaml + trimming file contents on read
NouemanKHAL Jul 12, 2023
b83e5bb
remove the .to_yaml
NouemanKHAL Jul 12, 2023
682dece
.trim -> .strip
NouemanKHAL Jul 12, 2023
df319a0
some fixes and improvements in the ruby scripts
NouemanKHAL Jul 12, 2023
72e9fa5
fix race condition when updating datadog config and logs config tags
NouemanKHAL Jul 12, 2023
8927419
fix unbound variables in prepare.sh
NouemanKHAL Jul 12, 2023
6a54dca
update README
NouemanKHAL Jul 13, 2023
13f0036
Update README.md
NouemanKHAL Jul 17, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,27 @@ env:
DD_API_KEY: <DATADOG_API_KEY>
```

#### Assigning Tags

For an overview about tags, read [Getting Started with Tags](https://docs.datadoghq.com/getting_started/tagging/).

Custom tags can be configured with the environment variable `DD_TAGS`. These tags will be attached to the application logs, metrics, and traces as span tags.

By default, `DD_TAGS` is expected to be a comma separated list of tags.

```yaml
env:
DD_TAGS: "key1:value1,key2:value2,key3:value3"
```
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we also include a note about where the custom tags are included?


To use a different separator, set `DD_TAGS_SEP` to the desired separator.

```yaml
env:
DD_TAGS: "key1:value1 key2:value2 key3:value3"
DD_TAGS_SEP: " "
```

### Instrument your application

Instrument your application to send custom metrics and APM traces through DogStatsD and the Datadog Trace Agent.
Expand Down
16 changes: 7 additions & 9 deletions bin/compile
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ fi
cp "${ROOT_DIR}/lib/trace-agent" "${DATADOG_DIR}/trace-agent"

cp "${ROOT_DIR}/lib/scripts/create_logs_config.rb" "${DATADOG_DIR}/scripts/create_logs_config.rb"
cp "${ROOT_DIR}/lib/scripts/parse_env_vars.rb" "${DATADOG_DIR}/scripts/parse_env_vars.rb"
cp "${ROOT_DIR}/lib/scripts/update_datadog_config.rb" "${DATADOG_DIR}/scripts/update_datadog_config.rb"

cp "${ROOT_DIR}/lib/scripts/update_tags.rb" "${DATADOG_DIR}/scripts/update_tags.rb"

cp "${ROOT_DIR}/lib/scripts/parse_env_vars.rb" "${DATADOG_DIR}/scripts/parse_env_vars.rb"
cp "${ROOT_DIR}/lib/scripts/nc.rb" "${DATADOG_DIR}/scripts/nc.rb"
cp "${ROOT_DIR}/lib/scripts/utils.sh" "${DATADOG_DIR}/scripts/utils.sh"
cp "${ROOT_DIR}/lib/scripts/check_datadog.sh" "${DATADOG_DIR}/scripts/check_datadog.sh"
Expand All @@ -59,18 +61,14 @@ chmod +x "${BUILD_DIR}/.profile.d/00-test-endpoint.sh"
chmod +x "${BUILD_DIR}/.profile.d/02-redirect-logs.sh"
chmod +x "${BUILD_DIR}/.profile.d/01-run-datadog.sh"

# export DD environment variables
# import helper functions
. "${DATADOG_DIR}/scripts/utils.sh"

# export DD environment variables
dd_export_env "${DATADOG_DIR}/.raw_datadog_env"

# sanitize env vars and export a new a env file
ruby "${DATADOG_DIR}/scripts/parse_env_vars.rb" "${DATADOG_DIR}/.raw_datadog_env" "${DATADOG_DIR}/.datadog_env"

# export DD_TAGS for ddtrace
DD_TAGS=$(ruby "${DATADOG_DIR}"/scripts/get_tags.rb)
export DD_TAGS
DD_DOGSTATSD_TAGS=$(ruby "${DATADOG_DIR}"/scripts/get_tags.rb)
export DD_DOGSTATSD_TAGS

# mark the script as finished, useful to sync the update_agent_config script
touch "${DATADOG_DIR}/.setup_completed"
export DD_TAGS=$(ruby "${DATADOG_DIR}"/scripts/get_tags.rb)
14 changes: 6 additions & 8 deletions bin/supply
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ fi
cp "${ROOT_DIR}/lib/trace-agent" "${DATADOG_DIR}/trace-agent"

cp "${ROOT_DIR}/lib/scripts/create_logs_config.rb" "${DATADOG_DIR}/scripts/create_logs_config.rb"
cp "${ROOT_DIR}/lib/scripts/update_datadog_config.rb" "${DATADOG_DIR}/scripts/update_datadog_config.rb"

cp "${ROOT_DIR}/lib/scripts/update_tags.rb" "${DATADOG_DIR}/scripts/update_tags.rb"

cp "${ROOT_DIR}/lib/scripts/parse_env_vars.rb" "${DATADOG_DIR}/scripts/parse_env_vars.rb"
Expand Down Expand Up @@ -59,18 +61,14 @@ chmod +x "${BUILD_DIR}/.profile.d/00-test-endpoint.sh"
chmod +x "${BUILD_DIR}/.profile.d/02-redirect-logs.sh"
chmod +x "${BUILD_DIR}/.profile.d/01-run-datadog.sh"

# export DD environment variables
# import helper functions
. "${DATADOG_DIR}/scripts/utils.sh"

# export DD environment variables
dd_export_env "${DATADOG_DIR}/.raw_datadog_env"

# sanitize env vars and export a new a env file
ruby "${DATADOG_DIR}/scripts/parse_env_vars.rb" "${DATADOG_DIR}/.raw_datadog_env" "${DATADOG_DIR}/.datadog_env"

# export DD_TAGS for ddtrace
DD_TAGS=$(ruby "${DATADOG_DIR}"/scripts/get_tags.rb)
export DD_TAGS
DD_DOGSTATSD_TAGS=$(ruby "${DATADOG_DIR}"/scripts/get_tags.rb)
export DD_DOGSTATSD_TAGS

# mark the script as finished, useful to sync the update_agent_config script
touch "${DATADOG_DIR}/.setup_completed"
export DD_TAGS=$(ruby "${DATADOG_DIR}"/scripts/get_tags.rb)
38 changes: 18 additions & 20 deletions lib/run-datadog.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ SUPPRESS_DD_AGENT_OUTPUT="${SUPPRESS_DD_AGENT_OUTPUT:-true}"
DD_ENABLE_CAPI_METADATA_COLLECTION="${DD_ENABLE_CAPI_METADATA_COLLECTION:-false}"
LOCKFILE="${DATADOG_DIR}/lock"
FIRST_RUN="${FIRST_RUN:-true}"
USER_TAGS="${DD_TAGS}"
DD_TAGS=$(ruby "${DATADOG_DIR}"/scripts/get_tags.rb)
export DD_TAGS
DD_DOGSTATSD_TAGS=$(ruby "${DATADOG_DIR}"/scripts/get_tags.rb)
export DD_DOGSTATSD_TAGS

export DD_TAGS=$(ruby "${DATADOG_DIR}/scripts/get_tags.rb")
echo "${DD_TAGS}" > "${DATADOG_DIR}/.dd_tags.txt"

source "${DATADOG_DIR}/scripts/utils.sh"

Expand Down Expand Up @@ -97,10 +95,6 @@ setup_datadog() {
# Create folder for storing PID files
mkdir run


# DSD requires its own config file
cp dist/datadog.yaml dist/dogstatsd.yaml

if [ -a ./agent ] && { [ "${DD_LOGS_ENABLED}" = "true" ] || [ "${DD_ENABLE_CHECKS}" = "true" ]; }; then
if [ "${DD_LOGS_ENABLED}" = "true" -a "${DD_LOGS_VALID_ENDPOINT}" = "false" ]; then
echo "Log endpoint not valid, not starting agent"
Expand All @@ -114,14 +108,18 @@ setup_datadog() {
sed -i "s~log_file: AGENT_LOG_FILE~log_file: ${DD_LOG_FILE}~" dist/datadog.yaml
fi
popd

# update datadog config
ruby "${DATADOG_DIR}/scripts/update_datadog_config.rb"

# mark the script as finished, useful to sync the update_agent_config script
touch "${DATADOG_DIR}/.setup_completed"

}

start_datadog() {
DD_TAGS="${USER_TAGS}"
DD_TAGS=$(ruby "${DATADOG_DIR}"/scripts/get_tags.rb)
export DD_TAGS
DD_DOGSTATSD_TAGS=$(ruby "${DATADOG_DIR}"/scripts/get_tags.rb)
export DD_DOGSTATSD_TAGS
export DD_TAGS=$(ruby "${DATADOG_DIR}/scripts/get_tags.rb")

pushd "${DATADOG_DIR}"
export DD_LOG_FILE="${DATADOG_DIR}/dogstatsd.log"
export DD_API_KEY
Expand Down Expand Up @@ -154,27 +152,27 @@ start_datadog() {

echo "Starting Datadog agent"
if [ "${SUPPRESS_DD_AGENT_OUTPUT}" = "true" ]; then
./agent run --cfgpath dist/ --pidfile run/agent.pid > /dev/null 2>&1 &
env -u DD_TAGS ./agent run --cfgpath dist/ --pidfile run/agent.pid > /dev/null 2>&1 &
else
./agent run --cfgpath dist/ --pidfile run/agent.pid &
env -u DD_TAGS ./agent run --cfgpath dist/ --pidfile run/agent.pid &
fi
fi
else
echo "Starting dogstatsd agent"
export DD_LOG_FILE=dogstatsd.log
if [ "${SUPPRESS_DD_AGENT_OUTPUT}" = "true" ]; then
./dogstatsd start --cfgpath dist/ > /dev/null 2>&1 &
env -u DD_TAGS ./dogstatsd start --cfgpath dist/ > /dev/null 2>&1 &
else
./dogstatsd start --cfgpath dist/ &
env -u DD_TAGS ./dogstatsd start --cfgpath dist/ &
fi
echo $! > run/dogstatsd.pid
fi
if [ "${FIRST_RUN}" = "true" ]; then
echo "Starting trace agent"
if [ "${SUPPRESS_DD_AGENT_OUTPUT}" = "true" ]; then
./trace-agent --config dist/datadog.yaml --pid run/trace-agent.pid > /dev/null 2>&1 &
env -u DD_TAGS ./trace-agent --config dist/datadog.yaml --pid run/trace-agent.pid > /dev/null 2>&1 &
NouemanKHAL marked this conversation as resolved.
Show resolved Hide resolved
else
./trace-agent --config dist/datadog.yaml --pid run/trace-agent.pid &
env -u DD_TAGS ./trace-agent --config dist/datadog.yaml --pid run/trace-agent.pid &
fi
FIRST_RUN=false
fi
Expand Down
5 changes: 2 additions & 3 deletions lib/scripts/create_logs_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@

require 'json'

dd_env_file = "/home/vcap/app/.datadog/.sourced_env_datadog"
node_agent_tags = "/home/vcap/app/.datadog/node_agent_tags.txt"

logs_config_dir = ENV['LOGS_CONFIG_DIR']
logs_config = ENV['LOGS_CONFIG']
dd_tags = ENV['DD_TAGS']
dd_node_agent_tags = ENV['DD_NODE_AGENTS_TAGS'] || (File.file?(node_agent_tags) ? File.read(node_agent_tags) : "")
dd_node_agent_tags = ENV['DD_NODE_AGENT_TAGS'] || (File.file?(node_agent_tags) ? File.read(node_agent_tags) : "")

def sanitize(tags_env_var, separator)
tags_list = tags_env_var.gsub(",\"", ";\"").split(separator)
Expand Down Expand Up @@ -42,7 +41,7 @@ def sanitize(tags_env_var, separator)
if !dd_node_agent_tags.nil?
tags_list += sanitize(dd_node_agent_tags, ",")
else
puts "Could not find DD_NODE_AGENTS_TAGS env var"
puts "Could not find DD_NODE_AGENT_TAGS env var"
end

if !tags_list.empty?
Expand Down
43 changes: 26 additions & 17 deletions lib/scripts/get_tags.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@

require 'json'

# if DD_TAGS[0] is comma or space, then set is as delimiter
# else continue as usual
NODE_AGENT_TAGS_FILE = "/home/vcap/app/.datadog/node_agent_tags.txt"

def parse_tags(tags)
delimiter = ','
delimiter = ' ' if tags.count(' ') > tags.count(',')
begin
delimiter = ','
delimiter = ' ' if tags.count(' ') > tags.count(',')
Copy link
Member Author

Choose a reason for hiding this comment

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

@sarah-witt keeping this line will keep this change backwards-compatible

if !ENV["DD_TAGS_SEP"].nil?
delimiter = ENV["DD_TAGS_SEP"]
end
return tags.split(delimiter)
rescue Exception => e
puts "there was an issue parsing the tags in #{tags.__name__}: #{e}"
puts "there was an issue parsing the tags in '#{tags}': #{e.message}"
return []
end
end

Expand All @@ -38,7 +41,17 @@ def parse_tags(tags)
# we do this to separate commas inside json values from tags separator commas
node_agent_tags = node_agent_tags.gsub(",\"", ";\"")
all_node_agent_tags = parse_tags(node_agent_tags)
tags += all_node_agent_tags.reject { |tag| tag.include?(';') }
if !all_node_agent_tags.empty?
tags += all_node_agent_tags.keep_if { |tag| !tag.include?(';') }
end
end

node_agent_tags_file = File.file?(NODE_AGENT_TAGS_FILE) ? File.read(NODE_AGENT_TAGS_FILE).strip : nil
if node_agent_tags_file
node_agent_tags = parse_tags(node_agent_tags_file)
if !node_agent_tags.empty?
tags += node_agent_tags
end
end

vcap_variables.each do |vcap_var_name|
Expand All @@ -63,7 +76,7 @@ def parse_tags(tags)
user_tags = parse_tags(user_tags)
tags += user_tags
rescue Exception => e
puts "there was an issue parsing the tags in TAGS: #{e}"
puts "there was an issue parsing the tags in TAGS: #{e.message}"
end
end

Expand All @@ -73,21 +86,17 @@ def parse_tags(tags)
user_tags = parse_tags(user_tags)
tags += user_tags
rescue Exception => e
puts "there was an issue parsing the tags in DD_TAGS: #{e}"
puts "there was an issue parsing the tags in DD_TAGS: #{e.message}"
end
end

version_file = '/home/vcap/app/.datadog/VERSION'
if File.exist?(version_file)
buildpack_version = File.open(version_file, 'r') { |file| file.read.chomp }
tags << "buildpack_version:#{buildpack_version}"
buildpack_version = File.open(version_file, 'r') { |file| file.read.strip }
tags << "buildpack_version:#{buildpack_version.strip}"
end

tags = tags.map { |tag| tag.gsub(' ', '_') }.uniq
tags = tags.map { |tag| tag.gsub(' ', '_') }
tags = tags.uniq

legacy_tags = ENV['LEGACY_TAGS_FORMAT'] || false
if legacy_tags
puts tags.join(',')
else
puts tags.join(' ')
end
puts tags.join(',')
30 changes: 16 additions & 14 deletions lib/scripts/update_agent_config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,36 @@ release_lock() {
}

write_tags_to_file() {
# combine DD_TAGS and DD_NODE_AGENT_TAGS into DD_TAGS
DD_TAGS=$(LEGACY_TAGS_FORMAT=true ruby "${DATADOG_DIR}"/scripts/get_tags.rb)
export DD_TAGS
DD_DOGSTATSD_TAGS=$(LEGACY_TAGS_FORMAT=true ruby "${DATADOG_DIR}"/scripts/get_tags.rb)
export DD_DOGSTATSD_TAGS
export DD_TAGS=$(ruby "${DATADOG_DIR}"/scripts/get_tags.rb)

export LOGS_CONFIG_DIR="${DATADOG_DIR}/dist/conf.d/logs.d"
export LOGS_CONFIG

# update logs configs with the new tags
if [ -n "${LOGS_CONFIG}" ] && [ "${DD_ENABLE_CAPI_METADATA_COLLECTION}" = "true" ]; then
mkdir -p "${LOGS_CONFIG_DIR}"
log_info "Updating logs config"
ruby "${DATADOG_DIR}/scripts/create_logs_config.rb"
fi

log_info "Updating node_agent_tags.txt"
ruby "${DATADOG_DIR}/scripts/update_tags.rb"

# update datadog config
ruby "${DATADOG_DIR}/scripts/update_datadog_config.rb"

if [ "${DD_ENABLE_CAPI_METADATA_COLLECTION}" = "true" ]; then
# update logs configs
if [ -n "${LOGS_CONFIG}" ]; then
mkdir -p "${LOGS_CONFIG_DIR}"
log_info "Updating logs config"
ruby "${DATADOG_DIR}/scripts/create_logs_config.rb"
fi
fi

# log DD_TAGS and DD_NODE_AGENT_TAGS values
log_debug "node_agent_tags.txt=$(cat "${DATADOG_DIR}"/node_agent_tags.txt)"
log_debug "(AFTER)DD_NODE_AGENT_TAGS=${DD_NODE_AGENT_TAGS}"
log_debug "DD_DOGSTATSD_TAGS=${DD_DOGSTATSD_TAGS}"
log_debug "DD_TAGS=${DD_TAGS}"
}

main() {
# source relevant DD tags
while ! [ -f "${DATADOG_DIR}/.setup_completed" ]; do
echo "Supply script not completed, waiting ..."
echo "Datadog setup is not completed, waiting ..."
sleep 1
done

Expand Down
Loading
Loading