2121from opentelemetry .attributes import BoundedAttributes
2222from opentelemetry .context import get_current
2323from opentelemetry .sdk ._logs import (
24+ LogData ,
2425 LogDeprecatedInitWarning ,
2526 LogDroppedAttributesWarning ,
2627 LogLimits ,
2728 LogRecord ,
2829)
2930from opentelemetry .sdk .resources import Resource
31+ from opentelemetry .sdk .util .instrumentation import InstrumentationScope
3032from opentelemetry .trace .span import TraceFlags
3133
3234
@@ -142,11 +144,22 @@ def test_log_record_dropped_attributes_set_limits_warning_once(self):
142144 attributes = attr ,
143145 limits = limits ,
144146 )
145- self .assertEqual (len (cw ), 1 )
146- self .assertIsInstance (cw [- 1 ].message , LogDroppedAttributesWarning )
147+
148+ # Check that at least one LogDroppedAttributesWarning was emitted
149+ dropped_attributes_warnings = [
150+ w for w in cw if isinstance (w .message , LogDroppedAttributesWarning )
151+ ]
152+ self .assertEqual (
153+ len (dropped_attributes_warnings ),
154+ 1 ,
155+ "Expected exactly one LogDroppedAttributesWarning due to simplefilter('once')" ,
156+ )
157+
158+ # Check the message content of the LogDroppedAttributesWarning
159+ warning_message = str (dropped_attributes_warnings [0 ].message )
147160 self .assertIn (
148161 "Log record attributes were dropped due to limits" ,
149- str ( cw [ - 1 ]. message ) ,
162+ warning_message ,
150163 )
151164
152165 def test_log_record_dropped_attributes_unset_limits (self ):
@@ -159,7 +172,7 @@ def test_log_record_dropped_attributes_unset_limits(self):
159172 self .assertTrue (result .dropped_attributes == 0 )
160173 self .assertEqual (attr , result .attributes )
161174
162- def test_log_record_deprecated_init_warning (self ):
175+ def test_log_record_context_deprecated_init_warning (self ):
163176 test_cases = [
164177 {"trace_id" : 123 },
165178 {"span_id" : 123 },
@@ -172,17 +185,66 @@ def test_log_record_deprecated_init_warning(self):
172185 for _ in range (10 ):
173186 LogRecord (** params )
174187
175- self .assertEqual (len (cw ), 1 )
176- self .assertIsInstance (cw [- 1 ].message , LogDeprecatedInitWarning )
177- self .assertIn (
178- "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 ),
180- )
188+ # Check that the LogDeprecatedInitWarning was emitted
189+ context_deprecated_warnings = [
190+ w
191+ for w in cw
192+ if isinstance (w .message , LogDeprecatedInitWarning )
193+ ]
194+ self .assertEqual (len (context_deprecated_warnings ), 2 )
195+
196+ # Check we have the expected message once
197+ log_record_context_warning = [
198+ w .message
199+ for w in cw
200+ if "LogRecord init with `trace_id`, `span_id`, and/or `trace_flags` is deprecated since 1.35.0. Use `context` instead."
201+ in str (w .message )
202+ ]
203+
204+ self .assertEqual (len (log_record_context_warning ), 1 )
181205
182206 with warnings .catch_warnings (record = True ) as cw :
183207 for _ in range (10 ):
184208 LogRecord (context = get_current ())
185- self .assertEqual (len (cw ), 0 )
209+
210+ # Check that no LogDeprecatedInitWarning was emitted when using context
211+ context_deprecated_warnings = [
212+ w for w in cw if isinstance (w .message , LogDeprecatedInitWarning )
213+ ]
214+ self .assertEqual (len (context_deprecated_warnings ), 1 )
215+
216+ # Check we have no message
217+ log_record_context_warning = [
218+ w .message
219+ for w in cw
220+ if "LogRecord init with `trace_id`, `span_id`, and/or `trace_flags` is deprecated since 1.35.0. Use `context` instead."
221+ in str (w .message )
222+ ]
223+
224+ self .assertEqual (len (log_record_context_warning ), 0 )
225+
226+ def test_log_record_init_deprecated_warning (self ):
227+ """Test that LogRecord initialization emits a LogDeprecatedInitWarning."""
228+ with warnings .catch_warnings (record = True ) as cw :
229+ warnings .simplefilter ("always" )
230+ LogRecord ()
231+
232+ # Check that at least one LogDeprecatedInitWarning was emitted
233+ log_record_init_warnings = [
234+ w for w in cw if isinstance (w .message , LogDeprecatedInitWarning )
235+ ]
236+ self .assertGreater (
237+ len (log_record_init_warnings ),
238+ 0 ,
239+ "Expected at least one LogDeprecatedInitWarning" ,
240+ )
241+
242+ # Check the message content of the LogDeprecatedInitWarning
243+ warning_message = str (log_record_init_warnings [0 ].message )
244+ self .assertIn (
245+ "LogRecord will be removed in 1.39.0 and replaced by ReadWriteLogRecord and ReadableLogRecord" ,
246+ warning_message ,
247+ )
186248
187249 # pylint:disable=protected-access
188250 def test_log_record_from_api_log_record (self ):
@@ -217,3 +279,33 @@ def test_log_record_from_api_log_record(self):
217279 self .assertEqual (record .attributes , {"a" : "b" })
218280 self .assertEqual (record .event_name , "an.event" )
219281 self .assertEqual (record .resource , resource )
282+
283+
284+ class TestLogData (unittest .TestCase ):
285+ def test_init_deprecated_warning (self ):
286+ """Test that LogData initialization emits a LogDeprecatedInitWarning."""
287+ log_record = LogRecord ()
288+
289+ with warnings .catch_warnings (record = True ) as cw :
290+ warnings .simplefilter ("always" )
291+ LogData (
292+ log_record = log_record ,
293+ instrumentation_scope = InstrumentationScope ("foo" , "bar" ),
294+ )
295+
296+ # Check that at least one LogDeprecatedInitWarning was emitted
297+ init_warnings = [
298+ w for w in cw if isinstance (w .message , LogDeprecatedInitWarning )
299+ ]
300+ self .assertGreater (
301+ len (init_warnings ),
302+ 0 ,
303+ "Expected at least one LogDeprecatedInitWarning" ,
304+ )
305+
306+ # Check the message content of the LogDeprecatedInitWarning
307+ warning_message = str (init_warnings [0 ].message )
308+ self .assertIn (
309+ "LogData will be removed in 1.39.0 and replaced by ReadWriteLogRecord and ReadableLogRecord" ,
310+ warning_message ,
311+ )
0 commit comments