Skip to content

Commit

Permalink
actually improve telemetry script
Browse files Browse the repository at this point in the history
  • Loading branch information
tbavelier committed Nov 8, 2024
1 parent 2f8f2b0 commit fa62222
Showing 1 changed file with 33 additions and 23 deletions.
56 changes: 33 additions & 23 deletions templates/installer/telemetry/send_telemetry.sh.epp
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,36 @@ TRACER_END_TIME=$(</tmp/puppet_stop_time)
DURATION=$(expr $TRACER_END_TIME - $TRACER_START_TIME)
RUNTIME_ID=$(</tmp/datadog_trace_id)

# Prepare trace payload
sed -i "s/TRACER_START_TIME_PLACEHOLDER/$TRACER_START_TIME/g" /tmp/trace_payload.json
sed -i "s/TRACER_END_TIME_PLACEHOLDER/$TRACER_END_TIME/g" /tmp/trace_payload.json
sed -i "s/DURATION_PLACEHOLDER/$DURATION/g" /tmp/trace_payload.json
sed -i "s/RUNTIME_ID_PLACEHOLDER/$RUNTIME_ID/g" /tmp/trace_payload.json

# Prepare log payload
sed -i "s/TRACER_END_TIME_PLACEHOLDER/$TRACER_END_TIME/g" /tmp/log_payload.json
sed -i "s/RUNTIME_ID_PLACEHOLDER/$RUNTIME_ID/g" /tmp/log_payload.json

# Send trace payload
curl -f -sSL --retry 5 -o /dev/null -X POST \
-H 'Content-Type: application/json' \
-H 'DD-API-KEY: <%= $api_key %>' \
-d '@/tmp/trace_payload.json' \
"$TELEMETRY_URL"

# Send log payload
curl -f -sSL --retry 5 -o /dev/null -X POST \
-H 'Content-Type: application/json' \
-H 'DD-API-KEY: <%= $api_key %>' \
-d '@/tmp/log_payload.json' \
"$TELEMETRY_URL"
# Prepare placeholders and values
declare -A placeholders=(
["TRACER_START_TIME_PLACEHOLDER"]=$TRACER_START_TIME
["TRACER_END_TIME_PLACEHOLDER"]=$TRACER_END_TIME
["DURATION_PLACEHOLDER"]=$DURATION
["RUNTIME_ID_PLACEHOLDER"]=$RUNTIME_ID
)

# Function to replace placeholders in a file
replace_placeholders() {
local file=$1
for placeholder in "${!placeholders[@]}"; do
sed -i "s/$placeholder/${placeholders[$placeholder]}/g" "$file"
done
}

# Prepare trace and log payloads
replace_placeholders /tmp/trace_payload.json
replace_placeholders /tmp/log_payload.json

# Function to send payload
send_payload() {
local file=$1
curl -f -sSL --retry 5 -o /dev/null -X POST \
-H 'Content-Type: application/json' \
-H 'DD-API-KEY: <%= $api_key %>' \
-d "@$file" \
"$TELEMETRY_URL"
}

# Send trace and log payloads
send_payload /tmp/trace_payload.json
send_payload /tmp/log_payload.json

0 comments on commit fa62222

Please sign in to comment.