Skip to content

feat(api): search an application's logs (part of #232) - #463

Merged
samuelmbabhazi merged 15 commits into
mainfrom
feat/232-logs-search
Nov 25, 2026
Merged

samuelmbabhazi merged 15 commits into
mainfrom
feat/232-logs-search

Conversation

@samuelmbabhazi

Copy link
Copy Markdown
Collaborator

Part of #232 (M6.1). This is the read side of ADR 0029: searching an application's logs. It is stacked on #461, which writes them. The web UI comes next.

What changed

  • GET …/applications/{applicationId}/logs (environment.read, sessions and API tokens).

    • Parameters:
      • text: at most 256 characters, a phrase the lines contain;
      • stream: stdout or stderr;
      • instance: the replica;
      • from and to: the last hour by default, and a period of 30 days at most, starting before it ends;
      • limit: 200 by default, 500 at most.
    • Answer:
      • kept, false without a log store;
      • lines, newest first, each with time, stream, instance, line and truncated.

    The live view asks again from its newest line's time.

  • logsQuery (domain, pure):

    • It filters on org and app, the labels the control plane wrote (feat(api): write agents' log batches to VictoriaLogs (part of #232) #461), from identifiers it validates as UUIDs.
    • It adds _time:[from, to], the stream and the instance, then the text as one quoted phrase: backslashes and quotes escaped, control characters removed.
    • It ends with | sort by (_time desc) | limit n.
  • logLineOf reads back only well-formed records.

  • VictoriaLogsStore.query calls /select/logsql/query.

    • The answer is read up to 16 MiB.
    • Any failure (connection, status, size, JSON) is a 503 "Logs temporarily unavailable.", never the store's text.
  • LogsQueries checks the permission and that the application exists in its environment, then searches.

  • Generated openapi.json, API client and route-role matrix. SPEC 12: the route.

How it was tested

  • Domain:
    • the exact query;
    • injection attempts stay one phrase and cannot add a filter, a pipe or another app: " | delete, a backslash before a quote, newlines and NUL, and } {org="other"};
    • a scope that is not two UUIDs is refused;
    • malformed records are dropped.
  • Store, against a local HTTP server: the query and limit parameters, JSON lines with blank lines between them, and a 500 or a non-JSON answer giving a 503 without the store's text.
  • Service: labels and drops; kept: false without a store; an unknown application or no permission gives 404.
  • nx run-many -t typecheck lint -p api passes, and the 53 observability tests pass.

Security considerations

#232)

The maintainer chose a dedicated ShipLogs stream, as for metrics (ADR 0024): no published port, agent credential or agent label to trust. Acknowledged batches and a cursor per container let logs survive a control plane outage within Docker's rotation; the agent redacts secret environment values first.

Signed-off-by: samuel mbabhazi <[email protected]>
ADR 0029: LogsOpen with the lease epoch, LogBatch of entries (container, stream, Docker time, line, truncated) acknowledged by sequence number, and Welcome.logs. ConfigUpdate.logs_endpoint is deprecated.

Signed-off-by: samuel mbabhazi <[email protected]>
ShipLogs on the gateway checks the open's lease epoch and the batches' order, and acknowledges each one once VictoriaLogs kept it. Lines are labelled from the node's placed allocations only, malformed entries are dropped, and each node writes at most 512 KiB/s with 4 MiB bursts. VICTORIALOGS_URL is now optional; Welcome.logs says whether logs are kept.

Signed-off-by: samuel mbabhazi <[email protected]>
GET …/applications/{id}/logs reads VictoriaLogs with a LogsQL query whose filters come from validated identifiers and whose text is one quoted phrase; it answers the lines newest first, at most 500, over 30 days at most, and kept: false without a log store.

Signed-off-by: samuel mbabhazi <[email protected]>
@coderabbitai

coderabbitai Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 31 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 3971dd2 and 025160d.

Files ignored due to path filters: 3

  • apps/api/openapi.json is excluded by !apps/api/openapi.json
  • docs/generated/route-role-matrix.md is excluded by !**/generated/**, !docs/generated/**
  • packages/api-client/src/gen/openapi.ts is excluded by !**/gen/**, !packages/api-client/src/gen/**
Files selected for processing: 17

  • apps/api/src/modules/observability/application/logs-ingest.spec.ts
  • apps/api/src/modules/observability/application/logs-ingest.ts
  • apps/api/src/modules/observability/application/logs-queries.spec.ts
  • apps/api/src/modules/observability/application/logs-queries.ts
  • apps/api/src/modules/observability/application/ports.ts
  • apps/api/src/modules/observability/domain/logs.spec.ts
  • apps/api/src/modules/observability/domain/logs.ts
  • apps/api/src/modules/observability/infrastructure/bounded-read.spec.ts
  • apps/api/src/modules/observability/infrastructure/bounded-read.ts
  • apps/api/src/modules/observability/infrastructure/victoria-logs.store.spec.ts
  • apps/api/src/modules/observability/infrastructure/victoria-logs.store.ts
  • apps/api/src/modules/observability/infrastructure/victoria-metrics.store.ts
  • apps/api/src/modules/observability/interface/logs.controller.spec.ts
  • apps/api/src/modules/observability/interface/logs.controller.ts
  • apps/api/src/modules/observability/interface/logs.dto.ts
  • apps/api/src/modules/observability/observability.module.ts
  • docs/SPEC.md
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Signed-off-by: samuel mbabhazi <[email protected]>

# Conflicts:
#	docs/SPEC.md
Signed-off-by: samuel mbabhazi <[email protected]>

# Conflicts:
#	docs/SPEC.md
Signed-off-by: samuel mbabhazi <[email protected]>

# Conflicts:
#	apps/api/src/modules/fleet/fleet.module.ts
Signed-off-by: samuel mbabhazi <[email protected]>

# Conflicts:
#	apps/api/src/modules/observability/domain/logs.spec.ts
#	docs/SPEC.md
#	docs/generated/route-role-matrix.md
#	packages/api-client/src/gen/openapi.ts
@samuelmbabhazi

Copy link
Copy Markdown
Collaborator Author

An independent review of this PR (high effort, while CodeRabbit is rate limited) raised eight points. All eight held up; 8e5e0d3 and 224a877 fix them.

  • The route matrix lost the exec-sessions row when feat(api): write agents' log batches to VictoriaLogs (part of #232) #461 was merged in. It is regenerated with both rows.
  • A body that broke mid-read escaped as a 500. Both stores now use one bounded reader, bounded-read.ts, which turns any read failure or an answer over the bound into the store's 503. It has tests for the bound, a broken body and discarding.
  • The dedupe merged genuine repeats. Times were cut to milliseconds at ingest. A line now keeps Docker's nanoseconds in VictoriaLogs, and the API answers every time with nine digits of fraction. VictoriaLogs trims trailing zeros, which also made times sort wrongly as text. A resend matches to the nanosecond; two different lines rarely do.
  • The live tail could skip lines. The route takes order=oldest, so a live view reads forward from its newest line, and again at once while an answer is full. The web panel will switch to it once this stack and the exec stack are on main; its branch is still awaiting visual validation.
  • Search matched whole words. The text is now a quoted substring, *"…"*, matched with its case. I checked it on the lab's VictoriaLogs 1.54.0: *"fter-46"* and *"GET /after-460?x"* match, and a quote stays escaped.
  • instance was read with Number(). It must now be one to three digits.
  • from and to were read without an offset, so they depended on the server's zone. They now need Z or ±hh:mm. The period check moved to logsPeriod, which is tested.
  • The duplicated reader is gone: the metrics store uses the shared one too.

SPEC 12 describes the substring, order, the offsets and the nine-digit times. nx typecheck lint for the API and web passes, and the observability, fleet and route matrix tests pass.

@samuelmbabhazi
samuelmbabhazi changed the base branch from feat/232-logs-ingest to main November 25, 2026 09:13
Signed-off-by: samuel mbabhazi <[email protected]>

# Conflicts:
#	apps/api/src/modules/observability/application/logs-ingest.spec.ts
#	apps/api/src/modules/observability/application/logs-ingest.ts
#	apps/api/src/modules/observability/application/ports.ts
#	apps/api/src/modules/observability/domain/logs.spec.ts
#	apps/api/src/modules/observability/domain/logs.ts
#	apps/api/src/modules/observability/infrastructure/victoria-logs.store.spec.ts
#	apps/api/src/modules/observability/infrastructure/victoria-logs.store.ts
#	apps/api/src/modules/observability/observability.module.ts
#	docs/SPEC.md
@samuelmbabhazi
samuelmbabhazi merged commit 66564e1 into main Nov 25, 2026
23 of 25 checks passed
@samuelmbabhazi
samuelmbabhazi deleted the feat/232-logs-search branch November 25, 2026 09:29
samuelmbabhazi added a commit that referenced this pull request Nov 25, 2026
…464)

Part of #232 (M6.1). The agent side of ADR 0029: it follows every
managed container's output and sends it to the control plane on the
`ShipLogs` stream, which #461 writes to VictoriaLogs.

Stacked on #459 (the Docker logs reader). Retarget to `main` once #459
is merged.

## What changed

- **`agent/internal/logship`**: the shipper.
- It follows every running container labelled `sk.managed`, from that
container's cursor, and rescans every 10 s for new ones. A follower that
ends is started again on the next scan, including when the daemon
restarts with live-restore.
- Lines go out in batches of at most 256 entries and 60 KiB, flushed
every second. At most 4 batches wait for their acknowledgement.
- When a batch is acknowledged, the cursor of each container in it moves
to that batch's last `LogLine.Resume`. `Resume` is the time from which
reading again loses nothing, even while a line of the other stream is
still being joined (#459). Lines therefore arrive at least once, and
none is lost while Docker still holds it.
- **Redaction** (`redact.go`): the values of the container's secret
environment variables of 8 bytes or more become `[redacted]` before a
line leaves the agent, longest first. Their names come from the new
`sk.secret_env` label, and their values from the container's own
environment. A redacted line is cut again to 16 KiB.
- **`actions`**: `CreateContainer` sets `sk.secret_env`, the sorted
names of the spec's secrets. Names are not secret.
- **`journal`**: a `log_cursors` table, keyed by container name. A
cursor only moves forward, and those of containers that are gone are
forgotten.
- **`gateway`**: `Streams.OpenLogs` opens the stream. The runner starts
the shipper with the session's lease epoch once Welcome says logs are
kept, and stops it with the session. A failed stream is retried after 5
s.
- **CI**: the integration job also runs `./agent/internal/logship/`.

## How it was tested

- Unit tests: batching and bounds, acknowledgements that move cursors
only once kept, out-of-order or unexpected answers ending the stream,
redaction (short values left alone, overlapping values, the cut after
redaction), and cursor storage.
- `TestRealShipperSendsAContainersRedactedLinesAndKeepsItsCursor` runs
against a real Docker Engine: a busybox container prints a secret on
stderr, which arrives as `token is [redacted]`, and the cursor is
stored. It now checks only its own container, so it also passes on an
engine that runs other managed containers (it did, in the lab node).
- **Lab**, agent `0.1.0-lab.27` from this branch on a node with an nginx
application:
- lines printed before, during and after an agent restart all reached
VictoriaLogs and were found by the API's search;
- the last acknowledged line came once more after the restart, as
at-least-once allows; #463 shows it once.
- `go test ./agent/...` and golangci-lint pass.

## Security considerations

- Container output is untrusted. Lines are bounded by #459 and by the
batch limits. The control plane validates every entry and sets every
label itself, so an agent can only write lines for its own placed
containers (#461).
- Secret values are read from the container's environment for redaction
only. They are never logged, stored or sent. Secrets mounted as files,
and values an application derives from a secret, are not redacted. ADR
0029 and THREAT_MODEL say so.
- No new action and no new shell invocation. The shipper uses Docker's
logs and inspect APIs only.

---------

Signed-off-by: samuel mbabhazi <[email protected]>
samuelmbabhazi added a commit that referenced this pull request Nov 25, 2026
… (part of #232) (#468)

Part of #232 (M6.1); closes #477: the application page's Logs and
Terminal tabs on real data, replacing the samples they showed. Stacked
on #457, the exec WebSocket; retarget to `main` once #457 is merged. The
maintainer validated the screens on desktop and phone on 2026-11-25,
from captures of a lab installation.

## What changed

- **Logs tab** (`logs-panel.ts`, `logs-view.ts`):
- the replicas' output, newest first, from `GET
…/applications/{id}/logs` (#463);
- filters All, stdout and stderr, and a text search (a substring,
matched with its case);
- a live view that asks again every 3 s while the tab is shown, and not
at all without a log store. It reads forward from shortly before its
newest line, oldest first, page after page (`order=oldest`, up to 5
pages a tick), so a burst between two ticks is not skipped. Each tick
waits for the previous read to end.
- Lines are merged without repeats. Times are shown in the viewer's time
zone. On a phone, the time, replica and stream sit above the line at
full width.
  - An installation without a log store says that it keeps no logs.
- **Terminal tab** (`terminal-panel.ts`, `terminal-view.ts`):
- a command line in the first replica (r0), over the exec WebSocket
(`/api/v1/exec/{id}`, #457): Connect, then one command per line;
- output bounded: a line keeps its first 4096 characters and is marked
with ` …` past them, dropping the rest up to its newline, and the screen
keeps at most 2000 lines and 1 Mi characters, the newest first (#477);
  - stdout and stderr shown apart, stderr in rose, and Disconnect;
- the notice that the session is recorded in the audit log and that no
SSH port is open.
- **Shared tab bar** (`shell/tabs.ts`): on a narrow screen it scrolls to
keep the open tab in view, for example when a link opens
`?tab=terminal`.
- **CSP**: `connect-src` adds `wss://` for the API's own host only
(`socketOrigin()`), so the terminal's WebSocket can connect.
- **`DeliveryApi`** gains `logs()` and `openExecSession()`. French
translations are in `fr.ts`.
- **API, logs route** (found in review): `from` written in UTC with up
to nine digits of fraction now goes into the LogsQL query as written
(`fromExact`), so a live view can read forward from a line's exact time.
A `Date` cut it to milliseconds and stalled on bursts.
- **After review:**
- each live read starts three seconds before the newest line, so lines
another server sent late still show;
  - only a submitted search applies;
  - a terminal left while connecting joins nothing;
- the header reads "shell connected", the one text change from the
validated captures.

## How it was tested

- Web unit tests (55 files): log merging, keys, times, the forward
reading (page after page from the last line, its bound, and a full page
that does not move), terminal frames, and the tab bar's scroll.
- `nx typecheck lint test` for web and API, and the browser suite
(Chromium against the production build, 28 tests), which enforces the
CSP.
- **Lab** (control plane 0.1.0-lab.26, agent 0.1.0-lab.27, an nginx
application):
- the Logs tab showed the replica's real lines, and the search found
them;
- the Terminal ran `echo`, `ls` and `nginx -v` in the container, with
stderr apart.

## Security considerations

- The terminal goes through the agent and its typed exec action, and it
is audited, as #455 to #457 describe. No new route.
- The CSP opens WebSockets to the API's own origin only.
- Log lines arrive already redacted by the agent (ADR 0029). The panel
renders them as text and never as HTML.



<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

<!-- coderabbit-ai-generated:v1 -->

## Summary by CodeRabbit

* **New Features**
* Added live application logs with stream filters, text search,
pause/resume, and retryable error handling.
* Added an interactive terminal for running commands in an application
replica.
  * Kept the active tab visible when navigating a narrow tab bar.
* **Bug Fixes**
* Improved log updates to include recently delayed entries and support
precise UTC time filters.
  * Allowed WebSocket connections from the configured public URL.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Signed-off-by: samuel mbabhazi <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant