2121from opentelemetry .attributes import BoundedAttributes
2222from opentelemetry .context import get_current
2323from opentelemetry .sdk ._logs import (
24- LogDeprecatedInitWarning ,
24+ LogRecordContextDeprecatedWarning ,
25+ LogRecordInitDeprecatedWarning ,
2526 LogDroppedAttributesWarning ,
2627 LogLimits ,
2728 LogRecord ,
@@ -142,11 +143,19 @@ def test_log_record_dropped_attributes_set_limits_warning_once(self):
142143 attributes = attr ,
143144 limits = limits ,
144145 )
145- self .assertEqual (len (cw ), 1 )
146- self .assertIsInstance (cw [- 1 ].message , LogDroppedAttributesWarning )
146+
147+ # Check that at least one LogDroppedAttributesWarning was emitted
148+ dropped_attributes_warnings = [
149+ w for w in cw if isinstance (w .message , LogDroppedAttributesWarning )
150+ ]
151+ self .assertEqual (len (dropped_attributes_warnings ), 1 ,
152+ "Expected exactly one LogDroppedAttributesWarning due to simplefilter('once')" )
153+
154+ # Check the message content of the LogDroppedAttributesWarning
155+ warning_message = str (dropped_attributes_warnings [0 ].message )
147156 self .assertIn (
148157 "Log record attributes were dropped due to limits" ,
149- str ( cw [ - 1 ]. message ) ,
158+ warning_message ,
150159 )
151160
152161 def test_log_record_dropped_attributes_unset_limits (self ):
@@ -159,7 +168,7 @@ def test_log_record_dropped_attributes_unset_limits(self):
159168 self .assertTrue (result .dropped_attributes == 0 )
160169 self .assertEqual (attr , result .attributes )
161170
162- def test_log_record_deprecated_init_warning (self ):
171+ def test_log_record_context_deprecated_init_warning (self ):
163172 test_cases = [
164173 {"trace_id" : 123 },
165174 {"span_id" : 123 },
@@ -172,17 +181,50 @@ def test_log_record_deprecated_init_warning(self):
172181 for _ in range (10 ):
173182 LogRecord (** params )
174183
175- self .assertEqual (len (cw ), 1 )
176- self .assertIsInstance (cw [- 1 ].message , LogDeprecatedInitWarning )
184+ # Check that at least one LogRecordContextDeprecatedWarning was emitted
185+ context_deprecated_warnings = [
186+ w for w in cw if isinstance (w .message , LogRecordContextDeprecatedWarning )
187+ ]
188+ self .assertEqual (len (context_deprecated_warnings ), 1 ,
189+ "Expected exactly one LogRecordContextDeprecatedWarning due to simplefilter('once')" )
190+
191+ # Check the message content of the LogRecordContextDeprecatedWarning
192+ warning_message = str (context_deprecated_warnings [0 ].message )
177193 self .assertIn (
178194 "LogRecord init with `trace_id`, `span_id`, and/or `trace_flags` is deprecated since 1.35.0. Use `context` instead." ,
179- str ( cw [ - 1 ]. message ) ,
195+ warning_message ,
180196 )
181197
182198 with warnings .catch_warnings (record = True ) as cw :
183199 for _ in range (10 ):
184200 LogRecord (context = get_current ())
185- self .assertEqual (len (cw ), 0 )
201+
202+ # Check that no LogRecordContextDeprecatedWarning was emitted when using context
203+ context_deprecated_warnings = [
204+ w for w in cw if isinstance (w .message , LogRecordContextDeprecatedWarning )
205+ ]
206+ self .assertEqual (len (context_deprecated_warnings ), 0 ,
207+ "Expected no LogRecordContextDeprecatedWarning when using context parameter" )
208+
209+ def test_log_record_init_deprecated_warning (self ):
210+ """Test that LogRecord initialization emits a LogRecordInitDeprecatedWarning."""
211+ with warnings .catch_warnings (record = True ) as cw :
212+ warnings .simplefilter ("always" )
213+ LogRecord ()
214+
215+ # Check that at least one LogRecordInitDeprecatedWarning was emitted
216+ log_record_init_warnings = [
217+ w for w in cw if isinstance (w .message , LogRecordInitDeprecatedWarning )
218+ ]
219+ self .assertGreater (len (log_record_init_warnings ), 0 ,
220+ "Expected at least one LogRecordInitDeprecatedWarning" )
221+
222+ # Check the message content of the LogRecordInitDeprecatedWarning
223+ warning_message = str (log_record_init_warnings [0 ].message )
224+ self .assertIn (
225+ "LogRecord will be substituted in 1.39.0 by ReadWriteLogRecord and ReadableLogRecord" ,
226+ warning_message ,
227+ )
186228
187229 # pylint:disable=protected-access
188230 def test_log_record_from_api_log_record (self ):
0 commit comments