Skip to content

Commit 4f5c640

Browse files
authored
sentry_runner(feat): Report development issues (#24512)
This change enables error reporting issues when using `sentry`'s CLI runner.
1 parent fac2efa commit 4f5c640

File tree

2 files changed

+40
-8
lines changed

2 files changed

+40
-8
lines changed

.envrc

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ report_to_sentry() {
4343
if ! require sentry-cli; then
4444
curl -sL https://sentry.io/get-cli/ | bash
4545
fi
46-
sentry-cli send-event -m "$error_message" --logfile "$_SENTRY_LOG_FILE" --level $log_level
46+
SENTRY_DSN=${SENTRY_DEVENV_DSN} \
47+
sentry-cli send-event -m "$error_message" --logfile "$_SENTRY_LOG_FILE" --level $log_level
4748
rm "$_SENTRY_LOG_FILE"
4849
}
4950

@@ -125,12 +126,19 @@ export NODE_OPTIONS=--max-old-space-size=4096
125126
# Enable this by default for development envs (CI/deploys do not use envrc)
126127
export SENTRY_UI_HOT_RELOAD=1
127128

129+
### You can override the exported variables with a .env file
130+
# All exports should happen before here unless they're safeguarded (see devenv error reporting below)
131+
if [ -f '.env' ]; then
132+
info ".env found. Reading it..."
133+
dotenv .env
134+
fi
135+
128136
## Notify of reporting to Sentry
129137
if [ -n "${SENTRY_DEVENV_NO_REPORT+x}" ]; then
130138
info "No development environment errors will be reported (since you've defined SENTRY_DEVENV_NO_REPORT)."
131139
else
132140
info "Development errors will be reported to Sentry.io."$'\n'" If you wish to opt-out, set SENTRY_DEVENV_NO_REPORT as an env variable."
133-
export SENTRY_DSN=https://[email protected]/1492057
141+
export SENTRY_DEVENV_DSN=https://[email protected]/1492057
134142
fi
135143

136144
### System ###
@@ -217,11 +225,6 @@ PATH_add node_modules/.bin
217225

218226
### Overrides ###
219227

220-
if [ -f '.env' ]; then
221-
info ".env found. Reading it..."
222-
dotenv .env
223-
fi
224-
225228
cat <<EOF
226229
${green}${bold}direnv: SUCCESS!
227230
${reset}

src/sentry/runner/__init__.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
import logging
12
import os
23
import click
34
import sys
45
import sentry
56
import datetime
7+
import sentry_sdk
68
from sentry.utils.imports import import_string
79
from sentry.utils.compat import map
810

@@ -161,4 +163,31 @@ def call_command(name, obj=None, **kwargs):
161163

162164

163165
def main():
164-
cli(prog_name=get_prog(), obj={}, max_content_width=100)
166+
func = cli
167+
kwargs = {
168+
"prog_name": get_prog(),
169+
"obj": {},
170+
"max_content_width": 100,
171+
}
172+
# This variable is *only* set as part of direnv/.envrc, thus, we cannot affect production
173+
if os.environ.get("SENTRY_DEVENV_DSN"):
174+
# We do this here because `configure_structlog` executes later
175+
logging.basicConfig(format="%(levelname)s:%(message)s", level=logging.INFO)
176+
logger = logging.getLogger(__name__)
177+
178+
logger.info(
179+
"The Sentry runner will report development issues to Sentry.io. "
180+
"Use SENTRY_DEVENV_NO_REPORT to avoid reporting issues."
181+
)
182+
try:
183+
func(**kwargs)
184+
except Exception as e:
185+
# This reports to the project sentry-dev-env
186+
with sentry_sdk.init(dsn=os.environ["SENTRY_DEVENV_DSN"]):
187+
if os.environ.get("USER"):
188+
sentry_sdk.set_user({"username": os.environ.get("USER")})
189+
sentry_sdk.capture_exception(e)
190+
logger.info("We have reported the error below to Sentry")
191+
raise e
192+
else:
193+
func(**kwargs)

0 commit comments

Comments
 (0)