structlog-sentry 2.0.3__tar.gz → 2.1.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.1.0
4
4
  Summary: Sentry integration for structlog
5
5
  Home-page: https://github.com/kiwicom/structlog-sentry
6
6
  License: MIT
@@ -18,6 +18,7 @@ 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
21
22
  Classifier: Topic :: Software Development :: Libraries :: Python Modules
22
23
  Requires-Dist: sentry-sdk
23
24
  Requires-Dist: structlog
@@ -77,6 +78,11 @@ Do not forget to add the `structlog.stdlib.add_log_level` and optionally the
77
78
  as events. Default is `logging.WARNING`.
78
79
  - `active` A flag to make this processor enabled/disabled.
79
80
  - `as_context` Send `event_dict` as extra info to Sentry. Default is `True`.
81
+ - `ignore_breadcrumb_data` A list of data keys that will be excluded from
82
+ [breadcrumb data](https://docs.sentry.io/platforms/python/enriching-events/breadcrumbs/#manual-breadcrumbs).
83
+ Defaults to keys which are already sent separately, i.e. `level`, `logger`,
84
+ `event` and `timestamp`. All other data in `event_dict` will be sent as
85
+ breadcrumb data.
80
86
  - `tag_keys` A list of keys. If any if these keys appear in `event_dict`,
81
87
  the key and its corresponding value in `event_dict` will be used as Sentry
82
88
  event tags. use `"__all__"` to report all key/value pairs of event as tags.
@@ -105,7 +111,7 @@ to enable this behavior, just pass `exc_info=True`.
105
111
 
106
112
  When you want to use structlog's built-in
107
113
  [`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`!
114
+ processor, make that the `SentryProcessor` comes _before_ `format_exc_info`!
109
115
  Otherwise, the `SentryProcessor` won't have an `exc_info` to work with, because
110
116
  it's removed from the event by `format_exc_info`.
111
117
 
@@ -203,7 +209,7 @@ INTEGRATIONS = [
203
209
  sentry_sdk.init(integrations=INTEGRATIONS)
204
210
  ```
205
211
 
206
- This integration tells `sentry_sdk` to *ignore* standard logging and captures the events manually.
212
+ This integration tells `sentry_sdk` to _ignore_ standard logging and captures the events manually.
207
213
 
208
214
  ## Testing
209
215
 
@@ -215,5 +221,5 @@ tox
215
221
 
216
222
  ## Contributing
217
223
 
218
- Create a merge request and tag @kiwicom/platform for review.
224
+ Create a merge request and tag @kiwicom/platform for review.
219
225
 
@@ -51,6 +51,11 @@ Do not forget to add the `structlog.stdlib.add_log_level` and optionally the
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.
@@ -79,7 +84,7 @@ to enable this behavior, just pass `exc_info=True`.
79
84
 
80
85
  When you want to use structlog's built-in
81
86
  [`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`!
87
+ processor, make that the `SentryProcessor` comes _before_ `format_exc_info`!
83
88
  Otherwise, the `SentryProcessor` won't have an `exc_info` to work with, because
84
89
  it's removed from the event by `format_exc_info`.
85
90
 
@@ -177,7 +182,7 @@ INTEGRATIONS = [
177
182
  sentry_sdk.init(integrations=INTEGRATIONS)
178
183
  ```
179
184
 
180
- This integration tells `sentry_sdk` to *ignore* standard logging and captures the events manually.
185
+ This integration tells `sentry_sdk` to _ignore_ standard logging and captures the events manually.
181
186
 
182
187
  ## Testing
183
188
 
@@ -189,4 +194,4 @@ tox
189
194
 
190
195
  ## Contributing
191
196
 
192
- Create a merge request and tag @kiwicom/platform for review.
197
+ 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.1.0"
4
4
  description = "Sentry integration for structlog"
5
5
  authors = ["Kiwi.com platform <platform@kiwi.com>"]
6
6
  license = "MIT"
@@ -19,6 +19,8 @@ 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"]
@@ -30,7 +32,7 @@ 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,7 +3,8 @@ 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
9
  from sentry_sdk import Hub
9
10
  from sentry_sdk.integrations.logging import _IGNORED_LOGGERS
@@ -38,6 +39,12 @@ 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,
@@ -51,6 +58,8 @@ 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.
@@ -68,13 +77,16 @@ class SentryProcessor:
68
77
  self._hub = hub
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
 
@@ -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_hub().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}
@@ -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