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 4 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)))
6 changes: 5 additions & 1 deletion lib/scripts/get_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@
if node_agent_tags is not None:
# we do this to separate commas inside json values from tags separator commas
node_agent_tags = node_agent_tags.replace(",\"", ";\"")
node_agent_tags = node_agent_tags.replace(" ", "_")
all_node_agent_tags = node_agent_tags.split(",")
tags = tags + [tag for tag in all_node_agent_tags if ";" not in tag]


for vcap_var_name in vcap_variables:
vcap_var = vcap_application.get(vcap_var_name)
if vcap_var is not None:
vcap_var = vcap_var.replace(" ", "_")
key = vcap_var_name
if vcap_var_name == 'name':
key = 'application_name'
Expand All @@ -48,6 +50,7 @@
if user_tags:
try:
user_tags = user_tags.split(',')
user_tags = user_tags.replace(" ", "_")
for tag in user_tags:
tags.append(tag)
except Exception as e:
Expand All @@ -56,14 +59,15 @@
user_tags = os.environ.get('DD_TAGS', None)
if user_tags:
try:
user_tags = user_tags.replace(" ", "_")
user_tags = user_tags.split(',')
for tag in user_tags:
tags.append(tag)
except Exception as e:
print("there was an issue parsing the tags in DD_TAGS: {}".format(e))

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")
8 changes: 6 additions & 2 deletions lib/scripts/update_agent_config_restart.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,18 @@ 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)
touch "$DATADOG_DIR/.new_datadog_env"
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 Down Expand Up @@ -71,6 +74,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