Skip to content

Commit 433afd9

Browse files
authored
Fix jq usage (#1814)
In this PR we fix our usage of jq: 1. Use the container. Before we were accidentally using jq installed in the environment 1. Rewrite generate_breadcrumb_json to read log file all at once 1. Build docker image for jq in unit tests
1 parent 794b9ff commit 433afd9

File tree

5 files changed

+16
-19
lines changed

5 files changed

+16
-19
lines changed

_unit-test/_test_setup.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ source "$(dirname $0)/../install/_lib.sh"
44
rm -rf /tmp/sentry-self-hosted-test-sandbox.*
55
_SANDBOX="$(mktemp -d /tmp/sentry-self-hosted-test-sandbox.XXX)"
66

7+
source "$basedir/install/detect-platform.sh"
8+
docker build -t sentry-self-hosted-jq-local --platform=$DOCKER_PLATFORM "$basedir/jq"
9+
710
report_success() {
811
echo "$(basename $0) - Success 👍"
912
}

_unit-test/error-handling-test.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ echo "Testing initial send_event"
1515
export log_path="$basedir/test_log.txt"
1616
echo "Test Logs" >"$log_path"
1717
echo "Error Msg" >>"$log_path"
18-
breadcrumbs=$(generate_breadcrumb_json | sed '$d' | jq -s -c)
18+
breadcrumbs=$(generate_breadcrumb_json | sed '$d' | $jq -s -c)
1919
SEND_EVENT_RESPONSE=$(send_event "12345123451234512345123451234512" "Test exited with status 1" "{\"ignore\": \"me\"}" "$breadcrumbs")
2020
rm "$log_path"
2121
test "$SEND_EVENT_RESPONSE" == 'Test Sending sentry-envelope-12345123451234512345123451234512'

install/build-docker-images.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ echo ""
66
$dc build --build-arg "http_proxy=${http_proxy:-}" --build-arg "https_proxy=${https_proxy:-}" --build-arg "no_proxy=${no_proxy:-}" --force-rm web
77
$dc build --build-arg "http_proxy=${http_proxy:-}" --build-arg "https_proxy=${https_proxy:-}" --build-arg "no_proxy=${no_proxy:-}" --force-rm
88
# Used in error-handling.sh for error envelope payloads
9-
docker build -t sentry-self-hosted-jq-local $basedir/jq
9+
docker build -t sentry-self-hosted-jq-local --platform=$DOCKER_PLATFORM $basedir/jq
1010
echo ""
1111
echo "Docker images built."
1212

install/error-handling.sh

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,7 @@ send_envelope() {
1414
}
1515

1616
generate_breadcrumb_json() {
17-
# Based on https://stackoverflow.com/a/1521498
18-
while IFS="" read -r line || [ -n "$line" ]; do
19-
jq -n -c --arg message "$line" \
20-
--arg category log \
21-
--arg level info \
22-
'$ARGS.named'
23-
done <$log_path
17+
cat $log_path | $jq -R -c 'split("\n") | {"message": (.[0]//""), "category": "log", "level": "info"}'
2418
}
2519

2620
send_event() {
@@ -42,7 +36,7 @@ send_event() {
4236
local file_length=$(wc -c <$log_path | awk '{print $1}')
4337

4438
# Add header for initial envelope information
45-
jq -n -c --arg event_id "$event_hash" \
39+
$jq -n -c --arg event_id "$event_hash" \
4640
--arg dsn "$SENTRY_DSN" \
4741
'$ARGS.named' >$envelope_file_path
4842
# Add header to specify the event type of envelope to be sent
@@ -56,24 +50,24 @@ send_event() {
5650
# Then we need the exception payload
5751
# https://develop.sentry.dev/sdk/event-payloads/exception/
5852
# but first we need to make the stacktrace which goes in the exception payload
59-
frames=$(echo "$traceback_json" | jq -s -c)
60-
stacktrace=$(jq -n -c --argjson frames "$frames" '$ARGS.named')
53+
frames=$(echo "$traceback_json" | $jq -s -c)
54+
stacktrace=$($jq -n -c --argjson frames "$frames" '$ARGS.named')
6155
exception=$(
62-
jq -n -c --arg "type" Error \
56+
$jq -n -c --arg "type" Error \
6357
--arg value "$error_msg" \
6458
--argjson stacktrace "$stacktrace" \
6559
'$ARGS.named'
6660
)
6761
event_body=$(
68-
jq -n -c --arg level error \
62+
$jq -n -c --arg level error \
6963
--argjson exception "{\"values\":[$exception]}" \
7064
--argjson breadcrumbs "{\"values\": $breadcrumbs}" \
7165
'$ARGS.named'
7266
)
7367
echo "$event_body" >>$envelope_file_path
7468
# Add attachment to the event
7569
attachment=$(
76-
jq -n -c --arg "type" attachment \
70+
$jq -n -c --arg "type" attachment \
7771
--arg length $file_length \
7872
--arg content_type "text/plain" \
7973
--arg filename install_log.txt \
@@ -173,7 +167,7 @@ cleanup() {
173167
# Create the breadcrumb payload now before stacktrace is printed
174168
# https://develop.sentry.dev/sdk/event-payloads/breadcrumbs/
175169
# Use sed to remove the last line, that is reported through the error message
176-
breadcrumbs=$(generate_breadcrumb_json | sed '$d' | jq -s -c)
170+
breadcrumbs=$(generate_breadcrumb_json | sed '$d' | $jq -s -c)
177171
printf -v err '%s' "Error in ${BASH_SOURCE[1]}:${BASH_LINENO[0]}."
178172
printf -v cmd_exit '%s' "'$cmd' exited with status $retcode"
179173
printf '%s\n%s\n' "$err" "$cmd_exit"
@@ -187,15 +181,15 @@ cleanup() {
187181
local lineno=${BASH_LINENO[$i - 1]}
188182
local funcname=${FUNCNAME[$i]}
189183
JSON=$(
190-
jq -n -c --arg filename $src \
184+
$jq -n -c --arg filename $src \
191185
--arg "function" $funcname \
192186
--arg lineno "$lineno" \
193187
'{"filename": $filename, "function": $function, "lineno": $lineno|tonumber}'
194188
)
195189
# If we're in the stacktrace of the file we failed on, we can add a context line with the command run that failed
196190
if [[ $i -eq 1 ]]; then
197191
JSON=$(
198-
jq -n -c --arg cmd "$cmd" \
192+
$jq -n -c --arg cmd "$cmd" \
199193
--argjson json "$JSON" \
200194
'$json + {"context_line": $cmd}'
201195
)

jq/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ RUN set -x \
88
&& apt-get clean \
99
&& rm -rf /var/lib/apt/lists/*
1010

11-
CMD ["jq"]
11+
ENTRYPOINT ["jq"]

0 commit comments

Comments
 (0)