|
3 | 3 |
|
4 | 4 | import structlog |
5 | 5 | from opentelemetry.trace import get_tracer |
| 6 | +from structlog.testing import LogCapture |
6 | 7 |
|
7 | 8 | from lite_bootstrap.instruments.logging_instrument import LoggingConfig, LoggingInstrument, MemoryLoggerFactory |
8 | 9 | from lite_bootstrap.instruments.opentelemetry_instrument import OpentelemetryConfig, OpenTelemetryInstrument |
9 | 10 |
|
10 | 11 |
|
11 | | -logger = structlog.getLogger(__name__) |
12 | 12 | std_logger = logging.getLogger(__name__) |
13 | 13 |
|
14 | 14 |
|
15 | 15 | def test_logging_instrument_simple() -> None: |
| 16 | + log_capture = LogCapture() |
16 | 17 | logging_instrument = LoggingInstrument( |
17 | 18 | bootstrap_config=LoggingConfig( |
18 | | - logging_unset_handlers=["uvicorn"], logging_buffer_capacity=0, service_debug=False |
| 19 | + logging_unset_handlers=["uvicorn"], |
| 20 | + logging_buffer_capacity=0, |
| 21 | + service_debug=False, |
| 22 | + logging_extra_processors=[log_capture], |
19 | 23 | ) |
20 | 24 | ) |
21 | 25 | try: |
22 | 26 | logging_instrument.bootstrap() |
| 27 | + |
| 28 | + logger = structlog.getLogger(__name__) |
23 | 29 | logger.info("testing structlog", key="value") |
| 30 | + std_logger.info("testing std logger", extra={"key": "value"}) |
24 | 31 | try: |
25 | 32 | msg = "some error" |
26 | 33 | raise ValueError(msg) # noqa: TRY301 |
27 | 34 | except ValueError: |
28 | 35 | logger.exception("logging error") |
29 | | - std_logger.info("testing std logger", extra={"key": "value"}) |
| 36 | + |
| 37 | + events_number = 2 |
| 38 | + assert len(log_capture.entries) == events_number |
30 | 39 | finally: |
31 | 40 | logging_instrument.teardown() |
32 | 41 |
|
33 | 42 |
|
34 | 43 | def test_logging_instrument_tracer_injection() -> None: |
| 44 | + log_capture = LogCapture() |
35 | 45 | logging_instrument = LoggingInstrument( |
36 | | - bootstrap_config=LoggingConfig(logging_unset_handlers=["uvicorn"], logging_buffer_capacity=0) |
| 46 | + bootstrap_config=LoggingConfig( |
| 47 | + logging_unset_handlers=["uvicorn"], |
| 48 | + logging_buffer_capacity=0, |
| 49 | + logging_extra_processors=[log_capture], |
| 50 | + ) |
37 | 51 | ) |
38 | 52 | opentelemetry_instrument = OpenTelemetryInstrument( |
39 | 53 | bootstrap_config=OpentelemetryConfig( |
40 | | - opentelemetry_endpoint="otl", |
41 | 54 | opentelemetry_log_traces=True, |
42 | 55 | ) |
43 | 56 | ) |
44 | 57 | try: |
45 | 58 | logging_instrument.bootstrap() |
46 | 59 | opentelemetry_instrument.bootstrap() |
| 60 | + |
| 61 | + logger = structlog.getLogger(__name__) |
47 | 62 | tracer = get_tracer(__name__) |
48 | 63 | logger.info("testing tracer injection without spans") |
49 | 64 | with tracer.start_as_current_span("my_fake_span") as span: |
50 | 65 | logger.info("testing tracer injection without span attributes") |
51 | 66 | span.set_attribute("example_attribute", "value") |
52 | 67 | span.add_event("example_event", {"event_attr": 1}) |
53 | 68 | logger.info("testing tracer injection with span attributes") |
| 69 | + |
| 70 | + assert log_capture.entries[0]["event"] == "testing tracer injection without spans" |
| 71 | + |
| 72 | + assert log_capture.entries[1]["event"] == "testing tracer injection without span attributes" |
| 73 | + assert log_capture.entries[2]["event"] == "testing tracer injection with span attributes" |
| 74 | + |
| 75 | + tracing1 = log_capture.entries[1]["tracing"] |
| 76 | + tracing2 = log_capture.entries[2]["tracing"] |
| 77 | + assert tracing1 |
| 78 | + assert tracing1 == tracing2 |
54 | 79 | finally: |
55 | 80 | logging_instrument.teardown() |
56 | 81 | opentelemetry_instrument.teardown() |
|
0 commit comments