Skip to content

Instantly share code, notes, and snippets.

@rylwin
Created November 23, 2022 14:17
Show Gist options
  • Save rylwin/83ea037a21fe4298f32690195a0ee729 to your computer and use it in GitHub Desktop.
Save rylwin/83ea037a21fe4298f32690195a0ee729 to your computer and use it in GitHub Desktop.
Fetch events for a given Sentry issue
#!/usr/bin/env bash
set -euo pipefail
USAGE="Usage: $(basename "$0") ISSUE_ID
Fetches all events for an issue, page by page.
At any time you feel you've retrieved enough events, use CTRL-C to stop
fetching.
Events are sent to stdout, so recommended usage is to pipe to a file e.g.,:
$(basename "$0") ISSUE_ID > output-file
"
if [ -z "${SENTRY_AUTH_TOKEN:-}" ]; then
echo "\$SENTRY_AUTH_TOKEN must be set
Make a token @ https://sentry.io/settings/account/api/auth-tokens/
Scopes: event:read, project:read
" >&2
exit 1
fi
headers_file="$(mktemp)"
issue_id="${1-}"
json_arrays_file="$(mktemp)"
url="https://sentry.io/api/0/issues/${issue_id}/events/?full=true"
if [ -z "$issue_id" ]; then
echo "$USAGE" >&2
exit 1
fi
combine_results() {
echo "combining..." >&2
jq '.[]' "$json_arrays_file" | jq --slurp
}
trap combine_results EXIT
requires() {
command -v "$1" > /dev/null || ( echo "Requires ${1}" >&2; exit 1 )
}
requires curl
requires jq
while [ "$url" ]; do
curl "${url}" \
-H "Authorization: Bearer $SENTRY_AUTH_TOKEN" \
--silent --show-error --dump-header /dev/stderr \
1>> "$json_arrays_file" \
2> "$headers_file"
echo "Fetched page. Will paginate until done, or ctrl-c to stop sooner" >&2
url="$(sed -n -E 's/link:.*<(.*?)>; rel="next"; results="true".*/\1/p' < "$headers_file")"
done
@rylwin
Copy link
Author

rylwin commented Nov 23, 2022

To workaround a temp API issue of results always being results="false", change:

url="$(sed -n -E 's/link:.*<(.*?)>; rel="next"; results="true".*/\1/p' < "$headers_file")"

to

url="$(sed -n -E 's/link:.*<(.*?)>; rel="next"; results.*/\1/p' < "$headers_file")"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment