Skip to content

Commit 4c2c04f

Browse files
committed
tests
1 parent 0c2a4a0 commit 4c2c04f

File tree

4 files changed

+182
-10
lines changed

4 files changed

+182
-10
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1616
([#4755](https://github.com/open-telemetry/opentelemetry-python/pull/4755))
1717
- logs: extend Logger.emit to accept separated keyword arguments
1818
([#4737](https://github.com/open-telemetry/opentelemetry-python/pull/4737))
19+
- logs: Added different warning classes as necessary for log class deprecation
20+
([#4771](https://github.com/open-telemetry/opentelemetry-python/pull/4771))
1921

2022
## Version 1.37.0/0.58b0 (2025-09-11)
2123

opentelemetry-sdk/tests/logs/test_export.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import os
1818
import time
1919
import unittest
20+
import warnings
2021
from concurrent.futures import ThreadPoolExecutor
2122
from sys import version_info
2223
from unittest.mock import Mock, patch
@@ -26,6 +27,8 @@
2627
from opentelemetry._logs import SeverityNumber
2728
from opentelemetry.sdk import trace
2829
from opentelemetry.sdk._logs import (
30+
ConsoleLogExporterDeprecatedWarning,
31+
InMemoryLogExporterDeprecatedWarning,
2932
LogData,
3033
LoggerProvider,
3134
LoggingHandler,
@@ -647,3 +650,87 @@ def formatter(record): # pylint: disable=unused-argument
647650
exporter.export([EMPTY_LOG])
648651

649652
mock_stdout.write.assert_called_once_with(mock_record_str)
653+
654+
def test_console_log_exporter_deprecated_warning(self):
655+
"""Test that ConsoleLogExporter initialization emits a deprecation warning."""
656+
with warnings.catch_warnings(record=True) as cw:
657+
warnings.simplefilter("always")
658+
ConsoleLogExporter()
659+
660+
# Check that at least one ConsoleLogExporterDeprecatedWarning was emitted
661+
console_warnings = [
662+
w for w in cw if isinstance(w.message, ConsoleLogExporterDeprecatedWarning)
663+
]
664+
self.assertGreater(len(console_warnings), 0,
665+
"Expected at least one ConsoleLogExporterDeprecatedWarning")
666+
667+
# Check the message content of the ConsoleLogExporterDeprecatedWarning
668+
warning_message = str(console_warnings[0].message)
669+
self.assertIn(
670+
"ConsoleLogExporter will be deprecated in 1.39.0 and then renamed to ConsoleLogRecordExporter",
671+
warning_message,
672+
)
673+
674+
def test_console_log_exporter_deprecated_warning_once(self):
675+
"""Test that ConsoleLogExporter deprecation warning is only shown once due to simplefilter('once')."""
676+
with warnings.catch_warnings(record=True) as cw:
677+
# Multiple instantiations should only warn once due to simplefilter("once")
678+
for _ in range(10):
679+
ConsoleLogExporter()
680+
681+
# Check that exactly one ConsoleLogExporterDeprecatedWarning was emitted
682+
console_warnings = [
683+
w for w in cw if isinstance(w.message, ConsoleLogExporterDeprecatedWarning)
684+
]
685+
self.assertEqual(len(console_warnings), 1,
686+
"Expected exactly one ConsoleLogExporterDeprecatedWarning due to simplefilter('once')")
687+
688+
# Check the message content
689+
warning_message = str(console_warnings[0].message)
690+
self.assertIn(
691+
"ConsoleLogExporter will be deprecated in 1.39.0 and then renamed to ConsoleLogRecordExporter",
692+
warning_message,
693+
)
694+
695+
696+
class TestInMemoryLogExporterDeprecation(unittest.TestCase):
697+
def test_in_memory_log_exporter_deprecated_warning(self):
698+
"""Test that InMemoryLogExporter initialization emits a deprecation warning."""
699+
with warnings.catch_warnings(record=True) as cw:
700+
warnings.simplefilter("always")
701+
InMemoryLogExporter()
702+
703+
# Check that at least one InMemoryLogExporterDeprecatedWarning was emitted
704+
in_memory_warnings = [
705+
w for w in cw if isinstance(w.message, InMemoryLogExporterDeprecatedWarning)
706+
]
707+
self.assertGreater(len(in_memory_warnings), 0,
708+
"Expected at least one InMemoryLogExporterDeprecatedWarning")
709+
710+
# Check the message content of the InMemoryLogExporterDeprecatedWarning
711+
warning_message = str(in_memory_warnings[0].message)
712+
self.assertIn(
713+
"InMemoryLogExporter will be deprecated in 1.39.0 and then renamed to InMemoryLogRecordExporter",
714+
warning_message,
715+
)
716+
717+
def test_in_memory_log_exporter_deprecated_warning_once(self):
718+
"""Test that InMemoryLogExporter deprecation warning is only shown once due to simplefilter('once')."""
719+
with warnings.catch_warnings(record=True) as cw:
720+
# Multiple instantiations should only warn once due to simplefilter("once")
721+
for _ in range(10):
722+
InMemoryLogExporter()
723+
724+
# Check that exactly one InMemoryLogExporterDeprecatedWarning was emitted
725+
in_memory_warnings = [
726+
w for w in cw if isinstance(w.message, InMemoryLogExporterDeprecatedWarning)
727+
]
728+
self.assertEqual(len(in_memory_warnings), 1,
729+
"Expected exactly one InMemoryLogExporterDeprecatedWarning due to simplefilter('once')")
730+
731+
# Check the message content
732+
warning_message = str(in_memory_warnings[0].message)
733+
self.assertIn(
734+
"InMemoryLogExporter will be deprecated in 1.39.0 and then renamed to InMemoryLogRecordExporter",
735+
warning_message,
736+
)

opentelemetry-sdk/tests/logs/test_log_limits.py

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@
1313
# limitations under the License.
1414

1515
import unittest
16+
import warnings
1617
from unittest.mock import patch
1718

18-
from opentelemetry.sdk._logs import LogLimits
19+
from opentelemetry.sdk._logs import LogLimits, LogLimitsInitDeprecatedWarning
1920
from opentelemetry.sdk._logs._internal import (
2021
_DEFAULT_OTEL_ATTRIBUTE_COUNT_LIMIT,
2122
)
@@ -70,3 +71,43 @@ def test_invalid_env_vars_raise(self):
7071
str(error.exception),
7172
f"Unexpected error message for {env_var}={bad_value}",
7273
)
74+
75+
def test_log_limits_init_deprecated_warning(self):
76+
"""Test that LogLimits initialization emits a deprecation warning."""
77+
with warnings.catch_warnings(record=True) as cw:
78+
warnings.simplefilter("always")
79+
LogLimits()
80+
81+
self.assertEqual(len(cw), 1)
82+
self.assertIsInstance(cw[-1].message, LogLimitsInitDeprecatedWarning)
83+
self.assertIn(
84+
"LogLimits will be deprecated in 1.39.0 and then renamed to LogRecordLimits",
85+
str(cw[-1].message),
86+
)
87+
88+
def test_log_limits_init_deprecated_warning_with_params(self):
89+
"""Test that LogLimits initialization with parameters still emits a deprecation warning."""
90+
with warnings.catch_warnings(record=True) as cw:
91+
warnings.simplefilter("always")
92+
LogLimits(max_attributes=10, max_attribute_length=100)
93+
94+
self.assertEqual(len(cw), 1)
95+
self.assertIsInstance(cw[-1].message, LogLimitsInitDeprecatedWarning)
96+
self.assertIn(
97+
"LogLimits will be deprecated in 1.39.0 and then renamed to LogRecordLimits",
98+
str(cw[-1].message),
99+
)
100+
101+
def test_log_limits_init_deprecated_warning_once(self):
102+
"""Test that LogLimits deprecation warning is only shown once due to simplefilter('once')."""
103+
with warnings.catch_warnings(record=True) as cw:
104+
# Multiple instantiations should only warn once due to simplefilter("once")
105+
for _ in range(10):
106+
LogLimits()
107+
108+
self.assertEqual(len(cw), 1)
109+
self.assertIsInstance(cw[-1].message, LogLimitsInitDeprecatedWarning)
110+
self.assertIn(
111+
"LogLimits will be deprecated in 1.39.0 and then renamed to LogRecordLimits",
112+
str(cw[-1].message),
113+
)

opentelemetry-sdk/tests/logs/test_log_record.py

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
from opentelemetry.attributes import BoundedAttributes
2222
from opentelemetry.context import get_current
2323
from 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

Comments
 (0)