structlog-sentry 2.0.0b2__tar.gz → 2.0.2__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.
- {structlog-sentry-2.0.0b2 → structlog_sentry-2.0.2}/.pre-commit-config.yaml +5 -5
- {structlog-sentry-2.0.0b2 → structlog_sentry-2.0.2}/PKG-INFO +13 -7
- {structlog-sentry-2.0.0b2 → structlog_sentry-2.0.2}/README.md +5 -6
- {structlog-sentry-2.0.0b2 → structlog_sentry-2.0.2}/pyproject.toml +3 -2
- {structlog-sentry-2.0.0b2 → structlog_sentry-2.0.2}/structlog_sentry/__init__.py +32 -3
- structlog-sentry-2.0.0b2/setup.py +0 -30
- {structlog-sentry-2.0.0b2 → structlog_sentry-2.0.2}/.coveragerc +0 -0
- {structlog-sentry-2.0.0b2 → structlog_sentry-2.0.2}/LICENSE +0 -0
- {structlog-sentry-2.0.0b2 → structlog_sentry-2.0.2}/structlog_sentry/py.typed +0 -0
- {structlog-sentry-2.0.0b2 → structlog_sentry-2.0.2}/tox.ini +0 -0
|
@@ -1,25 +1,25 @@
|
|
|
1
1
|
repos:
|
|
2
2
|
- repo: https://github.com/psf/black
|
|
3
|
-
rev:
|
|
3
|
+
rev: 23.3.0
|
|
4
4
|
hooks:
|
|
5
5
|
- id: black
|
|
6
6
|
language_version: python3
|
|
7
7
|
|
|
8
|
-
- repo: https://
|
|
9
|
-
rev:
|
|
8
|
+
- repo: https://github.com/pycqa/flake8
|
|
9
|
+
rev: 6.0.0
|
|
10
10
|
hooks:
|
|
11
11
|
- id: flake8
|
|
12
12
|
language_version: python3
|
|
13
13
|
|
|
14
14
|
- repo: https://github.com/pycqa/isort
|
|
15
|
-
rev: 5.
|
|
15
|
+
rev: 5.12.0
|
|
16
16
|
hooks:
|
|
17
17
|
- id: isort
|
|
18
18
|
# extra dependencies for config in pyproject.toml
|
|
19
19
|
additional_dependencies: [".[pyproject]"]
|
|
20
20
|
|
|
21
21
|
- repo: https://github.com/pre-commit/pre-commit-hooks
|
|
22
|
-
rev: v4.
|
|
22
|
+
rev: v4.4.0
|
|
23
23
|
hooks:
|
|
24
24
|
- id: trailing-whitespace
|
|
25
25
|
- id: end-of-file-fixer
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: structlog-sentry
|
|
3
|
-
Version: 2.0.
|
|
3
|
+
Version: 2.0.2
|
|
4
4
|
Summary: Sentry integration for structlog
|
|
5
5
|
Home-page: https://github.com/kiwicom/structlog-sentry
|
|
6
6
|
License: MIT
|
|
@@ -13,11 +13,18 @@ Classifier: Intended Audience :: Developers
|
|
|
13
13
|
Classifier: License :: OSI Approved :: MIT License
|
|
14
14
|
Classifier: Programming Language :: Python
|
|
15
15
|
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.7
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
21
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
22
|
Classifier: Programming Language :: Python :: 3.10
|
|
17
23
|
Classifier: Programming Language :: Python :: 3.7
|
|
18
24
|
Classifier: Programming Language :: Python :: 3.8
|
|
19
25
|
Classifier: Programming Language :: Python :: 3.9
|
|
20
26
|
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
27
|
+
Requires-Dist: packaging
|
|
21
28
|
Requires-Dist: sentry-sdk
|
|
22
29
|
Requires-Dist: structlog
|
|
23
30
|
Project-URL: Repository, https://github.com/kiwicom/structlog-sentry
|
|
@@ -71,19 +78,18 @@ Do not forget to add the `structlog.stdlib.add_log_level` and optionally the
|
|
|
71
78
|
`SentryProcessor` class takes the following arguments:
|
|
72
79
|
|
|
73
80
|
- `level` Events of this or higher levels will be reported as Sentry
|
|
74
|
-
breadcrumbs. Dfault is
|
|
81
|
+
breadcrumbs. Dfault is `logging.INFO`.
|
|
75
82
|
- `event_level` Events of this or higher levels will be reported to Sentry
|
|
76
|
-
as events. Default is
|
|
83
|
+
as events. Default is `logging.WARNING`.
|
|
77
84
|
- `active` A flag to make this processor enabled/disabled.
|
|
78
|
-
- `as_context` Send `event_dict` as extra info to Sentry.
|
|
79
|
-
Default is :obj:`True`.
|
|
85
|
+
- `as_context` Send `event_dict` as extra info to Sentry. Default is `True`.
|
|
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.
|
|
83
89
|
- `ignore_loggers` A list of logger names to ignore any events from.
|
|
84
90
|
- `verbose` Report the action taken by the logger in the `event_dict`.
|
|
85
|
-
Default is
|
|
86
|
-
- `hub` Optionally specify
|
|
91
|
+
Default is `False`.
|
|
92
|
+
- `hub` Optionally specify `sentry_sdk.Hub`.
|
|
87
93
|
|
|
88
94
|
Now events are automatically captured by Sentry with `log.error()`:
|
|
89
95
|
|
|
@@ -46,19 +46,18 @@ 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
|
|
49
|
+
breadcrumbs. Dfault is `logging.INFO`.
|
|
50
50
|
- `event_level` Events of this or higher levels will be reported to Sentry
|
|
51
|
-
as events. Default is
|
|
51
|
+
as events. Default is `logging.WARNING`.
|
|
52
52
|
- `active` A flag to make this processor enabled/disabled.
|
|
53
|
-
- `as_context` Send `event_dict` as extra info to Sentry.
|
|
54
|
-
Default is :obj:`True`.
|
|
53
|
+
- `as_context` Send `event_dict` as extra info to Sentry. Default is `True`.
|
|
55
54
|
- `tag_keys` A list of keys. If any if these keys appear in `event_dict`,
|
|
56
55
|
the key and its corresponding value in `event_dict` will be used as Sentry
|
|
57
56
|
event tags. use `"__all__"` to report all key/value pairs of event as tags.
|
|
58
57
|
- `ignore_loggers` A list of logger names to ignore any events from.
|
|
59
58
|
- `verbose` Report the action taken by the logger in the `event_dict`.
|
|
60
|
-
Default is
|
|
61
|
-
- `hub` Optionally specify
|
|
59
|
+
Default is `False`.
|
|
60
|
+
- `hub` Optionally specify `sentry_sdk.Hub`.
|
|
62
61
|
|
|
63
62
|
Now events are automatically captured by Sentry with `log.error()`:
|
|
64
63
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[tool.poetry]
|
|
2
2
|
name = "structlog-sentry"
|
|
3
|
-
version = "2.0.
|
|
3
|
+
version = "2.0.2"
|
|
4
4
|
description = "Sentry integration for structlog"
|
|
5
5
|
authors = ["Kiwi.com platform <platform@kiwi.com>"]
|
|
6
6
|
license = "MIT"
|
|
@@ -25,11 +25,12 @@ include = ["*.md", "*.toml", "*.txt", "*.yml", "*.yaml", ".coveragerc", "tox.ini
|
|
|
25
25
|
|
|
26
26
|
[tool.poetry.dependencies]
|
|
27
27
|
python = "^3.7"
|
|
28
|
+
packaging = "*"
|
|
28
29
|
sentry-sdk = "*"
|
|
29
30
|
structlog = "*"
|
|
30
31
|
|
|
31
32
|
[tool.poetry.dev-dependencies]
|
|
32
|
-
pytest = "^7.1.
|
|
33
|
+
pytest = "^7.1.3"
|
|
33
34
|
pytest-cov = "^3.0.0"
|
|
34
35
|
pytest-mock = "^3.8.2"
|
|
35
36
|
|
|
@@ -5,11 +5,15 @@ import sys
|
|
|
5
5
|
from fnmatch import fnmatch
|
|
6
6
|
from typing import Any, Iterable, Optional
|
|
7
7
|
|
|
8
|
-
from
|
|
8
|
+
from packaging import version
|
|
9
|
+
|
|
10
|
+
from sentry_sdk import VERSION, Hub
|
|
9
11
|
from sentry_sdk.integrations.logging import _IGNORED_LOGGERS
|
|
10
12
|
from sentry_sdk.utils import capture_internal_exceptions, event_from_exception
|
|
11
13
|
from structlog.types import EventDict, ExcInfo, WrappedLogger
|
|
12
14
|
|
|
15
|
+
_IS_OLD_VERSION = version.parse(VERSION) < version.parse("1.17.0")
|
|
16
|
+
|
|
13
17
|
|
|
14
18
|
def _figure_out_exc_info(v: Any) -> ExcInfo:
|
|
15
19
|
"""
|
|
@@ -107,7 +111,17 @@ class SentryProcessor:
|
|
|
107
111
|
has_exc_info = exc_info and exc_info != (None, None, None)
|
|
108
112
|
|
|
109
113
|
if has_exc_info:
|
|
110
|
-
|
|
114
|
+
options = self._get_hub().client.options
|
|
115
|
+
include_local_variables = options.get(
|
|
116
|
+
"include_local_variables", options.get("with_locals")
|
|
117
|
+
)
|
|
118
|
+
key = "with_locals" if _IS_OLD_VERSION else "include_local_variables"
|
|
119
|
+
event, hint = event_from_exception(
|
|
120
|
+
exc_info,
|
|
121
|
+
client_options={
|
|
122
|
+
key: include_local_variables,
|
|
123
|
+
},
|
|
124
|
+
)
|
|
111
125
|
else:
|
|
112
126
|
event, hint = {}, {}
|
|
113
127
|
|
|
@@ -163,6 +177,21 @@ class SentryProcessor:
|
|
|
163
177
|
event, hint = self._get_breadcrumb_and_hint(event_dict)
|
|
164
178
|
self._get_hub().add_breadcrumb(event, hint=hint)
|
|
165
179
|
|
|
180
|
+
@staticmethod
|
|
181
|
+
def _get_level_value(level_name: str) -> int:
|
|
182
|
+
"""Get numeric value for the log level name given."""
|
|
183
|
+
try:
|
|
184
|
+
# Try to get one of predefined log levels
|
|
185
|
+
return getattr(logging, level_name)
|
|
186
|
+
except AttributeError as e:
|
|
187
|
+
# May be it is a custom log level?
|
|
188
|
+
level = logging.getLevelName(level_name)
|
|
189
|
+
if isinstance(level, int):
|
|
190
|
+
return level
|
|
191
|
+
|
|
192
|
+
# Re-raise original error
|
|
193
|
+
raise ValueError(f"{level_name} is not a valid log level") from e
|
|
194
|
+
|
|
166
195
|
def __call__(
|
|
167
196
|
self, logger: WrappedLogger, name: str, event_dict: EventDict
|
|
168
197
|
) -> EventDict:
|
|
@@ -171,7 +200,7 @@ class SentryProcessor:
|
|
|
171
200
|
sentry_skip = event_dict.pop("sentry_skip", False)
|
|
172
201
|
|
|
173
202
|
if self.active and not sentry_skip and self._can_record(logger, event_dict):
|
|
174
|
-
level =
|
|
203
|
+
level = self._get_level_value(event_dict["level"].upper())
|
|
175
204
|
|
|
176
205
|
if level >= self.event_level:
|
|
177
206
|
self._handle_event(event_dict)
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
# -*- coding: utf-8 -*-
|
|
2
|
-
from setuptools import setup
|
|
3
|
-
|
|
4
|
-
packages = \
|
|
5
|
-
['structlog_sentry']
|
|
6
|
-
|
|
7
|
-
package_data = \
|
|
8
|
-
{'': ['*']}
|
|
9
|
-
|
|
10
|
-
install_requires = \
|
|
11
|
-
['sentry-sdk', 'structlog']
|
|
12
|
-
|
|
13
|
-
setup_kwargs = {
|
|
14
|
-
'name': 'structlog-sentry',
|
|
15
|
-
'version': '2.0.0b2',
|
|
16
|
-
'description': 'Sentry integration for structlog',
|
|
17
|
-
'long_description': '# structlog-sentry\n\n| What | Where |\n| ------------- | --------------------------------------------- |\n| Documentation | <https://github.com/kiwicom/structlog-sentry> |\n| Maintainer | @kiwicom/platform |\n\nBased on <https://gist.github.com/hynek/a1f3f92d57071ebc5b91>\n\n## Installation\n\nInstall the package with [pip](https://pip.pypa.io/):\n\n```\npip install structlog-sentry\n```\n\n## Usage\n\nThis module is intended to be used with `structlog` like this:\n\n```python\nimport sentry_sdk\nimport structlog\nfrom structlog_sentry import SentryProcessor\n\n\nsentry_sdk.init() # pass dsn in argument or via SENTRY_DSN env variable\n\nstructlog.configure(\n processors=[\n structlog.stdlib.add_logger_name, # optional, but before SentryProcessor()\n structlog.stdlib.add_log_level, # required before SentryProcessor()\n SentryProcessor(event_level=logging.ERROR),\n ],\n logger_factory=...,\n wrapper_class=...,\n)\n\n\nlog = structlog.get_logger()\n```\n\nDo not forget to add the `structlog.stdlib.add_log_level` and optionally the\n`structlog.stdlib.add_logger_name` processors before `SentryProcessor`. The\n`SentryProcessor` class takes the following arguments:\n\n- `level` Events of this or higher levels will be reported as Sentry\n breadcrumbs. Dfault is :obj:`logging.INFO`.\n- `event_level` Events of this or higher levels will be reported to Sentry\n as events. Default is :obj:`logging.WARNING`.\n- `active` A flag to make this processor enabled/disabled.\n- `as_context` Send `event_dict` as extra info to Sentry.\n Default is :obj:`True`.\n- `tag_keys` A list of keys. If any if these keys appear in `event_dict`,\n the key and its corresponding value in `event_dict` will be used as Sentry\n event tags. use `"__all__"` to report all key/value pairs of event as tags.\n- `ignore_loggers` A list of logger names to ignore any events from.\n- `verbose` Report the action taken by the logger in the `event_dict`.\n Default is :obj:`False`.\n- `hub` Optionally specify :obj:`sentry_sdk.Hub`.\n\nNow events are automatically captured by Sentry with `log.error()`:\n\n```python\ntry:\n 1/0\nexcept ZeroDivisionError:\n log.error("zero divsiion")\n\ntry:\n resp = requests.get(f"https://api.example.com/users/{user_id}/")\n resp.raise_for_status()\nexcept RequestException:\n log.error("request error", user_id=user_id)\n```\n\nThis won\'t automatically collect `sys.exc_info()` along with the message, if you want\nto enable this behavior, just pass `exc_info=True`.\n\nWhen you want to use structlog\'s built-in\n[`format_exc_info`](http://www.structlog.org/en/stable/api.html#structlog.processors.format_exc_info)\nprocessor, make that the `SentryProcessor` comes *before* `format_exc_info`!\nOtherwise, the `SentryProcessor` won\'t have an `exc_info` to work with, because\nit\'s removed from the event by `format_exc_info`.\n\nLogging calls with no `sys.exc_info()` are also automatically captured by Sentry\neither as breadcrumbs (if configured by the `level` argument) or as events:\n\n```python\nlog.info("info message", scope="accounts")\nlog.warning("warning message", scope="invoices")\nlog.error("error message", scope="products")\n```\n\nIf you do not want to forward a specific logs into Sentry, you can pass the\n`sentry_skip=True` optional argument to logger methods, like this:\n\n```python\nlog.error("error message", sentry_skip=True)\n```\n\n### Sentry Tags\n\nYou can set some or all of key/value pairs of structlog `event_dict` as sentry `tags`:\n\n```python\nstructlog.configure(\n processors=[\n structlog.stdlib.add_logger_name,\n structlog.stdlib.add_log_level,\n SentryProcessor(level=logging.ERROR, tag_keys=["city", "timezone"]),\n ],...\n)\n\nlog.error("error message", city="Tehran", timezone="UTC+3:30", movie_title="Some title")\n```\n\nthis will report the error and the sentry event will have **city** and **timezone** tags.\nIf you want to have all event data as tags, create the `SentryProcessor` with `tag_keys="__all__"`.\n\n```python\nstructlog.configure(\n processors=[\n structlog.stdlib.add_logger_name,\n structlog.stdlib.add_log_level,\n SentryProcessor(level=logging.ERROR, tag_keys="__all__"),\n ],...\n)\n```\n\n### Skip Context\n\nBy default `SentryProcessor` will send `event_dict` key/value pairs as contextual info to sentry.\nSometimes you may want to skip this, specially when sending the `event_dict` as sentry tags:\n\n```python\nstructlog.configure(\n processors=[\n structlog.stdlib.add_logger_name,\n structlog.stdlib.add_log_level,\n SentryProcessor(level=logging.ERROR, as_context=False, tag_keys="__all__"),\n ],...\n)\n```\n\n### Ignore specific loggers\n\nIf you want to ignore specific loggers from being processed by the `SentryProcessor` just pass\na list of loggers when instantiating the processor:\n\n```python\nstructlog.configure(\n processors=[\n structlog.stdlib.add_logger_name,\n structlog.stdlib.add_log_level,\n SentryProcessor(level=logging.ERROR, ignore_loggers=["some.logger"]),\n ],...\n)\n```\n\n### Logging as JSON\n\nIf you want to configure `structlog` to format the output as **JSON** (maybe for\n[elk-stack](https://www.elastic.co/elk-stack)) you have to disable standard logging\nintegration in Sentry SDK by passing the `LoggingIntegration(event_level=None, level=None)`\ninstance to `sentry_sdk.init` method. This prevents duplication of an event reported to sentry:\n\n```python\nfrom sentry_sdk.integrations.logging import LoggingIntegration\n\n\nINTEGRATIONS = [\n # ... other integrations\n LoggingIntegration(event_level=None, level=None),\n]\n\nsentry_sdk.init(integrations=INTEGRATIONS)\n```\n\nThis integration tells `sentry_sdk` to *ignore* standard logging and captures the events manually.\n\n## Testing\n\nTo run all tests:\n\n```\ntox\n```\n\n## Contributing\n\nCreate a merge request and tag @kiwicom/platform for review.\n',
|
|
18
|
-
'author': 'Kiwi.com platform',
|
|
19
|
-
'author_email': 'platform@kiwi.com',
|
|
20
|
-
'maintainer': None,
|
|
21
|
-
'maintainer_email': None,
|
|
22
|
-
'url': 'https://github.com/kiwicom/structlog-sentry',
|
|
23
|
-
'packages': packages,
|
|
24
|
-
'package_data': package_data,
|
|
25
|
-
'install_requires': install_requires,
|
|
26
|
-
'python_requires': '>=3.7,<4.0',
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
setup(**setup_kwargs)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|