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 tag parsing in DCA tags injection #144

Merged
merged 6 commits into from
Dec 5, 2022
Merged
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
1 change: 1 addition & 0 deletions bin/compile
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ fi
cp $ROOT_DIR/lib/trace-agent $BUILD_DIR/.datadog/trace-agent

cp $ROOT_DIR/lib/scripts/create_logs_config.py $BUILD_DIR/.datadog/scripts/create_logs_config.py
cp $ROOT_DIR/lib/scripts/parse_env_vars.py $BUILD_DIR/.datadog/scripts/parse_env_vars.py
cp $ROOT_DIR/lib/scripts/nc.py $BUILD_DIR/.datadog/scripts/nc.py
cp $ROOT_DIR/lib/scripts/utils.sh $BUILD_DIR/.datadog/scripts/utils.sh

Expand Down
2 changes: 2 additions & 0 deletions bin/supply
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ fi
cp $ROOT_DIR/lib/trace-agent $BUILD_DIR/.datadog/trace-agent

cp $ROOT_DIR/lib/scripts/create_logs_config.py $BUILD_DIR/.datadog/scripts/create_logs_config.py
cp $ROOT_DIR/lib/scripts/parse_env_vars.py $BUILD_DIR/.datadog/scripts/parse_env_vars.py
cp $ROOT_DIR/lib/scripts/nc.py $BUILD_DIR/.datadog/scripts/nc.py

cp $ROOT_DIR/lib/scripts/utils.sh $BUILD_DIR/.datadog/scripts/utils.sh

cp $ROOT_DIR/lib/scripts/update_agent_config_restart.sh $BUILD_DIR/.datadog/scripts/update_agent_config_restart.sh
Expand Down
24 changes: 15 additions & 9 deletions lib/scripts/create_logs_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,33 @@
LOGS_CONFIG = os.environ.get('LOGS_CONFIG')
DD_TAGS = os.environ.get('DD_TAGS')


config = {}

if not LOGS_CONFIG_DIR:
if LOGS_CONFIG_DIR is None:
print("ERROR: `LOGS_CONFIG_DIR` must be set in order to collect logs. For more info, see: https://github.com/DataDog/datadog-cloudfoundry-buildpack#log-collection")
exit(1)

if LOGS_CONFIG:
if LOGS_CONFIG is not None:
config["logs"] = json.loads(LOGS_CONFIG)

if DD_TAGS:
if DD_TAGS is not None:
config["logs"][0]["tags"] = DD_TAGS
else:
print("Could not find DD_TAGS env var")

else:
print("ERROR: `LOGS_CONFIG` must be set in order to collect logs. For more info, see: https://github.com/DataDog/datadog-cloudfoundry-buildpack#log-collection")
exit(1)

config = json.dumps(config)

path = LOGS_CONFIG_DIR + "/logs.yaml"

with open(path, 'w') as f:
print("writing {} to {}".format(config, path))
f.write(config)
f.write("\n")
try:
if not os.path.exists(LOGS_CONFIG_DIR):
os.makedirs(LOGS_CONFIG_DIR)
with open(path, 'w+') as f:
print("writing {} to {}".format(config, path))
f.write(config)
f.write("\n")
except Exception as e:
print("Could not write to log file: {}".format(str(e)))
2 changes: 1 addition & 1 deletion lib/scripts/get_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@
except Exception as e:
print("there was an issue parsing the tags in DD_TAGS: {}".format(e))

NouemanKHAL marked this conversation as resolved.
Show resolved Hide resolved
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))
Expand Down
24 changes: 24 additions & 0 deletions lib/scripts/parse_env_vars.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# 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.

import sys

if len(sys.argv) > 1:
env_file_name = sys.argv[1]
with open(env_file_name, 'r') as env_file:
env_vars = env_file.readlines()

if len(sys.argv) > 2:
new_env_file_name = sys.argv[2]
with open(new_env_file_name, 'w+') as new_env_file:
for env_var in env_vars:
env_var = env_var.strip()
if env_var.startswith("#"):
continue
env_var = env_var.replace(" ", "_")
new_env_file.write("export {}\n".format(env_var))
else:
print("Destination file not specified")
else:
print("Source file not specified")
11 changes: 7 additions & 4 deletions lib/scripts/update_agent_config_restart.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@ DATADOG_DIR="${DATADOG_DIR:-/home/vcap/app/.datadog}"
SUPPRESS_DD_AGENT_OUTPUT="${SUPPRESS_DD_AGENT_OUTPUT:-true}"

# correct way to export / source the .datadog_env file so that every variable is parsed
export $(grep -v '^#' $DATADOG_DIR/.datadog_env | xargs)
python "$DATADOG_DIR/scripts/parse_env_vars.py" "$DATADOG_DIR/.datadog_env" "$DATADOG_DIR/.new_datadog_env"
source "$DATADOG_DIR/.new_datadog_env"

export DD_TAGS=$(LEGACY_TAGS_FORMAT=true python $DATADOG_DIR/scripts/get_tags.py node-agent-tags)

# the agent cloud_foundry_container workloadmeta collector reads from this file
# See: https://github.com/DataDog/datadog-agent/blob/main/pkg/workloadmeta/collectors/internal/cloudfoundry/cf_container/cloudfoundry_container.go#L24
echo "$DD_TAGS" | awk '{ printf "%s", $0 }' > "$DATADOG_DIR/node_agent_tags.txt"

# for debugging purposes
printenv > /home/vcap/app/.datadog/.sourced_datadog_env
printenv > "$DATADOG_DIR/.sourced_datadog_env"

# import helper functions
source "$DATADOG_DIR/scripts/utils.sh"
Expand All @@ -31,8 +33,8 @@ stop_datadog() {
# first try to stop the agent so we don't lose data and then force it
if ! [ "$(find_pid ./agent)" = "" ]; then
echo "Stopping agent process, pid: $(cat $DATADOG_DIR/run/agent.pid)"
($DATADOG_DIR/agent stop --cfgpath $DATADOG_DIR/dist/) || true
find_pid_kill_and_wait $DATADOG_DIR/agent || true
("$DATADOG_DIR/agent" stop --cfgpath "$DATADOG_DIR/dist/") || true
find_pid_kill_and_wait "$DATADOG_DIR/agent" || true
kill_and_wait "$DATADOG_DIR/run/agent.pid" 5
rm -f "$DATADOG_DIR/run/agent.pid"
fi
Expand Down Expand Up @@ -71,6 +73,7 @@ start_datadog() {
export DD_LOG_FILE=$DATADOG_DIR/agent.log
export DD_IOT_HOST=false

echo "Starting Datadog agent"
python $DATADOG_DIR/scripts/create_logs_config.py

if [ "$SUPPRESS_DD_AGENT_OUTPUT" = "true" ]; then
Expand Down