Skip to content

Commit 36ef805

Browse files
authored
feat: Capture modules (#64)
* feat: Capture modules * fix: Stylefixes
1 parent 98e5ff5 commit 36ef805

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

sentry_sdk/integrations/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,14 @@ def get_default_integrations():
2323
from sentry_sdk.integrations.excepthook import ExcepthookIntegration
2424
from sentry_sdk.integrations.dedupe import DedupeIntegration
2525
from sentry_sdk.integrations.atexit import AtexitIntegration
26+
from sentry_sdk.integrations.modules import ModulesIntegration
2627

2728
yield LoggingIntegration()
2829
yield StdlibIntegration()
2930
yield ExcepthookIntegration()
3031
yield DedupeIntegration()
3132
yield AtexitIntegration()
33+
yield ModulesIntegration()
3234

3335

3436
def setup_integrations(integrations, with_defaults=True):

sentry_sdk/integrations/modules.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from __future__ import absolute_import
2+
3+
from sentry_sdk.api import configure_scope
4+
from sentry_sdk.integrations import Integration
5+
6+
_installed_modules = None
7+
8+
9+
def _generate_installed_modules():
10+
try:
11+
import pkg_resources
12+
except ImportError:
13+
return
14+
15+
for info in pkg_resources.working_set:
16+
yield info.key, info.version
17+
18+
19+
def _get_installed_modules():
20+
global _installed_modules
21+
if _installed_modules is None:
22+
_installed_modules = dict(_generate_installed_modules())
23+
return _installed_modules
24+
25+
26+
class ModulesIntegration(Integration):
27+
identifier = "modules"
28+
29+
def install(self):
30+
with configure_scope() as scope:
31+
32+
@scope.add_event_processor
33+
def processor(event, hint):
34+
if "modules" not in event:
35+
event["modules"] = dict(_get_installed_modules())
36+
return event
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import sentry_sdk
2+
3+
from sentry_sdk.integrations.modules import ModulesIntegration
4+
5+
6+
def test_basic(sentry_init, capture_events):
7+
sentry_init(integrations=[ModulesIntegration()])
8+
events = capture_events()
9+
10+
sentry_sdk.capture_exception(ValueError())
11+
12+
event, = events
13+
assert "sentry-sdk" in event["modules"]
14+
assert "pytest" in event["modules"]

0 commit comments

Comments
 (0)