Skip to content

Commit

Permalink
feat: Add ignore_breadcrumb_data param, send data with breadcrumbs
Browse files Browse the repository at this point in the history
  • Loading branch information
Robin D'Arcy authored and paveldedik committed Mar 13, 2024
1 parent 3cae523 commit a31d2e4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ Do not forget to add the `structlog.stdlib.add_log_level` and optionally the
as events. Default is `logging.WARNING`.
- `active` A flag to make this processor enabled/disabled.
- `as_context` Send `event_dict` as extra info to Sentry. Default is `True`.
- `ignore_breadcrumb_data` A list of data keys that will be excluded from
[breadcrumb data](https://docs.sentry.io/platforms/python/enriching-events/breadcrumbs/#manual-breadcrumbs).
Defaults to keys which are already sent separately, i.e. `level`, `logger`,
`event` and `timestamp`. All other data in `event_dict` will be sent as
breadcrumb data.
- `tag_keys` A list of keys. If any if these keys appear in `event_dict`,
the key and its corresponding value in `event_dict` will be used as Sentry
event tags. use `"__all__"` to report all key/value pairs of event as tags.
Expand Down
14 changes: 13 additions & 1 deletion structlog_sentry/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ def __init__(
event_level: int = logging.WARNING,
active: bool = True,
as_context: bool = True,
ignore_breadcrumb_data: Iterable[str] = (
"level",
"logger",
"event",
"timestamp",
),
tag_keys: list[str] | str | None = None,
ignore_loggers: Iterable[str] | None = None,
verbose: bool = False,
Expand All @@ -51,6 +57,8 @@ def __init__(
:param active: A flag to make this processor enabled/disabled.
:param as_context: Send `event_dict` as extra info to Sentry.
Default is :obj:`True`.
:param ignore_breadcrumb_data: A list of data keys that will be excluded from
breadcrumb data. Defaults to keys which are already sent separately.
:param tag_keys: A list of keys. If any if these keys appear in `event_dict`,
the key and its corresponding value in `event_dict` will be used as Sentry
event tags. use `"__all__"` to report all key/value pairs of event as tags.
Expand All @@ -68,6 +76,7 @@ def __init__(
self._hub = hub
self._as_context = as_context
self._original_event_dict: dict = {}
self.ignore_breadcrumb_data = ignore_breadcrumb_data

self._ignored_loggers: set[str] = set()
if ignore_loggers is not None:
Expand Down Expand Up @@ -131,13 +140,16 @@ def _get_event_and_hint(self, event_dict: EventDict) -> tuple[dict, dict]:
return event, hint

def _get_breadcrumb_and_hint(self, event_dict: EventDict) -> tuple[dict, dict]:
data = {
k: v for k, v in event_dict.items() if k not in self.ignore_breadcrumb_data
}
event = {
"type": "log",
"level": event_dict.get("level"), # type: ignore
"category": event_dict.get("logger"),
"message": event_dict["event"],
"timestamp": event_dict.get("timestamp"),
"data": {},
"data": data,
}

return event, {"log_record": event_dict}
Expand Down

0 comments on commit a31d2e4

Please sign in to comment.