-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_logging_instrument.py
More file actions
96 lines (81 loc) · 3.22 KB
/
test_logging_instrument.py
File metadata and controls
96 lines (81 loc) · 3.22 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
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
logger = structlog.getLogger(__name__)
std_logger = logging.getLogger(__name__)
def test_logging_instrument_simple() -> None:
logging_instrument = LoggingInstrument(
bootstrap_config=LoggingConfig(
logging_unset_handlers=["uvicorn"], logging_buffer_capacity=0, service_debug=False
)
)
try:
logging_instrument.bootstrap()
logger.info("testing structlog", key="value")
try:
msg = "some error"
raise ValueError(msg) # noqa: TRY301
except ValueError:
logger.exception("logging error")
std_logger.info("testing std logger", extra={"key": "value"})
finally:
logging_instrument.teardown()
def test_logging_instrument_tracer_injection() -> None:
logging_instrument = LoggingInstrument(
bootstrap_config=LoggingConfig(logging_unset_handlers=["uvicorn"], logging_buffer_capacity=0)
)
opentelemetry_instrument = OpenTelemetryInstrument(
bootstrap_config=OpentelemetryConfig(
opentelemetry_endpoint="otl",
opentelemetry_log_traces=True,
)
)
try:
logging_instrument.bootstrap()
opentelemetry_instrument.bootstrap()
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")
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()