Skip to content

Commit 97a4e5c

Browse files
Add context line, error msg to envelope (#1784)
* add context line, error msg to envelope
1 parent c573bdd commit 97a4e5c

File tree

3 files changed

+37
-18
lines changed

3 files changed

+37
-18
lines changed

_unit-test/error-handling-test.sh

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,19 @@ send_envelope() {
1212

1313
export -f send_envelope
1414
echo "Testing initial send_event"
15-
export log_file="test_log.txt"
16-
echo "Test Logs" >"$basedir/$log_file"
17-
SEND_EVENT_RESPONSE=$(send_event "12345123451234512345123451234512" "Test exited with status 1" "{\"ignore\": \"me\"}")
18-
rm "$basedir/$log_file"
15+
export log_path="$basedir/test_log.txt"
16+
echo "Test Logs" >"$log_path"
17+
echo "Error Msg" >>"$log_path"
18+
breadcrumbs=$(generate_breadcrumb_json | sed '$d' | jq -s -c)
19+
SEND_EVENT_RESPONSE=$(send_event "12345123451234512345123451234512" "Test exited with status 1" "{\"ignore\": \"me\"}" "$breadcrumbs")
20+
rm "$log_path"
1921
test "$SEND_EVENT_RESPONSE" == 'Test Sending sentry-envelope-12345123451234512345123451234512'
2022
ENVELOPE_CONTENTS=$(cat /tmp/sentry-envelope-12345123451234512345123451234512)
2123
test "$ENVELOPE_CONTENTS" == "$(cat "$basedir/_unit-test/snapshots/sentry-envelope-12345123451234512345123451234512")"
2224
echo "Pass."
2325

2426
echo "Testing send_event duplicate"
25-
SEND_EVENT_RESPONSE=$(send_event "12345123451234512345123451234512" "Test exited with status 1" "{\"ignore\": \"me\"}")
27+
SEND_EVENT_RESPONSE=$(send_event "12345123451234512345123451234512" "Test exited with status 1" "{\"ignore\": \"me\"}" "$breadcrumbs")
2628
test "$SEND_EVENT_RESPONSE" == "Looks like you've already sent this error to us, we're on it :)"
2729
echo "Pass."
2830
rm '/tmp/sentry-envelope-12345123451234512345123451234512'
@@ -31,8 +33,10 @@ echo "Testing cleanup without minimizing downtime"
3133
export REPORT_SELF_HOSTED_ISSUES=0
3234
export MINIMIZE_DOWNTIME=''
3335
export dc=':'
36+
echo "Test Logs" >"$log_path"
3437
CLEANUP_RESPONSE=$(cleanup ERROR)
35-
test "$CLEANUP_RESPONSE" == 'Error in ./_unit-test/error-handling-test.sh:34.
38+
rm "$log_path"
39+
test "$CLEANUP_RESPONSE" == 'Error in ./_unit-test/error-handling-test.sh:37.
3640
'\''local cmd="${BASH_COMMAND}"'\'' exited with status 0
3741
3842
Cleaning up...'
@@ -41,8 +45,10 @@ echo "Pass."
4145
echo "Testing cleanup while minimizing downtime"
4246
export REPORT_SELF_HOSTED_ISSUES=0
4347
export MINIMIZE_DOWNTIME=1
48+
echo "Test Logs" >"$log_path"
4449
CLEANUP_RESPONSE=$(cleanup ERROR)
45-
test "$CLEANUP_RESPONSE" == 'Error in ./_unit-test/error-handling-test.sh:44.
50+
rm "$log_path"
51+
test "$CLEANUP_RESPONSE" == 'Error in ./_unit-test/error-handling-test.sh:49.
4652
'\''local cmd="${BASH_COMMAND}"'\'' exited with status 0
4753
4854
*NOT* cleaning up, to clean your environment run "docker compose stop".'
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{"event_id":"12345123451234512345123451234512","dsn":"https://[email protected]/3"}
22
{"type":"event"}
33
{"level":"error","exception":{"values":[{"type":"Error","value":"Test exited with status 1","stacktrace":{"frames":[{"ignore":"me"}]}}]},"breadcrumbs":{"values":[{"message":"Test Logs","category":"log","level":"info"}]}}
4-
{"type":"attachment","length":10,"content_type":"text/plain","filename":"install_log.txt"}
4+
{"type":"attachment","length":20,"content_type":"text/plain","filename":"install_log.txt"}
55
Test Logs
6+
Error Msg

install/error-handling.sh

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export SENTRY_PROJECT=installer
66

77
jq="docker run --rm -i sentry-self-hosted-jq-local"
88
sentry_cli="docker run --rm -v /tmp:/work -e SENTRY_ORG=$SENTRY_ORG -e SENTRY_PROJECT=$SENTRY_PROJECT -e SENTRY_DSN=$SENTRY_DSN getsentry/sentry-cli"
9+
log_path="$basedir/$log_file"
910

1011
send_envelope() {
1112
# Send envelope
@@ -25,11 +26,11 @@ generate_breadcrumb_json() {
2526
send_event() {
2627
# Use traceback hash as the UUID since it is 32 characters long
2728
local event_hash=$1
28-
local error_message=$2
29+
local error_msg=$2
2930
local traceback_json=$3
31+
local breadcrumbs=$4
3032
local envelope_file="sentry-envelope-${event_hash}"
3133
local envelope_file_path="/tmp/$envelope_file"
32-
local log_path="$basedir/$log_file"
3334
# If the envelope file exists, we've already sent it
3435
if [[ -f $envelope_file_path ]]; then
3536
echo "Looks like you've already sent this error to us, we're on it :)"
@@ -52,18 +53,15 @@ send_event() {
5253
# See https://develop.sentry.dev/sdk/event-payloads/
5354
# for details about the event payload
5455

55-
# First, create the breadcrumb payload
56-
# https://develop.sentry.dev/sdk/event-payloads/breadcrumbs/
57-
breadcrumbs=$(generate_breadcrumb_json | jq -s -c)
58-
# Next we need the exception payload
56+
# Then we need the exception payload
5957
# https://develop.sentry.dev/sdk/event-payloads/exception/
6058
# but first we need to make the stacktrace which goes in the exception payload
6159
frames=$(echo "$traceback_json" | jq -s -c)
62-
stacktrace=$(jq -n -c --argjson frames $frames '$ARGS.named')
60+
stacktrace=$(jq -n -c --argjson frames "$frames" '$ARGS.named')
6361
exception=$(
6462
jq -n -c --arg "type" Error \
65-
--arg value "$error_message" \
66-
--argjson stacktrace $stacktrace \
63+
--arg value "$error_msg" \
64+
--argjson stacktrace "$stacktrace" \
6765
'$ARGS.named'
6866
)
6967
event_body=$(
@@ -170,6 +168,12 @@ cleanup() {
170168
DID_CLEAN_UP=1
171169
if [[ "$1" != "EXIT" ]]; then
172170
set +o xtrace
171+
# Save the error message that comes from the last line of the log file
172+
error_msg=$(tail -n 1 "$log_path")
173+
# Create the breadcrumb payload now before stacktrace is printed
174+
# https://develop.sentry.dev/sdk/event-payloads/breadcrumbs/
175+
# Use sed to remove the last line, that is reported through the error message
176+
breadcrumbs=$(generate_breadcrumb_json | sed '$d' | jq -s -c)
173177
printf -v err '%s' "Error in ${BASH_SOURCE[1]}:${BASH_LINENO[0]}."
174178
printf -v cmd_exit '%s' "'$cmd' exited with status $retcode"
175179
printf '%s\n%s\n' "$err" "$cmd_exit"
@@ -188,6 +192,14 @@ cleanup() {
188192
--arg lineno "$lineno" \
189193
'{"filename": $filename, "function": $function, "lineno": $lineno|tonumber}'
190194
)
195+
# If we're in the stacktrace of the file we failed on, we can add a context line with the command run that failed
196+
if [[ $i -eq 1 ]]; then
197+
JSON=$(
198+
jq -n -c --arg cmd "$cmd" \
199+
--argjson json "$JSON" \
200+
'$json + {"context_line": $cmd}'
201+
)
202+
fi
191203
printf -v traceback_json '%s\n' "$traceback_json$JSON"
192204
printf -v traceback '%s\n' "$traceback${indent//a/-}> $src:$funcname:$lineno"
193205
done
@@ -197,7 +209,7 @@ cleanup() {
197209
# Only send event when report issues flag is set and if trap signal is not INT (ctrl+c)
198210
if [[ "$REPORT_SELF_HOSTED_ISSUES" == 1 && "$1" != "INT" ]]; then
199211
local event_hash=$(echo -n "$cmd_exit $traceback" | docker run -i --rm busybox md5sum | cut -d' ' -f1)
200-
send_event "$event_hash" "$cmd_exit" "$traceback_json"
212+
send_event "$event_hash" "$error_msg" "$traceback_json" "$breadcrumbs"
201213
fi
202214

203215
if [[ -n "$MINIMIZE_DOWNTIME" ]]; then

0 commit comments

Comments
 (0)