plainx-sentry 0.10.1__py3-none-any.whl → 0.11.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.
@@ -1,5 +1,18 @@
1
1
  # plainx-sentry changelog
2
2
 
3
+ ## [0.11.0](https://github.com/davegaeddert/plainx-sentry/releases/v0.11.0) (2026-02-05)
4
+
5
+ ### What's changed
6
+
7
+ - Added `SentryMiddleware` for automatic request and user context on Sentry events ([950df07](https://github.com/davegaeddert/plainx-sentry/commit/950df07))
8
+ - Now requires Python 3.13+ ([dbfb0d7](https://github.com/davegaeddert/plainx-sentry/commit/dbfb0d7))
9
+ - Added type annotations throughout the codebase ([dbfb0d7](https://github.com/davegaeddert/plainx-sentry/commit/dbfb0d7))
10
+
11
+ ### Upgrade instructions
12
+
13
+ - Ensure you are running Python 3.13 or higher
14
+ - Add `SentryMiddleware` to your middleware stack after `SessionMiddleware` for automatic request/user context
15
+
3
16
  ## [0.7.0](https://github.com/davegaeddert/plainx-sentry/releases/plainx-sentry@0.7.0) (2025-07-19)
4
17
 
5
18
  ### What's changed
plainx/sentry/config.py CHANGED
@@ -11,7 +11,7 @@ from sentry_sdk.integrations.opentelemetry import SentryPropagator, SentrySpanPr
11
11
  class PlainxSentryConfig(PackageConfig):
12
12
  label = "plainxsentry"
13
13
 
14
- def ready(self):
14
+ def ready(self) -> None:
15
15
  if settings.SENTRY_DSN and settings.SENTRY_AUTO_INIT:
16
16
  sentry_sdk.init(
17
17
  settings.SENTRY_DSN,
@@ -0,0 +1,73 @@
1
+ from typing import Any
2
+
3
+ import sentry_sdk
4
+ from plain.auth import get_request_user
5
+ from plain.http import HttpMiddleware
6
+ from plain.http.request import Request
7
+ from plain.http.response import Response
8
+ from sentry_sdk.scope import should_send_default_pii
9
+
10
+
11
+ def _build_request_info(request: Request) -> dict[str, Any]:
12
+ """Build request context dictionary for Sentry events."""
13
+ try:
14
+ url = request.build_absolute_uri()
15
+ except Exception:
16
+ url = request.path
17
+
18
+ request_info = {
19
+ "method": request.method,
20
+ "url": url,
21
+ "query_string": request.query_string,
22
+ }
23
+
24
+ if should_send_default_pii():
25
+ request_info["headers"] = dict(request.headers)
26
+ request_info["cookies"] = dict(request.cookies)
27
+
28
+ return request_info
29
+
30
+
31
+ def _build_user_info(user: Any) -> dict[str, Any]:
32
+ """Build user context dictionary for Sentry events."""
33
+ user_info = {"id": str(user.id)}
34
+
35
+ if should_send_default_pii():
36
+ if email := getattr(user, "email", None):
37
+ user_info["email"] = email
38
+ if username := getattr(user, "username", None):
39
+ user_info["username"] = username
40
+
41
+ return user_info
42
+
43
+
44
+ class SentryMiddleware(HttpMiddleware):
45
+ """
46
+ Middleware that registers a Sentry event processor for request/user context.
47
+
48
+ Add this to your MIDDLEWARE setting after SessionMiddleware:
49
+
50
+ MIDDLEWARE = [
51
+ ...
52
+ "plain.sessions.middleware.SessionMiddleware",
53
+ "plainx.sentry.middleware.SentryMiddleware",
54
+ ...
55
+ ]
56
+ """
57
+
58
+ def process_request(self, request: Request) -> Response:
59
+ def event_processor(
60
+ event: dict[str, Any], hint: dict[str, Any]
61
+ ) -> dict[str, Any]:
62
+ if "request" not in event:
63
+ event["request"] = _build_request_info(request)
64
+
65
+ if "user" not in event:
66
+ user = get_request_user(request)
67
+ if user:
68
+ event["user"] = _build_user_info(user)
69
+
70
+ return event
71
+
72
+ sentry_sdk.get_current_scope().add_event_processor(event_processor)
73
+ return self.get_response(request)
@@ -1,4 +1,7 @@
1
+ from typing import Any
2
+
1
3
  import sentry_sdk
4
+ from jinja2.runtime import Context
2
5
  from plain.auth import get_request_user
3
6
  from plain.runtime import settings
4
7
  from plain.templates import register_template_extension
@@ -10,7 +13,9 @@ class SentryJSExtension(InclusionTagExtension):
10
13
  tags = {"sentry_js"}
11
14
  template_name = "sentry/js.html"
12
15
 
13
- def get_context(self, context, *args, **kwargs):
16
+ def get_context(
17
+ self, context: Context, *args: Any, **kwargs: Any
18
+ ) -> Context | dict[str, Any]:
14
19
  if not settings.SENTRY_DSN:
15
20
  return {}
16
21
 
@@ -45,7 +50,10 @@ class SentryJSExtension(InclusionTagExtension):
45
50
  class SentryFeedbackExtension(SentryJSExtension):
46
51
  tags = {"sentry_feedback"}
47
52
 
48
- def get_context(self, context, *args, **kwargs):
49
- context = super().get_context(context, *args, **kwargs)
50
- context["sentry_dialog_event_id"] = sentry_sdk.last_event_id()
51
- return context
53
+ def get_context(
54
+ self, context: Context, *args: Any, **kwargs: Any
55
+ ) -> dict[str, Any]:
56
+ parent_result = super().get_context(context, *args, **kwargs)
57
+ result: dict[str, Any] = dict(parent_result)
58
+ result["sentry_dialog_event_id"] = sentry_sdk.last_event_id()
59
+ return result
@@ -1,8 +1,8 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: plainx-sentry
3
- Version: 0.10.1
3
+ Version: 0.11.0
4
4
  Author-email: Dave Gaeddert <dave.gaeddert@gmail.com>
5
- Requires-Python: >=3.11
5
+ Requires-Python: >=3.13
6
6
  Requires-Dist: plain-auth>=0.16.0
7
7
  Requires-Dist: plain-sessions>=0.27.0
8
8
  Requires-Dist: sentry-sdk[opentelemetry]>=2.24.0
@@ -47,6 +47,27 @@ In Heroku, for example:
47
47
  heroku config:set SENTRY_DSN=<your-DSN>
48
48
  ```
49
49
 
50
+ ## User and request context
51
+
52
+ To attach user and request context to errors, add the middleware:
53
+
54
+ ```python
55
+ # settings.py
56
+ MIDDLEWARE = [
57
+ ...
58
+ "plain.sessions.middleware.SessionMiddleware",
59
+ "plainx.sentry.middleware.SentryMiddleware", # After SessionMiddleware
60
+ ...
61
+ ]
62
+ ```
63
+
64
+ This attaches to errors:
65
+
66
+ - **User context**: ID, email, username
67
+ - **Request context**: URL, method, query string, headers, cookies
68
+
69
+ Email, username, headers, and cookies require `SENTRY_PII_ENABLED=True` (the default).
70
+
50
71
  ## Configuration
51
72
 
52
73
  [Look at the `default_settings.py` for all available settings.](./plainx/sentry/default_settings.py)
@@ -0,0 +1,10 @@
1
+ plainx/sentry/CHANGELOG.md,sha256=Gw_klbDvuQEfHs9aP8z6B7tuntKjCiqd2m7vHNahBHs,1051
2
+ plainx/sentry/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
3
+ plainx/sentry/config.py,sha256=Oqdj__9aAPiSOcv5zCzKfek7W8Wt9jDVU6Ok1J7lm7I,1581
4
+ plainx/sentry/default_settings.py,sha256=DNX6OQ0-BqfjyWuszW7JnZhiDn0tGtS3Fa2W7VbTIF8,564
5
+ plainx/sentry/middleware.py,sha256=d5ROMQo4Kbu7BqziVtX0NBSYdnQ6bwAsF685jp9dlxE,2156
6
+ plainx/sentry/templates.py,sha256=Wx94C6rS0SeebdwxP0HnrwGxVvBOHr3sBXTuZeK8D2s,2092
7
+ plainx/sentry/templates/sentry/js.html,sha256=c38ltOa6RZWqElLbC6yeH82yxTCmSnZtloaqI7zxE2M,550
8
+ plainx_sentry-0.11.0.dist-info/METADATA,sha256=h1sHBOeiJOGE3Iiap4BKdLMpGMhQlXTo6ePZypBrsJk,2022
9
+ plainx_sentry-0.11.0.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
10
+ plainx_sentry-0.11.0.dist-info/RECORD,,
@@ -1,4 +1,4 @@
1
1
  Wheel-Version: 1.0
2
- Generator: hatchling 1.27.0
2
+ Generator: hatchling 1.28.0
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
@@ -1,9 +0,0 @@
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=HW3WJqdkh3SXCuon6i2Kj6Ip9lASr0lDeSi_V8KCuZ0,1864
6
- plainx/sentry/templates/sentry/js.html,sha256=c38ltOa6RZWqElLbC6yeH82yxTCmSnZtloaqI7zxE2M,550
7
- plainx_sentry-0.10.1.dist-info/METADATA,sha256=VP0eZhUn7lE6vIm29xUIPbqje50XF4GswfbeyHUTiGw,1515
8
- plainx_sentry-0.10.1.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
9
- plainx_sentry-0.10.1.dist-info/RECORD,,