Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit6c05bf3

Browse files
committed
Add sentry and simple_settings.py
1 parent5e4a29e commit6c05bf3

File tree

8 files changed

+71
-11
lines changed

8 files changed

+71
-11
lines changed

‎backend/book/settings.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
importdj_database_url
1919
importsnoop
2020
fromdjango.contrib.messagesimportconstantsasmessages
21+
frommainimportsimple_settings
2122

2223
BASE_DIR=Path(__file__).parent.parent
2324

‎backend/main/simple_settings.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
importos
2+
3+
importsentry_sdk
4+
fromlittleutilsimportsetup_quick_console_logging
5+
fromsentry_sdk.integrations.djangoimportDjangoIntegration
6+
7+
setup_quick_console_logging()
8+
9+
sentry_sdk.init(
10+
dsn=os.environ.get("SENTRY_DSN"),
11+
integrations=[DjangoIntegration()],
12+
send_default_pii=True
13+
)
14+
15+
CLOUDAMQP_URL=os.environ.get('CLOUDAMQP_URL')
16+
SEPARATE_WORKER_PROCESS=os.environ.get('SEPARATE_WORKER_PROCESS','False')[0].upper()=='T'

‎backend/main/views.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
fromdjango_user_agents.utilsimportget_user_agent
2222
fromlittleutilsimportselect_attrs
2323
frommarkdownimportmarkdown
24+
fromsentry_sdkimportcapture_exception
2425

2526
frommain.chapters.c03_variablesimportWritingPrograms
2627
frommain.chapters.c05_if_statementsimportUnderstandingProgramsWithSnoop
@@ -51,6 +52,7 @@ def api_view(request, method_name):
5152
ifnotisinstance(result,dict):
5253
result= {'result':result}
5354
exceptException:
55+
capture_exception()
5456
result=dict(
5557
error=dict(
5658
traceback=traceback.format_exc(),

‎backend/main/workers/master.py‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
importatexit
22
importmultiprocessing
3-
importos
43
importqueue
54
fromcollectionsimportdefaultdict
65
fromfunctoolsimportlru_cache
76
frommultiprocessing.contextimportProcess
87
fromthreadingimportThread
98

10-
fromlittleutilsimportsetup_quick_console_logging
11-
9+
frommainimportsimple_settings
1210
frommain.workers.communicationsimportAbstractCommunications,ThreadCommunications
1311
frommain.workers.utilsimportinternal_error_result,make_result
1412
frommain.workers.workerimportworker_loop_in_thread
@@ -54,6 +52,9 @@ def handle_entry(self, entry):
5452
defawait_result(self,callback):
5553
try:
5654
result=self._await_result()
55+
# if result["error"] and result["error"]["sentry_event"]:
56+
# event, hint = result["error"]["sentry_event"]
57+
# capture_event(event, hint)
5758
exceptException:
5859
result=internal_error_result()
5960
self.awaiting_input=result["awaiting_input"]
@@ -107,14 +108,13 @@ def callback(result):
107108

108109
@lru_cache()
109110
defmaster_communications()->AbstractCommunications:
110-
fromdjango.confimportsettings
111-
ifos.environ.get('CLOUDAMQP_URL'):
111+
ifsimple_settings.CLOUDAMQP_URL:
112112
from .pikaimportPikaCommunications
113113
comms=PikaCommunications()
114114
else:
115115
comms=ThreadCommunications()
116116

117-
ifnotsettings.SEPARATE_WORKER_PROCESS:
117+
ifnotsimple_settings.SEPARATE_WORKER_PROCESS:
118118
Thread(
119119
target=master_consumer_loop,
120120
args=[comms],
@@ -139,5 +139,4 @@ def main():
139139

140140

141141
if__name__=='__main__':
142-
setup_quick_console_logging()
143142
main()

‎backend/main/workers/utils.py‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
importsys
22
importtraceback
33

4+
fromsentry_sdkimportcapture_exception
5+
46

57
classSysStream:
68
def__init__(self,output,color):
@@ -61,7 +63,16 @@ def make_result(
6163
)
6264

6365

64-
definternal_error_result():
66+
definternal_error_result(sentry_offline=False):
67+
ifsentry_offline:
68+
sentry_event=None
69+
# TODO https://stackoverflow.com/questions/60801638/how-to-capture-an-easily-serialisable-exception-event-with-sentry
70+
# exc_info = sys.exc_info()
71+
# sentry_event = event_from_exception(exc_info)
72+
else:
73+
sentry_event=None
74+
capture_exception()
75+
6576
tb=traceback.format_exc()
6677
output=f"""
6778
INTERNAL ERROR IN COURSE:
@@ -74,5 +85,5 @@ def internal_error_result():
7485
returnmake_result(
7586
output=output,
7687
output_parts=[dict(color="red",text=output)],
77-
error=dict(traceback=tb),
88+
error=dict(traceback=tb,sentry_event=sentry_event),
7889
)

‎backend/main/workers/worker.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def worker_loop(task_queue, input_queue, result_queue):
7373
try:
7474
run_code(entry,input_queue,result_queue)
7575
exceptException:
76-
result_queue.put(internal_error_result())
76+
result_queue.put(internal_error_result(sentry_offline=True))
7777

7878

7979
defrun_code(entry,input_queue,result_queue):

‎poetry.lock‎

Lines changed: 31 additions & 1 deletion
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.

‎pyproject.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ psycopg2 = {version = "^2.8.4", optional = true}
2525
whitenoise ="^5.0.1"
2626
dj-database-url ="^0.5.0"
2727
django-crispy-forms ="^1.9.0"
28+
sentry-sdk ="^0.14.3"
2829

2930
[tool.poetry.extras]
3031
production = ["gevent","gunicorn","pika","psycopg2"]

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp