Skip to content

Commit 4ac20db

Browse files
committed
use format_attr in other places too
1 parent a56ff9b commit 4ac20db

File tree

2 files changed

+9
-11
lines changed

2 files changed

+9
-11
lines changed

sentry_sdk/logger.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from typing import Any, TYPE_CHECKING
55

66
import sentry_sdk
7-
from sentry_sdk.utils import safe_repr, capture_internal_exceptions
7+
from sentry_sdk.utils import format_attribute, safe_repr, capture_internal_exceptions
88

99
if TYPE_CHECKING:
1010
from sentry_sdk._types import Attributes, Log
@@ -34,29 +34,27 @@ def _capture_log(
3434
) -> None:
3535
body = template
3636

37-
attrs: "Attributes" = {}
37+
attributes: "Attributes" = {}
3838

3939
if "attributes" in kwargs:
40-
attrs.update(kwargs.pop("attributes"))
40+
for attribute, value in kwargs["attributes"] or {}:
41+
attributes[attribute] = format_attribute(value)
4142

4243
for k, v in kwargs.items():
43-
attrs[f"sentry.message.parameter.{k}"] = v
44+
attributes[f"sentry.message.parameter.{k}"] = format_attribute(v)
4445

4546
if kwargs:
4647
# only attach template if there are parameters
47-
attrs["sentry.message.template"] = template
48+
attributes["sentry.message.template"] = format_attribute(template)
4849

4950
with capture_internal_exceptions():
5051
body = template.format_map(_dict_default_key(kwargs))
5152

52-
for k, v in attrs.items():
53-
attrs[k] = v if isinstance(v, (str, int, bool, float)) else safe_repr(v)
54-
5553
sentry_sdk.get_current_scope()._capture_log(
5654
{
5755
"severity_text": severity_text,
5856
"severity_number": severity_number,
59-
"attributes": attrs,
57+
"attributes": attributes,
6058
"body": body,
6159
"time_unix_nano": time.time_ns(),
6260
"trace_id": None,

sentry_sdk/metrics.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from typing import Any, Optional, TYPE_CHECKING, Union
88

99
import sentry_sdk
10-
from sentry_sdk.utils import safe_repr
10+
from sentry_sdk.utils import format_attribute, safe_repr
1111

1212
if TYPE_CHECKING:
1313
from sentry_sdk._types import Attributes, Metric, MetricType
@@ -24,7 +24,7 @@ def _capture_metric(
2424

2525
if attributes:
2626
for k, v in attributes.items():
27-
attrs[k] = v if isinstance(v, (str, int, bool, float)) else safe_repr(v)
27+
attrs[k] = format_attribute(v)
2828

2929
metric: "Metric" = {
3030
"timestamp": time.time(),

0 commit comments

Comments
 (0)