1- from unittest import mock
1+ from unittest import mock
22
33import sentry_sdk
44from sentry_sdk import _experimental_logger as sentry_logger
@@ -8,12 +8,12 @@ def test_logs_disabled_by_default(sentry_init, capture_envelopes):
88 sentry_init ()
99 envelopes = capture_envelopes ()
1010
11- sentry_logger .trace ("This is a 'trace' log." )
12- sentry_logger .debug ("This is a 'debug' log..." )
13- sentry_logger .info ("This is a 'info' log..." )
14- sentry_logger .warn ("This is a 'warn' log..." )
15- sentry_logger .error ("This is a 'error' log..." )
16- sentry_logger .fatal ("This is a 'fatal' log..." )
11+ sentry_logger .trace ("This is a 'trace' log." )
12+ sentry_logger .debug ("This is a 'debug' log..." )
13+ sentry_logger .info ("This is a 'info' log..." )
14+ sentry_logger .warn ("This is a 'warn' log..." )
15+ sentry_logger .error ("This is a 'error' log..." )
16+ sentry_logger .fatal ("This is a 'fatal' log..." )
1717
1818 assert len (envelopes ) == 0
1919
@@ -29,39 +29,48 @@ def test_logs_basics(sentry_init, capture_envelopes):
2929 sentry_logger .error ("This is a 'error' log..." )
3030 sentry_logger .fatal ("This is a 'fatal' log..." )
3131
32- assert len (envelopes ) == 6 # We will batch those log items into a single envelope at some point
33-
32+ assert (
33+ len (envelopes ) == 6
34+ ) # We will batch those log items into a single envelope at some point
35+
3436 assert envelopes [0 ].items [0 ].payload .json ["severityText" ] == "trace"
3537 assert envelopes [0 ].items [0 ].payload .json ["severityNumber" ] == 1
36-
38+
3739 assert envelopes [1 ].items [0 ].payload .json ["severityText" ] == "debug"
3840 assert envelopes [1 ].items [0 ].payload .json ["severityNumber" ] == 5
39-
41+
4042 assert envelopes [2 ].items [0 ].payload .json ["severityText" ] == "info"
4143 assert envelopes [2 ].items [0 ].payload .json ["severityNumber" ] == 9
42-
44+
4345 assert envelopes [3 ].items [0 ].payload .json ["severityText" ] == "warn"
4446 assert envelopes [3 ].items [0 ].payload .json ["severityNumber" ] == 13
45-
47+
4648 assert envelopes [4 ].items [0 ].payload .json ["severityText" ] == "error"
4749 assert envelopes [4 ].items [0 ].payload .json ["severityNumber" ] == 17
48-
50+
4951 assert envelopes [5 ].items [0 ].payload .json ["severityText" ] == "fatal"
5052 assert envelopes [5 ].items [0 ].payload .json ["severityNumber" ] == 21
51-
53+
5254
5355def test_logs_before_emit_log (sentry_init , capture_envelopes ):
5456 def _before_log (record , hint ):
55- assert list (record .keys ()) == ['severity_text' , 'severity_number' , 'body' , 'attributes' , 'time_unix_nano' , 'trace_id' ]
56-
57- if record ['severity_text' ] in ['fatal' , 'error' ]:
57+ assert list (record .keys ()) == [
58+ "severity_text" ,
59+ "severity_number" ,
60+ "body" ,
61+ "attributes" ,
62+ "time_unix_nano" ,
63+ "trace_id" ,
64+ ]
65+
66+ if record ["severity_text" ] in ["fatal" , "error" ]:
5867 return None
59-
68+
6069 return record
61-
70+
6271 sentry_init (
6372 _experiments = {
64- "enable_sentry_logs" : True ,
73+ "enable_sentry_logs" : True ,
6574 "before_emit_log" : _before_log ,
6675 }
6776 )
@@ -84,7 +93,7 @@ def _before_log(record, hint):
8493
8594def test_logs_attributes (sentry_init , capture_envelopes ):
8695 """
87- Passing arbitrary attributes to log messages.
96+ Passing arbitrary attributes to log messages.
8897 """
8998 sentry_init (_experiments = {"enable_sentry_logs" : True })
9099 envelopes = capture_envelopes ()
@@ -96,43 +105,92 @@ def test_logs_attributes(sentry_init, capture_envelopes):
96105 "attr_string" : "string attribute" ,
97106 }
98107
99- sentry_logger .warn ("The recorded value was '{my_var}'" , my_var = "some value" , attributes = attrs )
108+ sentry_logger .warn (
109+ "The recorded value was '{my_var}'" , my_var = "some value" , attributes = attrs
110+ )
100111
101112 log_item = envelopes [0 ].items [0 ].payload .json
102113 assert log_item ["body" ]["stringValue" ] == "The recorded value was 'some value'"
103114
104- assert log_item ["attributes" ][1 ] == {"key" : "attr_int" , "value" : {"intValue" : "1" }} # TODO: this is strange.
105- assert log_item ["attributes" ][2 ] == {"key" : "attr_float" , "value" : {"doubleValue" : 2.0 }}
106- assert log_item ["attributes" ][3 ] == {"key" : "attr_bool" , "value" : {"boolValue" : True }}
107- assert log_item ["attributes" ][4 ] == {"key" : "attr_string" , "value" : {"stringValue" : "string attribute" }}
108- assert log_item ["attributes" ][5 ] == {"key" : "sentry.environment" , "value" : {"stringValue" : "production" }}
109- assert log_item ["attributes" ][6 ] == {"key" : "sentry.release" , "value" : {"stringValue" : mock .ANY }}
110- assert log_item ["attributes" ][7 ] == {"key" : "sentry.message.parameters.my_var" , "value" : {"stringValue" : "some value" }}
115+ assert log_item ["attributes" ][1 ] == {
116+ "key" : "attr_int" ,
117+ "value" : {"intValue" : "1" },
118+ } # TODO: this is strange.
119+ assert log_item ["attributes" ][2 ] == {
120+ "key" : "attr_float" ,
121+ "value" : {"doubleValue" : 2.0 },
122+ }
123+ assert log_item ["attributes" ][3 ] == {
124+ "key" : "attr_bool" ,
125+ "value" : {"boolValue" : True },
126+ }
127+ assert log_item ["attributes" ][4 ] == {
128+ "key" : "attr_string" ,
129+ "value" : {"stringValue" : "string attribute" },
130+ }
131+ assert log_item ["attributes" ][5 ] == {
132+ "key" : "sentry.environment" ,
133+ "value" : {"stringValue" : "production" },
134+ }
135+ assert log_item ["attributes" ][6 ] == {
136+ "key" : "sentry.release" ,
137+ "value" : {"stringValue" : mock .ANY },
138+ }
139+ assert log_item ["attributes" ][7 ] == {
140+ "key" : "sentry.message.parameters.my_var" ,
141+ "value" : {"stringValue" : "some value" },
142+ }
111143
112144
113145def test_logs_message_params (sentry_init , capture_envelopes ):
114146 """
115- This is the official way of how to pass vars to log messages.
147+ This is the official way of how to pass vars to log messages.
116148 """
117149 sentry_init (_experiments = {"enable_sentry_logs" : True })
118150 envelopes = capture_envelopes ()
119151
120- sentry_logger .warn ("The recorded value was '{int_var}'" , int_var = 1 )
121- sentry_logger .warn ("The recorded value was '{float_var}'" , float_var = 2.0 )
122- sentry_logger .warn ("The recorded value was '{bool_var}'" , bool_var = False )
123- sentry_logger .warn ("The recorded value was '{string_var}'" , string_var = "some string value" )
124-
125- assert envelopes [0 ].items [0 ].payload .json ["body" ]["stringValue" ] == "The recorded value was '1'"
126- assert envelopes [0 ].items [0 ].payload .json ["attributes" ][- 1 ] == {"key" : "sentry.message.parameters.int_var" , "value" : {'intValue' : "1" }} # TODO: this is strange.
152+ sentry_logger .warn ("The recorded value was '{int_var}'" , int_var = 1 )
153+ sentry_logger .warn ("The recorded value was '{float_var}'" , float_var = 2.0 )
154+ sentry_logger .warn ("The recorded value was '{bool_var}'" , bool_var = False )
155+ sentry_logger .warn (
156+ "The recorded value was '{string_var}'" , string_var = "some string value"
157+ )
127158
128- assert envelopes [1 ].items [0 ].payload .json ["body" ]["stringValue" ] == "The recorded value was '2.0'"
129- assert envelopes [1 ].items [0 ].payload .json ["attributes" ][- 1 ] == {"key" : "sentry.message.parameters.float_var" , "value" : {'doubleValue' : 2.0 }}
159+ assert (
160+ envelopes [0 ].items [0 ].payload .json ["body" ]["stringValue" ]
161+ == "The recorded value was '1'"
162+ )
163+ assert envelopes [0 ].items [0 ].payload .json ["attributes" ][- 1 ] == {
164+ "key" : "sentry.message.parameters.int_var" ,
165+ "value" : {"intValue" : "1" },
166+ } # TODO: this is strange.
167+
168+ assert (
169+ envelopes [1 ].items [0 ].payload .json ["body" ]["stringValue" ]
170+ == "The recorded value was '2.0'"
171+ )
172+ assert envelopes [1 ].items [0 ].payload .json ["attributes" ][- 1 ] == {
173+ "key" : "sentry.message.parameters.float_var" ,
174+ "value" : {"doubleValue" : 2.0 },
175+ }
130176
131- assert envelopes [2 ].items [0 ].payload .json ["body" ]["stringValue" ] == "The recorded value was 'False'"
132- assert envelopes [2 ].items [0 ].payload .json ["attributes" ][- 1 ] == {"key" : "sentry.message.parameters.bool_var" , "value" : {'boolValue' : False }}
177+ assert (
178+ envelopes [2 ].items [0 ].payload .json ["body" ]["stringValue" ]
179+ == "The recorded value was 'False'"
180+ )
181+ assert envelopes [2 ].items [0 ].payload .json ["attributes" ][- 1 ] == {
182+ "key" : "sentry.message.parameters.bool_var" ,
183+ "value" : {"boolValue" : False },
184+ }
133185
134- assert envelopes [3 ].items [0 ].payload .json ["body" ]["stringValue" ] == "The recorded value was 'some string value'"
135- assert envelopes [3 ].items [0 ].payload .json ["attributes" ][- 1 ] == {"key" : "sentry.message.parameters.string_var" , "value" : {'stringValue' : "some string value" }}
186+ assert (
187+ envelopes [3 ].items [0 ].payload .json ["body" ]["stringValue" ]
188+ == "The recorded value was 'some string value'"
189+ )
190+ assert envelopes [3 ].items [0 ].payload .json ["attributes" ][- 1 ] == {
191+ "key" : "sentry.message.parameters.string_var" ,
192+ "value" : {"stringValue" : "some string value" },
193+ }
136194
137195
138196def test_logs_tied_to_transactions (sentry_init , capture_envelopes ):
@@ -146,7 +204,10 @@ def test_logs_tied_to_transactions(sentry_init, capture_envelopes):
146204 sentry_logger .warn ("This is a log tied to a transaction" )
147205
148206 log_entry = envelopes [0 ].items [0 ].payload .json
149- assert log_entry ["attributes" ][- 1 ] == {'key' : 'sentry.trace.parent_span_id' , 'value' : {'stringValue' : trx .span_id }}
207+ assert log_entry ["attributes" ][- 1 ] == {
208+ "key" : "sentry.trace.parent_span_id" ,
209+ "value" : {"stringValue" : trx .span_id },
210+ }
150211
151212
152213def test_logs_tied_to_spans (sentry_init , capture_envelopes ):
@@ -161,4 +222,7 @@ def test_logs_tied_to_spans(sentry_init, capture_envelopes):
161222 sentry_logger .warn ("This is a log tied to a span" )
162223
163224 log_entry = envelopes [0 ].items [0 ].payload .json
164- assert log_entry ["attributes" ][- 1 ] == {'key' : 'sentry.trace.parent_span_id' , 'value' : {'stringValue' : span .span_id }}
225+ assert log_entry ["attributes" ][- 1 ] == {
226+ "key" : "sentry.trace.parent_span_id" ,
227+ "value" : {"stringValue" : span .span_id },
228+ }
0 commit comments