File: test_handler.py

package info (click to toggle)
python-opentelemetry 1.39.1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 6,968 kB
  • sloc: python: 53,083; sh: 398; makefile: 142; sql: 39
file content (538 lines) | stat: -rw-r--r-- 20,438 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
# Copyright The OpenTelemetry Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import logging
import os
import unittest
from unittest.mock import Mock, patch

from opentelemetry._logs import NoOpLoggerProvider, SeverityNumber
from opentelemetry._logs import get_logger as APIGetLogger
from opentelemetry.attributes import BoundedAttributes
from opentelemetry.sdk import trace
from opentelemetry.sdk._logs import (
    LoggerProvider,
    LoggingHandler,
    LogRecordProcessor,
    ReadableLogRecord,
)
from opentelemetry.sdk.environment_variables import OTEL_ATTRIBUTE_COUNT_LIMIT
from opentelemetry.semconv._incubating.attributes import code_attributes
from opentelemetry.semconv.attributes import exception_attributes
from opentelemetry.trace import (
    INVALID_SPAN_CONTEXT,
    set_span_in_context,
)


# pylint: disable=too-many-public-methods
class TestLoggingHandler(unittest.TestCase):
    def test_handler_default_log_level(self):
        processor, logger = set_up_test_logging(logging.NOTSET)

        # Make sure debug messages are ignored by default
        logger.debug("Debug message")
        assert processor.emit_count() == 0

        # Assert emit gets called for warning message
        with self.assertLogs(level=logging.WARNING):
            logger.warning("Warning message")
        self.assertEqual(processor.emit_count(), 1)

    def test_handler_custom_log_level(self):
        processor, logger = set_up_test_logging(logging.ERROR)

        with self.assertLogs(level=logging.WARNING):
            logger.warning("Warning message test custom log level")
        # Make sure any log with level < ERROR is ignored
        assert processor.emit_count() == 0

        with self.assertLogs(level=logging.ERROR):
            logger.error("Mumbai, we have a major problem")
        with self.assertLogs(level=logging.CRITICAL):
            logger.critical("No Time For Caution")
        self.assertEqual(processor.emit_count(), 2)

    # pylint: disable=protected-access
    def test_log_record_emit_noop(self):
        noop_logger_provder = NoOpLoggerProvider()
        logger_mock = APIGetLogger(
            __name__, logger_provider=noop_logger_provder
        )
        logger = logging.getLogger(__name__)
        handler_mock = Mock(spec=LoggingHandler)
        handler_mock._logger = logger_mock
        handler_mock.level = logging.WARNING
        logger.addHandler(handler_mock)
        with self.assertLogs(level=logging.WARNING):
            logger.warning("Warning message")

    def test_log_flush_noop(self):
        no_op_logger_provider = NoOpLoggerProvider()
        no_op_logger_provider.force_flush = Mock()

        logger = logging.getLogger("foo")
        handler = LoggingHandler(
            level=logging.NOTSET, logger_provider=no_op_logger_provider
        )
        logger.addHandler(handler)

        with self.assertLogs(level=logging.WARNING):
            logger.warning("Warning message")

        logger.handlers[0].flush()
        no_op_logger_provider.force_flush.assert_not_called()

    def test_log_record_no_span_context(self):
        processor, logger = set_up_test_logging(logging.WARNING)

        # Assert emit gets called for warning message
        with self.assertLogs(level=logging.WARNING):
            logger.warning("Warning message")

        record = processor.get_log_record(0)

        self.assertIsNotNone(record)
        self.assertEqual(
            record.log_record.trace_id, INVALID_SPAN_CONTEXT.trace_id
        )
        self.assertEqual(
            record.log_record.span_id, INVALID_SPAN_CONTEXT.span_id
        )
        self.assertEqual(
            record.log_record.trace_flags,
            INVALID_SPAN_CONTEXT.trace_flags,
        )

    def test_log_record_observed_timestamp(self):
        processor, logger = set_up_test_logging(logging.WARNING)

        with self.assertLogs(level=logging.WARNING):
            logger.warning("Warning message")

        record = processor.get_log_record(0)
        self.assertIsNotNone(record.log_record.observed_timestamp)

    def test_log_record_user_attributes(self):
        """Attributes can be injected into logs by adding them to the ReadWriteLogRecord"""
        processor, logger = set_up_test_logging(logging.WARNING)

        # Assert emit gets called for warning message
        with self.assertLogs(level=logging.WARNING):
            logger.warning("Warning message", extra={"http.status_code": 200})

        record = processor.get_log_record(0)

        self.assertIsNotNone(record)
        self.assertEqual(len(record.log_record.attributes), 4)
        self.assertEqual(record.log_record.attributes["http.status_code"], 200)
        self.assertTrue(
            record.log_record.attributes[
                code_attributes.CODE_FILE_PATH
            ].endswith("test_handler.py")
        )
        self.assertEqual(
            record.log_record.attributes[code_attributes.CODE_FUNCTION_NAME],
            "test_log_record_user_attributes",
        )
        # The line of the log statement is not a constant (changing tests may change that),
        # so only check that the attribute is present.
        self.assertTrue(
            code_attributes.CODE_LINE_NUMBER in record.log_record.attributes
        )
        self.assertTrue(
            isinstance(record.log_record.attributes, BoundedAttributes)
        )

    def test_log_record_exception(self):
        """Exception information will be included in attributes"""
        processor, logger = set_up_test_logging(logging.ERROR)

        try:
            raise ZeroDivisionError("division by zero")
        except ZeroDivisionError:
            with self.assertLogs(level=logging.ERROR):
                logger.exception("Zero Division Error")

        record = processor.get_log_record(0)

        self.assertIsNotNone(record)
        self.assertTrue(isinstance(record.log_record.body, str))
        self.assertEqual(record.log_record.body, "Zero Division Error")
        self.assertEqual(
            record.log_record.attributes[exception_attributes.EXCEPTION_TYPE],
            ZeroDivisionError.__name__,
        )
        self.assertEqual(
            record.log_record.attributes[
                exception_attributes.EXCEPTION_MESSAGE
            ],
            "division by zero",
        )
        stack_trace = record.log_record.attributes[
            exception_attributes.EXCEPTION_STACKTRACE
        ]
        self.assertIsInstance(stack_trace, str)
        self.assertTrue("Traceback" in stack_trace)
        self.assertTrue("ZeroDivisionError" in stack_trace)
        self.assertTrue("division by zero" in stack_trace)
        self.assertTrue(__file__ in stack_trace)

    def test_log_record_recursive_exception(self):
        """Exception information will be included in attributes even though it is recursive"""
        processor, logger = set_up_test_logging(logging.ERROR)

        try:
            raise ZeroDivisionError(
                ZeroDivisionError(ZeroDivisionError("division by zero"))
            )
        except ZeroDivisionError:
            with self.assertLogs(level=logging.ERROR):
                logger.exception("Zero Division Error")

        record = processor.get_log_record(0)

        self.assertIsNotNone(record)
        self.assertEqual(record.log_record.body, "Zero Division Error")
        self.assertEqual(
            record.log_record.attributes[exception_attributes.EXCEPTION_TYPE],
            ZeroDivisionError.__name__,
        )
        self.assertEqual(
            record.log_record.attributes[
                exception_attributes.EXCEPTION_MESSAGE
            ],
            "division by zero",
        )
        stack_trace = record.log_record.attributes[
            exception_attributes.EXCEPTION_STACKTRACE
        ]
        self.assertIsInstance(stack_trace, str)
        self.assertTrue("Traceback" in stack_trace)
        self.assertTrue("ZeroDivisionError" in stack_trace)
        self.assertTrue("division by zero" in stack_trace)
        self.assertTrue(__file__ in stack_trace)

    def test_log_exc_info_false(self):
        """Exception information will not be included in attributes"""
        processor, logger = set_up_test_logging(logging.NOTSET)

        try:
            raise ZeroDivisionError("division by zero")
        except ZeroDivisionError:
            with self.assertLogs(level=logging.ERROR):
                logger.error("Zero Division Error", exc_info=False)

        record = processor.get_log_record(0)

        self.assertIsNotNone(record)
        self.assertEqual(record.log_record.body, "Zero Division Error")
        self.assertNotIn(
            exception_attributes.EXCEPTION_TYPE,
            record.log_record.attributes,
        )
        self.assertNotIn(
            exception_attributes.EXCEPTION_MESSAGE,
            record.log_record.attributes,
        )
        self.assertNotIn(
            exception_attributes.EXCEPTION_STACKTRACE,
            record.log_record.attributes,
        )

    def test_log_record_exception_with_object_payload(self):
        processor, logger = set_up_test_logging(logging.ERROR)

        class CustomException(Exception):
            def __str__(self):
                return "CustomException stringified"

        try:
            raise CustomException("CustomException message")
        except CustomException as exception:
            with self.assertLogs(level=logging.ERROR):
                logger.exception(exception)

        record = processor.get_log_record(0)

        self.assertIsNotNone(record)
        self.assertTrue(isinstance(record.log_record.body, str))
        self.assertEqual(record.log_record.body, "CustomException stringified")
        self.assertEqual(
            record.log_record.attributes[exception_attributes.EXCEPTION_TYPE],
            CustomException.__name__,
        )
        self.assertEqual(
            record.log_record.attributes[
                exception_attributes.EXCEPTION_MESSAGE
            ],
            "CustomException message",
        )
        stack_trace = record.log_record.attributes[
            exception_attributes.EXCEPTION_STACKTRACE
        ]
        self.assertIsInstance(stack_trace, str)
        self.assertTrue("Traceback" in stack_trace)
        self.assertTrue("CustomException" in stack_trace)
        self.assertTrue(__file__ in stack_trace)

    def test_log_record_trace_correlation(self):
        processor, logger = set_up_test_logging(logging.WARNING)

        tracer = trace.TracerProvider().get_tracer(__name__)
        with tracer.start_as_current_span("test") as span:
            mock_context = set_span_in_context(span)

            with patch(
                "opentelemetry.sdk._logs._internal.get_current",
                return_value=mock_context,
            ):
                with self.assertLogs(level=logging.CRITICAL):
                    logger.critical("Critical message within span")

                record = processor.get_log_record(0)

                self.assertEqual(
                    record.log_record.body,
                    "Critical message within span",
                )
                self.assertEqual(record.log_record.severity_text, "CRITICAL")
                self.assertEqual(
                    record.log_record.severity_number,
                    SeverityNumber.FATAL,
                )
                self.assertEqual(record.log_record.context, mock_context)
                span_context = span.get_span_context()
                self.assertEqual(
                    record.log_record.trace_id, span_context.trace_id
                )
                self.assertEqual(
                    record.log_record.span_id, span_context.span_id
                )
                self.assertEqual(
                    record.log_record.trace_flags,
                    span_context.trace_flags,
                )

    def test_log_record_trace_correlation_deprecated(self):
        processor, logger = set_up_test_logging(logging.WARNING)

        tracer = trace.TracerProvider().get_tracer(__name__)
        with tracer.start_as_current_span("test") as span:
            with self.assertLogs(level=logging.CRITICAL):
                logger.critical("Critical message within span")

            record = processor.get_log_record(0)

            self.assertEqual(
                record.log_record.body, "Critical message within span"
            )
            self.assertEqual(record.log_record.severity_text, "CRITICAL")
            self.assertEqual(
                record.log_record.severity_number, SeverityNumber.FATAL
            )
            span_context = span.get_span_context()
            self.assertEqual(record.log_record.trace_id, span_context.trace_id)
            self.assertEqual(record.log_record.span_id, span_context.span_id)
            self.assertEqual(
                record.log_record.trace_flags, span_context.trace_flags
            )

    def test_warning_without_formatter(self):
        processor, logger = set_up_test_logging(logging.WARNING)
        logger.warning("Test message")

        record = processor.get_log_record(0)
        self.assertEqual(record.log_record.body, "Test message")

    def test_exception_without_formatter(self):
        processor, logger = set_up_test_logging(logging.WARNING)
        logger.exception("Test exception")

        record = processor.get_log_record(0)
        self.assertEqual(record.log_record.body, "Test exception")

    def test_warning_with_formatter(self):
        processor, logger = set_up_test_logging(
            logging.WARNING,
            formatter=logging.Formatter(
                "%(name)s - %(levelname)s - %(message)s"
            ),
        )
        logger.warning("Test message")

        record = processor.get_log_record(0)
        self.assertEqual(
            record.log_record.body, "foo - WARNING - Test message"
        )

    def test_log_body_is_always_string_with_formatter(self):
        processor, logger = set_up_test_logging(
            logging.WARNING,
            formatter=logging.Formatter(
                "%(name)s - %(levelname)s - %(message)s"
            ),
        )
        logger.warning(["something", "of", "note"])

        record = processor.get_log_record(0)
        self.assertIsInstance(record.log_record.body, str)

    @patch.dict(os.environ, {"OTEL_SDK_DISABLED": "true"})
    def test_handler_root_logger_with_disabled_sdk_does_not_go_into_recursion_error(
        self,
    ):
        processor, logger = set_up_test_logging(
            logging.NOTSET, root_logger=True
        )
        logger.warning("hello")

        self.assertEqual(processor.emit_count(), 0)

    @patch.dict(os.environ, {OTEL_ATTRIBUTE_COUNT_LIMIT: "3"})
    def test_otel_attribute_count_limit_respected_in_logging_handler(self):
        """Test that OTEL_ATTRIBUTE_COUNT_LIMIT is properly respected by LoggingHandler."""
        # Create a new LoggerProvider within the patched environment
        # This will create LogRecordLimits() that reads from the environment variable
        logger_provider = LoggerProvider()
        processor = FakeProcessor()
        logger_provider.add_log_record_processor(processor)
        logger = logging.getLogger("env_test")
        handler = LoggingHandler(
            level=logging.WARNING, logger_provider=logger_provider
        )
        logger.addHandler(handler)

        # Create a log record with many extra attributes
        extra_attrs = {f"custom_attr_{i}": f"value_{i}" for i in range(10)}

        with self.assertLogs(level=logging.WARNING):
            logger.warning(
                "Test message with many attributes", extra=extra_attrs
            )

        record = processor.get_log_record(0)

        # With OTEL_ATTRIBUTE_COUNT_LIMIT=3, should have exactly 3 attributes
        total_attrs = len(record.log_record.attributes)
        self.assertEqual(
            total_attrs,
            3,
            f"Should have exactly 3 attributes due to limit, got {total_attrs}",
        )

        # Should have 10 dropped attributes (10 custom + 3 code - 3 kept = 10 dropped)
        self.assertEqual(
            record.dropped_attributes,
            10,
            f"Should have 10 dropped attributes, got {record.dropped_attributes}",
        )

    @patch.dict(os.environ, {OTEL_ATTRIBUTE_COUNT_LIMIT: "5"})
    def test_otel_attribute_count_limit_includes_code_attributes(self):
        """Test that OTEL_ATTRIBUTE_COUNT_LIMIT applies to all attributes including code attributes."""
        # Create a new LoggerProvider within the patched environment
        # This will create LogRecordLimits() that reads from the environment variable
        logger_provider = LoggerProvider()
        processor = FakeProcessor()
        logger_provider.add_log_record_processor(processor)
        logger = logging.getLogger("env_test_2")
        handler = LoggingHandler(
            level=logging.WARNING, logger_provider=logger_provider
        )
        logger.addHandler(handler)

        # Create a log record with some extra attributes
        extra_attrs = {f"user_attr_{i}": f"value_{i}" for i in range(8)}

        with self.assertLogs(level=logging.WARNING):
            logger.warning("Test message", extra=extra_attrs)

        record = processor.get_log_record(0)

        # With OTEL_ATTRIBUTE_COUNT_LIMIT=5, should have exactly 5 attributes
        total_attrs = len(record.log_record.attributes)
        self.assertEqual(
            total_attrs,
            5,
            f"Should have exactly 5 attributes due to limit, got {total_attrs}",
        )

        # Should have 6 dropped attributes (8 user + 3 code - 5 kept = 6 dropped)
        self.assertEqual(
            record.dropped_attributes,
            6,
            f"Should have 6 dropped attributes, got {record.dropped_attributes}",
        )

    def test_logging_handler_without_env_var_uses_default_limit(self):
        """Test that without OTEL_ATTRIBUTE_COUNT_LIMIT, default limit (128) should apply."""
        processor, logger = set_up_test_logging(logging.WARNING)

        # Create a log record with many attributes (more than default limit of 128)
        extra_attrs = {f"attr_{i}": f"value_{i}" for i in range(150)}

        with self.assertLogs(level=logging.WARNING):
            logger.warning(
                "Test message with many attributes", extra=extra_attrs
            )

        record = processor.get_log_record(0)

        # Should be limited to default limit (128) total attributes
        total_attrs = len(record.log_record.attributes)
        self.assertEqual(
            total_attrs,
            128,
            f"Should have exactly 128 attributes (default limit), got {total_attrs}",
        )

        # Should have 25 dropped attributes (150 user + 3 code - 128 kept = 25 dropped)
        self.assertEqual(
            record.dropped_attributes,
            25,
            f"Should have 25 dropped attributes, got {record.dropped_attributes}",
        )


def set_up_test_logging(level, formatter=None, root_logger=False):
    logger_provider = LoggerProvider()
    processor = FakeProcessor()
    logger_provider.add_log_record_processor(processor)
    logger = logging.getLogger(None if root_logger else "foo")
    handler = LoggingHandler(level=level, logger_provider=logger_provider)
    if formatter:
        handler.setFormatter(formatter)
    logger.addHandler(handler)
    return processor, logger


class FakeProcessor(LogRecordProcessor):
    def __init__(self):
        self.log_data_emitted = []

    def on_emit(self, log_record: ReadableLogRecord):
        self.log_data_emitted.append(log_record)

    def shutdown(self):
        pass

    def force_flush(self, timeout_millis: int = 30000):
        pass

    def emit_count(self):
        return len(self.log_data_emitted)

    def get_log_record(self, i):
        return self.log_data_emitted[i]