Skip to content

Commit 7d11d1a

Browse files
committed
add script with sentry integration example
1 parent c2b498d commit 7d11d1a

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

python-script-sentry/module_loader.py

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import argparse
2+
import importlib
3+
import os
4+
import pkgutil
5+
import sentry_sdk
6+
from sentry_sdk import capture_exception
7+
8+
# find on https://docs.sentry.io/error-reporting/quickstart/?platform=python
9+
sentry_sdk.init(dsn=os.getenv('SENTRY_DSN'))
10+
11+
12+
def import_submodules(package):
13+
"""Import all submodules of a module, recursively, including subpackages.
14+
15+
:param package: package (name or actual module)
16+
:type package: str | module
17+
:rtype: dict[str, types.ModuleType]
18+
"""
19+
if isinstance(package, str):
20+
package = importlib.import_module(package)
21+
results = {}
22+
for loader, name, is_pkg in pkgutil.walk_packages(package.__path__):
23+
full_name = package.__name__ + '.' + name
24+
try:
25+
results[full_name] = importlib.import_module(full_name)
26+
if is_pkg:
27+
results.update(import_submodules(full_name))
28+
except ModuleNotFoundError as mnfe:
29+
print("module not found: {}".format(full_name))
30+
capture_exception(mnfe)
31+
except Exception as general_exception:
32+
print(general_exception)
33+
capture_exception(general_exception)
34+
return results
35+
36+
37+
if __name__ == "__main__":
38+
parser = argparse.ArgumentParser()
39+
parser.add_argument("package")
40+
args = parser.parse_args()
41+
42+
package_to_load = args.package
43+
results = import_submodules(package_to_load)
44+
for r in results:
45+
print(str(r))

python-script-sentry/requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sentry-sdk>=0.14.4

0 commit comments

Comments
 (0)