structlog-sentry 2.0.3__tar.gz → 2.2.0__tar.gz

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.
@@ -0,0 +1,30 @@
1
+ repos:
2
+ - repo: https://github.com/astral-sh/ruff-pre-commit
3
+ rev: v0.3.2
4
+ hooks:
5
+ - id: ruff-format
6
+
7
+ - repo: https://github.com/astral-sh/ruff-pre-commit
8
+ rev: v0.3.2
9
+ hooks:
10
+ - id: ruff
11
+ args: ["--fix", "--exit-non-zero-on-fix", "--config", "pyproject.toml"]
12
+
13
+ - repo: https://github.com/pre-commit/pre-commit-hooks
14
+ rev: v4.5.0
15
+ hooks:
16
+ - id: trailing-whitespace
17
+ - id: end-of-file-fixer
18
+ - id: debug-statements
19
+
20
+ - repo: https://github.com/pre-commit/mirrors-mypy
21
+ rev: v1.9.0
22
+ hooks:
23
+ - id: mypy
24
+
25
+ - repo: https://github.com/pre-commit/mirrors-prettier
26
+ rev: v3.1.0
27
+ hooks:
28
+ - id: prettier
29
+ name: prettier (*.md)
30
+ files: .*\.md
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: structlog-sentry
3
- Version: 2.0.3
3
+ Version: 2.2.0
4
4
  Summary: Sentry integration for structlog
5
5
  Home-page: https://github.com/kiwicom/structlog-sentry
6
6
  License: MIT
@@ -18,8 +18,10 @@ Classifier: Programming Language :: Python :: 3.8
18
18
  Classifier: Programming Language :: Python :: 3.9
19
19
  Classifier: Programming Language :: Python :: 3.10
20
20
  Classifier: Programming Language :: Python :: 3.11
21
+ Classifier: Programming Language :: Python :: 3.12
22
+ Classifier: Programming Language :: Python :: 3.13
21
23
  Classifier: Topic :: Software Development :: Libraries :: Python Modules
22
- Requires-Dist: sentry-sdk
24
+ Requires-Dist: sentry-sdk (>=2.0,<3.0)
23
25
  Requires-Dist: structlog
24
26
  Project-URL: Repository, https://github.com/kiwicom/structlog-sentry
25
27
  Description-Content-Type: text/markdown
@@ -72,18 +74,24 @@ Do not forget to add the `structlog.stdlib.add_log_level` and optionally the
72
74
  `SentryProcessor` class takes the following arguments:
73
75
 
74
76
  - `level` Events of this or higher levels will be reported as Sentry
75
- breadcrumbs. Dfault is `logging.INFO`.
77
+ breadcrumbs. Default is `logging.INFO`.
76
78
  - `event_level` Events of this or higher levels will be reported to Sentry
77
79
  as events. Default is `logging.WARNING`.
78
80
  - `active` A flag to make this processor enabled/disabled.
79
81
  - `as_context` Send `event_dict` as extra info to Sentry. Default is `True`.
82
+ - `ignore_breadcrumb_data` A list of data keys that will be excluded from
83
+ [breadcrumb data](https://docs.sentry.io/platforms/python/enriching-events/breadcrumbs/#manual-breadcrumbs).
84
+ Defaults to keys which are already sent separately, i.e. `level`, `logger`,
85
+ `event` and `timestamp`. All other data in `event_dict` will be sent as
86
+ breadcrumb data.
80
87
  - `tag_keys` A list of keys. If any if these keys appear in `event_dict`,
81
88
  the key and its corresponding value in `event_dict` will be used as Sentry
82
89
  event tags. use `"__all__"` to report all key/value pairs of event as tags.
83
90
  - `ignore_loggers` A list of logger names to ignore any events from.
84
91
  - `verbose` Report the action taken by the logger in the `event_dict`.
85
92
  Default is `False`.
86
- - `hub` Optionally specify `sentry_sdk.Hub`.
93
+ - `scope` Optionally specify `sentry_sdk.Client` (in `structlog-sentry<2.2`
94
+ this corresponds to `hub: sentry_sdk.Hub`).
87
95
 
88
96
  Now events are automatically captured by Sentry with `log.error()`:
89
97
 
@@ -105,7 +113,7 @@ to enable this behavior, just pass `exc_info=True`.
105
113
 
106
114
  When you want to use structlog's built-in
107
115
  [`format_exc_info`](http://www.structlog.org/en/stable/api.html#structlog.processors.format_exc_info)
108
- processor, make that the `SentryProcessor` comes *before* `format_exc_info`!
116
+ processor, make that the `SentryProcessor` comes _before_ `format_exc_info`!
109
117
  Otherwise, the `SentryProcessor` won't have an `exc_info` to work with, because
110
118
  it's removed from the event by `format_exc_info`.
111
119
 
@@ -134,7 +142,7 @@ structlog.configure(
134
142
  processors=[
135
143
  structlog.stdlib.add_logger_name,
136
144
  structlog.stdlib.add_log_level,
137
- SentryProcessor(level=logging.ERROR, tag_keys=["city", "timezone"]),
145
+ SentryProcessor(event_level=logging.ERROR, tag_keys=["city", "timezone"]),
138
146
  ],...
139
147
  )
140
148
 
@@ -149,7 +157,7 @@ structlog.configure(
149
157
  processors=[
150
158
  structlog.stdlib.add_logger_name,
151
159
  structlog.stdlib.add_log_level,
152
- SentryProcessor(level=logging.ERROR, tag_keys="__all__"),
160
+ SentryProcessor(event_level=logging.ERROR, tag_keys="__all__"),
153
161
  ],...
154
162
  )
155
163
  ```
@@ -164,7 +172,7 @@ structlog.configure(
164
172
  processors=[
165
173
  structlog.stdlib.add_logger_name,
166
174
  structlog.stdlib.add_log_level,
167
- SentryProcessor(level=logging.ERROR, as_context=False, tag_keys="__all__"),
175
+ SentryProcessor(event_level=logging.ERROR, as_context=False, tag_keys="__all__"),
168
176
  ],...
169
177
  )
170
178
  ```
@@ -179,7 +187,7 @@ structlog.configure(
179
187
  processors=[
180
188
  structlog.stdlib.add_logger_name,
181
189
  structlog.stdlib.add_log_level,
182
- SentryProcessor(level=logging.ERROR, ignore_loggers=["some.logger"]),
190
+ SentryProcessor(event_level=logging.ERROR, ignore_loggers=["some.logger"]),
183
191
  ],...
184
192
  )
185
193
  ```
@@ -203,7 +211,7 @@ INTEGRATIONS = [
203
211
  sentry_sdk.init(integrations=INTEGRATIONS)
204
212
  ```
205
213
 
206
- This integration tells `sentry_sdk` to *ignore* standard logging and captures the events manually.
214
+ This integration tells `sentry_sdk` to _ignore_ standard logging and captures the events manually.
207
215
 
208
216
  ## Testing
209
217
 
@@ -215,5 +223,5 @@ tox
215
223
 
216
224
  ## Contributing
217
225
 
218
- Create a merge request and tag @kiwicom/platform for review.
226
+ Create a merge request and tag @kiwicom/platform for review.
219
227
 
@@ -46,18 +46,24 @@ Do not forget to add the `structlog.stdlib.add_log_level` and optionally the
46
46
  `SentryProcessor` class takes the following arguments:
47
47
 
48
48
  - `level` Events of this or higher levels will be reported as Sentry
49
- breadcrumbs. Dfault is `logging.INFO`.
49
+ breadcrumbs. Default is `logging.INFO`.
50
50
  - `event_level` Events of this or higher levels will be reported to Sentry
51
51
  as events. Default is `logging.WARNING`.
52
52
  - `active` A flag to make this processor enabled/disabled.
53
53
  - `as_context` Send `event_dict` as extra info to Sentry. Default is `True`.
54
+ - `ignore_breadcrumb_data` A list of data keys that will be excluded from
55
+ [breadcrumb data](https://docs.sentry.io/platforms/python/enriching-events/breadcrumbs/#manual-breadcrumbs).
56
+ Defaults to keys which are already sent separately, i.e. `level`, `logger`,
57
+ `event` and `timestamp`. All other data in `event_dict` will be sent as
58
+ breadcrumb data.
54
59
  - `tag_keys` A list of keys. If any if these keys appear in `event_dict`,
55
60
  the key and its corresponding value in `event_dict` will be used as Sentry
56
61
  event tags. use `"__all__"` to report all key/value pairs of event as tags.
57
62
  - `ignore_loggers` A list of logger names to ignore any events from.
58
63
  - `verbose` Report the action taken by the logger in the `event_dict`.
59
64
  Default is `False`.
60
- - `hub` Optionally specify `sentry_sdk.Hub`.
65
+ - `scope` Optionally specify `sentry_sdk.Client` (in `structlog-sentry<2.2`
66
+ this corresponds to `hub: sentry_sdk.Hub`).
61
67
 
62
68
  Now events are automatically captured by Sentry with `log.error()`:
63
69
 
@@ -79,7 +85,7 @@ to enable this behavior, just pass `exc_info=True`.
79
85
 
80
86
  When you want to use structlog's built-in
81
87
  [`format_exc_info`](http://www.structlog.org/en/stable/api.html#structlog.processors.format_exc_info)
82
- processor, make that the `SentryProcessor` comes *before* `format_exc_info`!
88
+ processor, make that the `SentryProcessor` comes _before_ `format_exc_info`!
83
89
  Otherwise, the `SentryProcessor` won't have an `exc_info` to work with, because
84
90
  it's removed from the event by `format_exc_info`.
85
91
 
@@ -108,7 +114,7 @@ structlog.configure(
108
114
  processors=[
109
115
  structlog.stdlib.add_logger_name,
110
116
  structlog.stdlib.add_log_level,
111
- SentryProcessor(level=logging.ERROR, tag_keys=["city", "timezone"]),
117
+ SentryProcessor(event_level=logging.ERROR, tag_keys=["city", "timezone"]),
112
118
  ],...
113
119
  )
114
120
 
@@ -123,7 +129,7 @@ structlog.configure(
123
129
  processors=[
124
130
  structlog.stdlib.add_logger_name,
125
131
  structlog.stdlib.add_log_level,
126
- SentryProcessor(level=logging.ERROR, tag_keys="__all__"),
132
+ SentryProcessor(event_level=logging.ERROR, tag_keys="__all__"),
127
133
  ],...
128
134
  )
129
135
  ```
@@ -138,7 +144,7 @@ structlog.configure(
138
144
  processors=[
139
145
  structlog.stdlib.add_logger_name,
140
146
  structlog.stdlib.add_log_level,
141
- SentryProcessor(level=logging.ERROR, as_context=False, tag_keys="__all__"),
147
+ SentryProcessor(event_level=logging.ERROR, as_context=False, tag_keys="__all__"),
142
148
  ],...
143
149
  )
144
150
  ```
@@ -153,7 +159,7 @@ structlog.configure(
153
159
  processors=[
154
160
  structlog.stdlib.add_logger_name,
155
161
  structlog.stdlib.add_log_level,
156
- SentryProcessor(level=logging.ERROR, ignore_loggers=["some.logger"]),
162
+ SentryProcessor(event_level=logging.ERROR, ignore_loggers=["some.logger"]),
157
163
  ],...
158
164
  )
159
165
  ```
@@ -177,7 +183,7 @@ INTEGRATIONS = [
177
183
  sentry_sdk.init(integrations=INTEGRATIONS)
178
184
  ```
179
185
 
180
- This integration tells `sentry_sdk` to *ignore* standard logging and captures the events manually.
186
+ This integration tells `sentry_sdk` to _ignore_ standard logging and captures the events manually.
181
187
 
182
188
  ## Testing
183
189
 
@@ -189,4 +195,4 @@ tox
189
195
 
190
196
  ## Contributing
191
197
 
192
- Create a merge request and tag @kiwicom/platform for review.
198
+ Create a merge request and tag @kiwicom/platform for review.
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "structlog-sentry"
3
- version = "2.0.3"
3
+ version = "2.2.0"
4
4
  description = "Sentry integration for structlog"
5
5
  authors = ["Kiwi.com platform <platform@kiwi.com>"]
6
6
  license = "MIT"
@@ -19,18 +19,20 @@ classifiers=[
19
19
  "Programming Language :: Python :: 3.8",
20
20
  "Programming Language :: Python :: 3.9",
21
21
  "Programming Language :: Python :: 3.10",
22
+ "Programming Language :: Python :: 3.11",
23
+ "Programming Language :: Python :: 3.12",
22
24
  "Topic :: Software Development :: Libraries :: Python Modules",
23
25
  ]
24
26
  include = ["*.md", "*.toml", "*.txt", "*.yml", "*.yaml", ".coveragerc", "tox.ini", "structlog_sentry/py.typed"]
25
27
 
26
28
  [tool.poetry.dependencies]
27
29
  python = "^3.7"
28
- sentry-sdk = "*"
30
+ sentry-sdk = "^2.0"
29
31
  structlog = "*"
30
32
 
31
33
  [tool.poetry.dev-dependencies]
32
34
  pytest = "^7.1.3"
33
- pytest-cov = "^3.0.0"
35
+ pytest-cov = "^4.1.0"
34
36
  pytest-mock = "^3.8.2"
35
37
 
36
38
  [build-system]
@@ -3,9 +3,10 @@ from __future__ import annotations
3
3
  import logging
4
4
  import sys
5
5
  from fnmatch import fnmatch
6
- from typing import Any, Iterable, Optional
6
+ from typing import Any, Optional
7
+ from collections.abc import MutableMapping, Iterable
7
8
 
8
- from sentry_sdk import Hub
9
+ from sentry_sdk import Scope, get_isolation_scope
9
10
  from sentry_sdk.integrations.logging import _IGNORED_LOGGERS
10
11
  from sentry_sdk.utils import capture_internal_exceptions, event_from_exception
11
12
  from structlog.types import EventDict, ExcInfo, WrappedLogger
@@ -38,10 +39,16 @@ class SentryProcessor:
38
39
  event_level: int = logging.WARNING,
39
40
  active: bool = True,
40
41
  as_context: bool = True,
42
+ ignore_breadcrumb_data: Iterable[str] = (
43
+ "level",
44
+ "logger",
45
+ "event",
46
+ "timestamp",
47
+ ),
41
48
  tag_keys: list[str] | str | None = None,
42
49
  ignore_loggers: Iterable[str] | None = None,
43
50
  verbose: bool = False,
44
- hub: Hub | None = None,
51
+ scope: Scope | None = None,
45
52
  ) -> None:
46
53
  """
47
54
  :param level: Events of this or higher levels will be reported as
@@ -51,13 +58,15 @@ class SentryProcessor:
51
58
  :param active: A flag to make this processor enabled/disabled.
52
59
  :param as_context: Send `event_dict` as extra info to Sentry.
53
60
  Default is :obj:`True`.
61
+ :param ignore_breadcrumb_data: A list of data keys that will be excluded from
62
+ breadcrumb data. Defaults to keys which are already sent separately.
54
63
  :param tag_keys: A list of keys. If any if these keys appear in `event_dict`,
55
64
  the key and its corresponding value in `event_dict` will be used as Sentry
56
65
  event tags. use `"__all__"` to report all key/value pairs of event as tags.
57
66
  :param ignore_loggers: A list of logger names to ignore any events from.
58
67
  :param verbose: Report the action taken by the logger in the `event_dict`.
59
68
  Default is :obj:`False`.
60
- :param hub: Optionally specify :obj:`sentry_sdk.Hub`.
69
+ :param scope: Optionally specify :obj:`sentry_sdk.Scope`.
61
70
  """
62
71
  self.event_level = event_level
63
72
  self.level = level
@@ -65,16 +74,19 @@ class SentryProcessor:
65
74
  self.tag_keys = tag_keys
66
75
  self.verbose = verbose
67
76
 
68
- self._hub = hub
77
+ self._scope = scope
69
78
  self._as_context = as_context
70
79
  self._original_event_dict: dict = {}
80
+ self.ignore_breadcrumb_data = ignore_breadcrumb_data
71
81
 
72
82
  self._ignored_loggers: set[str] = set()
73
83
  if ignore_loggers is not None:
74
84
  self._ignored_loggers.update(set(ignore_loggers))
75
85
 
76
86
  @staticmethod
77
- def _get_logger_name(logger: WrappedLogger, event_dict: dict) -> Optional[str]:
87
+ def _get_logger_name(
88
+ logger: WrappedLogger, event_dict: MutableMapping[str, Any]
89
+ ) -> Optional[str]:
78
90
  """Get logger name from event_dict with a fallbacks to logger.name and
79
91
  record.name
80
92
 
@@ -95,8 +107,8 @@ class SentryProcessor:
95
107
 
96
108
  return logger_name
97
109
 
98
- def _get_hub(self) -> Hub:
99
- return self._hub or Hub.current
110
+ def _get_scope(self) -> Scope:
111
+ return self._scope or get_isolation_scope()
100
112
 
101
113
  def _get_event_and_hint(self, event_dict: EventDict) -> tuple[dict, dict]:
102
114
  """Create a sentry event and hint from structlog `event_dict` and sys.exc_info.
@@ -107,9 +119,11 @@ class SentryProcessor:
107
119
  has_exc_info = exc_info and exc_info != (None, None, None)
108
120
 
109
121
  if has_exc_info:
122
+ client = self._get_scope().client
123
+ options: dict[str, Any] = client.options if client else {}
110
124
  event, hint = event_from_exception(
111
125
  exc_info,
112
- client_options=self._get_hub().client.options,
126
+ client_options=options,
113
127
  )
114
128
  else:
115
129
  event, hint = {}, {}
@@ -131,13 +145,16 @@ class SentryProcessor:
131
145
  return event, hint
132
146
 
133
147
  def _get_breadcrumb_and_hint(self, event_dict: EventDict) -> tuple[dict, dict]:
148
+ data = {
149
+ k: v for k, v in event_dict.items() if k not in self.ignore_breadcrumb_data
150
+ }
134
151
  event = {
135
152
  "type": "log",
136
153
  "level": event_dict.get("level"), # type: ignore
137
154
  "category": event_dict.get("logger"),
138
155
  "message": event_dict["event"],
139
156
  "timestamp": event_dict.get("timestamp"),
140
- "data": {},
157
+ "data": data,
141
158
  }
142
159
 
143
160
  return event, {"log_record": event_dict}
@@ -155,7 +172,7 @@ class SentryProcessor:
155
172
  def _handle_event(self, event_dict: EventDict) -> None:
156
173
  with capture_internal_exceptions():
157
174
  event, hint = self._get_event_and_hint(event_dict)
158
- sid = self._get_hub().capture_event(event, hint=hint)
175
+ sid = self._get_scope().capture_event(event, hint=hint)
159
176
  if sid:
160
177
  event_dict["sentry_id"] = sid
161
178
  if self.verbose:
@@ -164,7 +181,7 @@ class SentryProcessor:
164
181
  def _handle_breadcrumb(self, event_dict: EventDict) -> None:
165
182
  with capture_internal_exceptions():
166
183
  event, hint = self._get_breadcrumb_and_hint(event_dict)
167
- self._get_hub().add_breadcrumb(event, hint=hint)
184
+ self._get_scope().add_breadcrumb(event, hint=hint)
168
185
 
169
186
  @staticmethod
170
187
  def _get_level_value(level_name: str) -> int:
@@ -185,7 +202,7 @@ class SentryProcessor:
185
202
  self, logger: WrappedLogger, name: str, event_dict: EventDict
186
203
  ) -> EventDict:
187
204
  """A middleware to process structlog `event_dict` and send it to Sentry."""
188
- self._original_event_dict = event_dict.copy()
205
+ self._original_event_dict = dict(event_dict)
189
206
  sentry_skip = event_dict.pop("sentry_skip", False)
190
207
 
191
208
  if self.active and not sentry_skip and self._can_record(logger, event_dict):
@@ -1,9 +1,18 @@
1
1
  [tox]
2
2
  envlist =
3
3
  lint
4
- py{37,38,39,310}
4
+ py{37,38,39,310,311,312}
5
5
  isolated_build = true
6
6
 
7
+ [gh-actions]
8
+ python =
9
+ 3.7: py37
10
+ 3.8: py38
11
+ 3.9: py39
12
+ 3.10: py310
13
+ 3.11: py311
14
+ 3.12: py312
15
+
7
16
  [testenv]
8
17
  whitelist_externals = poetry
9
18
  deps = poetry
@@ -1,31 +0,0 @@
1
- repos:
2
- - repo: https://github.com/psf/black
3
- rev: 23.3.0
4
- hooks:
5
- - id: black
6
- language_version: python3
7
-
8
- - repo: https://github.com/pycqa/flake8
9
- rev: 6.0.0
10
- hooks:
11
- - id: flake8
12
- language_version: python3
13
-
14
- - repo: https://github.com/pycqa/isort
15
- rev: 5.12.0
16
- hooks:
17
- - id: isort
18
- # extra dependencies for config in pyproject.toml
19
- additional_dependencies: [".[pyproject]"]
20
-
21
- - repo: https://github.com/pre-commit/pre-commit-hooks
22
- rev: v4.4.0
23
- hooks:
24
- - id: trailing-whitespace
25
- - id: end-of-file-fixer
26
- - id: debug-statements
27
-
28
- - repo: https://github.com/pre-commit/mirrors-mypy
29
- rev: v1.4.0
30
- hooks:
31
- - id: mypy