Skip to content

Instantly share code, notes, and snippets.

@barroco
Last active May 4, 2024 11:25
Show Gist options
  • Save barroco/1a0009500ebd963b6522 to your computer and use it in GitHub Desktop.
Save barroco/1a0009500ebd963b6522 to your computer and use it in GitHub Desktop.
Send Sentry error from shell using curl
#!/bin/sh
SENTRY_KEY=
SENTRY_SECRET=
SENTRY_PROJECTID=1
SENTRY_HOST=sentry.example.com
SCRIPT_ARGUMENTS=$@
capture_error()
{
MESSAGE=$1
EVENT_ID=`openssl rand -hex 32`
EVENT_TIMESTAMP=`date +"%Y-%m-%dT%H:%M:%S"`
SENTRY_TIMESTAMP=`date +%s`
curl --data "{
\"event_id\": \"$EVENT_ID\",
\"culprit\": \"$0\",
\"timestamp\": \"$EVENT_TIMESTAMP\",
\"message\": \"$MESSAGE\",
\"tags\": {
\"shell\": \"$SHELL\",
\"server_name\": \"`hostname`\",
\"path\": \"`pwd`\"
},
\"exception\": [{
\"type\": \"ScriptError\",
\"value\": \"$MESSAGE\",
\"module\": \"__builtins__\"
}],
\"extra\": {
\"sys.argv\": \"$SCRIPT_ARGUMENTS\"
}
}" -H "Content-Type: application/json" -H "X-Sentry-Auth: Sentry sentry_version=5, sentry_timestamp=$SENTRY_TIMESTAMP,
sentry_key=$SENTRY_KEY, sentry_client=raven-bash/0.1,
sentry_secret=$SENTRY_SECRET" http://$SENTRY_KEY:$SENTRY_SECRET@$SENTRY_HOST/api/$SENTRY_PROJECTID/store/
}
# Example:
capture_error "Unable to execute the command"
exit 1
@radupopa2010
Copy link

There was 1 error encountered while processing this event
Discarded invalid value for parameter 'timestamp' Collapse
{
"name": "timestamp",
"value": "2017-06-26T18:32:14"
}

you can fix it by adding --utc to this line:
EVENT_TIMESTAMP=$(date --utc +"%Y-%m-%dT%H:%M:%S")

@medliii
Copy link

medliii commented Mar 11, 2020

for v8

-H "Content-Type: application/json" \ -H "X-Sentry-Auth: Sentry sentry_version=7, sentry_client=sentry.php/2.3.1, sentry_key=$SENTRY_KEY, sentry_secret=$SENTRY_SECRET" \ http://$SENTRY_HOST/api/$SENTRY_PROJECTID/store/

@williamdes
Copy link

williamdes commented May 3, 2024

{"detail":"invalid JSON data","causes":["invalid value: string \"9773ac1f5429ced76d074c97bf4465512ece66c0cd652009023a3f272e0ab6d3\", expected an event identifier at line 2 column 82"]}

Fix:

- EVENT_ID=`openssl rand -hex 32`
+ EVENT_ID=`openssl rand -hex 16`

Tips: See: https://github.com/getsentry/sentry-data-schemas/blob/main/relay/event.schema.json

@williamdes
Copy link

And also

-H "Content-Type: application/json" \
-H "X-Sentry-Auth: Sentry sentry_version=7, sentry_key=$SENTRY_KEY, sentry_client=bash-reporter/0.1" \
    https://$SENTRY_HOST/api/$SENTRY_PROJECTID/store/

No need for SENTRY_SECRET anymore

Inspired by https://github.com/beestat/app/blob/9f1d678418c6dbe60c710af87f71680676d5a8b5/api/cora/request.php#L537-L573

@williamdes
Copy link

The schema says culprit is deprecated and should be removed.
See: https://github.com/getsentry/sentry-data-schemas/blob/main/relay/event.schema.json

@williamdes
Copy link

Here is a pure JS NodeJS example I build using this gist: https://gist.github.com/williamdes/1d0b02c1bf3b94ee0dbaecdbc26d12c1

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