Skip to content

Instantly share code, notes, and snippets.

@ktosiek
Created August 18, 2020 07:47
Show Gist options
  • Save ktosiek/f35a51cd71b151aaf6ff7f1e981ea01f to your computer and use it in GitHub Desktop.
Save ktosiek/f35a51cd71b151aaf6ff7f1e981ea01f to your computer and use it in GitHub Desktop.
Send a stream of events to Sentry
#!/usr/bin/env bash
# Heavily based on https://github.com/getsentry/onpremise/blob/fb125a1e4c40701b32f974f6eb2c46a05ca2cd78/test.sh#L80
load_options() {
while [[ -n "$1" ]]; do
case "$1" in
-h | --help) show_help; exit;;
--) shift; SENTRY_DSN=$1;;
*) echo "Unexpected argument: $1. Use --help for usage information."; exit 1;;
esac
shift
done
if [[ -z "$SENTRY_DSN" ]]; then
echo "Missing the DSN argument"
show_help
exit 1
fi
}
show_help() {
cat <<EOF
Usage: $0 [-h | --help] DSN
Send a stream of events to a specified DSN.
Options:
-h, --help Show this message and exit.
EOF
}
load_options $(getopt -u -n "$0" -o 'h' -l 'help' -- "$@")
SENTRY_TEST_HOST="${SENTRY_TEST_HOST:-http://localhost:9000}"
N=0
SUCCESSES=0
cleanup () {
echo
echo "Sent $N events, $SUCCESSES succeeded"
}
trap cleanup EXIT
# We ignore the protocol and the host as we already know those
DSN_PIECES=$(echo "$SENTRY_DSN" | sed -ne 's|^https\?://\([0-9a-z]\+\)@[^/]\+/\([0-9]\+\)$|\1\n\2|p')
SENTRY_KEY=${DSN_PIECES[0]}
PROJECT_ID=${DSN_PIECES[1]}
echo "Creating test events..."
while true; do
TEST_EVENT_ID=$(export LC_ALL=C; head /dev/urandom | tr -dc "a-f0-9" | head -c 32)
curl --max-time 1 -sf \
--data '{"event_id": "'"$TEST_EVENT_ID"'","level":"info","message":"Monitor event","extra":{"n":"'$N'"}}' \
-H 'Content-Type: application/json' \
-H "X-Sentry-Auth: Sentry sentry_version=7, sentry_key=$SENTRY_KEY, sentry_client=test-bash/0.1" \
"$SENTRY_TEST_HOST/api/$PROJECT_ID/store/" \
-o /dev/null
RESULT=$?
echo $(date -Iseconds) $N "$TEST_EVENT_ID" $RESULT
N=$((N + 1))
if [[ $RESULT -eq 0 ]]; then
SUCCESSES=$((SUCCESSES + 1))
fi
sleep 0.5
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment