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

Fix DD_TAGS export with nodejs buildpack #155

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
18 changes: 4 additions & 14 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.py" "${DATADOG_DIR}/scripts/parse_env_vars.py"
cp "${ROOT_DIR}/lib/scripts/update_tags.rb" "${DATADOG_DIR}/scripts/update_tags.rb"

cp "${ROOT_DIR}/lib/scripts/parse_env_vars.py" "${DATADOG_DIR}/scripts/parse_env_vars.py"
cp "${ROOT_DIR}/lib/scripts/detect_buildpack.sh" "${DATADOG_DIR}/scripts/detect_buildpack.sh"

cp "${ROOT_DIR}/lib/scripts/nc.py" "${DATADOG_DIR}/scripts/nc.py"
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 @@ -50,25 +52,13 @@ if [ -f "${DATADOG_DIR}/dogstatsd" ]; then
fi

chmod +x "${DATADOG_DIR}/scripts/utils.sh"
chmod +x "${DATADOG_DIR}/scripts/detect_buildpack.sh"
chmod +x "${DATADOG_DIR}/scripts/check_datadog.sh"

chmod +x "${DATADOG_DIR}/trace-agent"
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
. "${DATADOG_DIR}/scripts/utils.sh"
dd_export_env "${DATADOG_DIR}/.raw_datadog_env"

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

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

# mark the script as finished, useful to sync the update_agent_config script
touch "${DATADOG_DIR}/.setup_completed"
18 changes: 4 additions & 14 deletions bin/supply
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ cp "${ROOT_DIR}/lib/scripts/create_logs_config.rb" "${DATADOG_DIR}/scripts/creat
cp "${ROOT_DIR}/lib/scripts/update_tags.rb" "${DATADOG_DIR}/scripts/update_tags.rb"

cp "${ROOT_DIR}/lib/scripts/parse_env_vars.py" "${DATADOG_DIR}/scripts/parse_env_vars.py"
cp "${ROOT_DIR}/lib/scripts/detect_buildpack.sh" "${DATADOG_DIR}/scripts/detect_buildpack.sh"

cp "${ROOT_DIR}/lib/scripts/nc.py" "${DATADOG_DIR}/scripts/nc.py"
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 @@ -50,25 +52,13 @@ if [ -f "${DATADOG_DIR}/dogstatsd" ]; then
fi

chmod +x "${DATADOG_DIR}/scripts/utils.sh"
chmod +x "${DATADOG_DIR}/scripts/detect_buildpack.sh"
chmod +x "${DATADOG_DIR}/scripts/check_datadog.sh"

chmod +x "${DATADOG_DIR}/trace-agent"
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
. "${DATADOG_DIR}/scripts/utils.sh"
dd_export_env "${DATADOG_DIR}/.raw_datadog_env"

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

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

# mark the script as finished, useful to sync the update_agent_config script
touch "${DATADOG_DIR}/.setup_completed"
touch "${DATADOG_DIR}/.setup_completed"
27 changes: 21 additions & 6 deletions lib/run-datadog.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,27 @@ DD_ENABLE_CAPI_METADATA_COLLECTION="${DD_ENABLE_CAPI_METADATA_COLLECTION:-false}
LOCKFILE="${DATADOG_DIR}/lock"
FIRST_RUN="${FIRST_RUN:-true}"
USER_TAGS="${DD_TAGS}"

# detect what language buildpack is running
. "${DATADOG_DIR}/scripts/detect_buildpack.sh"

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

# sanitize env vars and export a new a env file
python "${DATADOG_DIR}/scripts/parse_env_vars.py" "${DATADOG_DIR}/.raw_datadog_env" "${DATADOG_DIR}/.datadog_env"
# export DD_TAGS for ddtrace
DD_TAGS=$(python "${DATADOG_DIR}"/scripts/get_tags.py)
export DD_TAGS

DD_DOGSTATSD_TAGS=$(python "${DATADOG_DIR}"/scripts/get_tags.py)
export DD_DOGSTATSD_TAGS

source "${DATADOG_DIR}/scripts/utils.sh"
log_info "DD_TAGS separator: ${DD_TAGS_SEPARATOR}"

setup_datadog() {

pushd "${DATADOG_DIR}"

export DD_LOG_FILE="${DATADOG_DIR}/dogstatsd.log"
Expand Down Expand Up @@ -117,11 +130,13 @@ setup_datadog() {
}

start_datadog() {
DD_TAGS="${USER_TAGS}"
DD_TAGS=$(python "${DATADOG_DIR}"/scripts/get_tags.py)
export DD_TAGS
DD_DOGSTATSD_TAGS=$(python "${DATADOG_DIR}"/scripts/get_tags.py)
export DD_DOGSTATSD_TAGS
if [ "${FIRST_RUN}" != "true" ]; then
DD_TAGS="${USER_TAGS}"
DD_TAGS=$(python "${DATADOG_DIR}"/scripts/get_tags.py)
export DD_TAGS
DD_DOGSTATSD_TAGS=$(python "${DATADOG_DIR}"/scripts/get_tags.py)
export DD_DOGSTATSD_TAGS
fi
pushd "${DATADOG_DIR}"
export DD_LOG_FILE="${DATADOG_DIR}/dogstatsd.log"
export DD_API_KEY
Expand Down
3 changes: 2 additions & 1 deletion lib/scripts/create_logs_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
dd_env_file = "/home/vcap/app/.datadog/.sourced_env_datadog"
node_agent_tags = "/home/vcap/app/.datadog/node_agent_tags.txt"

dd_tags_separator = ENV.fetch('DD_TAGS_SEPARATOR', ' ')
logs_config_dir = ENV['LOGS_CONFIG_DIR']
logs_config = ENV['LOGS_CONFIG']
dd_tags = ENV['DD_TAGS']
Expand All @@ -34,7 +35,7 @@ def sanitize(tags_env_var, separator)
tags_list = []

if !dd_tags.nil?
tags_list += sanitize(dd_tags, " ")
tags_list += sanitize(dd_tags, dd_tags_separator)
else
puts "Could not find DD_TAGS env var"
end
Expand Down
18 changes: 18 additions & 0 deletions lib/scripts/detect_buildpack.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env bash

# Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2.0 License.
# This product includes software developed at Datadog (https://www.datadoghq.com/).
# Copyright 2022-Present Datadog, Inc.
HOME_DIR="${HOME_DIR:-/home/vcap/}"
DD_DETECTED_BUILDPACK=""
if [ -f "${HOME_DIR}"/staging_info.yml ]; then
DD_DETECTED_BUILDPACK=$(cat "${HOME_DIR}"/staging_info.yml | jq '.detected_buildpack')
elif [ -f "${BUILD_DIR}"/package.json ]; then
DD_DETECTED_BUILDPACK="node"
fi
if echo "${DD_DETECTED_BUILDPACK}" | grep -q "node" ; then
DD_TAGS_SEPARATOR=", "
else
DD_TAGS_SEPARATOR=" "
fi
export DD_TAGS_SEPARATOR
10 changes: 5 additions & 5 deletions lib/scripts/get_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ def parse_tags(tags):
delimiter = ','
if tags.count(' ') > tags.count(','):
delimiter = ' '
elif tags.count(' ') == tags.count(',') and len(tags) != 0:
delimiter = ', '
try:
return tags.split(delimiter)
except Exception as e:
Expand Down Expand Up @@ -78,8 +80,6 @@ def parse_tags(tags):
tags = [ tag.replace(" ", "_") for tag in tags ]
tags = list(dict.fromkeys(tags))

legacy_tags = os.environ.get('LEGACY_TAGS_FORMAT', False)
if legacy_tags:
print(','.join(tags))
else:
print(' '.join(tags))
separator = os.environ.get('DD_TAGS_SEPARATOR', " ")

print(separator.join(tags))
2 changes: 1 addition & 1 deletion lib/scripts/parse_env_vars.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
if len(env_parts) != 2:
continue
name, value = env_parts
if name not in ["export DD_TAGS", "export TAGS"]:
if name not in ["export DD_TAGS", "export TAGS", "export DD_TAGS_SEPARATOR"]:
value = value.replace(" ", "_")

# skip empty env vars
Expand Down
17 changes: 9 additions & 8 deletions lib/scripts/update_agent_config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ release_lock() {

write_tags_to_file() {
# combine DD_TAGS and DD_NODE_AGENT_TAGS into DD_TAGS
DD_TAGS=$(LEGACY_TAGS_FORMAT=true python "${DATADOG_DIR}"/scripts/get_tags.py)
DD_TAGS=$(python "${DATADOG_DIR}"/scripts/get_tags.py)
export DD_TAGS
DD_DOGSTATSD_TAGS=$(LEGACY_TAGS_FORMAT=true python "${DATADOG_DIR}"/scripts/get_tags.py)
DD_DOGSTATSD_TAGS=$(python "${DATADOG_DIR}"/scripts/get_tags.py)
export DD_DOGSTATSD_TAGS
export LOGS_CONFIG_DIR="${DATADOG_DIR}/dist/conf.d/logs.d"
export LOGS_CONFIG
Expand All @@ -36,12 +36,17 @@ write_tags_to_file() {
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}"

# finishing up
log_info "exporting .sourced_datadog_env file"
dd_export_env "${DATADOG_DIR}/.sourced_datadog_env"

}

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

Expand Down Expand Up @@ -92,10 +97,6 @@ main() {
# update node_agent_tags.txt
write_tags_to_file

# finishing up
log_info "exporting .sourced_datadog_env file"
dd_export_env "${DATADOG_DIR}/.sourced_datadog_env"

# mark to the monit_datadog function in run-datadog.sh that the script is finished
log_info "creating tags_updated file"
touch "${DATADOG_DIR}/tags_updated"
Expand Down
3 changes: 2 additions & 1 deletion lib/scripts/update_tags.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#!/usr/bin/env ruby

# env vars
DD_TAGS_SEPARATOR = ENV.fetch('DD_TAGS_SEPARATOR', " ")
DATADOG_DIR = ENV.fetch("DATADOG_DIR", "/home/vcap/app/.datadog")
DD_TAGS = ENV.fetch("DD_TAGS", "")
DD_NODE_AGENT_TAGS = ENV.fetch("DD_NODE_AGENT_TAGS", "")
Expand Down Expand Up @@ -33,7 +34,7 @@ def sanitize(tags_env_var, separator)
end

if ! DD_TAGS.empty?
tags.concat(sanitize(DD_TAGS, " "))
tags.concat(sanitize(DD_TAGS, DD_TAGS_SEPARATOR))
end

# if the script is executed during the warmup period, merge incoming tags with the existing tags
Expand Down
4 changes: 4 additions & 0 deletions lib/scripts/utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export DOGSTATSD_CMD="./dogstatsd start --cfgpath dist/"

# the logging level of the update_agent_script.log file
export DD_UPDATE_SCRIPT_LOG_LEVEL="${DD_UPDATE_SCRIPT_LOG_LEVEL:-"INFO"}"
export DD_TAGS_SEPARATOR="${DD_TAGS_SEPARATOR:-" "}"

dd_export_env() {
local env_file="$1"
Expand Down Expand Up @@ -59,6 +60,9 @@ dd_export_env() {
if [ -n "${DD_UPDATE_SCRIPT_LOG_LEVEL}" ]; then
echo "export DD_UPDATE_SCRIPT_LOG_LEVEL='${DD_UPDATE_SCRIPT_LOG_LEVEL}'" >> "${env_file}"
fi
if [ -n "${DD_TAGS_SEPARATOR}" ]; then
echo "export DD_TAGS_SEPARATOR='${DD_TAGS_SEPARATOR}'" >> "${env_file}"
fi
}

safe_source() {
Expand Down