Skip to content

Commit 38f95a2

Browse files
committed
fix tests
1 parent 538c9de commit 38f95a2

File tree

2 files changed

+33
-32
lines changed

2 files changed

+33
-32
lines changed

sentry_sdk/_log_batcher.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
import json
21
import os
32
import random
43
import threading
54
from datetime import datetime, timezone
65
from typing import Optional, List, Callable, TYPE_CHECKING, Any
76

87
from sentry_sdk.utils import format_timestamp, safe_repr
9-
from sentry_sdk.envelope import Envelope, Item
8+
from sentry_sdk.envelope import Envelope, Item, PayloadRef
109

1110
if TYPE_CHECKING:
1211
from sentry_sdk._types import Log
@@ -112,7 +111,11 @@ def format_attribute(val):
112111
return {"value": val, "type": "string"}
113112
return {"value": safe_repr(val), "type": "string"}
114113

115-
print(log["attributes"])
114+
if "sentry.severity_number" not in log["attributes"]:
115+
log["attributes"]["sentry.severity_number"] = log["severity_number"]
116+
if "sentry.severity_text" not in log["attributes"]:
117+
log["attributes"]["sentry.severity_text"] = log["severity_text"]
118+
116119
res = {
117120
"timestamp": int(log["time_unix_nano"]) / 1.0e9,
118121
"trace_id": log.get("trace_id", "00000000-0000-0000-0000-000000000000"),
@@ -123,10 +126,6 @@ def format_attribute(val):
123126
},
124127
}
125128

126-
res["attributes"]["sentry.severity_number"] = format_attribute(
127-
log["severity_number"]
128-
)
129-
130129
return res
131130

132131
def _flush(self):
@@ -146,8 +145,8 @@ def _flush(self):
146145
headers={
147146
"item_count": len(self._log_buffer),
148147
},
149-
payload=json.dumps(
150-
{
148+
payload=PayloadRef(
149+
json={
151150
"items": [
152151
self._log_to_transport_format(log)
153152
for log in self._log_buffer

tests/test_logs.py

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,42 +19,44 @@
1919

2020

2121
def otel_attributes_to_dict(otel_attrs):
22-
# type: (List[Mapping[str, Any]]) -> Mapping[str, Any]
22+
# type: (Mapping[str, Any]) -> Mapping[str, Any]
2323
def _convert_attr(attr):
2424
# type: (Mapping[str, Union[str, float, bool]]) -> Any
25-
if "boolValue" in attr:
26-
return bool(attr["boolValue"])
27-
if "doubleValue" in attr:
28-
return float(attr["doubleValue"])
29-
if "intValue" in attr:
30-
return int(attr["intValue"])
31-
if attr["stringValue"].startswith("{"):
25+
if attr["type"] == "boolean":
26+
return bool(attr["value"])
27+
if attr["type"] == "double":
28+
return float(attr["value"])
29+
if attr["type"] == "integer":
30+
return int(attr["value"])
31+
if attr["value"].startswith("{"):
3232
try:
3333
return json.loads(attr["stringValue"])
3434
except ValueError:
3535
pass
36-
return str(attr["stringValue"])
36+
return str(attr["value"])
3737

38-
return {item["key"]: _convert_attr(item["value"]) for item in otel_attrs}
38+
return {k: _convert_attr(v) for (k, v) in otel_attrs.items()}
3939

4040

4141
def envelopes_to_logs(envelopes: List[Envelope]) -> List[Log]:
4242
res = [] # type: List[Log]
4343
for envelope in envelopes:
4444
for item in envelope.items:
45-
if item.type == "otel_log":
46-
log_json = item.payload.json
47-
log = {
48-
"severity_text": log_json["severityText"],
49-
"severity_number": log_json["severityNumber"],
50-
"body": log_json["body"]["stringValue"],
51-
"attributes": otel_attributes_to_dict(log_json["attributes"]),
52-
"time_unix_nano": int(log_json["timeUnixNano"]),
53-
"trace_id": None,
54-
} # type: Log
55-
if "traceId" in log_json:
56-
log["trace_id"] = log_json["traceId"]
57-
res.append(log)
45+
if item.type == "log":
46+
for log_json in item.payload.json["items"]:
47+
log = {
48+
"severity_text": log_json["attributes"]["sentry.severity_text"][
49+
"value"
50+
],
51+
"severity_number": int(
52+
log_json["attributes"]["sentry.severity_number"]["value"]
53+
),
54+
"body": log_json["body"],
55+
"attributes": otel_attributes_to_dict(log_json["attributes"]),
56+
"time_unix_nano": int(float(log_json["timestamp"]) * 1e9),
57+
"trace_id": log_json["trace_id"],
58+
} # type: Log
59+
res.append(log)
5860
return res
5961

6062

0 commit comments

Comments
 (0)