8000 feat: Add sys.argv to extra · etherscan-io/sentry-python@038e8ed · GitHub
[go: up one dir, main page]

Skip to content

Commit 038e8ed

Browse files
committed
feat: Add sys.argv to extra
Fix getsentry#129
1 parent eb98703 commit 038e8ed

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

sentry_sdk/integrations/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,23 @@ def iter_default_integrations():
2121
- `DedupeIntegration`
2222
- `AtexitIntegration`
2323
- `ModulesIntegration`
24+
- `ArgvIntegration`
2425
"""
2526
from sentry_sdk.integrations.logging import LoggingIntegration
2627
from sentry_sdk.integrations.stdlib import StdlibIntegration
2728
from sentry_sdk.integrations.excepthook import ExcepthookIntegration
2829
from sentry_sdk.integrations.dedupe import DedupeIntegration
2930
from sentry_sdk.integrations.atexit import AtexitIntegration
3031
from sentry_sdk.integrations.modules import ModulesIntegration
32+
from sentry_sdk.integrations.argv import ArgvIntegration
3133

3234
yield LoggingIntegration
3335
yield StdlibIntegration
3436
yield ExcepthookIntegration
3537
yield DedupeIntegration
3638
yield AtexitIntegration
3739
yield ModulesIntegration
40+
yield ArgvIntegration
3841

3942

4043
def setup_integrations(integrations, with_defaults=True):

sentry_sdk/integrations/argv.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from __future__ import absolute_import
2+
3+
import sys
4+
5+
from sentry_sdk.hub import Hub
6+
from sentry_sdk.integrations import Integration
7+
from sentry_sdk.scope import add_global_event_processor
8+
9+
10+
class ArgvIntegration(Integration):
11+
identifier = "argv"
12+
13+
@staticmethod
14+
def setup_once():
15+
@add_global_event_processor
16+
def processor(event, hint):
17+
if Hub.current.get_integration(ArgvIntegration) is not None:
18+
extra = event.setdefault("extra", {})
19+
# If some event processor decided to set extra to e.g. an
20+
# `int`, don't crash. Not here.
21+
if isinstance(extra, dict):
22+
extra["sys.argv"] = sys.argv
23+
24+
return event

tests/integrations/argv/test_argv.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import sys
2+
3+
from sentry_sdk import capture_message
4+
from sentry_sdk.integrations.argv import ArgvIntegration
5+
6+
7+
def test_basic(sentry_init, capture_events, monkeypatch):
8+
sentry_init(integrations=[ArgvIntegration()])
9+
10+
argv = ["foo", "bar", "baz"]
11+
monkeypatch.setattr(sys, "argv", argv)
12+
13+
events = capture_events()
14+
capture_message("hi")
15+
event, = events
16+
assert event["extra"]["sys.argv"] == argv

0 commit comments

Comments
 (0)
0