plainx-sentry 0.4.0__py3-none-any.whl → 0.5.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
plainx/sentry/middleware.py
CHANGED
|
@@ -41,6 +41,10 @@ class SentryMiddleware:
|
|
|
41
41
|
self.get_response = get_response
|
|
42
42
|
|
|
43
43
|
def __call__(self, request):
|
|
44
|
+
# Don't do anything if Sentry is not active
|
|
45
|
+
if not sentry_sdk.get_client().is_active():
|
|
46
|
+
return self.get_response(request)
|
|
47
|
+
|
|
44
48
|
def event_processor(event, hint):
|
|
45
49
|
# request gets attached directly to an event,
|
|
46
50
|
# not necessarily in the "context"
|
|
@@ -66,44 +70,45 @@ class SentryMiddleware:
|
|
|
66
70
|
|
|
67
71
|
return event
|
|
68
72
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
73
|
+
with sentry_sdk.isolation_scope() as scope:
|
|
74
|
+
# Reset the scope (and breadcrumbs) for each request
|
|
75
|
+
scope.clear()
|
|
76
|
+
scope.add_event_processor(event_processor)
|
|
77
|
+
|
|
78
|
+
# Sentry's Django integration patches the WSGIHandler.
|
|
79
|
+
# We could make our own WSGIHandler and patch it or call it directly from gunicorn,
|
|
80
|
+
# but putting our middleware at the top of MIDDLEWARE is pretty close and easier.
|
|
81
|
+
with sentry_sdk.start_transaction(
|
|
82
|
+
op="http.server", name=request.path_info
|
|
83
|
+
) as transaction:
|
|
84
|
+
if connection:
|
|
85
|
+
# Also get spans for db queries
|
|
86
|
+
with connection.execute_wrapper(trace_db):
|
|
87
|
+
response = self.get_response(request)
|
|
88
|
+
else:
|
|
89
|
+
# No db presumably
|
|
82
90
|
response = self.get_response(request)
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
transaction.set_http_status(response.status_code)
|
|
105
|
-
|
|
106
|
-
return response
|
|
91
|
+
|
|
92
|
+
if resolver_match := getattr(request, "resolver_match", None):
|
|
93
|
+
# Rename the transaction using a pattern,
|
|
94
|
+
# and attach other url/views tags we can use to filter
|
|
95
|
+
transaction.name = f"route:{resolver_match.route}"
|
|
96
|
+
transaction.set_tag("url_namespace", resolver_match.namespace)
|
|
97
|
+
transaction.set_tag("url_name", resolver_match.url_name)
|
|
98
|
+
transaction.set_tag("view_class", resolver_match.view.view_class.__qualname__)
|
|
99
|
+
|
|
100
|
+
# Don't need to filter on this, but do want the context to view
|
|
101
|
+
transaction.set_context(
|
|
102
|
+
"url_params",
|
|
103
|
+
{
|
|
104
|
+
"args": resolver_match.args,
|
|
105
|
+
"kwargs": resolver_match.kwargs,
|
|
106
|
+
},
|
|
107
|
+
)
|
|
108
|
+
|
|
109
|
+
transaction.set_http_status(response.status_code)
|
|
110
|
+
|
|
111
|
+
return response
|
|
107
112
|
|
|
108
113
|
|
|
109
114
|
class SentryWorkerMiddleware:
|
|
@@ -111,6 +116,10 @@ class SentryWorkerMiddleware:
|
|
|
111
116
|
self.run_job = run_job
|
|
112
117
|
|
|
113
118
|
def __call__(self, job):
|
|
119
|
+
# Don't do anything if Sentry is not active
|
|
120
|
+
if not sentry_sdk.get_client().is_active():
|
|
121
|
+
return self.run_job(job)
|
|
122
|
+
|
|
114
123
|
def event_processor(event, hint):
|
|
115
124
|
with capture_internal_exceptions():
|
|
116
125
|
# Attach it directly to any events
|
|
@@ -118,27 +127,28 @@ class SentryWorkerMiddleware:
|
|
|
118
127
|
extra["plain.worker"] = {"job": job.as_json()}
|
|
119
128
|
return event
|
|
120
129
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
130
|
+
with sentry_sdk.isolation_scope() as scope:
|
|
131
|
+
# Reset the scope (and breadcrumbs) for each request
|
|
132
|
+
scope.clear()
|
|
133
|
+
scope.add_event_processor(event_processor)
|
|
134
|
+
|
|
135
|
+
with sentry_sdk.start_transaction(
|
|
136
|
+
op="plain.worker.job",
|
|
137
|
+
name=f"job:{job.job_class}",
|
|
138
|
+
source=TransactionSource.TASK,
|
|
139
|
+
) as transaction:
|
|
140
|
+
if connection:
|
|
141
|
+
# Also get spans for db queries
|
|
142
|
+
with connection.execute_wrapper(trace_db):
|
|
143
|
+
job_result = self.run_job(job)
|
|
144
|
+
else:
|
|
145
|
+
# No db presumably
|
|
133
146
|
job_result = self.run_job(job)
|
|
134
|
-
else:
|
|
135
|
-
# No db presumably
|
|
136
|
-
job_result = self.run_job(job)
|
|
137
147
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
148
|
+
with capture_internal_exceptions():
|
|
149
|
+
# Don't need to filter on this, but do want the context to view
|
|
150
|
+
transaction.set_context("job", job.as_json())
|
|
141
151
|
|
|
142
|
-
|
|
152
|
+
transaction.set_status("ok")
|
|
143
153
|
|
|
144
|
-
|
|
154
|
+
return job_result
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
plainx/sentry/__init__.py,sha256=Klz3mH_itM2RXzJ_sWWJRIoZcS0jCRxWrhVVIPsG5FY,123
|
|
2
2
|
plainx/sentry/config.py,sha256=EwMnY3DaanIGkgtKD_4h8ncx6MH_MxOML8BF5Hx5GBA,723
|
|
3
3
|
plainx/sentry/default_settings.py,sha256=DNX6OQ0-BqfjyWuszW7JnZhiDn0tGtS3Fa2W7VbTIF8,564
|
|
4
|
-
plainx/sentry/middleware.py,sha256=
|
|
4
|
+
plainx/sentry/middleware.py,sha256=yA5gWAcyfP30VitaXrUJArfHKC4O3_hr9uN0HMZA4Mo,6019
|
|
5
5
|
plainx/sentry/templates.py,sha256=YovKIswcsjLRjHdmRzdaxG1PHjGfz8fc5TeNE9wcYQA,2046
|
|
6
6
|
plainx/sentry/templates/sentry/js.html,sha256=aOjWAwuUaecGbC-5yZWP1aaGyYAcM-GK0dru-4VOYoE,491
|
|
7
|
-
plainx_sentry-0.
|
|
8
|
-
plainx_sentry-0.
|
|
9
|
-
plainx_sentry-0.
|
|
7
|
+
plainx_sentry-0.5.0.dist-info/METADATA,sha256=mv4iGcs5UH9Ka_MjVnP9G_Zxe2tjsQZDL_uewAaokQ8,1559
|
|
8
|
+
plainx_sentry-0.5.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
|
9
|
+
plainx_sentry-0.5.0.dist-info/RECORD,,
|
|
File without changes
|