Repository: firecracker-microvm/firecracker
File: tests/integration_tests/functional/test_instrumented_firecracker.py (Python)

Code before:

        # Generate some log traffic
        resp = vm.api.describe.get()
        assert resp.status_code == 200
        resp = vm.api.machine_config.get()
        assert resp.status_code == 200

        pre_tracing_log_data = vm.log_data

        # Ensure TRACE logs are being captured
        logger_config = {
            "level": "Trace",
            "show_level": True,
            "show_log_origin": True,
        }
        resp = vm.api.logger.put(**logger_config)
        assert resp.status_code == 204

        # Another API call after enabling TRACE
        resp = vm.api.describe.get()
        assert resp.status_code == 200

    finally:
        vm.kill()

    assert TRACE_LEVEL_HINT not in pre_tracing_log_data, (
        "TRACE level logs were found before setting log level to TRACE. "
    )

    pre_tracing_log_matches = re.findall(MARKER_REGEX, pre_tracing_log_data)

    assert (
        len(pre_tracing_log_matches) == 0
    ), f"Expected no log-instrument traces in logs before enabling TRACE, but found: {pre_tracing_log_matches}"

    post_tracing_log_data = vm.log_data

    assert TRACE_LEVEL_HINT in post_tracing_log_data, "Expected TRACE level logs in output"

    post_tracing_log_matches = re.findall(MARKER_REGEX, post_tracing_log_data)

    assert (
        len(post_tracing_log_matches) > 0
    ), f"Expected to find log-instrument traces in logs, but found none. Log data: {post_tracing_log_data[:1000]}..."

    entry_traces = [match for match in post_tracing_log_matches if ">>" in match]
    exit_traces = [match for match in post_tracing_log_matches if "<<" in match]

    assert len(entry_traces) > 0, "Expected to find function entry traces (>>)"
    assert len(exit_traces) > 0, "Expected to find function exit traces (<<)"


Review comment (suggestion):
There are some [styling findings](https://buildkite.com/firecracker/firecracker-pr/builds/14968#019a5314-38e3-43d7-8d26-67053d7ca998/305-312). You can use `./tools/devtool fmt` to autoformat.

Code after:


def test_log_instrument_firecracker_basic_functionality(
    instrumented_binary, microvm_factory
):
    """Test that instrumented Firecracker can start and handle basic API calls with trace logging."""
    vm = microvm_factory.build(fc_binary_path=instrumented_binary)
    vm.spawn(log_level="Info", log_show_level=True, log_show_origin=True)

    # Generate some log traffic
    _ = vm.api.describe.get()
    _ = vm.api.machine_config.get()

    pre_tracing_log_data = vm.log_data

    # Ensure TRACE logs are being captured
    logger_config = {
        "level": "Trace",
        "show_level": True,
        "show_log_origin": True,
    }
    _ = vm.api.logger.put(**logger_config)

    # Another API call after enabling TRACE
    _ = vm.api.describe.get()

    assert (
        TRACE_LEVEL_HINT not in pre_tracing_log_data
    ), "TRACE level logs were found before setting log level to TRACE. "

    pre_tracing_log_matches = re.findall(MARKER_REGEX, pre_tracing_log_data)

    assert (
        len(pre_tracing_log_matches) == 0
    ), f"Expected no log-instrument traces in logs before enabling TRACE, but found: {pre_tracing_log_matches}"

    post_tracing_log_data = vm.log_data

    assert (
        TRACE_LEVEL_HINT in post_tracing_log_data
    ), "Expected TRACE level logs in output"

    post_tracing_log_matches = re.findall(MARKER_REGEX, post_tracing_log_data)

    assert (
        len(post_tracing_log_matches) > 0
    ), f"Expected to find log-instrument traces in logs, but found none. Log data: {post_tracing_log_data[:1000]}..."

    entry_traces = [match for match in post_tracing_log_matches if ">>" in match]
    exit_traces = [match for match in post_tracing_log_matches if "<<" in match]