plainx-sentry 0.6.0__py3-none-any.whl → 0.8.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/CHANGELOG.md +11 -0
- plainx/sentry/__init__.py +0 -3
- plainx/sentry/config.py +18 -0
- plainx/sentry/templates.py +3 -2
- {plainx_sentry-0.6.0.dist-info → plainx_sentry-0.8.0.dist-info}/METADATA +2 -8
- plainx_sentry-0.8.0.dist-info/RECORD +9 -0
- plainx/sentry/middleware.py +0 -156
- plainx_sentry-0.6.0.dist-info/RECORD +0 -9
- {plainx_sentry-0.6.0.dist-info → plainx_sentry-0.8.0.dist-info}/WHEEL +0 -0
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# plainx-sentry changelog
|
|
2
|
+
|
|
3
|
+
## [0.7.0](https://github.com/davegaeddert/plainx-sentry/releases/plainx-sentry@0.7.0) (2025-07-19)
|
|
4
|
+
|
|
5
|
+
### What's changed
|
|
6
|
+
|
|
7
|
+
- Middleware was removed in favor of using the OpenTelemetry integration and new otel instrumentation in Plain.
|
|
8
|
+
|
|
9
|
+
### Upgrade instructions
|
|
10
|
+
|
|
11
|
+
- Remove `SentryMiddleware` and `SentryWorkerMiddleware` from your `app/settings.py`.
|
plainx/sentry/__init__.py
CHANGED
plainx/sentry/config.py
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import sentry_sdk
|
|
2
|
+
from opentelemetry import trace
|
|
3
|
+
from opentelemetry.propagate import set_global_textmap
|
|
4
|
+
from opentelemetry.sdk.trace import TracerProvider
|
|
2
5
|
from plain.packages import PackageConfig, register_config
|
|
3
6
|
from plain.runtime import settings
|
|
7
|
+
from sentry_sdk.integrations.opentelemetry import SentryPropagator, SentrySpanProcessor
|
|
4
8
|
|
|
5
9
|
|
|
6
10
|
@register_config
|
|
@@ -16,5 +20,19 @@ class PlainxSentryConfig(PackageConfig):
|
|
|
16
20
|
send_default_pii=settings.SENTRY_PII_ENABLED,
|
|
17
21
|
traces_sample_rate=settings.SENTRY_TRACES_SAMPLE_RATE,
|
|
18
22
|
profiles_sample_rate=settings.SENTRY_PROFILES_SAMPLE_RATE,
|
|
23
|
+
instrumenter="otel",
|
|
19
24
|
**settings.SENTRY_INIT_KWARGS,
|
|
20
25
|
)
|
|
26
|
+
|
|
27
|
+
# Set up OpenTelemetry tracing with Sentry
|
|
28
|
+
if trace.get_tracer_provider() and not isinstance(
|
|
29
|
+
trace.get_tracer_provider(), trace.ProxyTracerProvider
|
|
30
|
+
):
|
|
31
|
+
raise RuntimeError(
|
|
32
|
+
"A tracer provider already exists. Sentry's OpenTelemetry integration requires a clean tracer provider."
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
provider = TracerProvider()
|
|
36
|
+
provider.add_span_processor(SentrySpanProcessor())
|
|
37
|
+
trace.set_tracer_provider(provider)
|
|
38
|
+
set_global_textmap(SentryPropagator())
|
plainx/sentry/templates.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import sentry_sdk
|
|
2
|
+
from plain.auth import get_request_user
|
|
2
3
|
from plain.runtime import settings
|
|
3
4
|
from plain.templates import register_template_extension
|
|
4
5
|
from plain.templates.jinja.extensions import InclusionTagExtension
|
|
@@ -25,8 +26,8 @@ class SentryJSExtension(InclusionTagExtension):
|
|
|
25
26
|
}
|
|
26
27
|
|
|
27
28
|
if "request" in context:
|
|
28
|
-
#
|
|
29
|
-
user =
|
|
29
|
+
# Get the authenticated user from the request
|
|
30
|
+
user = get_request_user(context["request"])
|
|
30
31
|
else:
|
|
31
32
|
# Get user directly if no request (like in server error context)
|
|
32
33
|
user = context.get("user", None)
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: plainx-sentry
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.8.0
|
|
4
4
|
Author-email: Dave Gaeddert <dave.gaeddert@gmail.com>
|
|
5
5
|
Requires-Python: >=3.11
|
|
6
|
-
Requires-Dist: sentry-sdk>=2.24.0
|
|
6
|
+
Requires-Dist: sentry-sdk[opentelemetry]>=2.24.0
|
|
7
7
|
Description-Content-Type: text/markdown
|
|
8
8
|
|
|
9
9
|
# plainx-sentry
|
|
@@ -20,12 +20,6 @@ INSTALLED_PACKAGES = [
|
|
|
20
20
|
# ...
|
|
21
21
|
"plainx.sentry",
|
|
22
22
|
]
|
|
23
|
-
|
|
24
|
-
MIDDLEWARE = [
|
|
25
|
-
# Put SentryMiddleware as early as possible in the middleware stack
|
|
26
|
-
"plainx.sentry.SentryMiddleware",
|
|
27
|
-
# ...
|
|
28
|
-
]
|
|
29
23
|
```
|
|
30
24
|
|
|
31
25
|
In your `base.html`, load `sentry` and include the `sentry_js` tag:
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
plainx/sentry/CHANGELOG.md,sha256=kE1atfmZkThPdQ5fS7yylRMc_REJXel9zLjrDNgiGoc,373
|
|
2
|
+
plainx/sentry/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
3
|
+
plainx/sentry/config.py,sha256=mtA9uyFtXgGqaUI-wraG06klKA5fNzTKunS2DNm1KK8,1573
|
|
4
|
+
plainx/sentry/default_settings.py,sha256=DNX6OQ0-BqfjyWuszW7JnZhiDn0tGtS3Fa2W7VbTIF8,564
|
|
5
|
+
plainx/sentry/templates.py,sha256=5dR1CwYfftnkzBan46sNHaHGu-ydQzrAhV9n_L9bb5k,2053
|
|
6
|
+
plainx/sentry/templates/sentry/js.html,sha256=aOjWAwuUaecGbC-5yZWP1aaGyYAcM-GK0dru-4VOYoE,491
|
|
7
|
+
plainx_sentry-0.8.0.dist-info/METADATA,sha256=oCOW9dxdvS4ESNc9ME0rc2vVgr8243z5mnQKvvb-1qE,1442
|
|
8
|
+
plainx_sentry-0.8.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
|
9
|
+
plainx_sentry-0.8.0.dist-info/RECORD,,
|
plainx/sentry/middleware.py
DELETED
|
@@ -1,156 +0,0 @@
|
|
|
1
|
-
import sentry_sdk
|
|
2
|
-
from plain.runtime import settings
|
|
3
|
-
from sentry_sdk.tracing import TransactionSource
|
|
4
|
-
from sentry_sdk.utils import capture_internal_exceptions
|
|
5
|
-
|
|
6
|
-
try:
|
|
7
|
-
from plain.models.db import db_connection
|
|
8
|
-
except ImportError:
|
|
9
|
-
db_connection = None
|
|
10
|
-
|
|
11
|
-
import logging
|
|
12
|
-
|
|
13
|
-
logger = logging.getLogger(__name__)
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
def trace_db(execute, sql, params, many, context):
|
|
17
|
-
with sentry_sdk.start_span(op="db", description=sql) as span:
|
|
18
|
-
# Mostly borrowed from the Sentry Django integration...
|
|
19
|
-
data = {
|
|
20
|
-
"db.params": params,
|
|
21
|
-
"db.executemany": many,
|
|
22
|
-
"db.system": db_connection.vendor,
|
|
23
|
-
"db.name": db_connection.settings_dict.get("NAME"),
|
|
24
|
-
"db.user": db_connection.settings_dict.get("USER"),
|
|
25
|
-
"server.address": db_connection.settings_dict.get("HOST"),
|
|
26
|
-
"server.port": db_connection.settings_dict.get("PORT"),
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
sentry_sdk.add_breadcrumb(message=sql, category="query", data=data)
|
|
30
|
-
|
|
31
|
-
for k, v in data.items():
|
|
32
|
-
span.set_data(k, v)
|
|
33
|
-
|
|
34
|
-
result = execute(sql, params, many, context)
|
|
35
|
-
|
|
36
|
-
return result
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
class SentryMiddleware:
|
|
40
|
-
def __init__(self, get_response):
|
|
41
|
-
self.get_response = get_response
|
|
42
|
-
|
|
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
|
-
|
|
48
|
-
def event_processor(event, hint):
|
|
49
|
-
# request gets attached directly to an event,
|
|
50
|
-
# not necessarily in the "context"
|
|
51
|
-
request_info = event.setdefault("request", {})
|
|
52
|
-
request_info["url"] = request.build_absolute_uri()
|
|
53
|
-
request_info["method"] = request.method
|
|
54
|
-
request_info["query_string"] = request.meta.get("QUERY_STRING", "")
|
|
55
|
-
# Headers and env need some PII filtering, ideally,
|
|
56
|
-
# among other filters... similar for GET/POST data?
|
|
57
|
-
# request_info["headers"] = dict(request.headers)
|
|
58
|
-
try:
|
|
59
|
-
request_info["data"] = request.body.decode("utf-8")
|
|
60
|
-
except Exception:
|
|
61
|
-
pass
|
|
62
|
-
|
|
63
|
-
if user := getattr(request, "user", None):
|
|
64
|
-
event["user"] = {"id": str(user.pk)}
|
|
65
|
-
if settings.SENTRY_PII_ENABLED:
|
|
66
|
-
if email := getattr(user, "email", None):
|
|
67
|
-
event["user"]["email"] = email
|
|
68
|
-
if username := getattr(user, "username", None):
|
|
69
|
-
event["user"]["username"] = username
|
|
70
|
-
|
|
71
|
-
return event
|
|
72
|
-
|
|
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 db_connection:
|
|
85
|
-
# Also get spans for db queries
|
|
86
|
-
with db_connection.execute_wrapper(trace_db):
|
|
87
|
-
response = self.get_response(request)
|
|
88
|
-
else:
|
|
89
|
-
# No db presumably
|
|
90
|
-
response = self.get_response(request)
|
|
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(
|
|
99
|
-
"view_class", resolver_match.view.view_class.__qualname__
|
|
100
|
-
)
|
|
101
|
-
|
|
102
|
-
# Don't need to filter on this, but do want the context to view
|
|
103
|
-
transaction.set_context(
|
|
104
|
-
"url_params",
|
|
105
|
-
{
|
|
106
|
-
"args": resolver_match.args,
|
|
107
|
-
"kwargs": resolver_match.kwargs,
|
|
108
|
-
},
|
|
109
|
-
)
|
|
110
|
-
|
|
111
|
-
transaction.set_http_status(response.status_code)
|
|
112
|
-
|
|
113
|
-
return response
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
class SentryWorkerMiddleware:
|
|
117
|
-
def __init__(self, run_job):
|
|
118
|
-
self.run_job = run_job
|
|
119
|
-
|
|
120
|
-
def __call__(self, job):
|
|
121
|
-
# Don't do anything if Sentry is not active
|
|
122
|
-
if not sentry_sdk.get_client().is_active():
|
|
123
|
-
return self.run_job(job)
|
|
124
|
-
|
|
125
|
-
def event_processor(event, hint):
|
|
126
|
-
with capture_internal_exceptions():
|
|
127
|
-
# Attach it directly to any events
|
|
128
|
-
extra = event.setdefault("extra", {})
|
|
129
|
-
extra["plain.worker"] = {"job": job.as_json()}
|
|
130
|
-
return event
|
|
131
|
-
|
|
132
|
-
with sentry_sdk.isolation_scope() as scope:
|
|
133
|
-
# Reset the scope (and breadcrumbs) for each request
|
|
134
|
-
scope.clear()
|
|
135
|
-
scope.add_event_processor(event_processor)
|
|
136
|
-
|
|
137
|
-
with sentry_sdk.start_transaction(
|
|
138
|
-
op="plain.worker.job",
|
|
139
|
-
name=f"job:{job.job_class}",
|
|
140
|
-
source=TransactionSource.TASK,
|
|
141
|
-
) as transaction:
|
|
142
|
-
if db_connection:
|
|
143
|
-
# Also get spans for db queries
|
|
144
|
-
with db_connection.execute_wrapper(trace_db):
|
|
145
|
-
job_result = self.run_job(job)
|
|
146
|
-
else:
|
|
147
|
-
# No db presumably
|
|
148
|
-
job_result = self.run_job(job)
|
|
149
|
-
|
|
150
|
-
with capture_internal_exceptions():
|
|
151
|
-
# Don't need to filter on this, but do want the context to view
|
|
152
|
-
transaction.set_context("job", job.as_json())
|
|
153
|
-
|
|
154
|
-
transaction.set_status("ok")
|
|
155
|
-
|
|
156
|
-
return job_result
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
plainx/sentry/__init__.py,sha256=Klz3mH_itM2RXzJ_sWWJRIoZcS0jCRxWrhVVIPsG5FY,123
|
|
2
|
-
plainx/sentry/config.py,sha256=EwMnY3DaanIGkgtKD_4h8ncx6MH_MxOML8BF5Hx5GBA,723
|
|
3
|
-
plainx/sentry/default_settings.py,sha256=DNX6OQ0-BqfjyWuszW7JnZhiDn0tGtS3Fa2W7VbTIF8,564
|
|
4
|
-
plainx/sentry/middleware.py,sha256=gGLxsRFHGJ5f6yLnpMnz8p7AnUnhg1I_vkd0bHtSKic,6098
|
|
5
|
-
plainx/sentry/templates.py,sha256=ARLvITEyrOQjnuuRkdHvQT0bCar1ya2j-wueDjC4tu4,2048
|
|
6
|
-
plainx/sentry/templates/sentry/js.html,sha256=aOjWAwuUaecGbC-5yZWP1aaGyYAcM-GK0dru-4VOYoE,491
|
|
7
|
-
plainx_sentry-0.6.0.dist-info/METADATA,sha256=4_qF1CiLjhjFJ4WBjoLzq-aRHFo4_Qe6vlsR9SdTVwQ,1559
|
|
8
|
-
plainx_sentry-0.6.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
|
9
|
-
plainx_sentry-0.6.0.dist-info/RECORD,,
|
|
File without changes
|