-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_logging_instrument.py
More file actions
119 lines (98 loc) · 4.03 KB
/
test_logging_instrument.py
File metadata and controls
119 lines (98 loc) · 4.03 KB
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
import logging
from io import StringIO
import structlog
from opentelemetry.trace import get_tracer
from lite_bootstrap.instruments.logging_instrument import LoggingConfig, LoggingInstrument, MemoryLoggerFactory
from lite_bootstrap.instruments.opentelemetry_instrument import OpentelemetryConfig, OpenTelemetryInstrument
from tests.conftest import LoggingMock
def test_logging_instrument_simple(logging_mock: LoggingMock) -> None:
logging_instrument = LoggingInstrument(
bootstrap_config=LoggingConfig(
logging_unset_handlers=["uvicorn"],
logging_buffer_capacity=0,
service_debug=False,
logging_extra_processors=[logging_mock],
)
)
try:
logging_instrument.bootstrap()
std_logger = logging.getLogger(__name__)
std_logger.info("testing std logger", extra={"key": "value"})
logger = structlog.getLogger(__name__)
logger.info("testing structlog", key="value")
try:
msg = "some error"
raise ValueError(msg) # noqa: TRY301
except ValueError:
logger.exception("logging error")
events_number = 3
assert len(logging_mock.entries) == events_number
finally:
logging_instrument.teardown()
def test_logging_instrument_tracer_injection(logging_mock: LoggingMock) -> None:
logging_instrument = LoggingInstrument(
bootstrap_config=LoggingConfig(
logging_unset_handlers=["uvicorn"],
logging_buffer_capacity=0,
logging_extra_processors=[logging_mock],
)
)
opentelemetry_instrument = OpenTelemetryInstrument(
bootstrap_config=OpentelemetryConfig(
opentelemetry_log_traces=True,
)
)
try:
logging_instrument.bootstrap()
opentelemetry_instrument.bootstrap()
logger = structlog.getLogger(__name__)
tracer = get_tracer(__name__)
logger.info("testing tracer injection without spans")
with tracer.start_as_current_span("my_fake_span") as span:
logger.info("testing tracer injection without span attributes")
span.set_attribute("example_attribute", "value")
span.add_event("example_event", {"event_attr": 1})
logger.info("testing tracer injection with span attributes")
assert logging_mock.entries[0]["event"] == "testing tracer injection without spans"
assert logging_mock.entries[1]["event"] == "testing tracer injection without span attributes"
assert logging_mock.entries[2]["event"] == "testing tracer injection with span attributes"
tracing1 = logging_mock.entries[1]["tracing"]
tracing2 = logging_mock.entries[2]["tracing"]
assert tracing1
assert tracing1 == tracing2
finally:
logging_instrument.teardown()
opentelemetry_instrument.teardown()
def test_memory_logger_factory_info() -> None:
test_capacity = 10
test_flush_level = logging.ERROR
test_stream = StringIO()
logger_factory = MemoryLoggerFactory(
logging_buffer_capacity=test_capacity,
logging_flush_level=test_flush_level,
logging_log_level=logging.INFO,
log_stream=test_stream,
)
test_logger = logger_factory()
test_message = "test message"
for current_log_index in range(test_capacity):
test_logger.info(test_message)
log_contents = test_stream.getvalue()
if current_log_index == test_capacity - 1:
assert test_message in log_contents
else:
assert not log_contents
def test_memory_logger_factory_error() -> None:
test_capacity = 10
test_flush_level = logging.ERROR
test_stream = StringIO()
logger_factory = MemoryLoggerFactory(
logging_buffer_capacity=test_capacity,
logging_flush_level=test_flush_level,
logging_log_level=logging.INFO,
log_stream=test_stream,
)
test_logger = logger_factory()
error_message = "error message"
test_logger.error(error_message)
assert error_message in test_stream.getvalue()