-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: /usr/bin/script wrapper to send full tty output to sentry #124
Open
joshuarli
wants to merge
6
commits into
main
Choose a base branch
from
script-wrapper
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,8 @@ | |
|
||
import argparse | ||
import os | ||
import subprocess | ||
import sys | ||
from collections.abc import Sequence | ||
|
||
from devenv import bootstrap | ||
|
@@ -75,17 +77,62 @@ def devenv(argv: Sequence[str], config_path: str) -> ExitCode: | |
|
||
|
||
def main() -> ExitCode: | ||
import sys | ||
# this is also used to see if we're the child process | ||
script_logfile = os.environ.get("SCRIPT") | ||
|
||
if script_logfile: | ||
return devenv(sys.argv, f"{home}/.config/sentry-devenv/config.ini") | ||
|
||
import tempfile | ||
|
||
_, fp = tempfile.mkstemp() | ||
# script (macos/linux) runs the subcommand with a tty, and tees the output to a file. | ||
# this way we can very easily capture all output from devenv and send it | ||
# to sentry as an attachment if an error occurs. | ||
cmd = ("/usr/bin/script", "-qe", fp, *sys.argv) | ||
|
||
# the reason we're subprocessing instead of os.execv(cmd[0], cmd) | ||
# is that script must exit (so that the complete log file is committed to disk) | ||
# before sentry sends the event... | ||
rc = subprocess.call(cmd) | ||
|
||
if rc == 0: | ||
return rc | ||
|
||
# i'd love to be able to send a full event in the child then | ||
# upload the attachment to that event id in the parent, but unfortunately there's | ||
# no easy way to add an attachment to an existing event, | ||
# so we have to give up getting a python stacktrace, | ||
# and can only use sentry-sdk to send an attachment. | ||
import sentry_sdk | ||
from sentry_sdk.scope import Scope | ||
import getpass | ||
|
||
sentry_sdk.init( | ||
# https://sentry.sentry.io/settings/projects/sentry-dev-env/keys/ | ||
dsn="https://[email protected]/5723503", | ||
# enable performance monitoring | ||
enable_tracing=True, | ||
# disable performance monitoring | ||
enable_tracing=False, | ||
) | ||
|
||
return devenv(sys.argv, f"{home}/.config/sentry-devenv/config.ini") | ||
scope = Scope.get_current_scope() | ||
|
||
# would really like to be able to set filename to the python exception title | ||
# because seeing KeyboardInterrupt vs CalledProcessError is more helpful than | ||
# "tmp29387ldf", but see above comment | ||
scope.add_attachment(path=fp) | ||
|
||
client = Scope.get_client() | ||
|
||
user = getpass.getuser() | ||
computer = client.options.get("server_name", "unknown") | ||
|
||
# events are grouped under user@computer | ||
scope.fingerprint = [f"{user}@{computer}"] | ||
|
||
sentry_sdk.capture_message(f"{user}@{computer}") | ||
|
||
return rc | ||
|
||
|
||
if __name__ == "__main__": | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we can do something something trace id and have both exist? and just link them after the fact?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ahhh that might work, i'm thinking we start a transaction in the parent and start a span for the child process then only transaction.finish() if an error happens in the child
not sure what the transaction would look like in sentry ui (like, can we easily see the child error event) but gonna give it a go