Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "lines" and "verbose" query parameters for advanced logs #5287

Merged
merged 1 commit into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions supervisor/api/host.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,14 @@ async def advanced_logs_handler(
"supported for now."
)

if request.headers[ACCEPT] == CONTENT_TYPE_X_LOG:
if "verbose" in request.query or request.headers[ACCEPT] == CONTENT_TYPE_X_LOG:
agners marked this conversation as resolved.
Show resolved Hide resolved
log_formatter = LogFormatter.VERBOSE

if RANGE in request.headers:
if "lines" in request.query:
lines = request.query.get("lines", DEFAULT_RANGE)
# entries=cursor[[:num_skip]:num_entries]
range_header = f"entries=:-{lines}:"
elif RANGE in request.headers:
range_header = request.headers.get(RANGE)
else:
range_header = f"entries=:-{DEFAULT_RANGE}:"
Expand Down
45 changes: 45 additions & 0 deletions tests/api/test_host.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,51 @@ async def test_advanced_logs(
)


async def test_advaced_logs_query_parameters(
api_client: TestClient,
coresys: CoreSys,
journald_logs: MagicMock,
journal_logs_reader: MagicMock,
):
"""Test advanced logging API entries controlled by query parameters."""
# Check lines query parameter
await api_client.get("/host/logs?lines=53")
journald_logs.assert_called_once_with(
params={"SYSLOG_IDENTIFIER": coresys.host.logs.default_identifiers},
range_header="entries=:-53:",
accept=LogFormat.JOURNAL,
)

journald_logs.reset_mock()

# Check verbose logs formatter via query parameter
await api_client.get("/host/logs?verbose")
journald_logs.assert_called_once_with(
params={"SYSLOG_IDENTIFIER": coresys.host.logs.default_identifiers},
range_header=DEFAULT_RANGE,
accept=LogFormat.JOURNAL,
)
journal_logs_reader.assert_called_with(ANY, LogFormatter.VERBOSE)

journal_logs_reader.reset_mock()
journald_logs.reset_mock()

# Query parameters should take precedence over headers
await api_client.get(
"/host/logs?lines=53&verbose",
headers={
"Range": "entries=:-19:10",
"Accept": "text/plain",
},
)
journald_logs.assert_called_once_with(
params={"SYSLOG_IDENTIFIER": coresys.host.logs.default_identifiers},
range_header="entries=:-53:",
accept=LogFormat.JOURNAL,
)
journal_logs_reader.assert_called_with(ANY, LogFormatter.VERBOSE)


async def test_advanced_logs_boot_id_offset(
api_client: TestClient, coresys: CoreSys, journald_logs: MagicMock
):
Expand Down
Loading