From 3466a111764f5659e246630de6deb0b2bb06dbc3 Mon Sep 17 00:00:00 2001 From: Noueman Khalikine Date: Fri, 14 Apr 2023 01:56:53 +0200 Subject: [PATCH 01/26] parse DD_TAGS using DD_TAGS_SEP as delimiter, defaults to comma --- lib/scripts/get_tags.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/scripts/get_tags.py b/lib/scripts/get_tags.py index 296faba..b9e0d2d 100644 --- a/lib/scripts/get_tags.py +++ b/lib/scripts/get_tags.py @@ -12,9 +12,7 @@ # else continue as usual def parse_tags(tags): - delimiter = ',' - if tags.count(' ') > tags.count(','): - delimiter = ' ' + delimiter = os.environ.get("DD_TAGS_SEP", ',') try: return tags.split(delimiter) except Exception as e: @@ -38,7 +36,7 @@ def parse_tags(tags): if node_agent_tags: # These are always comma separated # See https://github.com/DataDog/datadog-agent/blob/main/pkg/cloudfoundry/containertagger/container_tagger.go#L133 - + # we do this to separate commas inside json values from tags separator commas node_agent_tags = node_agent_tags.replace(",\"", ";\"") all_node_agent_tags = parse_tags(node_agent_tags) @@ -82,4 +80,4 @@ def parse_tags(tags): if legacy_tags: print(','.join(tags)) else: - print(' '.join(tags)) \ No newline at end of file + print(','.join(tags)) From d611a15076ec099e128af7ab60ba90a230bd21a9 Mon Sep 17 00:00:00 2001 From: Noueman Khalikine Date: Fri, 14 Apr 2023 20:00:46 +0200 Subject: [PATCH 02/26] update compile & supply scripts --- bin/compile | 14 ++++++-------- bin/supply | 12 ++++++------ 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/bin/compile b/bin/compile index cbd552b..4bb8a04 100755 --- a/bin/compile +++ b/bin/compile @@ -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_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.py" "${DATADOG_DIR}/scripts/parse_env_vars.py" 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" @@ -57,18 +59,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 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" \ No newline at end of file diff --git a/bin/supply b/bin/supply index 5bc8211..c879e37 100755 --- a/bin/supply +++ b/bin/supply @@ -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.py" "${DATADOG_DIR}/scripts/parse_env_vars.py" @@ -57,18 +59,16 @@ 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 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 +export DD_TAGS=$(python "${DATADOG_DIR}/scripts/get_tags.py") # mark the script as finished, useful to sync the update_agent_config script touch "${DATADOG_DIR}/.setup_completed" From a8ba46b246e3f7181e01c762f2305cab16e15fd3 Mon Sep 17 00:00:00 2001 From: Noueman Khalikine Date: Fri, 14 Apr 2023 20:01:19 +0200 Subject: [PATCH 03/26] add ruby script to update the datadog.yaml config tags and dogstatsd_tags --- lib/scripts/update_datadog_config.rb | 60 ++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 lib/scripts/update_datadog_config.rb diff --git a/lib/scripts/update_datadog_config.rb b/lib/scripts/update_datadog_config.rb new file mode 100644 index 0000000..a8533ef --- /dev/null +++ b/lib/scripts/update_datadog_config.rb @@ -0,0 +1,60 @@ +# 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. + +#!/usr/bin/env ruby + +require 'yaml' + +NODE_AGENT_TAGS_FILE = "/home/vcap/app/.datadog/node_agent_tags.txt" +DD_TAGS_FILE = "/home/vcap/app/.datadog/.dd_tags.txt" + +def sanitize(tags_env_var, delimiter) + tags_list = tags_env_var.gsub(",\"", ";\"").split(delimiter) + tags_list.keep_if { |element| !element.include?(";") } + tags_list = tags_list.map { |tag| tag.gsub(" ", "_") } + return tags_list.uniq +end + +def read_yaml_file(file_path) + yaml_file = File.read(file_path) + YAML.load(yaml_file) +end + +def write_yaml_file(file_path, data) + File.write(file_path, YAML.dump(data)) +end + +def get_tags() + dd_tags = File.file?(DD_TAGS_FILE) ? File.read(DD_TAGS_FILE) : nil + dd_node_agent_tags = ENV['DD_NODE_AGENT_TAGS'] || (File.file?(NODE_AGENT_TAGS_FILE) ? File.read(NODE_AGENT_TAGS_FILE) : nil) + + tags = [] + + if !dd_tags.nil? + tags += sanitize(dd_tags, ",") + end + + if !dd_node_agent_tags.nil? + tags += sanitize(dd_node_agent_tags, ",") + end + + return tags.uniq +end + +def main + tags = get_tags() + + if !tags.empty? + file_path = '/home/vcap/app/.datadog/dist/datadog.yaml' + + data = read_yaml_file(file_path) + + data['tags'] = tags + data['dogstatsd_tags'] = tags + + write_yaml_file(file_path, data) + end +end + +main \ No newline at end of file From 352173d21fe39412c43f3cffcf41e0f83a348ea8 Mon Sep 17 00:00:00 2001 From: Noueman Khalikine Date: Fri, 14 Apr 2023 20:01:50 +0200 Subject: [PATCH 04/26] update get_tags.py script to export tags comma-separated --- lib/scripts/get_tags.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/lib/scripts/get_tags.py b/lib/scripts/get_tags.py index b9e0d2d..12f9a6e 100644 --- a/lib/scripts/get_tags.py +++ b/lib/scripts/get_tags.py @@ -8,9 +8,6 @@ import json import sys -# if DD_TAGS[0] is comma or space, then set is as delimiter -# else continue as usual - def parse_tags(tags): delimiter = os.environ.get("DD_TAGS_SEP", ',') try: From 49744c53e5c2369945fcaa663dfbef22315281a5 Mon Sep 17 00:00:00 2001 From: Noueman Khalikine Date: Fri, 14 Apr 2023 20:03:56 +0200 Subject: [PATCH 05/26] unset DD_TAGS when starting agents + update DD_TAGS export logic --- lib/run-datadog.sh | 32 ++++++++++++-------------------- 1 file changed, 12 insertions(+), 20 deletions(-) diff --git a/lib/run-datadog.sh b/lib/run-datadog.sh index 7592c61..6b9484f 100644 --- a/lib/run-datadog.sh +++ b/lib/run-datadog.sh @@ -9,11 +9,8 @@ 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=$(python "${DATADOG_DIR}"/scripts/get_tags.py) -export DD_TAGS -DD_DOGSTATSD_TAGS=$(python "${DATADOG_DIR}"/scripts/get_tags.py) -export DD_DOGSTATSD_TAGS +export DD_TAGS=$(python "${DATADOG_DIR}/scripts/get_tags.py") +echo "${DD_TAGS}" > "${DATADOG_DIR}/.dd_tags.txt" source "${DATADOG_DIR}/scripts/utils.sh" @@ -97,10 +94,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" @@ -114,14 +107,13 @@ setup_datadog() { sed -i "s~log_file: AGENT_LOG_FILE~log_file: ${DD_LOG_FILE}~" dist/datadog.yaml fi popd >/dev/null + + # update datadog config + ruby "${DATADOG_DIR}/scripts/update_datadog_config.rb" } 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 + export DD_TAGS=$(python "${DATADOG_DIR}/scripts/get_tags.py") pushd "${DATADOG_DIR}" >/dev/null export DD_LOG_FILE="${DATADOG_DIR}/dogstatsd.log" export DD_API_KEY @@ -154,27 +146,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 & 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 From 56663643cc1a9cb3937838228d7333285ffa8f46 Mon Sep 17 00:00:00 2001 From: Noueman Khalikine Date: Fri, 14 Apr 2023 20:04:46 +0200 Subject: [PATCH 06/26] fix typo in DD_NODE_AGENT_TAGS name + cleanup --- lib/scripts/create_logs_config.rb | 5 +-- lib/scripts/update_agent_config.sh | 22 ++++++---- lib/scripts/update_tags.rb | 69 +++++++++++++++++------------- 3 files changed, 55 insertions(+), 41 deletions(-) diff --git a/lib/scripts/create_logs_config.rb b/lib/scripts/create_logs_config.rb index d53c491..e7495d9 100644 --- a/lib/scripts/create_logs_config.rb +++ b/lib/scripts/create_logs_config.rb @@ -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) @@ -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? diff --git a/lib/scripts/update_agent_config.sh b/lib/scripts/update_agent_config.sh index bc8308f..429c390 100644 --- a/lib/scripts/update_agent_config.sh +++ b/lib/scripts/update_agent_config.sh @@ -15,18 +15,22 @@ 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) - export DD_TAGS - DD_DOGSTATSD_TAGS=$(LEGACY_TAGS_FORMAT=true python "${DATADOG_DIR}"/scripts/get_tags.py) - export DD_DOGSTATSD_TAGS + export DD_TAGS=$(python "${DATADOG_DIR}"/scripts/get_tags.py) + 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" + 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" + ruby "${DATADOG_DIR}/scripts/update_datadog_config.rb" + fi + + # update datadog config + ruby "${DATADOG_DIR}/scripts/update_datadog_config.rb" fi log_info "Updating node_agent_tags.txt" diff --git a/lib/scripts/update_tags.rb b/lib/scripts/update_tags.rb index 5589db8..c3767aa 100644 --- a/lib/scripts/update_tags.rb +++ b/lib/scripts/update_tags.rb @@ -4,21 +4,10 @@ #!/usr/bin/env ruby -# env vars 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", "") DD_UPDATE_SCRIPT_WARMUP = ENV.fetch("DD_UPDATE_SCRIPT_WARMUP", "180") - -# file paths -timestamp_file = File.join(DATADOG_DIR, "startup_time") -node_agent_tags_file = File.join(DATADOG_DIR, "node_agent_tags.txt") - -# read startup time set by the buildpack supply script -timestamp = File.exists?(timestamp_file) ? File.read(timestamp_file).strip.to_i : 0 - -# storing all tags on this variable -tags = [] +NODE_AGENT_TAGS_FILE = File.join(DATADOG_DIR, "/node_agent_tags.txt") +DD_TAGS_FILE = File.join(DATADOG_DIR, "/.dd_tags.txt") def sanitize(tags_env_var, separator) tags_list = tags_env_var.gsub(",\"", ";\"").split(separator) @@ -28,25 +17,47 @@ def sanitize(tags_env_var, separator) return tags_list.uniq end -if ! DD_NODE_AGENT_TAGS.empty? - tags.concat(sanitize(DD_NODE_AGENT_TAGS, ",")) -end +def get_tags() + dd_tags = ENV['DD_TAGS'] ||File.file?(DD_TAGS_FILE) ? File.read(DD_TAGS_FILE) : nil + dd_node_agent_tags = ENV['DD_NODE_AGENT_TAGS'] || (File.file?(NODE_AGENT_TAGS_FILE) ? File.read(NODE_AGENT_TAGS_FILE) : nil) -if ! DD_TAGS.empty? - tags.concat(sanitize(DD_TAGS, " ")) -end + tags = [] + + if !dd_tags.nil? + tags += sanitize(dd_tags, ",") + end -# if the script is executed during the warmup period, merge incoming tags with the existing tags -# otherwise, override existing tags -if Time.now.to_i - timestamp <= DD_UPDATE_SCRIPT_WARMUP.to_i - if File.exists?(node_agent_tags_file) - node_tags = File.read(node_agent_tags_file).split(',') - tags.concat(node_tags) + if !dd_node_agent_tags.nil? + tags += sanitize(dd_node_agent_tags, ",") end + + return tags.uniq end -# remove duplicates -tags = tags.uniq +def main + timestamp_file = File.join(DATADOG_DIR, "startup_time") + node_agent_tags_file = File.join(DATADOG_DIR, "node_agent_tags.txt") + + # read startup time set by the buildpack supply script + timestamp = File.exists?(timestamp_file) ? File.read(timestamp_file).strip.to_i : 0 + + # storing all tags on this variable + tags = get_tags() + + # if the script is executed during the warmup period, merge incoming tags with the existing tags + # otherwise, override existing tags + if Time.now.to_i - timestamp <= DD_UPDATE_SCRIPT_WARMUP.to_i + if File.exists?(node_agent_tags_file) + node_tags = File.read(node_agent_tags_file).split(',') + tags.concat(node_tags) + end + end + + # remove duplicates + tags = tags.uniq + + # export tags + File.write(node_agent_tags_file, tags.join(",")) +end -# export tags -File.write(node_agent_tags_file, tags.join(",")) \ No newline at end of file +main \ No newline at end of file From 90949a0a318ed82f6976c51810ac7b8819b0c568 Mon Sep 17 00:00:00 2001 From: Noueman Khalikine Date: Fri, 14 Apr 2023 20:18:04 +0200 Subject: [PATCH 07/26] update README --- README.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/README.md b/README.md index 5158a17..ec3d71e 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,19 @@ env: DD_API_KEY: ``` +#### Assigning Tags + +Custom tags can be configured via the environmnet variable `DD_TAGS` in the application manifest. + +The tags must be a comma separated list. To use a different separator, set the `DD_TAGS_SEP` variable 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. From 43b3dd51a0e9f45cb6deb1b8265cd6cda8285bc7 Mon Sep 17 00:00:00 2001 From: Noueman Khalikine Date: Mon, 17 Apr 2023 19:44:41 +0200 Subject: [PATCH 08/26] update bin/compile to match bin/supply --- bin/compile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bin/compile b/bin/compile index 4bb8a04..a5368c4 100755 --- a/bin/compile +++ b/bin/compile @@ -2,7 +2,6 @@ echo "-----> DatadogBuildpack/compile" - BIN_DIR=$(cd $(dirname $0); pwd) ROOT_DIR=$(dirname ${BIN_DIR}) BUILD_DIR=$1 @@ -68,5 +67,7 @@ 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=$(python "${DATADOG_DIR}/scripts/get_tags.py") + # mark the script as finished, useful to sync the update_agent_config script touch "${DATADOG_DIR}/.setup_completed" \ No newline at end of file From a306ed313afd8e7b43316580e3551a50633dc3f6 Mon Sep 17 00:00:00 2001 From: NouemanKHAL Date: Tue, 18 Apr 2023 16:28:34 +0200 Subject: [PATCH 09/26] Apply suggestions from code review Co-authored-by: May Lee --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ec3d71e..e82fa4e 100644 --- a/README.md +++ b/README.md @@ -39,9 +39,9 @@ env: #### Assigning Tags -Custom tags can be configured via the environmnet variable `DD_TAGS` in the application manifest. +Custom tags can be configured with the environment variable `DD_TAGS` in the application manifest. -The tags must be a comma separated list. To use a different separator, set the `DD_TAGS_SEP` variable to the desired separator. +The tags must be a comma separated list. To use a different separator, set `DD_TAGS_SEP` to the desired separator. ```yaml env: From 2dff7052beddda2b8ca8fcef3e92dc3176462753 Mon Sep 17 00:00:00 2001 From: Noueman Khalikine Date: Wed, 26 Apr 2023 15:17:05 +0200 Subject: [PATCH 10/26] small cleanup: remove LEGACY_TAGS_FORMAT --- lib/scripts/get_tags.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/lib/scripts/get_tags.py b/lib/scripts/get_tags.py index 12f9a6e..02233d3 100644 --- a/lib/scripts/get_tags.py +++ b/lib/scripts/get_tags.py @@ -73,8 +73,4 @@ 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)) +print(','.join(tags)) From 20e11b4d37a507addf63d632aed3b9ab142a1a29 Mon Sep 17 00:00:00 2001 From: Noueman Khalikine Date: Wed, 26 Apr 2023 15:20:30 +0200 Subject: [PATCH 11/26] apply suggestion --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index e82fa4e..f9439ce 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,7 @@ env: DD_TAGS_SEP: "," ``` +Custom tags will be added to the application logs, metrics, and traces as Span tags. ### Instrument your application From 859be6b3b0995bff3f641d860e090a63c2956901 Mon Sep 17 00:00:00 2001 From: NouemanKHAL Date: Wed, 26 Apr 2023 19:09:11 +0200 Subject: [PATCH 12/26] Update README.md Co-authored-by: May Lee --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f9439ce..7ea605a 100644 --- a/README.md +++ b/README.md @@ -49,7 +49,7 @@ env: DD_TAGS_SEP: "," ``` -Custom tags will be added to the application logs, metrics, and traces as Span tags. +Custom tags are added to the application logs, metrics, and traces as span tags. ### Instrument your application From 037f502a29aadf88e3eb5acfa69c8af7cd25dc9d Mon Sep 17 00:00:00 2001 From: NouemanKHAL Date: Thu, 25 May 2023 11:52:24 +0200 Subject: [PATCH 13/26] Add `buildpack_version` tag to logs, traces and metrics (#164) --- bin/compile | 2 ++ bin/supply | 2 ++ lib/scripts/get_tags.py | 6 ++++++ 3 files changed, 10 insertions(+) diff --git a/bin/compile b/bin/compile index a5368c4..5728678 100755 --- a/bin/compile +++ b/bin/compile @@ -43,6 +43,8 @@ cp -r "${ROOT_DIR}/lib/test-endpoint.sh" "${BUILD_DIR}/.profile.d/00-test-endpoi cp "${ROOT_DIR}/lib/run-datadog.sh" "${BUILD_DIR}/.profile.d/01-run-datadog.sh" cp "${ROOT_DIR}/lib/redirect-logs.sh" "${BUILD_DIR}/.profile.d/02-redirect-logs.sh" +cp "${ROOT_DIR}/VERSION" "${DATADOG_DIR}/VERSION" + if [ -f "${DATADOG_DIR}/agent" ]; then chmod +x "${DATADOG_DIR}/agent" fi diff --git a/bin/supply b/bin/supply index c879e37..96cd9c8 100755 --- a/bin/supply +++ b/bin/supply @@ -44,6 +44,8 @@ cp -r "${ROOT_DIR}/lib/test-endpoint.sh" "${BUILD_DIR}/.profile.d/00-test-endpoi cp "${ROOT_DIR}/lib/run-datadog.sh" "${BUILD_DIR}/.profile.d/01-run-datadog.sh" cp "${ROOT_DIR}/lib/redirect-logs.sh" "${BUILD_DIR}/.profile.d/02-redirect-logs.sh" +cp "${ROOT_DIR}/VERSION" "${DATADOG_DIR}/VERSION" + if [ -f "${DATADOG_DIR}/agent" ]; then chmod +x "${DATADOG_DIR}/agent" fi diff --git a/lib/scripts/get_tags.py b/lib/scripts/get_tags.py index 02233d3..ea90ff9 100644 --- a/lib/scripts/get_tags.py +++ b/lib/scripts/get_tags.py @@ -70,6 +70,12 @@ def parse_tags(tags): except Exception as e: print("there was an issue parsing the tags in DD_TAGS: {}".format(e)) +version_file = "/home/vcap/app/.datadog/VERSION" +if os.path.exists(version_file): + with open(version_file, 'r') as file: + buildpack_version = file.read().rstrip() + tags.append("buildpack_version:{}".format(buildpack_version)) + tags = [ tag.replace(" ", "_") for tag in tags ] tags = list(dict.fromkeys(tags)) From e1b3995695c3dca7b16bbd630d54e8443ccc9bcf Mon Sep 17 00:00:00 2001 From: Noueman Khalikine Date: Wed, 12 Jul 2023 14:15:40 +0200 Subject: [PATCH 14/26] Revert "Add `buildpack_version` tag to logs, traces and metrics (#164)" This reverts commit 037f502a29aadf88e3eb5acfa69c8af7cd25dc9d. --- bin/compile | 2 -- bin/supply | 2 -- lib/scripts/get_tags.py | 6 ------ 3 files changed, 10 deletions(-) diff --git a/bin/compile b/bin/compile index 5728678..a5368c4 100755 --- a/bin/compile +++ b/bin/compile @@ -43,8 +43,6 @@ cp -r "${ROOT_DIR}/lib/test-endpoint.sh" "${BUILD_DIR}/.profile.d/00-test-endpoi cp "${ROOT_DIR}/lib/run-datadog.sh" "${BUILD_DIR}/.profile.d/01-run-datadog.sh" cp "${ROOT_DIR}/lib/redirect-logs.sh" "${BUILD_DIR}/.profile.d/02-redirect-logs.sh" -cp "${ROOT_DIR}/VERSION" "${DATADOG_DIR}/VERSION" - if [ -f "${DATADOG_DIR}/agent" ]; then chmod +x "${DATADOG_DIR}/agent" fi diff --git a/bin/supply b/bin/supply index 96cd9c8..c879e37 100755 --- a/bin/supply +++ b/bin/supply @@ -44,8 +44,6 @@ cp -r "${ROOT_DIR}/lib/test-endpoint.sh" "${BUILD_DIR}/.profile.d/00-test-endpoi cp "${ROOT_DIR}/lib/run-datadog.sh" "${BUILD_DIR}/.profile.d/01-run-datadog.sh" cp "${ROOT_DIR}/lib/redirect-logs.sh" "${BUILD_DIR}/.profile.d/02-redirect-logs.sh" -cp "${ROOT_DIR}/VERSION" "${DATADOG_DIR}/VERSION" - if [ -f "${DATADOG_DIR}/agent" ]; then chmod +x "${DATADOG_DIR}/agent" fi diff --git a/lib/scripts/get_tags.py b/lib/scripts/get_tags.py index ea90ff9..02233d3 100644 --- a/lib/scripts/get_tags.py +++ b/lib/scripts/get_tags.py @@ -70,12 +70,6 @@ def parse_tags(tags): except Exception as e: print("there was an issue parsing the tags in DD_TAGS: {}".format(e)) -version_file = "/home/vcap/app/.datadog/VERSION" -if os.path.exists(version_file): - with open(version_file, 'r') as file: - buildpack_version = file.read().rstrip() - tags.append("buildpack_version:{}".format(buildpack_version)) - tags = [ tag.replace(" ", "_") for tag in tags ] tags = list(dict.fromkeys(tags)) From fafbd8179db995176059be0ff72dd12d307be6f2 Mon Sep 17 00:00:00 2001 From: Noueman Khalikine Date: Wed, 12 Jul 2023 14:30:20 +0200 Subject: [PATCH 15/26] remove extra line --- lib/scripts/update_agent_config.sh | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/scripts/update_agent_config.sh b/lib/scripts/update_agent_config.sh index 2467f85..bd89063 100644 --- a/lib/scripts/update_agent_config.sh +++ b/lib/scripts/update_agent_config.sh @@ -28,9 +28,7 @@ write_tags_to_file() { mkdir -p "${LOGS_CONFIG_DIR}" log_info "Updating logs config" ruby "${DATADOG_DIR}/scripts/create_logs_config.rb" - ruby "${DATADOG_DIR}/scripts/update_datadog_config.rb" fi - # update datadog config ruby "${DATADOG_DIR}/scripts/update_datadog_config.rb" fi From 60cd53a8b9f9f7e6ff91284f0f8ff188c7e5a612 Mon Sep 17 00:00:00 2001 From: Noueman Khalikine Date: Wed, 12 Jul 2023 14:37:04 +0200 Subject: [PATCH 16/26] cleanup merge --- bin/compile | 12 ++++++------ bin/supply | 5 +---- lib/run-datadog.sh | 18 ++++++++---------- lib/scripts/get_tags.rb | 9 +++------ lib/scripts/update_agent_config.sh | 9 +++------ 5 files changed, 21 insertions(+), 32 deletions(-) diff --git a/bin/compile b/bin/compile index 3ec3a44..37e1841 100755 --- a/bin/compile +++ b/bin/compile @@ -2,6 +2,7 @@ echo "-----> DatadogBuildpack/compile" + BIN_DIR=$(cd $(dirname $0); pwd) ROOT_DIR=$(dirname ${BIN_DIR}) BUILD_DIR=$1 @@ -30,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" @@ -68,10 +71,7 @@ dd_export_env "${DATADOG_DIR}/.raw_datadog_env" 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 +export DD_TAGS=$(ruby "${DATADOG_DIR}"/scripts/get_tags.rb) # mark the script as finished, useful to sync the update_agent_config script -touch "${DATADOG_DIR}/.setup_completed" \ No newline at end of file +touch "${DATADOG_DIR}/.setup_completed" diff --git a/bin/supply b/bin/supply index af68aa7..50e1be8 100755 --- a/bin/supply +++ b/bin/supply @@ -71,10 +71,7 @@ dd_export_env "${DATADOG_DIR}/.raw_datadog_env" 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 +export DD_TAGS=$(ruby "${DATADOG_DIR}"/scripts/get_tags.rb) # mark the script as finished, useful to sync the update_agent_config script touch "${DATADOG_DIR}/.setup_completed" diff --git a/lib/run-datadog.sh b/lib/run-datadog.sh index c9ae795..b9b6113 100644 --- a/lib/run-datadog.sh +++ b/lib/run-datadog.sh @@ -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" @@ -110,14 +108,14 @@ 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" } 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 diff --git a/lib/scripts/get_tags.rb b/lib/scripts/get_tags.rb index 8f57f9a..2c99dc1 100644 --- a/lib/scripts/get_tags.rb +++ b/lib/scripts/get_tags.rb @@ -10,6 +10,8 @@ def parse_tags(tags) delimiter = ',' delimiter = ' ' if tags.count(' ') > tags.count(',') + if len(os.environ.get("DD_TAGS_SEP")) > 0: + delimiter = os.environ.get("DD_TAGS_SEP") begin return tags.split(delimiter) rescue Exception => e @@ -85,9 +87,4 @@ def parse_tags(tags) tags = tags.map { |tag| tag.gsub(' ', '_') }.uniq -legacy_tags = ENV['LEGACY_TAGS_FORMAT'] || false -if legacy_tags - puts tags.join(',') -else - puts tags.join(' ') -end +puts tags.join(',') diff --git a/lib/scripts/update_agent_config.sh b/lib/scripts/update_agent_config.sh index bd89063..10e206d 100644 --- a/lib/scripts/update_agent_config.sh +++ b/lib/scripts/update_agent_config.sh @@ -14,11 +14,8 @@ 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 @@ -39,7 +36,7 @@ write_tags_to_file() { # 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() { From 3d3bbdbac75746cff4a24e4be0e2e67b1c0d5319 Mon Sep 17 00:00:00 2001 From: Noueman Khalikine Date: Wed, 12 Jul 2023 14:47:05 +0200 Subject: [PATCH 17/26] keep the tags separator logic backward-compatible --- lib/scripts/get_tags.rb | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/lib/scripts/get_tags.rb b/lib/scripts/get_tags.rb index 2c99dc1..9c09430 100644 --- a/lib/scripts/get_tags.rb +++ b/lib/scripts/get_tags.rb @@ -4,19 +4,18 @@ require 'json' -# if DD_TAGS[0] is comma or space, then set is as delimiter -# else continue as usual - def parse_tags(tags) delimiter = ',' delimiter = ' ' if tags.count(' ') > tags.count(',') - if len(os.environ.get("DD_TAGS_SEP")) > 0: - delimiter = os.environ.get("DD_TAGS_SEP") - begin - return tags.split(delimiter) + + if ENV["DD_TAGS_SEP"].is_set? + 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}" - end end vcap_app_string = ENV['VCAP_APPLICATION'] || '{}' From 487017848489b3c596cb0150ae2370b86967615a Mon Sep 17 00:00:00 2001 From: Noueman Khalikine Date: Wed, 12 Jul 2023 16:03:16 +0200 Subject: [PATCH 18/26] fix parse_tags method --- lib/scripts/get_tags.rb | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/lib/scripts/get_tags.rb b/lib/scripts/get_tags.rb index 9c09430..58ff6df 100644 --- a/lib/scripts/get_tags.rb +++ b/lib/scripts/get_tags.rb @@ -5,17 +5,17 @@ require 'json' def parse_tags(tags) - delimiter = ',' - delimiter = ' ' if tags.count(' ') > tags.count(',') - - if ENV["DD_TAGS_SEP"].is_set? - delimiter = ENV["DD_TAGS_SEP"] - end - - return tags.split(delimiter) - + begin + delimiter = ',' + delimiter = ' ' if tags.count(' ') > tags.count(',') + 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 vcap_app_string = ENV['VCAP_APPLICATION'] || '{}' @@ -39,7 +39,9 @@ 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.select { |tag| !tag.include?(';') } + end end vcap_variables.each do |vcap_var_name| @@ -64,7 +66,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 @@ -74,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 DD_TAGS: #{e}" + puts "there was an issue parsing the tags in DD_TAGS: #{e.message}" end end From bcc889b534544b5ec3342f11e1e6747acc885403 Mon Sep 17 00:00:00 2001 From: Noueman Khalikine Date: Wed, 12 Jul 2023 16:49:51 +0200 Subject: [PATCH 19/26] writing ruby array via .to_yaml + trimming file contents on read --- lib/scripts/get_tags.rb | 4 ++-- lib/scripts/update_datadog_config.rb | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/scripts/get_tags.rb b/lib/scripts/get_tags.rb index 58ff6df..6fabda8 100644 --- a/lib/scripts/get_tags.rb +++ b/lib/scripts/get_tags.rb @@ -40,7 +40,7 @@ def parse_tags(tags) node_agent_tags = node_agent_tags.gsub(",\"", ";\"") all_node_agent_tags = parse_tags(node_agent_tags) if !all_node_agent_tags.empty? - tags += all_node_agent_tags.select { |tag| !tag.include?(';') } + tags += all_node_agent_tags.keep_if { |tag| !tag.include?(';') } end end @@ -82,7 +82,7 @@ def parse_tags(tags) version_file = '/home/vcap/app/.datadog/VERSION' if File.exist?(version_file) - buildpack_version = File.open(version_file, 'r') { |file| file.read.chomp } + buildpack_version = File.open(version_file, 'r') { |file| file.read.chomp.trim } tags << "buildpack_version:#{buildpack_version}" end diff --git a/lib/scripts/update_datadog_config.rb b/lib/scripts/update_datadog_config.rb index a8533ef..65c1382 100644 --- a/lib/scripts/update_datadog_config.rb +++ b/lib/scripts/update_datadog_config.rb @@ -27,7 +27,7 @@ def write_yaml_file(file_path, data) def get_tags() dd_tags = File.file?(DD_TAGS_FILE) ? File.read(DD_TAGS_FILE) : nil - dd_node_agent_tags = ENV['DD_NODE_AGENT_TAGS'] || (File.file?(NODE_AGENT_TAGS_FILE) ? File.read(NODE_AGENT_TAGS_FILE) : nil) + dd_node_agent_tags = ENV['DD_NODE_AGENT_TAGS'] || (File.file?(NODE_AGENT_TAGS_FILE) ? File.read(NODE_AGENT_TAGS_FILE).chomp.trim : nil) tags = [] @@ -50,8 +50,8 @@ def main data = read_yaml_file(file_path) - data['tags'] = tags - data['dogstatsd_tags'] = tags + data['tags'] = tags.to_yaml + data['dogstatsd_tags'] = tags.to_yaml write_yaml_file(file_path, data) end From b83e5bb7dbe366e507aea1c9305ca989be709c38 Mon Sep 17 00:00:00 2001 From: Noueman Khalikine Date: Wed, 12 Jul 2023 17:18:32 +0200 Subject: [PATCH 20/26] remove the .to_yaml --- lib/scripts/update_datadog_config.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/scripts/update_datadog_config.rb b/lib/scripts/update_datadog_config.rb index 65c1382..08b5be3 100644 --- a/lib/scripts/update_datadog_config.rb +++ b/lib/scripts/update_datadog_config.rb @@ -50,8 +50,8 @@ def main data = read_yaml_file(file_path) - data['tags'] = tags.to_yaml - data['dogstatsd_tags'] = tags.to_yaml + data['tags'] = tags + data['dogstatsd_tags'] = tags write_yaml_file(file_path, data) end From 682decef50dbf5fbcd6c92884f381069dc3aee6f Mon Sep 17 00:00:00 2001 From: Noueman Khalikine Date: Wed, 12 Jul 2023 17:34:48 +0200 Subject: [PATCH 21/26] .trim -> .strip --- lib/scripts/get_tags.rb | 2 +- lib/scripts/update_datadog_config.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/scripts/get_tags.rb b/lib/scripts/get_tags.rb index 6fabda8..05890f5 100644 --- a/lib/scripts/get_tags.rb +++ b/lib/scripts/get_tags.rb @@ -82,7 +82,7 @@ def parse_tags(tags) version_file = '/home/vcap/app/.datadog/VERSION' if File.exist?(version_file) - buildpack_version = File.open(version_file, 'r') { |file| file.read.chomp.trim } + buildpack_version = File.open(version_file, 'r') { |file| file.read.chomp.strip } tags << "buildpack_version:#{buildpack_version}" end diff --git a/lib/scripts/update_datadog_config.rb b/lib/scripts/update_datadog_config.rb index 08b5be3..6f7c3bc 100644 --- a/lib/scripts/update_datadog_config.rb +++ b/lib/scripts/update_datadog_config.rb @@ -27,7 +27,7 @@ def write_yaml_file(file_path, data) def get_tags() dd_tags = File.file?(DD_TAGS_FILE) ? File.read(DD_TAGS_FILE) : nil - dd_node_agent_tags = ENV['DD_NODE_AGENT_TAGS'] || (File.file?(NODE_AGENT_TAGS_FILE) ? File.read(NODE_AGENT_TAGS_FILE).chomp.trim : nil) + dd_node_agent_tags = ENV['DD_NODE_AGENT_TAGS'] || (File.file?(NODE_AGENT_TAGS_FILE) ? File.read(NODE_AGENT_TAGS_FILE).chomp.strip : nil) tags = [] From df319a0d3737ba50a226920403f9e6fac7072073 Mon Sep 17 00:00:00 2001 From: Noueman Khalikine Date: Wed, 12 Jul 2023 17:55:53 +0200 Subject: [PATCH 22/26] some fixes and improvements in the ruby scripts --- lib/scripts/get_tags.rb | 7 ++++--- lib/scripts/update_datadog_config.rb | 8 +++++--- lib/scripts/update_tags.rb | 2 +- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/lib/scripts/get_tags.rb b/lib/scripts/get_tags.rb index 05890f5..6d333df 100644 --- a/lib/scripts/get_tags.rb +++ b/lib/scripts/get_tags.rb @@ -82,10 +82,11 @@ def parse_tags(tags) version_file = '/home/vcap/app/.datadog/VERSION' if File.exist?(version_file) - buildpack_version = File.open(version_file, 'r') { |file| file.read.chomp.strip } - 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 puts tags.join(',') diff --git a/lib/scripts/update_datadog_config.rb b/lib/scripts/update_datadog_config.rb index 6f7c3bc..825a17b 100644 --- a/lib/scripts/update_datadog_config.rb +++ b/lib/scripts/update_datadog_config.rb @@ -18,7 +18,7 @@ def sanitize(tags_env_var, delimiter) def read_yaml_file(file_path) yaml_file = File.read(file_path) - YAML.load(yaml_file) + return YAML.load(yaml_file) end def write_yaml_file(file_path, data) @@ -26,8 +26,8 @@ def write_yaml_file(file_path, data) end def get_tags() - dd_tags = File.file?(DD_TAGS_FILE) ? File.read(DD_TAGS_FILE) : nil - dd_node_agent_tags = ENV['DD_NODE_AGENT_TAGS'] || (File.file?(NODE_AGENT_TAGS_FILE) ? File.read(NODE_AGENT_TAGS_FILE).chomp.strip : nil) + dd_tags = File.file?(DD_TAGS_FILE) ? File.read(DD_TAGS_FILE).strip : nil + dd_node_agent_tags = ENV['DD_NODE_AGENT_TAGS'] || (File.file?(NODE_AGENT_TAGS_FILE) ? File.read(NODE_AGENT_TAGS_FILE).strip : nil) tags = [] @@ -48,6 +48,8 @@ def main if !tags.empty? file_path = '/home/vcap/app/.datadog/dist/datadog.yaml' + puts "updating datadog.yaml with the tags: '#{tags}'" + data = read_yaml_file(file_path) data['tags'] = tags diff --git a/lib/scripts/update_tags.rb b/lib/scripts/update_tags.rb index df7c93a..1f9de42 100644 --- a/lib/scripts/update_tags.rb +++ b/lib/scripts/update_tags.rb @@ -13,7 +13,7 @@ def sanitize(tags_env_var, separator) tags_list = tags_env_var.gsub(",\"", ";\"").split(separator) tags_list.keep_if { |element| !element.include?(";") } tags_list.keep_if { |element| !element.include?("app_instance_guid") } - tags_list = tags_list.map { |tag| tag.gsub(" ", "_") } + tags_list = tags_list.map { |tag| tag.gsub(" ", "_").strip } return tags_list.uniq end From 72e9fa592af45c874cc53706664b89edfdb8c1e2 Mon Sep 17 00:00:00 2001 From: Noueman Khalikine Date: Thu, 13 Jul 2023 00:17:20 +0200 Subject: [PATCH 23/26] fix race condition when updating datadog config and logs config tags --- bin/compile | 5 +---- bin/supply | 5 +---- lib/run-datadog.sh | 4 ++++ lib/scripts/get_tags.rb | 10 ++++++++++ lib/scripts/update_agent_config.sh | 13 +++++++------ lib/scripts/update_datadog_config.rb | 4 ++-- 6 files changed, 25 insertions(+), 16 deletions(-) diff --git a/bin/compile b/bin/compile index 37e1841..8c43150 100755 --- a/bin/compile +++ b/bin/compile @@ -71,7 +71,4 @@ dd_export_env "${DATADOG_DIR}/.raw_datadog_env" ruby "${DATADOG_DIR}/scripts/parse_env_vars.rb" "${DATADOG_DIR}/.raw_datadog_env" "${DATADOG_DIR}/.datadog_env" # export DD_TAGS for ddtrace -export DD_TAGS=$(ruby "${DATADOG_DIR}"/scripts/get_tags.rb) - -# 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) \ No newline at end of file diff --git a/bin/supply b/bin/supply index 50e1be8..2f701c0 100755 --- a/bin/supply +++ b/bin/supply @@ -71,7 +71,4 @@ dd_export_env "${DATADOG_DIR}/.raw_datadog_env" ruby "${DATADOG_DIR}/scripts/parse_env_vars.rb" "${DATADOG_DIR}/.raw_datadog_env" "${DATADOG_DIR}/.datadog_env" # export DD_TAGS for ddtrace -export DD_TAGS=$(ruby "${DATADOG_DIR}"/scripts/get_tags.rb) - -# 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) \ No newline at end of file diff --git a/lib/run-datadog.sh b/lib/run-datadog.sh index b9b6113..130526c 100644 --- a/lib/run-datadog.sh +++ b/lib/run-datadog.sh @@ -111,6 +111,10 @@ setup_datadog() { # 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() { diff --git a/lib/scripts/get_tags.rb b/lib/scripts/get_tags.rb index 6d333df..3d52995 100644 --- a/lib/scripts/get_tags.rb +++ b/lib/scripts/get_tags.rb @@ -4,6 +4,8 @@ require 'json' +NODE_AGENT_TAGS_FILE = "/home/vcap/app/.datadog/node_agent_tags.txt" + def parse_tags(tags) begin delimiter = ',' @@ -44,6 +46,14 @@ def parse_tags(tags) 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| vcap_var = vcap_application[vcap_var_name] next unless vcap_var diff --git a/lib/scripts/update_agent_config.sh b/lib/scripts/update_agent_config.sh index 10e206d..703ed53 100644 --- a/lib/scripts/update_agent_config.sh +++ b/lib/scripts/update_agent_config.sh @@ -19,6 +19,12 @@ write_tags_to_file() { export LOGS_CONFIG_DIR="${DATADOG_DIR}/dist/conf.d/logs.d" export LOGS_CONFIG + 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 @@ -26,13 +32,8 @@ write_tags_to_file() { log_info "Updating logs config" ruby "${DATADOG_DIR}/scripts/create_logs_config.rb" fi - # update datadog config - ruby "${DATADOG_DIR}/scripts/update_datadog_config.rb" fi - log_info "Updating node_agent_tags.txt" - ruby "${DATADOG_DIR}/scripts/update_tags.rb" - # 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}" @@ -42,7 +43,7 @@ write_tags_to_file() { 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 diff --git a/lib/scripts/update_datadog_config.rb b/lib/scripts/update_datadog_config.rb index 825a17b..66cd39a 100644 --- a/lib/scripts/update_datadog_config.rb +++ b/lib/scripts/update_datadog_config.rb @@ -26,8 +26,8 @@ def write_yaml_file(file_path, data) end def get_tags() - dd_tags = File.file?(DD_TAGS_FILE) ? File.read(DD_TAGS_FILE).strip : nil - dd_node_agent_tags = ENV['DD_NODE_AGENT_TAGS'] || (File.file?(NODE_AGENT_TAGS_FILE) ? File.read(NODE_AGENT_TAGS_FILE).strip : nil) + dd_tags = ENV['DD_TAGS'] || File.file?(DD_TAGS_FILE) ? File.read(DD_TAGS_FILE).strip : nil + dd_node_agent_tags = File.file?(NODE_AGENT_TAGS_FILE) ? File.read(NODE_AGENT_TAGS_FILE).strip : nil tags = [] From 892741999bc2580a53fff88faacd04a69ca09f5a Mon Sep 17 00:00:00 2001 From: Noueman Khalikine Date: Thu, 13 Jul 2023 00:47:24 +0200 Subject: [PATCH 24/26] fix unbound variables in prepare.sh --- scripts/prepare.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/prepare.sh b/scripts/prepare.sh index 49d6b07..b505ef1 100755 --- a/scripts/prepare.sh +++ b/scripts/prepare.sh @@ -61,11 +61,11 @@ function main() { DOWNLOAD="true" elif [ ! -f ${SRCDIR}/lib/agent ]; then DOWNLOAD="true" - elif [ -n "${REFRESH_ASSETS}" ]; then + elif [ -n "${REFRESH_ASSETS:-}" ]; then DOWNLOAD="true" fi - if [ -n "${DOWNLOAD}" ]; then + if [ -n "${DOWNLOAD:-}" ]; then # Delete the old ones rm -f ${SRCDIR}/lib/agent rm -f ${SRCDIR}/lib/dogstatsd From 6a54dca28324f35b9dddbf6453f324d346870aba Mon Sep 17 00:00:00 2001 From: Noueman Khalikine Date: Thu, 13 Jul 2023 14:01:40 +0200 Subject: [PATCH 25/26] update README --- README.md | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index e7ba1e9..515b138 100644 --- a/README.md +++ b/README.md @@ -39,17 +39,24 @@ env: #### Assigning Tags -Custom tags can be configured with the environment variable `DD_TAGS` in the application manifest. +For an overview about tags, read the [Getting Started with Tags](https://docs.datadoghq.com/getting_started/tagging/). -The tags must be a comma separated list. To use a different separator, set `DD_TAGS_SEP` to the desired separator. +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" - DD_TAGS_SEP: "," ``` -Custom tags are added to the application logs, metrics, and traces as span tags. +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 From 13f0036f1d6be9f2e45c57ff4ec0998629696443 Mon Sep 17 00:00:00 2001 From: NouemanKHAL Date: Mon, 17 Jul 2023 18:26:29 +0200 Subject: [PATCH 26/26] Update README.md Co-authored-by: Sarah Witt --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 515b138..e1bb27f 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ env: #### Assigning Tags -For an overview about tags, read the [Getting Started with Tags](https://docs.datadoghq.com/getting_started/tagging/). +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.