Skip to content

Commit 8c201ff

Browse files
committed
improve tests
1 parent d1bc396 commit 8c201ff

File tree

1 file changed

+40
-52
lines changed

1 file changed

+40
-52
lines changed

tests/test_logs.py

Lines changed: 40 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import logging
22
import sys
3+
from typing import List, Any
34
from unittest import mock
45
import pytest
56

@@ -12,9 +13,16 @@
1213
)
1314

1415

16+
def otel_attributes_to_dict(otel_attrs: List[Any]):
17+
return {item["key"]: item["value"] for item in otel_attrs}
18+
19+
1520
@minimum_python_37
1621
def test_logs_disabled_by_default(sentry_init, capture_envelopes):
1722
sentry_init()
23+
24+
python_logger = logging.Logger("some-logger")
25+
1826
envelopes = capture_envelopes()
1927

2028
sentry_logger.trace("This is a 'trace' log.")
@@ -23,6 +31,7 @@ def test_logs_disabled_by_default(sentry_init, capture_envelopes):
2331
sentry_logger.warn("This is a 'warn' log...")
2432
sentry_logger.error("This is a 'error' log...")
2533
sentry_logger.fatal("This is a 'fatal' log...")
34+
python_logger.warning("sad")
2635

2736
assert len(envelopes) == 0
2837

@@ -124,34 +133,14 @@ def test_logs_attributes(sentry_init, capture_envelopes):
124133
log_item = envelopes[0].items[0].payload.json
125134
assert log_item["body"]["stringValue"] == "The recorded value was 'some value'"
126135

127-
assert log_item["attributes"][1] == {
128-
"key": "attr_int",
129-
"value": {"intValue": "1"},
130-
} # TODO: this is strange.
131-
assert log_item["attributes"][2] == {
132-
"key": "attr_float",
133-
"value": {"doubleValue": 2.0},
134-
}
135-
assert log_item["attributes"][3] == {
136-
"key": "attr_bool",
137-
"value": {"boolValue": True},
138-
}
139-
assert log_item["attributes"][4] == {
140-
"key": "attr_string",
141-
"value": {"stringValue": "string attribute"},
142-
}
143-
assert log_item["attributes"][5] == {
144-
"key": "sentry.environment",
145-
"value": {"stringValue": "production"},
146-
}
147-
assert log_item["attributes"][6] == {
148-
"key": "sentry.release",
149-
"value": {"stringValue": mock.ANY},
150-
}
151-
assert log_item["attributes"][7] == {
152-
"key": "sentry.message.parameters.my_var",
153-
"value": {"stringValue": "some value"},
154-
}
136+
attrs = otel_attributes_to_dict(log_item["attributes"])
137+
assert attrs["attr_int"] == {"intValue": "1"}
138+
assert attrs["attr_float"] == {"doubleValue": 2.0}
139+
assert attrs["attr_bool"] == {"boolValue": True}
140+
assert attrs["attr_string"] == {"stringValue": "string attribute"}
141+
assert attrs["sentry.environment"] == {"stringValue": "production"}
142+
assert attrs["sentry.release"] == {"stringValue": mock.ANY}
143+
assert attrs["sentry.message.parameters.my_var"] == {"stringValue": "some value"}
155144

156145

157146
@minimum_python_37
@@ -173,37 +162,33 @@ def test_logs_message_params(sentry_init, capture_envelopes):
173162
envelopes[0].items[0].payload.json["body"]["stringValue"]
174163
== "The recorded value was '1'"
175164
)
176-
assert envelopes[0].items[0].payload.json["attributes"][-1] == {
177-
"key": "sentry.message.parameters.int_var",
178-
"value": {"intValue": "1"},
179-
} # TODO: this is strange.
165+
assert otel_attributes_to_dict(envelopes[0].items[0].payload.json["attributes"])[
166+
"sentry.message.parameters.int_var"
167+
] == {"intValue": "1"}
180168

181169
assert (
182170
envelopes[1].items[0].payload.json["body"]["stringValue"]
183171
== "The recorded value was '2.0'"
184172
)
185-
assert envelopes[1].items[0].payload.json["attributes"][-1] == {
186-
"key": "sentry.message.parameters.float_var",
187-
"value": {"doubleValue": 2.0},
188-
}
173+
assert otel_attributes_to_dict(envelopes[1].items[0].payload.json["attributes"])[
174+
"sentry.message.parameters.float_var"
175+
] == {"doubleValue": 2.0}
189176

190177
assert (
191178
envelopes[2].items[0].payload.json["body"]["stringValue"]
192179
== "The recorded value was 'False'"
193180
)
194-
assert envelopes[2].items[0].payload.json["attributes"][-1] == {
195-
"key": "sentry.message.parameters.bool_var",
196-
"value": {"boolValue": False},
197-
}
181+
assert otel_attributes_to_dict(envelopes[2].items[0].payload.json["attributes"])[
182+
"sentry.message.parameters.bool_var"
183+
] == {"boolValue": False}
198184

199185
assert (
200186
envelopes[3].items[0].payload.json["body"]["stringValue"]
201187
== "The recorded value was 'some string value'"
202188
)
203-
assert envelopes[3].items[0].payload.json["attributes"][-1] == {
204-
"key": "sentry.message.parameters.string_var",
205-
"value": {"stringValue": "some string value"},
206-
}
189+
assert otel_attributes_to_dict(envelopes[2].items[0].payload.json["attributes"])[
190+
"sentry.message.parameters.string_var"
191+
] == {"stringValue": "some string value"}
207192

208193

209194
@minimum_python_37
@@ -236,11 +221,8 @@ def test_logs_tied_to_spans(sentry_init, capture_envelopes):
236221
with sentry_sdk.start_span(description="test-span") as span:
237222
sentry_logger.warn("This is a log tied to a span")
238223

239-
log_entry = envelopes[0].items[0].payload.json
240-
assert log_entry["attributes"][-1] == {
241-
"key": "sentry.trace.parent_span_id",
242-
"value": {"stringValue": span.span_id},
243-
}
224+
attrs = otel_attributes_to_dict(envelopes[0].items[0].payload.json["attributes"])
225+
assert attrs["sentry.trace.parent_span_id"] == {"stringValue": span.span_id}
244226

245227

246228
@minimum_python_37
@@ -255,10 +237,16 @@ def test_logger_integration_warning(sentry_init, capture_envelopes):
255237
python_logger.warning("this is %s a template %s", "1", "2")
256238

257239
log_entry = envelopes[0].items[0].payload.json
258-
assert log_entry["attributes"][0] == {
259-
"key": "sentry.message.template",
260-
"value": {"stringValue": "this is %s a template %s"},
240+
attrs = otel_attributes_to_dict(log_entry["attributes"])
241+
assert attrs["sentry.message.template"] == {
242+
"stringValue": "this is %s a template %s"
261243
}
244+
assert "code.file.path" in attrs
245+
assert "code.line.number" in attrs
246+
assert attrs["logger.name"] == {"stringValue": "test-logger"}
247+
assert attrs["sentry.environment"] == {"stringValue": "production"}
248+
assert attrs["sentry.message.parameters.0"] == {"stringValue": "1"}
249+
assert attrs["sentry.message.parameters.1"]
262250
assert log_entry["severityNumber"] == 13
263251
assert log_entry["severityText"] == "warn"
264252

0 commit comments

Comments
 (0)