mirror of
https://github.com/cozystack/cozystack.git
synced 2026-11-21 20:23:39 +09:00
feat(dashboard): add logs and metrics views to the console (#5218)
## What this PR does Adds logs and resource-consumption views to the console, backed by the observability proxy from the design proposals [Tenant application logs](https://github.com/cozystack/community/pull/95) and [Tenant resource-consumption metrics](https://github.com/cozystack/community/pull/94). This PR is useful only together with #5214, which adds the console nginx route to the proxy. The application tabs need #5219 and the per-application roots from its follow-up PR. Until those are installed, each view shows a quiet "not available on this cluster" state instead of an error, so the order of merging does not break the console. What is added: - A "Logs" page at the namespace level, under a new "Observability" sidebar group, reading the namespace logs root. It lives at `/console/-/observability/logs`: every one- or two-segment console path is an application list or detail page, and `-` cannot start an application plural or name, so the page never shadows an application's own pages. - "Logs" and "Metrics" tabs on application pages (every application kind except VMDisk, which runs no containers), reading the per-application roots. - Logs view: - time range, a LogsQL filter, and pod and container pickers that turn into a stream filter; - "Follow" opens a live tail. The status shows "Live" as soon as the stream is open, even before the first line; on a dropped stream or a load refusal (`429`) it reconnects from the newest line seen, with backoff; it stops on `400`/`401`/`403`/`404`; - the reader can scroll up while lines stream; the newest 1000 lines are kept; - while a new range or filter loads, the previous lines stay on screen with a note saying so, or the view says it is loading; it never reports "no lines" for a query it has not read yet. - Metrics view: per-pod CPU and memory charts and application-wide network charts, refreshed every minute, with ranges from 1 hour to 7 days. The `rate` window is at least 5 minutes and four steps, so queries routed to the long-term store (coarser deduplication) still return data. No query names a namespace or kind; the proxy adds the scope. - Error handling by proxy status: scope-wide answers (`401`/`403`/`404`) replace the whole view with one notice; limit answers keep the proxy's own advice. A Kubernetes `Status` body on a proxy path means the request reached kube-apiserver instead of the proxy and is shown as "not available". - Shared UI pieces: an accessible `Select` and a text `Input` matching the console's controls, and an `EmptyState`. - `K8sClient.raw()` for untyped requests over the same credential path as `/apis`; the client now keeps caller headers given in any form. - Charts use uPlot, loaded in a separate lazy chunk, so the main bundle does not grow. #### How it was tested - `pnpm typecheck`, `pnpm test`, `pnpm lint` and `pnpm build` pass. - Screenshots of the views were taken against a mock API and on a test cluster. - Verified on a test cluster through the full browser chain (OIDC login, gatekeeper, gateway, console nginx, proxy), as a user with the `view` role in one tenant: the Logs page works; Follow keeps a single tail open for more than two minutes without reconnecting; Follow on a quiet filter shows "Live"; the Metrics tab of a Redis instance draws its charts; requests from the same session to another tenant's roots answer `403`. - Not verified: a screen reader (roles and names are asserted in tests only) and viewports narrower than 1440px. #### Known limitation The first lines a newly started pod writes can be missing from an already open tail, although a history query shows them. This comes from the log store's live-tail window relative to log collection delay, not from the console. ### Screenshots <img width="1440" height="900" alt="cluster-follow-live" src="https://github.com/user-attachments/assets/6efd9bfb-f05b-4498-bd20-0af4e5cda01f" /> <img width="1440" height="900" alt="cluster-logs-empty" src="https://github.com/user-attachments/assets/673d8295-cd3d-409d-b871-290705d0a6a2" /> <img width="1440" height="900" alt="cluster-logs" src="https://github.com/user-attachments/assets/621c2312-8427-4353-b199-44bdd12b78f7" /> <img width="1440" height="900" alt="cluster-pod-select" src="https://github.com/user-attachments/assets/92ea9c72-4a3c-4562-9c3a-2170730ab085" /> <img width="1400" height="1000" alt="cluster-redis-metrics" src="https://github.com/user-attachments/assets/3533d069-01b9-41de-bba9-70b940d05c6e" /> <img width="1440" height="900" alt="follow-waiting" src="https://github.com/user-attachments/assets/eb318d13-5188-4632-842a-43c0bbbcff5a" /> <img width="1440" height="900" alt="logs-empty" src="https://github.com/user-attachments/assets/c3d89e81-7249-444d-a3fa-9ab44c1ca86e" /> <img width="1440" height="900" alt="logs" src="https://github.com/user-attachments/assets/ec6caa9c-5eda-4361-99ea-359e5d002f04" /> <img width="1440" height="900" alt="metrics-empty" src="https://github.com/user-attachments/assets/db16da15-a618-4dbe-a531-593bc9b916dc" /> ### Downstream repositories - [ ] No downstream repository is affected by this change - [ ] [cozystack/website](https://github.com/cozystack/website) - follow-up: - [ ] [cozystack/terraform-provider-cozystack](https://github.com/cozystack/terraform-provider-cozystack) - follow-up: - [ ] [cozystack/ansible-cozystack](https://github.com/cozystack/ansible-cozystack) - follow-up: - [ ] [cozystack/ccp](https://github.com/cozystack/ccp) - follow-up: - [ ] [cozystack/talm](https://github.com/cozystack/talm) - follow-up: - [ ] [cozystack/cozyhr](https://github.com/cozystack/cozyhr) - follow-up: - [ ] [cozystack/cozy-proxy](https://github.com/cozystack/cozy-proxy) - follow-up: - [ ] [cozystack/cozystack-telemetry-server](https://github.com/cozystack/cozystack-telemetry-server) - follow-up: - [ ] [cozystack/external-apps-example](https://github.com/cozystack/external-apps-example) - follow-up: - [ ] [cozystack/examples](https://github.com/cozystack/examples) - follow-up: - [ ] [cozystack/community](https://github.com/cozystack/community) - follow-up: Downstream: no trigger matched. Candidate: none affected, because the whole diff is inside the vendored console (`packages/system/dashboard/images/console`), which per the trigger map belongs to this repository and has no downstream copy. A user guide page for the new console views on the website would be useful but is not required by the trigger map. ### Release note ```release-note feat(dashboard): the console gains a namespace Logs page and Logs and Metrics tabs on application pages, with live log following, per-pod CPU and memory charts, and application-wide network charts. The views need the observability proxy; without it they show "not available on this cluster". ``` <!-- This is an auto-generated comment: release notes by coderabbit.ai --> <!-- coderabbit-ai-generated:v1 --> ## Summary by CodeRabbit * **New Features** * Added Metrics and Logs tabs to supported application details, plus a tenant-wide Logs page in the sidebar. * View CPU, memory, and network charts over selectable time ranges, with optional sample tables and automatic refresh. * Search logs using pod and container filters, choose a history range, and follow live updates with connection status. * Added distinct loading, empty, and error messages for observability views, including guidance for unavailable services and query limits. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
45 files changed
+3184
-7
No files matched your search
@@ -32,6 +32,8 @@ Implications:
|
||||
- **RJSF** (`@rjsf/core` + `validator-ajv8`) for application config forms.
|
||||
- **Vitest + jsdom + @testing-library/react** for tests.
|
||||
- **Monaco** for YAML view, **noVNC** for the VM console tab.
|
||||
- **uPlot** for the Metrics tab charts, lazy-loaded with that tab.
|
||||
- Metrics and logs come from the observability proxy, not kube-apiserver: `lib/observability/proxy.ts` reads its native Prometheus JSON and VictoriaLogs NDJSON through `K8sClient.raw`, under the paths nginx routes to it ahead of `/apis`.
|
||||
|
||||
## Dev loop
|
||||
|
||||
@@ -79,6 +81,7 @@ the apiserver checks that user's RBAC.
|
||||
to a chunked-encoding WATCH against the same `resourceVersion`. Don't
|
||||
poll. Don't add `refetchInterval`. If you need a one-shot, pass
|
||||
`{ watch: false }`.
|
||||
The one exception is the metrics panels in `components/observability`: the observability proxy serves the Prometheus query API, which has no watch, so they refresh on a fixed interval instead. Log lines stream through the tail endpoint and are not polled.
|
||||
4. **Tenant scoping.** Most resources live in `tenant-<name>` namespaces.
|
||||
Pull the active tenant from `useTenantContext()` — never read the
|
||||
namespace from a URL param or guess it.
|
||||
|
||||
@@ -31,7 +31,8 @@
|
||||
"react": "^19.2.4",
|
||||
"react-dom": "^19.2.4",
|
||||
"react-router": "^7.6.1",
|
||||
"simple-icons": "^16.22.0"
|
||||
"simple-icons": "^16.22.0",
|
||||
"uplot": "^1.6.32"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@tailwindcss/vite": "^4.2.2",
|
||||
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
import { describe, it, expect, vi, afterEach } from "vitest"
|
||||
import { K8sClient } from "@cozystack/k8s-client"
|
||||
|
||||
afterEach(() => {
|
||||
vi.unstubAllGlobals()
|
||||
})
|
||||
|
||||
describe("K8sClient request headers", () => {
|
||||
it("lets a method's own content type replace the JSON default", async () => {
|
||||
const fetchMock = vi.fn(async () => new Response("{}"))
|
||||
vi.stubGlobal("fetch", fetchMock)
|
||||
const client = new K8sClient({ getToken: async () => "t0k" })
|
||||
|
||||
await client.patch("apps", "v1", "deployments", "web", { spec: {} }, "default", "merge")
|
||||
|
||||
const [, init] = fetchMock.mock.calls[0] as unknown as [string, RequestInit]
|
||||
const headers = new Headers(init.headers)
|
||||
expect(headers.get("Content-Type")).toBe("application/merge-patch+json")
|
||||
expect(headers.get("Authorization")).toBe("Bearer t0k")
|
||||
})
|
||||
|
||||
it("sends JSON by default", async () => {
|
||||
const fetchMock = vi.fn(async () => new Response("{}"))
|
||||
vi.stubGlobal("fetch", fetchMock)
|
||||
|
||||
await new K8sClient().getApiGroups()
|
||||
|
||||
const [, init] = fetchMock.mock.calls[0] as unknown as [string, RequestInit]
|
||||
expect(new Headers(init.headers).get("Content-Type")).toBe("application/json")
|
||||
})
|
||||
})
|
||||
+65
@@ -0,0 +1,65 @@
|
||||
import { describe, it, expect, vi, afterEach } from "vitest"
|
||||
import { K8sClient } from "@cozystack/k8s-client"
|
||||
|
||||
afterEach(() => {
|
||||
vi.unstubAllGlobals()
|
||||
vi.restoreAllMocks()
|
||||
})
|
||||
|
||||
describe("K8sClient.raw", () => {
|
||||
it("returns the response untouched, including a non-2xx one", async () => {
|
||||
const res = new Response("not found", { status: 404 })
|
||||
vi.stubGlobal("fetch", vi.fn(async () => res))
|
||||
const client = new K8sClient()
|
||||
|
||||
await expect(client.raw("/x")).resolves.toBe(res)
|
||||
})
|
||||
|
||||
it("prefixes the base URL and attaches the bearer token", async () => {
|
||||
const fetchMock = vi.fn(async () => new Response("{}"))
|
||||
vi.stubGlobal("fetch", fetchMock)
|
||||
const client = new K8sClient({ baseUrl: "/base", getToken: async () => "t0k" })
|
||||
|
||||
await client.raw("/apis/x", { headers: { Accept: "application/json" } })
|
||||
|
||||
const [url, init] = fetchMock.mock.calls[0] as unknown as [string, RequestInit]
|
||||
expect(url).toBe("/base/apis/x")
|
||||
const headers = new Headers(init.headers)
|
||||
expect(headers.get("Accept")).toBe("application/json")
|
||||
expect(headers.get("Authorization")).toBe("Bearer t0k")
|
||||
})
|
||||
|
||||
it.each([
|
||||
["a Headers instance", new Headers({ Accept: "application/x-ndjson" })],
|
||||
["a tuple list", [["Accept", "application/x-ndjson"]] as [string, string][]],
|
||||
])("keeps caller headers passed as %s", async (_, given) => {
|
||||
const fetchMock = vi.fn(async () => new Response("{}"))
|
||||
vi.stubGlobal("fetch", fetchMock)
|
||||
const client = new K8sClient({ getToken: async () => "t0k" })
|
||||
|
||||
await client.raw("/x", { headers: given })
|
||||
|
||||
const [, init] = fetchMock.mock.calls[0] as unknown as [string, RequestInit]
|
||||
const headers = new Headers(init.headers)
|
||||
expect(headers.get("Accept")).toBe("application/x-ndjson")
|
||||
expect(headers.get("Authorization")).toBe("Bearer t0k")
|
||||
})
|
||||
|
||||
it("routes a 401 through the client's unauthorized handler", async () => {
|
||||
const onUnauthorized = vi.fn()
|
||||
vi.stubGlobal("fetch", vi.fn(async () => new Response("", { status: 401 })))
|
||||
const client = new K8sClient({ onUnauthorized })
|
||||
|
||||
const res = await client.raw("/x")
|
||||
|
||||
expect(res.status).toBe(401)
|
||||
expect(onUnauthorized).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
|
||||
it("does not swallow a network failure", async () => {
|
||||
vi.stubGlobal("fetch", vi.fn(async () => { throw new TypeError("offline") }))
|
||||
const client = new K8sClient()
|
||||
|
||||
await expect(client.raw("/x")).rejects.toThrow("offline")
|
||||
})
|
||||
})
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
import { describe, it, expect } from "vitest"
|
||||
import { render, screen } from "@testing-library/react"
|
||||
import { EmptyState } from "./EmptyState.tsx"
|
||||
|
||||
describe("EmptyState", () => {
|
||||
it("announces its message as a status", () => {
|
||||
render(<EmptyState title="No data for this range." hint="Pick a longer range." />)
|
||||
|
||||
expect(screen.getByRole("status")).toHaveTextContent("No data for this range.Pick a longer range.")
|
||||
})
|
||||
|
||||
it("marks itself busy while something is loading", () => {
|
||||
render(<EmptyState busy title="Loading…" />)
|
||||
|
||||
expect(screen.getByRole("status")).toHaveAttribute("aria-busy", "true")
|
||||
})
|
||||
})
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
import type { ReactNode } from "react"
|
||||
import { cn, Spinner } from "@cozystack/ui"
|
||||
|
||||
interface EmptyStateProps {
|
||||
title: ReactNode
|
||||
hint?: ReactNode
|
||||
action?: ReactNode
|
||||
busy?: boolean
|
||||
tone?: "light" | "dark"
|
||||
className?: string
|
||||
}
|
||||
|
||||
export function EmptyState({ title, hint, action, busy, tone = "light", className }: EmptyStateProps) {
|
||||
const dark = tone === "dark"
|
||||
return (
|
||||
<div
|
||||
role="status"
|
||||
aria-busy={busy || undefined}
|
||||
className={cn("flex h-full flex-col items-center justify-center gap-1.5 px-4 py-8 text-center", className)}
|
||||
>
|
||||
{busy && <Spinner className={cn("mb-1 size-5", dark && "text-slate-500")} />}
|
||||
<p className={cn("text-sm", dark ? "text-slate-300" : "text-slate-600")}>{title}</p>
|
||||
{hint && <p className={cn("text-xs", dark ? "text-slate-500" : "text-slate-400")}>{hint}</p>}
|
||||
{action && <div className="mt-2">{action}</div>}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
+81
@@ -0,0 +1,81 @@
|
||||
import { describe, it, expect } from "vitest"
|
||||
import { fireEvent, render, screen } from "@testing-library/react"
|
||||
import { LogLines } from "./LogLines.tsx"
|
||||
|
||||
const entry = (n: number) => ({ _time: `2026-11-04T10:00:0${n}Z`, _msg: `line ${n}`, _stream_id: "s" })
|
||||
|
||||
function sized(list: HTMLElement, scrollHeight: number) {
|
||||
Object.defineProperty(list, "scrollHeight", { configurable: true, value: scrollHeight })
|
||||
Object.defineProperty(list, "clientHeight", { configurable: true, value: 100 })
|
||||
}
|
||||
|
||||
describe("LogLines", () => {
|
||||
it("keeps the console frame and shows the placeholder inside it when there are no lines", () => {
|
||||
render(<LogLines entries={[]} placeholder="Waiting for new log lines…" />)
|
||||
|
||||
const frame = screen.getByRole("region", { name: "Log lines" })
|
||||
expect(frame).toHaveTextContent("Waiting for new log lines…")
|
||||
expect(screen.queryByRole("list")).toBeNull()
|
||||
})
|
||||
|
||||
it("puts the lines in the same frame once they arrive", () => {
|
||||
const { rerender } = render(<LogLines entries={[]} placeholder="Waiting for new log lines…" />)
|
||||
const frame = screen.getByRole("region", { name: "Log lines" })
|
||||
|
||||
rerender(<LogLines entries={[entry(1)]} placeholder="Waiting for new log lines…" />)
|
||||
|
||||
expect(screen.getByRole("region", { name: "Log lines" })).toBe(frame)
|
||||
expect(frame).toContainElement(screen.getByRole("list"))
|
||||
expect(frame).not.toHaveTextContent("Waiting")
|
||||
})
|
||||
|
||||
it("shows a line without _time with no timestamp and no empty source", () => {
|
||||
render(<LogLines placeholder="" entries={[{ _msg: "visible message" }]} />)
|
||||
|
||||
expect(screen.getByRole("listitem")).toHaveTextContent(/^visible message$/)
|
||||
})
|
||||
|
||||
it("shows the remaining fields of a row without _msg", () => {
|
||||
render(<LogLines placeholder="" entries={[{ kubernetes_pod_name: "db-0", rows: "42" }]} />)
|
||||
|
||||
expect(screen.getByRole("listitem")).toHaveTextContent(/^db-0\/ rows=42$/)
|
||||
})
|
||||
|
||||
it("keeps following the newest line while the reader is at the bottom", () => {
|
||||
const { rerender } = render(<LogLines placeholder="" entries={[entry(1)]} />)
|
||||
const list = screen.getByRole("list")
|
||||
sized(list, 500)
|
||||
|
||||
rerender(<LogLines placeholder="" entries={[entry(1), entry(2)]} />)
|
||||
|
||||
expect(list.scrollTop).toBe(500)
|
||||
})
|
||||
|
||||
it("leaves the position alone once the reader scrolls up", () => {
|
||||
const { rerender } = render(<LogLines placeholder="" entries={[entry(1)]} />)
|
||||
const list = screen.getByRole("list")
|
||||
sized(list, 500)
|
||||
list.scrollTop = 120
|
||||
fireEvent.scroll(list)
|
||||
|
||||
rerender(<LogLines placeholder="" entries={[entry(1), entry(2)]} />)
|
||||
|
||||
expect(list.scrollTop).toBe(120)
|
||||
})
|
||||
|
||||
it("follows again once the list is cleared for a new run", () => {
|
||||
const { rerender } = render(<LogLines placeholder="" entries={[entry(1)]} />)
|
||||
const first = screen.getByRole("list")
|
||||
sized(first, 500)
|
||||
first.scrollTop = 120
|
||||
fireEvent.scroll(first)
|
||||
|
||||
rerender(<LogLines placeholder="" entries={[]} />)
|
||||
rerender(<LogLines placeholder="" entries={[entry(2)]} />)
|
||||
const second = screen.getByRole("list")
|
||||
sized(second, 300)
|
||||
rerender(<LogLines placeholder="" entries={[entry(2), entry(3)]} />)
|
||||
|
||||
expect(second.scrollTop).toBe(300)
|
||||
})
|
||||
})
|
||||
+62
@@ -0,0 +1,62 @@
|
||||
import { useEffect, useRef, type ReactNode } from "react"
|
||||
import { CONTAINER_FIELD, POD_FIELD } from "../../lib/observability/logs.ts"
|
||||
import type { LogEntry } from "../../lib/observability/proxy.ts"
|
||||
|
||||
const BOTTOM_SLACK_PX = 24
|
||||
const SHOWN_APART = new Set(["_time", "_msg", "_stream_id", POD_FIELD, CONTAINER_FIELD])
|
||||
|
||||
function source(e: LogEntry): string | null {
|
||||
const pod = e[POD_FIELD]
|
||||
const container = e[CONTAINER_FIELD]
|
||||
return pod || container ? `${pod ?? ""}/${container ?? ""}` : null
|
||||
}
|
||||
|
||||
// A `fields` or `stats` pipe can leave a row without _msg; its other fields are then the content.
|
||||
function message(e: LogEntry): string {
|
||||
if (e._msg !== undefined) return e._msg
|
||||
return Object.entries(e)
|
||||
.filter(([k, v]) => v !== undefined && !SHOWN_APART.has(k))
|
||||
.map(([k, v]) => `${k}=${v}`)
|
||||
.join(" ")
|
||||
}
|
||||
|
||||
export function LogLines({ entries, placeholder }: { entries: LogEntry[]; placeholder: ReactNode }) {
|
||||
const ref = useRef<HTMLOListElement>(null)
|
||||
const pinned = useRef(true)
|
||||
|
||||
useEffect(() => {
|
||||
if (entries.length === 0) pinned.current = true
|
||||
else if (ref.current && pinned.current) ref.current.scrollTop = ref.current.scrollHeight
|
||||
}, [entries])
|
||||
|
||||
return (
|
||||
<section
|
||||
aria-label="Log lines"
|
||||
className="h-[60vh] min-h-72 overflow-hidden rounded-lg bg-slate-950 font-mono text-xs leading-5 text-slate-100 shadow-xs"
|
||||
>
|
||||
{entries.length === 0 ? (
|
||||
placeholder
|
||||
) : (
|
||||
<ol
|
||||
ref={ref}
|
||||
onScroll={(e) => {
|
||||
const el = e.currentTarget
|
||||
pinned.current = el.scrollHeight - el.scrollTop - el.clientHeight <= BOTTOM_SLACK_PX
|
||||
}}
|
||||
className="h-full overflow-auto p-3"
|
||||
>
|
||||
{entries.map((e, i) => {
|
||||
const from = source(e)
|
||||
return (
|
||||
<li key={`${e._stream_id}-${e._time}-${i}`} className="whitespace-pre-wrap break-all">
|
||||
{e._time !== undefined && <><span className="text-slate-400">{new Date(e._time).toLocaleTimeString()}</span>{" "}</>}
|
||||
{from !== null && <><span className="text-sky-300">{from}</span>{" "}</>}
|
||||
{message(e)}
|
||||
</li>
|
||||
)
|
||||
})}
|
||||
</ol>
|
||||
)}
|
||||
</section>
|
||||
)
|
||||
}
|
||||
+416
@@ -0,0 +1,416 @@
|
||||
import { describe, it, expect, vi } from "vitest"
|
||||
import { render, screen, waitFor, within } from "@testing-library/react"
|
||||
import userEvent from "@testing-library/user-event"
|
||||
import { K8sClient, K8sProvider } from "@cozystack/k8s-client"
|
||||
import { renderWithK8sProvider } from "../../test-utils/render.tsx"
|
||||
import { LogsView } from "./LogsView.tsx"
|
||||
|
||||
const app = { namespace: "tenant-a", plural: "postgreses", name: "db" }
|
||||
const ROOT = "/apis/apps.cozystack.io/v1alpha1/namespaces/tenant-a/postgreses/db/log/"
|
||||
|
||||
const line = (time: string, msg: string, pod = "db-0") =>
|
||||
JSON.stringify({ _time: time, _msg: msg, _stream_id: "s", kubernetes_pod_name: pod, kubernetes_container_name: "postgres" })
|
||||
|
||||
function clientWith(handler: (endpoint: string, params: URLSearchParams) => Response | Promise<Response>) {
|
||||
const client = new K8sClient()
|
||||
const raw = vi.spyOn(client, "raw").mockImplementation(async (path) => {
|
||||
const url = new URL(path, "http://x")
|
||||
return handler(url.pathname, url.searchParams)
|
||||
})
|
||||
return { client, raw }
|
||||
}
|
||||
|
||||
const values = (...v: string[]) => Response.json({ values: v.map((value) => ({ value, hits: 1 })) })
|
||||
|
||||
function defaultHandler(endpoint: string, params?: URLSearchParams): Response | Promise<Response> {
|
||||
if (endpoint.endsWith("stream_field_values")) {
|
||||
return params?.get("field") === "kubernetes_pod_name" ? values("db-0", "db-1") : values("postgres")
|
||||
}
|
||||
if (endpoint.endsWith("query")) return new Response(`${line("2026-11-04T10:00:01Z", "ready to accept connections")}\n`)
|
||||
return new Promise(() => {})
|
||||
}
|
||||
|
||||
// The provider's own QueryClient keeps the previous result while a key changes; it is
|
||||
// shared across tests, so each test below uses namespaces no other test reads.
|
||||
function renderWithProductionDefaults(client: K8sClient, namespace: string) {
|
||||
const view = (ns: string) => (
|
||||
<K8sProvider client={client}>
|
||||
<LogsView scope={{ namespace: ns }} />
|
||||
</K8sProvider>
|
||||
)
|
||||
const result = render(view(namespace))
|
||||
return { switchTo: (ns: string) => result.rerender(view(ns)) }
|
||||
}
|
||||
|
||||
describe("LogsView with the production query defaults", () => {
|
||||
it("drops the previous tenant's lines and pods while the next tenant loads", async () => {
|
||||
const { client } = clientWith((endpoint, params) =>
|
||||
endpoint.includes("/tenant-switch-b/") ? new Promise(() => {}) : defaultHandler(endpoint, params),
|
||||
)
|
||||
const { switchTo } = renderWithProductionDefaults(client, "tenant-switch-a")
|
||||
expect(await screen.findByText("ready to accept connections")).toBeInTheDocument()
|
||||
await userEvent.click(screen.getByRole("combobox", { name: "Pod" }))
|
||||
await screen.findByRole("option", { name: "db-1" })
|
||||
await userEvent.keyboard("{Escape}")
|
||||
|
||||
switchTo("tenant-switch-b")
|
||||
|
||||
const frame = screen.getByRole("region", { name: "Log lines" })
|
||||
expect(within(frame).getByText("Loading log lines…")).toBeInTheDocument()
|
||||
expect(screen.queryByText("ready to accept connections")).toBeNull()
|
||||
await userEvent.click(screen.getByRole("combobox", { name: "Pod" }))
|
||||
expect((await screen.findAllByRole("option")).map((o) => o.textContent)).toEqual(["All"])
|
||||
})
|
||||
|
||||
it("starts the next tenant with no filter, pod or container of the previous one", async () => {
|
||||
const { client, raw } = clientWith(defaultHandler)
|
||||
const { switchTo } = renderWithProductionDefaults(client, "tenant-reset-a")
|
||||
await userEvent.type(screen.getByLabelText("LogsQL filter"), "error{Enter}")
|
||||
await userEvent.click(screen.getByRole("combobox", { name: "Pod" }))
|
||||
await userEvent.click(await screen.findByRole("option", { name: "db-1" }))
|
||||
await waitFor(() =>
|
||||
expect(raw.mock.calls.some(([p]) => new URL(p, "http://x").searchParams.get("query") === '{kubernetes_pod_name="db-1"} (error)')).toBe(true),
|
||||
)
|
||||
|
||||
switchTo("tenant-reset-b")
|
||||
|
||||
await waitFor(() => expect(raw.mock.calls.some(([p]) => p.includes("/tenant-reset-b/") && p.includes("select/logsql/query?"))).toBe(true))
|
||||
const first = raw.mock.calls.find(([p]) => p.includes("/tenant-reset-b/") && p.includes("select/logsql/query?"))!
|
||||
expect(new URL(first[0], "http://x").searchParams.get("query")).toBe("*")
|
||||
expect(screen.getByRole("combobox", { name: "Pod" })).toHaveTextContent("All")
|
||||
expect(screen.getByRole("combobox", { name: "Container" })).toHaveTextContent("All")
|
||||
expect(screen.getByLabelText("LogsQL filter")).toHaveValue("")
|
||||
})
|
||||
|
||||
it("keeps the current lines while a new filter in the same tenant loads", async () => {
|
||||
const { client } = clientWith((endpoint, params) =>
|
||||
endpoint.endsWith("query") && params.get("query") === "slow" ? new Promise(() => {}) : defaultHandler(endpoint, params),
|
||||
)
|
||||
renderWithProductionDefaults(client, "tenant-filter-a")
|
||||
expect(await screen.findByText("ready to accept connections")).toBeInTheDocument()
|
||||
|
||||
await userEvent.type(screen.getByLabelText("LogsQL filter"), "slow{Enter}")
|
||||
|
||||
expect(screen.getByText("ready to accept connections")).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it("says it is loading, not that nothing matched, while a longer range replaces an empty one", async () => {
|
||||
const { client } = clientWith((endpoint, params) => {
|
||||
if (!endpoint.endsWith("query")) return defaultHandler(endpoint, params)
|
||||
return Date.parse(params.get("start") ?? "") < Date.now() - 2 * 3600_000
|
||||
? new Promise(() => {})
|
||||
: new Response("")
|
||||
})
|
||||
renderWithProductionDefaults(client, "tenant-range-a")
|
||||
const frame = () => screen.getByRole("region", { name: "Log lines" })
|
||||
expect(await within(frame()).findByText("No log lines for this range and filter.")).toBeInTheDocument()
|
||||
|
||||
await userEvent.click(screen.getByRole("button", { name: "24h" }))
|
||||
|
||||
await waitFor(() => expect(within(frame()).getByRole("status")).toHaveAttribute("aria-busy", "true"))
|
||||
expect(within(frame()).queryByText("No log lines for this range and filter.")).toBeNull()
|
||||
})
|
||||
|
||||
it("marks the lines of the previous range as standing in while a longer range loads", async () => {
|
||||
const { client } = clientWith((endpoint, params) =>
|
||||
endpoint.endsWith("query") && Date.parse(params.get("start") ?? "") < Date.now() - 2 * 3600_000
|
||||
? new Promise(() => {})
|
||||
: defaultHandler(endpoint, params),
|
||||
)
|
||||
renderWithProductionDefaults(client, "tenant-range-b")
|
||||
expect(await screen.findByText("ready to accept connections")).toBeInTheDocument()
|
||||
expect(screen.queryByText(/previous lines/)).toBeNull()
|
||||
|
||||
await userEvent.click(screen.getByRole("button", { name: "24h" }))
|
||||
|
||||
expect(await screen.findByText(/previous lines/)).toHaveAttribute("aria-busy", "true")
|
||||
expect(screen.getByText("ready to accept connections")).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it("drops the standing-in note once the longer range answers", async () => {
|
||||
let answer: (r: Response) => void = () => {}
|
||||
const longer = new Promise<Response>((resolve) => { answer = resolve })
|
||||
const { client } = clientWith((endpoint, params) =>
|
||||
endpoint.endsWith("query") && Date.parse(params.get("start") ?? "") < Date.now() - 2 * 3600_000
|
||||
? longer
|
||||
: defaultHandler(endpoint, params),
|
||||
)
|
||||
renderWithProductionDefaults(client, "tenant-range-c")
|
||||
expect(await screen.findByText("ready to accept connections")).toBeInTheDocument()
|
||||
await userEvent.click(screen.getByRole("button", { name: "24h" }))
|
||||
expect(await screen.findByText(/previous lines/)).toBeInTheDocument()
|
||||
|
||||
answer(new Response(`${line("2026-11-03T12:00:00Z", "checkpoint complete")}\n`))
|
||||
|
||||
expect(await screen.findByText("checkpoint complete")).toBeInTheDocument()
|
||||
expect(screen.queryByText(/previous lines/)).toBeNull()
|
||||
expect(screen.queryByText("ready to accept connections")).toBeNull()
|
||||
})
|
||||
|
||||
it("shows no standing-in note while following", async () => {
|
||||
const { client } = clientWith((endpoint, params) =>
|
||||
endpoint.endsWith("tail")
|
||||
? new Response(`${line("2026-11-04T10:00:02Z", "live line")}\n`)
|
||||
: endpoint.endsWith("query") && params.get("query")?.includes("error")
|
||||
? new Promise(() => {})
|
||||
: defaultHandler(endpoint, params),
|
||||
)
|
||||
renderWithProductionDefaults(client, "tenant-follow-a")
|
||||
expect(await screen.findByText("ready to accept connections")).toBeInTheDocument()
|
||||
|
||||
await userEvent.type(screen.getByLabelText("LogsQL filter"), "error{Enter}")
|
||||
expect(await screen.findByText(/previous lines/)).toBeInTheDocument()
|
||||
await userEvent.click(screen.getByRole("button", { name: "Follow" }))
|
||||
|
||||
expect(await screen.findByText("live line")).toBeInTheDocument()
|
||||
expect(screen.queryByText(/previous lines/)).toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
describe("LogsView", () => {
|
||||
it("shows recent lines from the application root", async () => {
|
||||
const { client, raw } = clientWith(defaultHandler)
|
||||
|
||||
renderWithK8sProvider(<LogsView scope={app} />, { client })
|
||||
|
||||
expect(await screen.findByText("ready to accept connections")).toBeInTheDocument()
|
||||
const call = raw.mock.calls.find(([p]) => p.includes("select/logsql/query?"))!
|
||||
expect(call[0].startsWith(`${ROOT}select/logsql/query?`)).toBe(true)
|
||||
const params = new URL(call[0], "http://x").searchParams
|
||||
expect(params.get("query")).toBe("*")
|
||||
expect(params.get("limit")).toBe("500")
|
||||
})
|
||||
|
||||
it("narrows to the picked pod with a stream filter", async () => {
|
||||
const { client, raw } = clientWith(defaultHandler)
|
||||
renderWithK8sProvider(<LogsView scope={app} />, { client })
|
||||
|
||||
await userEvent.click(screen.getByRole("combobox", { name: "Pod" }))
|
||||
await userEvent.click(await screen.findByRole("option", { name: "db-1" }))
|
||||
|
||||
await waitFor(() =>
|
||||
expect(
|
||||
raw.mock.calls.some(([p]) => new URL(p, "http://x").searchParams.get("query") === '{kubernetes_pod_name="db-1"} (*)'),
|
||||
).toBe(true),
|
||||
)
|
||||
})
|
||||
|
||||
it("offers All next to every pod and starts on it", async () => {
|
||||
const { client } = clientWith(defaultHandler)
|
||||
renderWithK8sProvider(<LogsView scope={app} />, { client })
|
||||
const pod = screen.getByRole("combobox", { name: "Pod" })
|
||||
expect(pod).toHaveTextContent("All")
|
||||
|
||||
await userEvent.click(pod)
|
||||
|
||||
expect(await screen.findByRole("option", { name: "db-1" })).toBeInTheDocument()
|
||||
expect(screen.getAllByRole("option").map((o) => o.textContent)).toEqual(["All", "db-0", "db-1"])
|
||||
})
|
||||
|
||||
it("keeps the log console and says so inside it when nothing matches", async () => {
|
||||
const { client } = clientWith((endpoint, params) =>
|
||||
endpoint.endsWith("query") ? new Response("") : defaultHandler(endpoint, params),
|
||||
)
|
||||
|
||||
renderWithK8sProvider(<LogsView scope={app} />, { client })
|
||||
|
||||
const frame = screen.getByRole("region", { name: "Log lines" })
|
||||
expect(await within(frame).findByText("No log lines for this range and filter.")).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it("suggests a longer range only while one exists", async () => {
|
||||
const { client } = clientWith((endpoint, params) =>
|
||||
endpoint.endsWith("query") ? new Response("") : defaultHandler(endpoint, params),
|
||||
)
|
||||
renderWithK8sProvider(<LogsView scope={app} />, { client })
|
||||
const frame = () => screen.getByRole("region", { name: "Log lines" })
|
||||
expect(await within(frame()).findByText("Try a longer range or a broader filter.")).toBeInTheDocument()
|
||||
|
||||
await userEvent.click(screen.getByRole("button", { name: "24h" }))
|
||||
|
||||
await waitFor(() => expect(within(frame()).getByText("Try a broader filter.")).toBeInTheDocument())
|
||||
expect(within(frame()).queryByText(/longer range/)).toBeNull()
|
||||
})
|
||||
|
||||
it("shows loading inside the log console", () => {
|
||||
const { client } = clientWith((endpoint, params) =>
|
||||
endpoint.endsWith("query") ? new Promise(() => {}) : defaultHandler(endpoint, params),
|
||||
)
|
||||
|
||||
renderWithK8sProvider(<LogsView scope={app} />, { client })
|
||||
|
||||
const frame = screen.getByRole("region", { name: "Log lines" })
|
||||
expect(within(frame).getByRole("status")).toHaveAttribute("aria-busy", "true")
|
||||
})
|
||||
|
||||
it("says it is waiting while Follow has no line yet", async () => {
|
||||
const { client } = clientWith(defaultHandler)
|
||||
renderWithK8sProvider(<LogsView scope={app} />, { client })
|
||||
|
||||
await userEvent.click(screen.getByRole("button", { name: "Follow" }))
|
||||
|
||||
const frame = screen.getByRole("region", { name: "Log lines" })
|
||||
expect(within(frame).getByText("Waiting for new log lines…")).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it("applies the LogsQL filter on submit", async () => {
|
||||
const { client, raw } = clientWith(defaultHandler)
|
||||
renderWithK8sProvider(<LogsView scope={app} />, { client })
|
||||
|
||||
await userEvent.type(screen.getByLabelText("LogsQL filter"), "error{Enter}")
|
||||
|
||||
await waitFor(() =>
|
||||
expect(raw.mock.calls.some(([p]) => p.includes("select/logsql/query?") && p.includes("query=error"))).toBe(true),
|
||||
)
|
||||
})
|
||||
|
||||
it("follows the live tail when Follow is pressed", async () => {
|
||||
const { client } = clientWith((endpoint, params) =>
|
||||
endpoint.endsWith("tail") ? new Response(`${line("2026-11-04T10:00:05Z", "checkpoint complete")}\n`) : defaultHandler(endpoint, params),
|
||||
)
|
||||
renderWithK8sProvider(<LogsView scope={app} />, { client })
|
||||
|
||||
await userEvent.click(screen.getByRole("button", { name: "Follow" }))
|
||||
|
||||
expect(await screen.findByText("checkpoint complete")).toBeInTheDocument()
|
||||
expect(screen.getByRole("button", { name: "Follow" })).toHaveAttribute("aria-pressed", "true")
|
||||
})
|
||||
|
||||
it("says the tail stopped when the proxy refuses the query", async () => {
|
||||
const { client } = clientWith((endpoint, params) =>
|
||||
endpoint.endsWith("tail") ? new Response("pipe not allowed in tail", { status: 400 }) : defaultHandler(endpoint, params),
|
||||
)
|
||||
renderWithK8sProvider(<LogsView scope={app} />, { client })
|
||||
|
||||
await userEvent.click(screen.getByRole("button", { name: "Follow" }))
|
||||
|
||||
expect(await screen.findByText(/Stopped/)).toBeInTheDocument()
|
||||
expect(screen.getByText("pipe not allowed in tail")).toBeInTheDocument()
|
||||
const frame = screen.getByRole("region", { name: "Log lines" })
|
||||
expect(within(frame).getByText("Follow stopped before any line arrived.")).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it("renders a cluster without the proxy as not available", async () => {
|
||||
const { client } = clientWith(() => new Response("404 page not found", { status: 404 }))
|
||||
|
||||
renderWithK8sProvider(<LogsView scope={app} />, { client })
|
||||
|
||||
expect(await screen.findByText("Logs are not available on this cluster.")).toBeInTheDocument()
|
||||
expect(screen.queryByLabelText("LogsQL filter")).toBeNull()
|
||||
})
|
||||
|
||||
it("renders the tenant logs page as not available when kube-apiserver answers instead of the proxy", async () => {
|
||||
const { client } = clientWith(() =>
|
||||
Response.json({ kind: "Status", apiVersion: "v1", status: "Failure", reason: "Forbidden", code: 403 }, { status: 403 }),
|
||||
)
|
||||
|
||||
renderWithK8sProvider(<LogsView scope={{ namespace: "tenant-a" }} />, { client })
|
||||
|
||||
expect(await screen.findByText("Logs are not available on this cluster.")).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it("keeps the controls and explains a rejected query", async () => {
|
||||
const { client } = clientWith((endpoint, params) =>
|
||||
endpoint.endsWith("query") ? new Response("cannot parse query", { status: 400 }) : defaultHandler(endpoint, params),
|
||||
)
|
||||
|
||||
renderWithK8sProvider(<LogsView scope={app} />, { client })
|
||||
|
||||
expect(await screen.findByText("cannot parse query")).toBeInTheDocument()
|
||||
expect(screen.getByLabelText("LogsQL filter")).toBeInTheDocument()
|
||||
const frame = screen.getByRole("region", { name: "Log lines" })
|
||||
expect(within(frame).getByText("No log lines to show.")).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it("offers All once when some lines carry no pod name", async () => {
|
||||
const { client } = clientWith((endpoint, params) =>
|
||||
endpoint.endsWith("stream_field_values") && params.get("field") === "kubernetes_pod_name"
|
||||
? values("", "db-0")
|
||||
: defaultHandler(endpoint, params),
|
||||
)
|
||||
renderWithK8sProvider(<LogsView scope={app} />, { client })
|
||||
|
||||
await userEvent.click(screen.getByRole("combobox", { name: "Pod" }))
|
||||
|
||||
await screen.findByRole("option", { name: "db-0" })
|
||||
expect(screen.getAllByRole("option").map((o) => o.textContent)).toEqual(["All", "db-0"])
|
||||
})
|
||||
|
||||
it("shows rows of a query whose pipe removed _time", async () => {
|
||||
const { client } = clientWith((endpoint, params) =>
|
||||
endpoint.endsWith("query") ? new Response('{"_msg":"visible message"}\n') : defaultHandler(endpoint, params),
|
||||
)
|
||||
renderWithK8sProvider(<LogsView scope={app} />, { client })
|
||||
|
||||
await userEvent.type(screen.getByLabelText("LogsQL filter"), "* | fields _msg{Enter}")
|
||||
|
||||
const frame = screen.getByRole("region", { name: "Log lines" })
|
||||
expect(await within(frame).findByText("visible message")).toBeInTheDocument()
|
||||
expect(within(frame).queryByText(/No log lines/)).toBeNull()
|
||||
})
|
||||
|
||||
it("keeps the view when only the pod list is refused", async () => {
|
||||
const status = JSON.stringify({ kind: "Status", apiVersion: "v1", status: "Failure", code: 404 })
|
||||
const { client } = clientWith((endpoint, params) =>
|
||||
endpoint.endsWith("stream_field_values") ? new Response(status, { status: 404 }) : defaultHandler(endpoint, params),
|
||||
)
|
||||
|
||||
renderWithK8sProvider(<LogsView scope={app} />, { client })
|
||||
|
||||
expect(await screen.findByText("ready to accept connections")).toBeInTheDocument()
|
||||
expect(screen.queryByText("Logs are not available on this cluster.")).toBeNull()
|
||||
})
|
||||
|
||||
it("retries a failed history query on request", async () => {
|
||||
let calls = 0
|
||||
const { client } = clientWith((endpoint, params) =>
|
||||
endpoint.endsWith("query") && calls++ === 0 ? new Response("upstream down", { status: 502 }) : defaultHandler(endpoint, params),
|
||||
)
|
||||
renderWithK8sProvider(<LogsView scope={app} />, { client })
|
||||
|
||||
await userEvent.click(await screen.findByRole("button", { name: "Retry" }))
|
||||
|
||||
expect(await screen.findByText("ready to accept connections")).toBeInTheDocument()
|
||||
expect(screen.queryByRole("button", { name: "Retry" })).toBeNull()
|
||||
})
|
||||
|
||||
it("says it is loading while a retry runs", async () => {
|
||||
let calls = 0
|
||||
const { client } = clientWith((endpoint, params) => {
|
||||
if (!endpoint.endsWith("query")) return defaultHandler(endpoint, params)
|
||||
return calls++ === 0 ? new Response("upstream down", { status: 502 }) : new Promise(() => {})
|
||||
})
|
||||
renderWithK8sProvider(<LogsView scope={app} />, { client })
|
||||
|
||||
await userEvent.click(await screen.findByRole("button", { name: "Retry" }))
|
||||
|
||||
const frame = screen.getByRole("region", { name: "Log lines" })
|
||||
await waitFor(() => expect(within(frame).getByRole("status")).toHaveAttribute("aria-busy", "true"))
|
||||
expect(within(frame).queryByRole("button", { name: "Retry" })).toBeNull()
|
||||
})
|
||||
|
||||
it("opens a new history result at its newest line, not the old scroll position", async () => {
|
||||
const { client } = clientWith((endpoint, params) =>
|
||||
endpoint.endsWith("query")
|
||||
? new Response(`${line("2026-11-04T10:00:01Z", `result for ${params.get("query")}`)}\n`)
|
||||
: defaultHandler(endpoint, params),
|
||||
)
|
||||
const { queryClient } = renderWithK8sProvider(<LogsView scope={app} />, { client })
|
||||
queryClient.setDefaultOptions({ queries: { ...queryClient.getDefaultOptions().queries, placeholderData: (prev: unknown) => prev } })
|
||||
await screen.findByText("result for *")
|
||||
const before = screen.getByRole("list")
|
||||
|
||||
await userEvent.type(screen.getByLabelText("LogsQL filter"), "error{Enter}")
|
||||
|
||||
await screen.findByText("result for error")
|
||||
expect(screen.getByRole("list")).not.toBe(before)
|
||||
})
|
||||
|
||||
it("names the source pod and container of each line", async () => {
|
||||
const { client } = clientWith(defaultHandler)
|
||||
|
||||
renderWithK8sProvider(<LogsView scope={{ namespace: "tenant-a" }} />, { client })
|
||||
|
||||
expect(await screen.findByText("db-0/postgres")).toBeInTheDocument()
|
||||
})
|
||||
})
|
||||
+180
@@ -0,0 +1,180 @@
|
||||
import { useState } from "react"
|
||||
import { useQuery } from "@tanstack/react-query"
|
||||
import { useK8sClient } from "@cozystack/k8s-client"
|
||||
import { Button } from "@cozystack/ui"
|
||||
import { Input } from "../ui/input.tsx"
|
||||
import { SelectItem, SelectLabel, SelectPopup, SelectRoot, SelectTrigger, SelectValue } from "../ui/select.tsx"
|
||||
import {
|
||||
CONTAINER_FIELD,
|
||||
LOG_RANGES,
|
||||
POD_FIELD,
|
||||
buildLogsQuery,
|
||||
type LogRangeKey,
|
||||
} from "../../lib/observability/logs.ts"
|
||||
import {
|
||||
fieldValues,
|
||||
isScopeWide,
|
||||
logsRoot,
|
||||
queryLogs,
|
||||
type ObservabilityScope,
|
||||
} from "../../lib/observability/proxy.ts"
|
||||
import { useLogTail } from "../../lib/observability/use-log-tail.ts"
|
||||
import { ProxyNotice } from "./ProxyNotice.tsx"
|
||||
import { RangePicker } from "./RangePicker.tsx"
|
||||
import { LogLines } from "./LogLines.tsx"
|
||||
import { EmptyState } from "./EmptyState.tsx"
|
||||
|
||||
const HISTORY_LIMIT = 500
|
||||
const FOLLOW_CAP = 1000
|
||||
const TAIL_STATE_LABEL = { connecting: "Connecting…", live: "Live", reconnecting: "Reconnecting…", stopped: "Stopped" }
|
||||
|
||||
function windowFor(range: LogRangeKey) {
|
||||
const end = Date.now()
|
||||
return { start: new Date(end - LOG_RANGES[range] * 1000).toISOString(), end: new Date(end).toISOString() }
|
||||
}
|
||||
|
||||
const ALL = ""
|
||||
|
||||
function Picker({ label, value, options, onChange }: { label: string; value: string; options: string[]; onChange: (v: string) => void }) {
|
||||
const items = [{ value: ALL, label: "All" }, ...options.filter((o) => o !== ALL).map((o) => ({ value: o, label: o }))]
|
||||
return (
|
||||
<SelectRoot items={items} value={value} onValueChange={(v) => onChange(v ?? ALL)}>
|
||||
<div className="flex min-w-0 items-center gap-1.5">
|
||||
<SelectLabel>{label}</SelectLabel>
|
||||
<SelectTrigger className="w-40" title={value || undefined}>
|
||||
<SelectValue />
|
||||
</SelectTrigger>
|
||||
</div>
|
||||
<SelectPopup>
|
||||
{items.map((item) => (
|
||||
<SelectItem key={item.value} value={item.value}>
|
||||
{item.label}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectPopup>
|
||||
</SelectRoot>
|
||||
)
|
||||
}
|
||||
|
||||
const RANGE_KEYS = Object.keys(LOG_RANGES) as LogRangeKey[]
|
||||
const LONGEST_RANGE = RANGE_KEYS[RANGE_KEYS.length - 1]
|
||||
|
||||
function placeholderFor({ follow, stopped, loading, failed, longestRange, retry }: { follow: boolean; stopped: boolean; loading: boolean; failed: boolean; longestRange: boolean; retry: () => void }) {
|
||||
if (follow) {
|
||||
return stopped ? (
|
||||
<EmptyState tone="dark" title="Follow stopped before any line arrived." />
|
||||
) : (
|
||||
<EmptyState tone="dark" busy title="Waiting for new log lines…" hint="Lines written from now on appear here." />
|
||||
)
|
||||
}
|
||||
if (loading) return <EmptyState tone="dark" busy title="Loading log lines…" />
|
||||
if (failed) {
|
||||
return (
|
||||
<EmptyState
|
||||
tone="dark"
|
||||
title="No log lines to show."
|
||||
action={<Button variant="outline" size="sm" onClick={retry}>Retry</Button>}
|
||||
/>
|
||||
)
|
||||
}
|
||||
return <EmptyState tone="dark" title="No log lines for this range and filter." hint={longestRange ? "Try a broader filter." : "Try a longer range or a broader filter."} />
|
||||
}
|
||||
|
||||
// Filter, pod, container and the lines already read belong to one scope; a new tenant or application starts clean.
|
||||
export function LogsView({ scope }: { scope: ObservabilityScope }) {
|
||||
const root = logsRoot(scope)
|
||||
return <ScopedLogsView key={root} root={root} />
|
||||
}
|
||||
|
||||
function ScopedLogsView({ root }: { root: string }) {
|
||||
const client = useK8sClient()
|
||||
const [range, setRange] = useState<LogRangeKey>("1h")
|
||||
const [draft, setDraft] = useState("")
|
||||
const [filter, setFilter] = useState("")
|
||||
const [pod, setPod] = useState("")
|
||||
const [container, setContainer] = useState("")
|
||||
const [followRun, setFollowRun] = useState<number | null>(null)
|
||||
const follow = followRun !== null
|
||||
const query = buildLogsQuery({ filter, pod, container })
|
||||
|
||||
const pickerQuery = (field: string, narrowTo?: string) => ({
|
||||
queryKey: ["observability", "logs-fields", root, field, range, narrowTo ?? ""],
|
||||
queryFn: ({ signal }: { signal: AbortSignal }) =>
|
||||
fieldValues(client, root, {
|
||||
field,
|
||||
stream: true,
|
||||
query: buildLogsQuery({ filter: "", pod: narrowTo }),
|
||||
...windowFor(range),
|
||||
}, signal),
|
||||
retry: false,
|
||||
})
|
||||
const pods = useQuery(pickerQuery(POD_FIELD))
|
||||
const containers = useQuery(pickerQuery(CONTAINER_FIELD, pod || undefined))
|
||||
|
||||
const history = useQuery({
|
||||
queryKey: ["observability", "logs", root, query, range],
|
||||
queryFn: ({ signal }) => queryLogs(client, root, { query, limit: HISTORY_LIMIT, ...windowFor(range) }, signal),
|
||||
enabled: !follow,
|
||||
retry: false,
|
||||
})
|
||||
const tail = useLogTail(root, query, followRun, { cap: FOLLOW_CAP })
|
||||
|
||||
// The pod list is optional: refusing it alone must not hide lines the query can still read.
|
||||
const wide = [history.error, tail.error].find(isScopeWide)
|
||||
if (wide) {
|
||||
return (
|
||||
<div className="p-6">
|
||||
<ProxyNotice what="Logs" error={wide} />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
const error = follow ? tail.error : history.error
|
||||
const entries = follow ? tail.entries : (history.data ?? [])
|
||||
|
||||
return (
|
||||
<div className="space-y-3 p-6">
|
||||
<div className="flex flex-wrap items-center gap-3">
|
||||
<form
|
||||
className="flex min-w-64 flex-1"
|
||||
onSubmit={(e) => {
|
||||
e.preventDefault()
|
||||
setFilter(draft)
|
||||
}}
|
||||
>
|
||||
<Input
|
||||
aria-label="LogsQL filter"
|
||||
value={draft}
|
||||
onValueChange={setDraft}
|
||||
placeholder='LogsQL filter, e.g. error or "connection refused"'
|
||||
className="font-mono"
|
||||
/>
|
||||
</form>
|
||||
<Picker label="Pod" value={pod} options={pods.data ?? []} onChange={(v) => { setPod(v); setContainer("") }} />
|
||||
<Picker label="Container" value={container} options={containers.data ?? []} onChange={setContainer} />
|
||||
{!follow && <RangePicker options={RANGE_KEYS} value={range} onChange={setRange} />}
|
||||
<Button variant={follow ? "default" : "outline"} size="sm" aria-pressed={follow} onClick={() => setFollowRun(follow ? null : Date.now())}>
|
||||
Follow
|
||||
</Button>
|
||||
</div>
|
||||
{follow && (
|
||||
<p className="text-xs text-slate-500">
|
||||
{TAIL_STATE_LABEL[tail.state]}
|
||||
{` · newest ${FOLLOW_CAP} lines kept`}
|
||||
</p>
|
||||
)}
|
||||
{!follow && history.isPlaceholderData && entries.length > 0 && (
|
||||
<p role="status" aria-busy="true" className="text-xs text-slate-500">
|
||||
Showing the previous lines until the new range or filter loads…
|
||||
</p>
|
||||
)}
|
||||
{error ? <ProxyNotice what="Logs" error={error} /> : null}
|
||||
<LogLines
|
||||
// Previous results stay as placeholder data while a new query loads, so only a new key resets the scroll pin.
|
||||
key={followRun ?? `history\n${query}\n${range}`}
|
||||
entries={entries}
|
||||
placeholder={placeholderFor({ follow, stopped: tail.state === "stopped", loading: history.isLoading || history.isPlaceholderData, failed: Boolean(error), longestRange: range === LONGEST_RANGE, retry: () => void history.refetch() })}
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
+169
@@ -0,0 +1,169 @@
|
||||
import { describe, it, expect, vi } from "vitest"
|
||||
import { render, screen, waitFor, within } from "@testing-library/react"
|
||||
import userEvent from "@testing-library/user-event"
|
||||
import { K8sClient, K8sProvider } from "@cozystack/k8s-client"
|
||||
import { renderWithK8sProvider } from "../../test-utils/render.tsx"
|
||||
import type { ChartData } from "../../lib/observability/metrics.ts"
|
||||
|
||||
vi.mock("./TimeSeriesChart.tsx", () => ({
|
||||
TimeSeriesChart: ({ data }: { data: ChartData }) => (
|
||||
<div data-testid="chart">{data.series.map((s) => s.label).join(",")}</div>
|
||||
),
|
||||
}))
|
||||
|
||||
const { MetricsView } = await import("./MetricsView.tsx")
|
||||
|
||||
const scope = { namespace: "tenant-a", plural: "postgreses", name: "db" }
|
||||
|
||||
function matrix(pod: string) {
|
||||
return Response.json({
|
||||
status: "success",
|
||||
data: { resultType: "matrix", result: [{ metric: { pod }, values: [[1, "1"]] }] },
|
||||
})
|
||||
}
|
||||
|
||||
function clientWith(handler: (url: URL) => Response | Promise<Response>) {
|
||||
const client = new K8sClient()
|
||||
const raw = vi.spyOn(client, "raw").mockImplementation(async (path) => handler(new URL(path, "http://x")))
|
||||
return { client, raw }
|
||||
}
|
||||
|
||||
describe("MetricsView", () => {
|
||||
it("draws CPU, memory and network from the application root", async () => {
|
||||
const { client, raw } = clientWith(() => matrix("db-0"))
|
||||
|
||||
renderWithK8sProvider(<MetricsView scope={scope} />, { client })
|
||||
|
||||
await waitFor(() => expect(screen.getAllByTestId("chart")).toHaveLength(3))
|
||||
expect(screen.getByText("CPU (cores)")).toBeInTheDocument()
|
||||
expect(screen.getByText("Memory (working set)")).toBeInTheDocument()
|
||||
expect(screen.getByText("Network")).toBeInTheDocument()
|
||||
for (const [path] of raw.mock.calls) {
|
||||
expect(path.startsWith("/apis/apps.cozystack.io/v1alpha1/namespaces/tenant-a/postgreses/db/metrics/api/v1/query_range?")).toBe(true)
|
||||
}
|
||||
})
|
||||
|
||||
it("requeries with a coarser step when a longer range is picked", async () => {
|
||||
const { client, raw } = clientWith(() => matrix("db-0"))
|
||||
renderWithK8sProvider(<MetricsView scope={scope} />, { client })
|
||||
await waitFor(() => expect(raw).toHaveBeenCalled())
|
||||
|
||||
await userEvent.click(screen.getByRole("button", { name: "7d" }))
|
||||
|
||||
await waitFor(() =>
|
||||
expect(raw.mock.calls.some(([p]) => new URL(p, "http://x").searchParams.get("step") === "2520")).toBe(true),
|
||||
)
|
||||
})
|
||||
|
||||
it("shows one quiet notice instead of three errors when the proxy is absent", async () => {
|
||||
const { client } = clientWith(() => new Response("404 page not found", { status: 404 }))
|
||||
|
||||
renderWithK8sProvider(<MetricsView scope={scope} />, { client })
|
||||
|
||||
expect(await screen.findByText("Metrics are not available on this cluster.")).toBeInTheDocument()
|
||||
expect(screen.getAllByRole("status")).toHaveLength(1)
|
||||
expect(screen.queryByText("CPU (cores)")).toBeNull()
|
||||
})
|
||||
|
||||
it("keeps the other panels when one query hits a limit", async () => {
|
||||
const { client } = clientWith((url) =>
|
||||
url.searchParams.get("query")?.includes("memory")
|
||||
? Response.json({ status: "error", error: "too many series" }, { status: 422 })
|
||||
: matrix("db-0"),
|
||||
)
|
||||
|
||||
renderWithK8sProvider(<MetricsView scope={scope} />, { client })
|
||||
|
||||
expect(await screen.findByText("too many series. Choose a shorter range.")).toBeInTheDocument()
|
||||
await waitFor(() => expect(screen.getAllByTestId("chart")).toHaveLength(2))
|
||||
})
|
||||
|
||||
it("says so when a panel has no data", async () => {
|
||||
const { client } = clientWith(() => Response.json({ status: "success", data: { resultType: "matrix", result: [] } }))
|
||||
|
||||
renderWithK8sProvider(<MetricsView scope={scope} />, { client })
|
||||
|
||||
await waitFor(() => expect(screen.getAllByText("No data for this range.")).toHaveLength(3))
|
||||
for (const status of screen.getAllByRole("status")) expect(status).toHaveClass("h-[230px]")
|
||||
})
|
||||
|
||||
it("suggests a longer range only while one exists", async () => {
|
||||
const { client } = clientWith(() => Response.json({ status: "success", data: { resultType: "matrix", result: [] } }))
|
||||
renderWithK8sProvider(<MetricsView scope={scope} />, { client })
|
||||
await waitFor(() => expect(screen.getAllByText("Try a longer range.")).toHaveLength(3))
|
||||
|
||||
await userEvent.click(screen.getByRole("button", { name: "7d" }))
|
||||
|
||||
await waitFor(() => expect(screen.getAllByText("No data for this range.")).toHaveLength(3))
|
||||
expect(screen.queryByText("Try a longer range.")).toBeNull()
|
||||
})
|
||||
|
||||
it("shows a panel's samples as a table on request", async () => {
|
||||
const { client } = clientWith(() => matrix("db-0"))
|
||||
renderWithK8sProvider(<MetricsView scope={scope} />, { client })
|
||||
await waitFor(() => expect(screen.getAllByTestId("chart")).toHaveLength(3))
|
||||
const toggle = screen.getByRole("button", { name: "Show data for CPU (cores)" })
|
||||
expect(toggle).toHaveAttribute("aria-expanded", "false")
|
||||
|
||||
await userEvent.click(toggle)
|
||||
|
||||
const table = screen.getByRole("region", { name: "CPU (cores) data" })
|
||||
expect(toggle).toHaveAttribute("aria-expanded", "true")
|
||||
expect(toggle).toHaveAttribute("aria-controls", table.id)
|
||||
expect(toggle).toHaveTextContent("Hide data")
|
||||
expect(within(table).getByRole("columnheader", { name: "db-0" })).toBeInTheDocument()
|
||||
expect(screen.queryByRole("region", { name: "Memory (working set) data" })).toBeNull()
|
||||
})
|
||||
|
||||
it("offers no table while a panel has no samples", () => {
|
||||
const { client } = clientWith(() => new Promise<Response>(() => {}))
|
||||
|
||||
renderWithK8sProvider(<MetricsView scope={scope} />, { client })
|
||||
|
||||
expect(screen.getByRole("button", { name: "Show data for Network" })).toBeDisabled()
|
||||
})
|
||||
|
||||
it("offers no table for a panel with no series or an error", async () => {
|
||||
const { client } = clientWith((url) =>
|
||||
url.searchParams.get("query")?.includes("memory")
|
||||
? Response.json({ status: "error", error: "too many series" }, { status: 422 })
|
||||
: Response.json({ status: "success", data: { resultType: "matrix", result: [] } }),
|
||||
)
|
||||
renderWithK8sProvider(<MetricsView scope={scope} />, { client })
|
||||
|
||||
await screen.findByText("too many series. Choose a shorter range.")
|
||||
await waitFor(() => expect(screen.getAllByText("No data for this range.")).toHaveLength(2))
|
||||
for (const title of ["CPU (cores)", "Memory (working set)", "Network"]) {
|
||||
expect(screen.getByRole("button", { name: `Show data for ${title}` })).toBeDisabled()
|
||||
}
|
||||
})
|
||||
|
||||
it("drops the previous application's charts while the next one loads, with the production query defaults", async () => {
|
||||
const { client } = clientWith((url) => (url.pathname.includes("/switch-b/") ? new Promise<Response>(() => {}) : matrix("db-0")))
|
||||
const view = (name: string) => (
|
||||
<K8sProvider client={client}>
|
||||
<MetricsView scope={{ ...scope, name }} />
|
||||
</K8sProvider>
|
||||
)
|
||||
const { rerender } = render(view("switch-a"))
|
||||
await waitFor(() => expect(screen.getAllByTestId("chart")).toHaveLength(3))
|
||||
|
||||
rerender(view("switch-b"))
|
||||
|
||||
expect(screen.queryAllByTestId("chart")).toHaveLength(0)
|
||||
expect(screen.getAllByText("Loading…")).toHaveLength(3)
|
||||
})
|
||||
|
||||
it("holds each chart's place while it loads", () => {
|
||||
const { client } = clientWith(() => new Promise<Response>(() => {}))
|
||||
|
||||
renderWithK8sProvider(<MetricsView scope={scope} />, { client })
|
||||
|
||||
const statuses = screen.getAllByRole("status")
|
||||
expect(statuses).toHaveLength(3)
|
||||
for (const status of statuses) {
|
||||
expect(status).toHaveAttribute("aria-busy", "true")
|
||||
expect(status).toHaveClass("h-[230px]")
|
||||
}
|
||||
})
|
||||
})
|
||||
+105
@@ -0,0 +1,105 @@
|
||||
import { useId, useState } from "react"
|
||||
import { useQueries } from "@tanstack/react-query"
|
||||
import { useK8sClient } from "@cozystack/k8s-client"
|
||||
import { Button, Section } from "@cozystack/ui"
|
||||
import {
|
||||
METRIC_PANELS,
|
||||
RANGES,
|
||||
rangeWindow,
|
||||
toChartData,
|
||||
type RangeKey,
|
||||
} from "../../lib/observability/metrics.ts"
|
||||
import { isScopeWide, metricsRoot, queryRange, type ObservabilityScope } from "../../lib/observability/proxy.ts"
|
||||
import { ProxyNotice } from "./ProxyNotice.tsx"
|
||||
import { TimeSeriesChart } from "./TimeSeriesChart.tsx"
|
||||
import { RangePicker } from "./RangePicker.tsx"
|
||||
import { EmptyState } from "./EmptyState.tsx"
|
||||
import { SeriesTable } from "./SeriesTable.tsx"
|
||||
|
||||
const REFRESH_MS = 60_000
|
||||
// The uPlot canvas plus its one-row legend, so a placeholder takes the chart's place without a jump.
|
||||
const CHART_SLOT = "h-[230px] rounded-md border border-dashed border-slate-200 bg-slate-50"
|
||||
|
||||
export function MetricsView({ scope }: { scope: ObservabilityScope }) {
|
||||
const client = useK8sClient()
|
||||
const root = metricsRoot(scope)
|
||||
const [range, setRange] = useState<RangeKey>("1h")
|
||||
const [tables, setTables] = useState<ReadonlySet<string>>(new Set())
|
||||
const idBase = useId()
|
||||
const rangeKeys = Object.keys(RANGES) as RangeKey[]
|
||||
const longestRange = range === rangeKeys[rangeKeys.length - 1]
|
||||
|
||||
const results = useQueries({
|
||||
queries: METRIC_PANELS.map((panel) => ({
|
||||
queryKey: ["observability", "metrics", root, panel.id, range],
|
||||
queryFn: async ({ signal }: { signal: AbortSignal }) => {
|
||||
const window = rangeWindow(range, Math.floor(Date.now() / 1000))
|
||||
const series = await Promise.all(
|
||||
panel.queries(window).map(async (q) => ({
|
||||
label: q.label,
|
||||
series: await queryRange(client, root, { query: q.expr, ...window }, signal),
|
||||
})),
|
||||
)
|
||||
return toChartData(series)
|
||||
},
|
||||
retry: false,
|
||||
refetchInterval: REFRESH_MS,
|
||||
})),
|
||||
})
|
||||
|
||||
const wide = results.find((r) => isScopeWide(r.error))?.error
|
||||
|
||||
return (
|
||||
<div className="space-y-4 p-6">
|
||||
<div className="flex items-center justify-between">
|
||||
<h2 className="text-sm font-semibold text-slate-900">Resource consumption</h2>
|
||||
<RangePicker options={rangeKeys} value={range} onChange={setRange} />
|
||||
</div>
|
||||
{wide ? (
|
||||
<ProxyNotice what="Metrics" error={wide} />
|
||||
) : (
|
||||
METRIC_PANELS.map((panel, i) => {
|
||||
const { data, error, isLoading } = results[i]
|
||||
const hasSeries = !error && !isLoading && !!data && data.series.length > 0
|
||||
const tableOpen = hasSeries && tables.has(panel.id)
|
||||
const tableId = `${idBase}-${panel.id}-data`
|
||||
const toggle = (
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
disabled={!hasSeries}
|
||||
aria-expanded={tableOpen}
|
||||
aria-controls={tableOpen ? tableId : undefined}
|
||||
onClick={() =>
|
||||
setTables((prev) => {
|
||||
const next = new Set(prev)
|
||||
if (!next.delete(panel.id)) next.add(panel.id)
|
||||
return next
|
||||
})
|
||||
}
|
||||
>
|
||||
{tableOpen ? "Hide data" : "Show data"}{" "}
|
||||
<span className="sr-only">for {panel.title}</span>
|
||||
</Button>
|
||||
)
|
||||
return (
|
||||
<Section key={panel.id} title={panel.title} actions={toggle}>
|
||||
{error ? (
|
||||
<ProxyNotice what="Metrics" error={error} />
|
||||
) : isLoading || !data ? (
|
||||
<EmptyState busy title="Loading…" className={CHART_SLOT} />
|
||||
) : data.series.length === 0 ? (
|
||||
<EmptyState title="No data for this range." hint={longestRange ? undefined : "Try a longer range."} className={CHART_SLOT} />
|
||||
) : (
|
||||
<>
|
||||
<TimeSeriesChart data={data} format={panel.format} />
|
||||
{tableOpen && <SeriesTable id={tableId} title={panel.title} data={data} format={panel.format} />}
|
||||
</>
|
||||
)}
|
||||
</Section>
|
||||
)
|
||||
})
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
import { describe, it, expect } from "vitest"
|
||||
import { render, screen } from "@testing-library/react"
|
||||
import { ProxyError } from "../../lib/observability/proxy.ts"
|
||||
import { ProxyNotice } from "./ProxyNotice.tsx"
|
||||
|
||||
describe("ProxyNotice", () => {
|
||||
it("renders a 404 as not available on this cluster, not as an error", () => {
|
||||
render(<ProxyNotice what="Metrics" error={new ProxyError("not-available", 404, "page not found")} />)
|
||||
|
||||
expect(screen.getByRole("status")).toHaveTextContent("Metrics are not available on this cluster.")
|
||||
expect(screen.queryByRole("alert")).toBeNull()
|
||||
})
|
||||
|
||||
it("renders a 502 as a quiet notice about the store", () => {
|
||||
render(<ProxyNotice what="Logs" error={new ProxyError("store-unreachable", 502, "dial tcp")} />)
|
||||
|
||||
expect(screen.getByRole("status")).toHaveTextContent("The logs store is unreachable right now.")
|
||||
})
|
||||
|
||||
it.each([
|
||||
["forbidden", 403, "You do not have permission to read metrics here."],
|
||||
["limit", 422, "too many points. Choose a shorter range."],
|
||||
["rate-limited", 429, "Too many requests. Try again in a moment."],
|
||||
["timeout", 504, "The store did not answer in time. Choose a shorter range or a narrower filter."],
|
||||
["unavailable", 503, "The service is temporarily unavailable. Try again in a moment."],
|
||||
] as const)("explains %s", (kind, status, text) => {
|
||||
render(<ProxyNotice what="Metrics" error={new ProxyError(kind, status, "too many points")} />)
|
||||
|
||||
expect(screen.getByRole("alert")).toHaveTextContent(text)
|
||||
})
|
||||
|
||||
it.each([
|
||||
"the application has more than 50 pods in this window; use a shorter range",
|
||||
"1200 points per series exceed the limit of 1000; raise step",
|
||||
])("does not add advice to a limit message that already carries its own: %s", (message) => {
|
||||
render(<ProxyNotice what="Metrics" error={new ProxyError("limit", 422, message)} />)
|
||||
|
||||
expect(screen.getByRole("alert").textContent).toBe(`${message}.`)
|
||||
})
|
||||
|
||||
it("still advises a shorter range for a store limit whose own advice is for operators", () => {
|
||||
const message = "the number of matching timeseries exceeds 300000; either narrow down the search or increase -search.max* command-line flag values"
|
||||
render(<ProxyNotice what="Metrics" error={new ProxyError("limit", 422, message)} />)
|
||||
|
||||
expect(screen.getByRole("alert").textContent).toBe(`${message}. Choose a shorter range.`)
|
||||
})
|
||||
|
||||
it("shows the server message for a rejected query", () => {
|
||||
render(<ProxyNotice what="Logs" error={new ProxyError("bad-request", 400, "cannot parse query")} />)
|
||||
|
||||
expect(screen.getByRole("alert")).toHaveTextContent("cannot parse query")
|
||||
})
|
||||
|
||||
it("falls back to the message of an unexpected error", () => {
|
||||
render(<ProxyNotice what="Logs" error={new Error("boom")} />)
|
||||
|
||||
expect(screen.getByRole("alert")).toHaveTextContent("boom")
|
||||
})
|
||||
})
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
import { ProxyError } from "../../lib/observability/proxy.ts"
|
||||
|
||||
interface ProxyNoticeProps {
|
||||
what: "Metrics" | "Logs"
|
||||
error: unknown
|
||||
}
|
||||
|
||||
// Remedies the observability proxy itself appends; vmselect's 422 text carries
|
||||
// advice for operators (raise -search.max* flags), so it still gets ours.
|
||||
const PROXY_REMEDIES = ["; use a shorter range", "; raise step"]
|
||||
|
||||
function describe(what: ProxyNoticeProps["what"], error: unknown): { quiet: boolean; text: string } {
|
||||
const lower = what.toLowerCase()
|
||||
if (!(error instanceof ProxyError)) {
|
||||
return { quiet: false, text: error instanceof Error ? error.message : String(error) }
|
||||
}
|
||||
switch (error.kind) {
|
||||
case "not-available":
|
||||
return { quiet: true, text: `${what} are not available on this cluster.` }
|
||||
case "store-unreachable":
|
||||
return { quiet: true, text: `The ${lower} store is unreachable right now.` }
|
||||
case "unauthorized":
|
||||
return { quiet: false, text: "Your session has expired. Sign in again." }
|
||||
case "forbidden":
|
||||
return { quiet: false, text: `You do not have permission to read ${lower} here.` }
|
||||
case "limit":
|
||||
return {
|
||||
quiet: false,
|
||||
text: PROXY_REMEDIES.some((r) => error.message.endsWith(r)) ? `${error.message}.` : `${error.message}. Choose a shorter range.`,
|
||||
}
|
||||
case "rate-limited":
|
||||
return { quiet: false, text: "Too many requests. Try again in a moment." }
|
||||
case "timeout":
|
||||
return { quiet: false, text: "The store did not answer in time. Choose a shorter range or a narrower filter." }
|
||||
case "unavailable":
|
||||
return { quiet: false, text: "The service is temporarily unavailable. Try again in a moment." }
|
||||
default:
|
||||
return { quiet: false, text: error.message }
|
||||
}
|
||||
}
|
||||
|
||||
export function ProxyNotice({ what, error }: ProxyNoticeProps) {
|
||||
const { quiet, text } = describe(what, error)
|
||||
if (quiet) {
|
||||
return (
|
||||
<p role="status" className="rounded-md border border-slate-200 bg-slate-50 px-4 py-3 text-sm text-slate-600">
|
||||
{text}
|
||||
</p>
|
||||
)
|
||||
}
|
||||
return (
|
||||
<p role="alert" className="rounded-md border border-amber-200 bg-amber-50 px-4 py-3 text-sm text-amber-800">
|
||||
{text}
|
||||
</p>
|
||||
)
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
import { describe, it, expect, vi } from "vitest"
|
||||
import { render, screen, within } from "@testing-library/react"
|
||||
import userEvent from "@testing-library/user-event"
|
||||
import { RangePicker } from "./RangePicker.tsx"
|
||||
|
||||
describe("RangePicker", () => {
|
||||
it("groups the ranges under one name and marks the chosen one pressed", () => {
|
||||
render(<RangePicker options={["1h", "6h"]} value="6h" onChange={() => {}} />)
|
||||
|
||||
const group = screen.getByRole("group", { name: "Time range" })
|
||||
expect(within(group).getByRole("button", { name: "6h" })).toHaveAttribute("aria-pressed", "true")
|
||||
expect(within(group).getByRole("button", { name: "1h" })).toHaveAttribute("aria-pressed", "false")
|
||||
})
|
||||
|
||||
it("reports the range clicked", async () => {
|
||||
const onChange = vi.fn()
|
||||
render(<RangePicker options={["1h", "6h"]} value="6h" onChange={onChange} />)
|
||||
|
||||
await userEvent.click(screen.getByRole("button", { name: "1h" }))
|
||||
|
||||
expect(onChange).toHaveBeenCalledWith("1h")
|
||||
})
|
||||
})
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
import { cn } from "@cozystack/ui"
|
||||
|
||||
export function RangePicker<K extends string>({
|
||||
options,
|
||||
value,
|
||||
onChange,
|
||||
}: {
|
||||
options: readonly K[]
|
||||
value: K
|
||||
onChange: (value: K) => void
|
||||
}) {
|
||||
return (
|
||||
<div role="group" aria-label="Time range" className="inline-flex h-7 items-center rounded-lg border border-slate-300 bg-white p-0.5 shadow-xs">
|
||||
{options.map((key) => (
|
||||
<button
|
||||
key={key}
|
||||
type="button"
|
||||
aria-pressed={value === key}
|
||||
onClick={() => onChange(key)}
|
||||
className={cn(
|
||||
"h-full rounded-md px-2.5 text-xs outline-none focus-visible:ring-2 focus-visible:ring-slate-400 focus-visible:ring-offset-2",
|
||||
value === key ? "bg-slate-900 text-white" : "text-slate-600 hover:bg-slate-100",
|
||||
)}
|
||||
>
|
||||
{key}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
import { describe, it, expect } from "vitest"
|
||||
import { render, screen, within } from "@testing-library/react"
|
||||
import { SeriesTable } from "./SeriesTable.tsx"
|
||||
|
||||
const data = {
|
||||
timestamps: [1_700_000_000, 1_700_000_060],
|
||||
series: [
|
||||
{ label: "db-0", values: [1, 2] },
|
||||
{ label: "db-1", values: [null, 3] },
|
||||
],
|
||||
}
|
||||
|
||||
describe("SeriesTable", () => {
|
||||
it("is a named region the keyboard can reach", () => {
|
||||
render(<SeriesTable id="t" title="Memory" data={data} format={String} />)
|
||||
|
||||
const region = screen.getByRole("region", { name: "Memory data" })
|
||||
expect(region).toHaveAttribute("id", "t")
|
||||
expect(region).toHaveAttribute("tabindex", "0")
|
||||
})
|
||||
|
||||
it("has a time column and one column per series", () => {
|
||||
render(<SeriesTable id="t" title="Memory" data={data} format={String} />)
|
||||
|
||||
expect(screen.getAllByRole("columnheader").map((h) => h.textContent)).toEqual(["Time", "db-0", "db-1"])
|
||||
})
|
||||
|
||||
it("lists every sample newest first in the chart's units", () => {
|
||||
render(<SeriesTable id="t" title="Memory" data={data} format={(v) => `${v} B`} />)
|
||||
|
||||
const rows = screen.getAllByRole("row").slice(1)
|
||||
expect(rows).toHaveLength(2)
|
||||
expect(within(rows[0]).getAllByRole("cell").map((c) => c.textContent)).toEqual(["2 B", "3 B"])
|
||||
expect(within(rows[1]).getAllByRole("cell").map((c) => c.textContent)).toEqual(["1 B", "–"])
|
||||
expect(within(rows[0]).getByRole("rowheader")).toHaveTextContent(new Date(1_700_000_060_000).toLocaleTimeString([], { hour: "2-digit", minute: "2-digit", second: "2-digit" }))
|
||||
})
|
||||
})
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
import { formatSampleTime, type ChartData } from "../../lib/observability/metrics.ts"
|
||||
|
||||
interface SeriesTableProps {
|
||||
id: string
|
||||
title: string
|
||||
data: ChartData
|
||||
format: (value: number) => string
|
||||
}
|
||||
|
||||
export function SeriesTable({ id, title, data, format }: SeriesTableProps) {
|
||||
const newestFirst = data.timestamps.map((t, i) => ({ t, i })).reverse()
|
||||
return (
|
||||
<div
|
||||
id={id}
|
||||
role="region"
|
||||
aria-label={`${title} data`}
|
||||
tabIndex={0}
|
||||
className="mt-3 max-h-64 overflow-auto rounded-md border border-slate-200 outline-none focus-visible:ring-2 focus-visible:ring-slate-400 focus-visible:ring-offset-2"
|
||||
>
|
||||
<table className="w-full text-xs tabular-nums">
|
||||
<caption className="sr-only">{`${title}, newest first`}</caption>
|
||||
<thead className="sticky top-0 bg-slate-50 text-slate-500">
|
||||
<tr>
|
||||
<th scope="col" className="px-3 py-1.5 text-left font-medium">Time</th>
|
||||
{data.series.map((s, j) => (
|
||||
<th key={j} scope="col" className="px-3 py-1.5 text-right font-medium">{s.label}</th>
|
||||
))}
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody className="divide-y divide-slate-100 text-slate-700">
|
||||
{newestFirst.map(({ t, i }) => (
|
||||
<tr key={t}>
|
||||
<th scope="row" className="px-3 py-1 text-left font-normal whitespace-nowrap">{formatSampleTime(t)}</th>
|
||||
{data.series.map((s, j) => {
|
||||
const v = s.values[i]
|
||||
return <td key={j} className="px-3 py-1 text-right">{v === null ? "–" : format(v)}</td>
|
||||
})}
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
+69
@@ -0,0 +1,69 @@
|
||||
import { describe, it, expect, vi, beforeEach } from "vitest"
|
||||
import { render } from "@testing-library/react"
|
||||
|
||||
type LegendValue = (u: unknown, v: number | null, sidx: number, idx: number | null) => string
|
||||
type Opts = { series: { label?: string; value?: LegendValue }[] }
|
||||
|
||||
const created: { opts: Opts; data: unknown[]; destroy: ReturnType<typeof vi.fn> }[] = []
|
||||
|
||||
vi.mock("uplot", () => ({
|
||||
default: class {
|
||||
destroy = vi.fn()
|
||||
constructor(opts: Opts, data: unknown[]) {
|
||||
created.push({ opts, data, destroy: this.destroy })
|
||||
}
|
||||
setSize() {}
|
||||
},
|
||||
}))
|
||||
|
||||
const { TimeSeriesChart } = await import("./TimeSeriesChart.tsx")
|
||||
const { formatSampleTime } = await import("../../lib/observability/metrics.ts")
|
||||
|
||||
beforeEach(() => {
|
||||
created.length = 0
|
||||
})
|
||||
|
||||
describe("TimeSeriesChart", () => {
|
||||
const data = {
|
||||
timestamps: [1, 2],
|
||||
series: [
|
||||
{ label: "db-0", values: [1, 2] },
|
||||
{ label: "db-1", values: [null, 3] },
|
||||
],
|
||||
}
|
||||
|
||||
it("hands uPlot the timestamps first and one labelled series per input", () => {
|
||||
render(<TimeSeriesChart data={data} format={String} />)
|
||||
|
||||
expect(created).toHaveLength(1)
|
||||
expect(created[0].data).toEqual([[1, 2], [1, 2], [null, 3]])
|
||||
expect(created[0].opts.series.slice(1).map((s) => s.label)).toEqual(["db-0", "db-1"])
|
||||
})
|
||||
|
||||
it("shows each series' latest value in the legend while nothing is hovered", () => {
|
||||
render(<TimeSeriesChart data={{ ...data, timestamps: [1, 2, 3], series: [{ label: "db-0", values: [1, 2, null] }] }} format={(v) => `${v} B`} />)
|
||||
|
||||
const legend = created[0].opts.series[1].value!
|
||||
|
||||
expect(legend(null, null, 1, null)).toBe("2 B")
|
||||
expect(legend(null, 1, 1, 0)).toBe("1 B")
|
||||
expect(legend(null, null, 1, 2)).toBe("–")
|
||||
})
|
||||
|
||||
it("labels the legend's time as latest while nothing is hovered", () => {
|
||||
render(<TimeSeriesChart data={data} format={String} />)
|
||||
|
||||
const time = created[0].opts.series[0].value!
|
||||
|
||||
expect(time(null, null, 0, null)).toBe("latest")
|
||||
expect(time(null, 2, 0, 1)).toBe(formatSampleTime(2))
|
||||
})
|
||||
|
||||
it("destroys the chart on unmount", () => {
|
||||
const { unmount } = render(<TimeSeriesChart data={data} format={String} />)
|
||||
|
||||
unmount()
|
||||
|
||||
expect(created[0].destroy).toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
import { useEffect, useRef } from "react"
|
||||
import uPlot from "uplot"
|
||||
import "uplot/dist/uPlot.min.css"
|
||||
import { formatSampleTime, type ChartData } from "../../lib/observability/metrics.ts"
|
||||
|
||||
const COLORS = ["#2563eb", "#16a34a", "#d97706", "#9333ea", "#dc2626", "#0891b2", "#4b5563", "#db2777"]
|
||||
const HEIGHT = 200
|
||||
|
||||
interface TimeSeriesChartProps {
|
||||
data: ChartData
|
||||
format: (value: number) => string
|
||||
}
|
||||
|
||||
export function TimeSeriesChart({ data, format }: TimeSeriesChartProps) {
|
||||
const ref = useRef<HTMLDivElement>(null)
|
||||
|
||||
useEffect(() => {
|
||||
const el = ref.current
|
||||
if (!el) return
|
||||
const fmt = (v: number | null) => (v === null ? "–" : format(v))
|
||||
const latest = data.series.map((s) => s.values.findLast((v) => v !== null) ?? null)
|
||||
const chart = new uPlot(
|
||||
{
|
||||
width: el.clientWidth || 600,
|
||||
height: HEIGHT,
|
||||
scales: { x: { time: true } },
|
||||
legend: { live: true },
|
||||
series: [
|
||||
{ value: (_: uPlot, v: number | null, _sidx: number, idx: number | null) => (idx === null || v === null ? "latest" : formatSampleTime(v)) },
|
||||
...data.series.map((s, i) => ({
|
||||
label: s.label,
|
||||
stroke: COLORS[i % COLORS.length],
|
||||
width: 1.5,
|
||||
// Without a hovered point the legend shows the latest sample instead of a dash.
|
||||
value: (_: uPlot, v: number | null, sidx: number, idx: number | null) => fmt(idx === null ? latest[sidx - 1] : v),
|
||||
})),
|
||||
],
|
||||
axes: [{}, { size: 84, values: (_: uPlot, splits: number[]) => splits.map(format) }],
|
||||
},
|
||||
[data.timestamps, ...data.series.map((s) => s.values)],
|
||||
el,
|
||||
)
|
||||
if (typeof ResizeObserver === "undefined") return () => chart.destroy()
|
||||
const observer = new ResizeObserver(() => chart.setSize({ width: el.clientWidth, height: HEIGHT }))
|
||||
observer.observe(el)
|
||||
return () => {
|
||||
observer.disconnect()
|
||||
chart.destroy()
|
||||
}
|
||||
}, [data, format])
|
||||
|
||||
return <div ref={ref} className="w-full" />
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
import { describe, it, expect, vi } from "vitest"
|
||||
import { render, screen } from "@testing-library/react"
|
||||
import userEvent from "@testing-library/user-event"
|
||||
import { Input } from "./input.tsx"
|
||||
|
||||
describe("Input", () => {
|
||||
it("is a labelled text box that reports what is typed", async () => {
|
||||
const onValueChange = vi.fn()
|
||||
render(<Input aria-label="Filter" defaultValue="" onValueChange={onValueChange} />)
|
||||
|
||||
await userEvent.type(screen.getByRole("textbox", { name: "Filter" }), "x")
|
||||
|
||||
expect(onValueChange).toHaveBeenCalledWith("x", expect.anything())
|
||||
})
|
||||
|
||||
it("keeps the caller's classes next to its own", () => {
|
||||
render(<Input aria-label="Filter" className="font-mono" />)
|
||||
|
||||
expect(screen.getByRole("textbox", { name: "Filter" })).toHaveClass("font-mono", "rounded-lg")
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,17 @@
|
||||
import { Input as InputPrimitive } from "@base-ui/react/input"
|
||||
import { cn } from "@cozystack/ui"
|
||||
|
||||
function Input({ className, ...props }: InputPrimitive.Props) {
|
||||
return (
|
||||
<InputPrimitive
|
||||
data-slot="input"
|
||||
className={cn(
|
||||
"h-7 w-full min-w-0 rounded-lg border border-slate-300 bg-white px-2.5 text-xs text-slate-900 shadow-xs outline-none transition-colors placeholder:text-slate-400 focus-visible:ring-2 focus-visible:ring-slate-400 focus-visible:ring-offset-2 data-[disabled]:pointer-events-none data-[disabled]:opacity-50",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
export { Input }
|
||||
+64
@@ -0,0 +1,64 @@
|
||||
import { describe, it, expect, vi } from "vitest"
|
||||
import { render, screen } from "@testing-library/react"
|
||||
import userEvent from "@testing-library/user-event"
|
||||
import { SelectItem, SelectLabel, SelectPopup, SelectRoot, SelectTrigger, SelectValue } from "./select.tsx"
|
||||
|
||||
const items = [
|
||||
{ value: "", label: "All" },
|
||||
{ value: "db-0", label: "db-0" },
|
||||
{ value: "db-1", label: "db-1" },
|
||||
]
|
||||
|
||||
function PodSelect({ value = "", onValueChange = () => {} }: { value?: string; onValueChange?: (v: string) => void }) {
|
||||
return (
|
||||
<SelectRoot items={items} value={value} onValueChange={(v) => onValueChange(v ?? "")}>
|
||||
<SelectLabel>Pod</SelectLabel>
|
||||
<SelectTrigger>
|
||||
<SelectValue />
|
||||
</SelectTrigger>
|
||||
<SelectPopup>
|
||||
{items.map((i) => (
|
||||
<SelectItem key={i.value} value={i.value}>
|
||||
{i.label}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectPopup>
|
||||
</SelectRoot>
|
||||
)
|
||||
}
|
||||
|
||||
describe("Select", () => {
|
||||
it("names the trigger by its label and shows the chosen item", () => {
|
||||
render(<PodSelect value="db-1" />)
|
||||
|
||||
expect(screen.getByRole("combobox", { name: "Pod" })).toHaveTextContent("db-1")
|
||||
})
|
||||
|
||||
it("shows the label of an empty-string value", () => {
|
||||
render(<PodSelect />)
|
||||
|
||||
expect(screen.getByRole("combobox", { name: "Pod" })).toHaveTextContent("All")
|
||||
})
|
||||
|
||||
it("lists the items and reports the one clicked", async () => {
|
||||
const onValueChange = vi.fn()
|
||||
render(<PodSelect onValueChange={onValueChange} />)
|
||||
|
||||
await userEvent.click(screen.getByRole("combobox", { name: "Pod" }))
|
||||
await userEvent.click(await screen.findByRole("option", { name: "db-1" }))
|
||||
|
||||
expect(onValueChange).toHaveBeenCalledWith("db-1")
|
||||
})
|
||||
|
||||
it("can be driven from the keyboard", async () => {
|
||||
const onValueChange = vi.fn()
|
||||
render(<PodSelect onValueChange={onValueChange} />)
|
||||
|
||||
screen.getByRole("combobox", { name: "Pod" }).focus()
|
||||
await userEvent.keyboard("{ArrowDown}")
|
||||
expect(await screen.findByRole("listbox")).toBeInTheDocument()
|
||||
await userEvent.keyboard("{ArrowDown}{Enter}")
|
||||
|
||||
expect(onValueChange).toHaveBeenCalledWith("db-0")
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,107 @@
|
||||
import { Select as SelectPrimitive } from "@base-ui/react/select"
|
||||
import { Check, ChevronDown } from "lucide-react"
|
||||
import { cn } from "@cozystack/ui"
|
||||
|
||||
const SelectRoot = SelectPrimitive.Root
|
||||
|
||||
function SelectLabel({
|
||||
className,
|
||||
...props
|
||||
}: SelectPrimitive.Label.Props) {
|
||||
return (
|
||||
<SelectPrimitive.Label
|
||||
data-slot="select-label"
|
||||
className={cn("text-xs text-slate-600", className)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function SelectTrigger({
|
||||
className,
|
||||
children,
|
||||
...props
|
||||
}: SelectPrimitive.Trigger.Props) {
|
||||
return (
|
||||
<SelectPrimitive.Trigger
|
||||
data-slot="select-trigger"
|
||||
className={cn(
|
||||
"inline-flex h-7 min-w-0 items-center justify-between gap-1.5 rounded-lg border border-slate-300 bg-white px-2 text-xs text-slate-900 shadow-xs outline-none transition-colors select-none hover:bg-slate-50 focus-visible:ring-2 focus-visible:ring-slate-400 focus-visible:ring-offset-2 data-[disabled]:pointer-events-none data-[disabled]:opacity-50 data-[popup-open]:bg-slate-50",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
>
|
||||
{children}
|
||||
<SelectPrimitive.Icon className="shrink-0 text-slate-400">
|
||||
<ChevronDown className="size-3.5" />
|
||||
</SelectPrimitive.Icon>
|
||||
</SelectPrimitive.Trigger>
|
||||
)
|
||||
}
|
||||
|
||||
function SelectValue({
|
||||
className,
|
||||
...props
|
||||
}: SelectPrimitive.Value.Props) {
|
||||
return (
|
||||
<SelectPrimitive.Value
|
||||
data-slot="select-value"
|
||||
className={cn("truncate", className)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function SelectPopup({
|
||||
className,
|
||||
children,
|
||||
...props
|
||||
}: SelectPrimitive.Popup.Props) {
|
||||
return (
|
||||
<SelectPrimitive.Portal>
|
||||
<SelectPrimitive.Positioner className="z-50 outline-none" sideOffset={4} alignItemWithTrigger={false}>
|
||||
<SelectPrimitive.Popup
|
||||
data-slot="select-popup"
|
||||
className={cn(
|
||||
"max-h-[min(var(--available-height),20rem)] max-w-[min(var(--available-width),32rem)] min-w-[var(--anchor-width)] overflow-y-auto rounded-lg bg-white py-1 text-xs text-slate-900 shadow-lg ring-1 ring-slate-900/8 outline-none",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
>
|
||||
<SelectPrimitive.List>{children}</SelectPrimitive.List>
|
||||
</SelectPrimitive.Popup>
|
||||
</SelectPrimitive.Positioner>
|
||||
</SelectPrimitive.Portal>
|
||||
)
|
||||
}
|
||||
|
||||
function SelectItem({
|
||||
className,
|
||||
children,
|
||||
...props
|
||||
}: SelectPrimitive.Item.Props) {
|
||||
return (
|
||||
<SelectPrimitive.Item
|
||||
data-slot="select-item"
|
||||
className={cn(
|
||||
"grid cursor-default grid-cols-[1rem_1fr] items-center gap-1.5 px-2 py-1.5 outline-none select-none data-[highlighted]:bg-slate-100 data-[selected]:font-medium",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
>
|
||||
<SelectPrimitive.ItemIndicator className="col-start-1">
|
||||
<Check className="size-3.5" />
|
||||
</SelectPrimitive.ItemIndicator>
|
||||
<SelectPrimitive.ItemText className="col-start-2 break-all">{children}</SelectPrimitive.ItemText>
|
||||
</SelectPrimitive.Item>
|
||||
)
|
||||
}
|
||||
|
||||
export {
|
||||
SelectRoot,
|
||||
SelectLabel,
|
||||
SelectTrigger,
|
||||
SelectValue,
|
||||
SelectPopup,
|
||||
SelectItem,
|
||||
}
|
||||
+89
@@ -0,0 +1,89 @@
|
||||
import { describe, it, expect } from "vitest"
|
||||
import { buildLogsQuery, compareTime, mergeTail } from "./logs.ts"
|
||||
|
||||
describe("buildLogsQuery", () => {
|
||||
it("passes the filter through when nothing is picked", () => {
|
||||
expect(buildLogsQuery({ filter: "error or warn" })).toBe("error or warn")
|
||||
expect(buildLogsQuery({ filter: " " })).toBe("*")
|
||||
})
|
||||
|
||||
it("ANDs the picked pod and container as a stream filter around the whole filter", () => {
|
||||
expect(buildLogsQuery({ filter: "error or warn", pod: "db-0", container: "postgres" })).toBe(
|
||||
'{kubernetes_pod_name="db-0",kubernetes_container_name="postgres"} (error or warn)',
|
||||
)
|
||||
})
|
||||
|
||||
it("keeps the user's pipes after the scoped filter", () => {
|
||||
expect(buildLogsQuery({ filter: 'a or "x|y" | fields _msg', pod: "db-0" })).toBe(
|
||||
'{kubernetes_pod_name="db-0"} (a or "x|y") | fields _msg',
|
||||
)
|
||||
})
|
||||
|
||||
it("escapes quotes in picked values", () => {
|
||||
expect(buildLogsQuery({ filter: "", pod: 'a"b' })).toBe('{kubernetes_pod_name="a\\"b"} (*)')
|
||||
})
|
||||
})
|
||||
|
||||
describe("compareTime", () => {
|
||||
it("orders RFC3339 times with differing fraction lengths", () => {
|
||||
expect(compareTime("2026-11-04T10:00:01Z", "2026-11-04T10:00:01.1Z")).toBeLessThan(0)
|
||||
expect(compareTime("2026-11-04T10:00:01.5Z", "2026-11-04T10:00:01.123Z")).toBeGreaterThan(0)
|
||||
expect(compareTime("2026-11-04T10:00:01.10Z", "2026-11-04T10:00:01.1Z")).toBe(0)
|
||||
})
|
||||
})
|
||||
|
||||
const e = (time: string, msg: string, stream = "s1") => ({ _time: time, _msg: msg, _stream_id: stream })
|
||||
|
||||
describe("mergeTail", () => {
|
||||
it("appends and keeps only the newest lines over the cap", () => {
|
||||
const out = mergeTail([e("t1", "a"), e("t2", "b")], [e("t3", "c")], 2)
|
||||
|
||||
expect(out.map((x) => x._msg)).toEqual(["b", "c"])
|
||||
})
|
||||
|
||||
it("drops replayed lines at or before the resume point", () => {
|
||||
const buffer = [e("2026-01-01T00:00:01Z", "a"), e("2026-01-01T00:00:02Z", "b")]
|
||||
|
||||
const out = mergeTail(buffer, [e("2026-01-01T00:00:02Z", "b"), e("2026-01-01T00:00:03Z", "c")], 10, "2026-01-01T00:00:02Z")
|
||||
|
||||
expect(out.map((x) => x._msg)).toEqual(["a", "b", "c"])
|
||||
})
|
||||
|
||||
it("keeps a line at the resume point from another stream", () => {
|
||||
const buffer = [e("2026-01-01T00:00:02Z", "b", "s1")]
|
||||
|
||||
const out = mergeTail(buffer, [e("2026-01-01T00:00:02Z", "other", "s2")], 10, "2026-01-01T00:00:02Z")
|
||||
|
||||
expect(out).toHaveLength(2)
|
||||
})
|
||||
|
||||
it("never deduplicates lines without _time, even on a resume", () => {
|
||||
const out = mergeTail([{ _msg: "a" }], [{ _msg: "a" }], 10, "2026-01-01T00:00:02Z")
|
||||
|
||||
expect(out).toHaveLength(2)
|
||||
})
|
||||
|
||||
it("never deduplicates lines a pipe stripped of _stream_id, even on a resume", () => {
|
||||
const t = "2026-01-01T00:00:02Z"
|
||||
const buffer = [{ _time: t, _msg: "from stream 1" }]
|
||||
|
||||
const out = mergeTail(buffer, [{ _time: t, _msg: "from stream 2" }], 10, t)
|
||||
|
||||
expect(out.map((x) => x._msg)).toEqual(["from stream 1", "from stream 2"])
|
||||
})
|
||||
|
||||
it("deduplicates only the complete rows of a mixed resume", () => {
|
||||
const t = "2026-01-01T00:00:02Z"
|
||||
const buffer = [e(t, "seen"), { _time: t, _msg: "no stream" }, { _stream_id: "s1", _msg: "no time" }]
|
||||
|
||||
const out = mergeTail(buffer, [e(t, "seen"), { _time: t, _msg: "no stream" }, { _stream_id: "s1", _msg: "no time" }], 10, t)
|
||||
|
||||
expect(out.map((x) => x._msg)).toEqual(["seen", "no stream", "no time", "no stream", "no time"])
|
||||
})
|
||||
|
||||
it("does not deduplicate outside a resume", () => {
|
||||
const out = mergeTail([e("t1", "a")], [e("t1", "a")], 10)
|
||||
|
||||
expect(out).toHaveLength(2)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,79 @@
|
||||
import type { LogEntry } from "./proxy.ts"
|
||||
|
||||
export const LOG_RANGES = {
|
||||
"15m": 15 * 60,
|
||||
"1h": 3600,
|
||||
"6h": 6 * 3600,
|
||||
"24h": 24 * 3600,
|
||||
} as const
|
||||
|
||||
export type LogRangeKey = keyof typeof LOG_RANGES
|
||||
|
||||
export const POD_FIELD = "kubernetes_pod_name"
|
||||
export const CONTAINER_FIELD = "kubernetes_container_name"
|
||||
|
||||
function quote(value: string): string {
|
||||
return `"${value.replace(/\\/g, "\\\\").replace(/"/g, '\\"')}"`
|
||||
}
|
||||
|
||||
function splitAtFirstPipe(query: string): [string, string] {
|
||||
let quoteChar = ""
|
||||
for (let i = 0; i < query.length; i++) {
|
||||
const c = query[i]
|
||||
if (quoteChar) {
|
||||
if (c === "\\") i++
|
||||
else if (c === quoteChar) quoteChar = ""
|
||||
} else if (c === '"' || c === "'" || c === "`") {
|
||||
quoteChar = c
|
||||
} else if (c === "|") {
|
||||
return [query.slice(0, i), query.slice(i + 1)]
|
||||
}
|
||||
}
|
||||
return [query, ""]
|
||||
}
|
||||
|
||||
// The picker narrows the filter part only: AND binds tighter than OR in
|
||||
// LogsQL, and the user's pipes must still run after the narrowed filter.
|
||||
export function buildLogsQuery({ filter, pod, container }: { filter: string; pod?: string; container?: string }): string {
|
||||
const trimmed = filter.trim() || "*"
|
||||
const matchers = [
|
||||
pod ? `${POD_FIELD}=${quote(pod)}` : "",
|
||||
container ? `${CONTAINER_FIELD}=${quote(container)}` : "",
|
||||
].filter(Boolean)
|
||||
if (matchers.length === 0) return trimmed
|
||||
const [head, pipes] = splitAtFirstPipe(trimmed)
|
||||
const scoped = `{${matchers.join(",")}} (${head.trim() || "*"})`
|
||||
return pipes.trim() ? `${scoped} | ${pipes.trim()}` : scoped
|
||||
}
|
||||
|
||||
// VictoriaLogs trims trailing zeros from the fraction, so the strings do not sort lexically.
|
||||
function padFraction(time: string): string {
|
||||
const m = /^([^.Z]+)(?:\.(\d+))?Z$/.exec(time)
|
||||
return m ? `${m[1]}.${(m[2] ?? "").padEnd(9, "0")}Z` : time
|
||||
}
|
||||
|
||||
export function compareTime(a: string, b: string): number {
|
||||
const x = padFraction(a)
|
||||
const y = padFraction(b)
|
||||
return x < y ? -1 : x > y ? 1 : 0
|
||||
}
|
||||
|
||||
// Null when a pipe removed either identity field: such a row cannot be told from another stream's.
|
||||
const entryKey = (e: LogEntry) =>
|
||||
e._stream_id !== undefined && e._time !== undefined ? `${e._stream_id}\u0000${e._time}` : null
|
||||
|
||||
// (_stream_id, _time) is not unique in VictoriaLogs, so dedup applies only to
|
||||
// lines a resumed tail replays at or before the resume point, and only to
|
||||
// lines that still carry both fields.
|
||||
export function mergeTail(buffer: LogEntry[], incoming: LogEntry[], cap: number, resumedFrom?: string): LogEntry[] {
|
||||
let fresh = incoming
|
||||
if (resumedFrom) {
|
||||
const seen = new Set(buffer.map(entryKey).filter((k) => k !== null))
|
||||
fresh = incoming.filter((e) => {
|
||||
const key = entryKey(e)
|
||||
return key === null || compareTime(e._time ?? "", resumedFrom) > 0 || !seen.has(key)
|
||||
})
|
||||
}
|
||||
const merged = buffer.concat(fresh)
|
||||
return merged.length > cap ? merged.slice(merged.length - cap) : merged
|
||||
}
|
||||
+116
@@ -0,0 +1,116 @@
|
||||
import { describe, it, expect } from "vitest"
|
||||
import {
|
||||
METRIC_PANELS,
|
||||
formatBytes,
|
||||
formatCores,
|
||||
rangeWindow,
|
||||
toChartData,
|
||||
} from "./metrics.ts"
|
||||
|
||||
describe("rangeWindow", () => {
|
||||
it("ends now and spans the range in about 240 points", () => {
|
||||
const w = rangeWindow("6h", 1_000_000)
|
||||
|
||||
expect(w.end).toBe(1_000_000)
|
||||
expect(w.start).toBe(1_000_000 - 6 * 3600)
|
||||
expect(w.step).toBe(90)
|
||||
})
|
||||
|
||||
it("never steps finer than 15s", () => {
|
||||
expect(rangeWindow("1h", 0).step).toBe(15)
|
||||
})
|
||||
})
|
||||
|
||||
describe("METRIC_PANELS", () => {
|
||||
const window = { start: 0, end: 3600, step: 15 }
|
||||
|
||||
it("rates CPU over at least five minutes so sparse scrapes still draw", () => {
|
||||
const [query] = METRIC_PANELS.find((p) => p.id === "cpu")!.queries(window)
|
||||
|
||||
expect(query.expr).toBe(
|
||||
'sum by (pod) (rate(container_cpu_usage_seconds_total{container!="",container!="POD"}[300s]))',
|
||||
)
|
||||
})
|
||||
|
||||
// Long ranges are served by the longterm store, deduplicated at 5m, where a
|
||||
// narrower window can hold no samples and the rate comes back empty.
|
||||
it.each([
|
||||
[15, "300s"],
|
||||
[75, "300s"],
|
||||
[90, "360s"],
|
||||
[2520, "10080s"],
|
||||
])("rates over max(5m, 4 x step) for step %is", (step, window) => {
|
||||
for (const panel of METRIC_PANELS) {
|
||||
for (const q of panel.queries({ start: 0, end: 1, step })) {
|
||||
if (q.expr.includes("rate(")) expect(q.expr).toContain(`[${window}]`)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
it("reads memory as working set per pod", () => {
|
||||
const [query] = METRIC_PANELS.find((p) => p.id === "memory")!.queries(window)
|
||||
|
||||
expect(query.expr).toBe('sum by (pod) (container_memory_working_set_bytes{container!="",container!="POD"})')
|
||||
})
|
||||
|
||||
it("draws network receive and transmit as two named series", () => {
|
||||
const queries = METRIC_PANELS.find((p) => p.id === "network")!.queries(window)
|
||||
|
||||
expect(queries.map((q) => q.label)).toEqual(["receive", "transmit"])
|
||||
expect(queries[0].expr).toBe("sum(rate(container_network_receive_bytes_total[300s]))")
|
||||
expect(queries[1].expr).toBe("sum(rate(container_network_transmit_bytes_total[300s]))")
|
||||
})
|
||||
|
||||
it("names no application kind in any query", () => {
|
||||
for (const panel of METRIC_PANELS) {
|
||||
for (const q of panel.queries(window)) expect(q.expr).not.toMatch(/apps_cozystack_io|namespace=/)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
describe("toChartData", () => {
|
||||
it("aligns series on the union of timestamps and leaves gaps as null", () => {
|
||||
const data = toChartData([
|
||||
{ label: "a", series: [{ metric: {}, values: [[10, "1"], [20, "2"]] }] },
|
||||
{ label: "b", series: [{ metric: {}, values: [[20, "5"], [30, "6"]] }] },
|
||||
])
|
||||
|
||||
expect(data.timestamps).toEqual([10, 20, 30])
|
||||
expect(data.series).toEqual([
|
||||
{ label: "a", values: [1, 2, null] },
|
||||
{ label: "b", values: [null, 5, 6] },
|
||||
])
|
||||
})
|
||||
|
||||
it("labels each series by its pod, falling back to the query label", () => {
|
||||
const data = toChartData([
|
||||
{
|
||||
label: "cpu",
|
||||
series: [
|
||||
{ metric: { pod: "db-1" }, values: [[1, "0.5"]] },
|
||||
{ metric: {}, values: [[1, "0.1"]] },
|
||||
],
|
||||
},
|
||||
])
|
||||
|
||||
expect(data.series.map((s) => s.label)).toEqual(["db-1", "cpu"])
|
||||
})
|
||||
|
||||
it("drops NaN samples instead of drawing them as zero", () => {
|
||||
const data = toChartData([{ label: "x", series: [{ metric: {}, values: [[1, "NaN"]] }] }])
|
||||
|
||||
expect(data.series[0].values).toEqual([null])
|
||||
})
|
||||
})
|
||||
|
||||
describe("formatters", () => {
|
||||
it("formats CPU in millicores below one core", () => {
|
||||
expect(formatCores(0.25)).toBe("250m")
|
||||
expect(formatCores(2)).toBe("2")
|
||||
})
|
||||
|
||||
it("formats bytes with binary units", () => {
|
||||
expect(formatBytes(512)).toBe("512 B")
|
||||
expect(formatBytes(3 * 1024 * 1024)).toBe("3.0 MiB")
|
||||
})
|
||||
})
|
||||
+125
@@ -0,0 +1,125 @@
|
||||
import type { PromSeries } from "./proxy.ts"
|
||||
|
||||
export const RANGES = {
|
||||
"1h": 3600,
|
||||
"6h": 6 * 3600,
|
||||
"24h": 24 * 3600,
|
||||
"7d": 7 * 24 * 3600,
|
||||
} as const
|
||||
|
||||
export type RangeKey = keyof typeof RANGES
|
||||
|
||||
export interface RangeWindow {
|
||||
start: number
|
||||
end: number
|
||||
step: number
|
||||
}
|
||||
|
||||
const POINTS = 240
|
||||
const MIN_STEP = 15
|
||||
|
||||
export function rangeWindow(range: RangeKey, nowSeconds: number): RangeWindow {
|
||||
const span = RANGES[range]
|
||||
return { start: nowSeconds - span, end: nowSeconds, step: Math.max(MIN_STEP, Math.round(span / POINTS)) }
|
||||
}
|
||||
|
||||
// The longterm store keeps one sample per 5m, so a narrower window can be empty;
|
||||
// 4 x step also keeps samples between points from being skipped.
|
||||
function rateWindow(step: number): string {
|
||||
return `${Math.max(300, step * 4)}s`
|
||||
}
|
||||
|
||||
const CONTAINERS = 'container!="",container!="POD"'
|
||||
|
||||
export interface MetricQuery {
|
||||
label: string
|
||||
expr: string
|
||||
}
|
||||
|
||||
export interface MetricPanel {
|
||||
id: "cpu" | "memory" | "network"
|
||||
title: string
|
||||
format: (value: number) => string
|
||||
queries: (window: RangeWindow) => MetricQuery[]
|
||||
}
|
||||
|
||||
export function formatCores(value: number): string {
|
||||
if (value === 0) return "0"
|
||||
if (Math.abs(value) < 1) return `${Math.round(value * 1000)}m`
|
||||
return `${Number(value.toFixed(2))}`
|
||||
}
|
||||
|
||||
const BYTE_UNITS = ["B", "KiB", "MiB", "GiB", "TiB"]
|
||||
|
||||
export function formatBytes(value: number): string {
|
||||
let v = value
|
||||
let unit = 0
|
||||
while (Math.abs(v) >= 1024 && unit < BYTE_UNITS.length - 1) {
|
||||
v /= 1024
|
||||
unit++
|
||||
}
|
||||
return unit === 0 ? `${Math.round(v)} B` : `${v.toFixed(1)} ${BYTE_UNITS[unit]}`
|
||||
}
|
||||
|
||||
// The proxy injects the namespace or application scope, so these stay kind-agnostic.
|
||||
export const METRIC_PANELS: MetricPanel[] = [
|
||||
{
|
||||
id: "cpu",
|
||||
title: "CPU (cores)",
|
||||
format: formatCores,
|
||||
queries: ({ step }) => [
|
||||
{
|
||||
label: "cpu",
|
||||
expr: `sum by (pod) (rate(container_cpu_usage_seconds_total{${CONTAINERS}}[${rateWindow(step)}]))`,
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
id: "memory",
|
||||
title: "Memory (working set)",
|
||||
format: formatBytes,
|
||||
queries: () => [
|
||||
{ label: "memory", expr: `sum by (pod) (container_memory_working_set_bytes{${CONTAINERS}})` },
|
||||
],
|
||||
},
|
||||
{
|
||||
id: "network",
|
||||
title: "Network",
|
||||
format: (v) => `${formatBytes(v)}/s`,
|
||||
queries: ({ step }) => [
|
||||
{ label: "receive", expr: `sum(rate(container_network_receive_bytes_total[${rateWindow(step)}]))` },
|
||||
{ label: "transmit", expr: `sum(rate(container_network_transmit_bytes_total[${rateWindow(step)}]))` },
|
||||
],
|
||||
},
|
||||
]
|
||||
|
||||
export function formatSampleTime(seconds: number): string {
|
||||
return new Date(seconds * 1000).toLocaleString([], { month: "short", day: "numeric", hour: "2-digit", minute: "2-digit", second: "2-digit" })
|
||||
}
|
||||
|
||||
export interface ChartSeries {
|
||||
label: string
|
||||
values: (number | null)[]
|
||||
}
|
||||
|
||||
export interface ChartData {
|
||||
timestamps: number[]
|
||||
series: ChartSeries[]
|
||||
}
|
||||
|
||||
export function toChartData(results: { label: string; series: PromSeries[] }[]): ChartData {
|
||||
const flat = results.flatMap(({ label, series }) =>
|
||||
series.map((s) => ({ label: s.metric.pod ?? label, points: new Map(s.values.map(([t, v]) => [t, Number(v)])) })),
|
||||
)
|
||||
const timestamps = [...new Set(flat.flatMap((s) => [...s.points.keys()]))].sort((a, b) => a - b)
|
||||
return {
|
||||
timestamps,
|
||||
series: flat.map(({ label, points }) => ({
|
||||
label,
|
||||
values: timestamps.map((t) => {
|
||||
const v = points.get(t)
|
||||
return v === undefined || Number.isNaN(v) ? null : v
|
||||
}),
|
||||
})),
|
||||
}
|
||||
}
|
||||
+242
@@ -0,0 +1,242 @@
|
||||
import { describe, it, expect, vi } from "vitest"
|
||||
import { K8sClient } from "@cozystack/k8s-client"
|
||||
import {
|
||||
ProxyError,
|
||||
errorKindForStatus,
|
||||
fieldValues,
|
||||
isScopeWide,
|
||||
logsRoot,
|
||||
metricsRoot,
|
||||
queryLogs,
|
||||
queryRange,
|
||||
tailLogs,
|
||||
} from "./proxy.ts"
|
||||
|
||||
const app = { namespace: "tenant-a", plural: "postgreses", name: "db" }
|
||||
const ns = { namespace: "tenant-a" }
|
||||
|
||||
function clientReturning(...responses: Response[]) {
|
||||
const client = new K8sClient({ onUnauthorized: () => {} })
|
||||
const raw = vi.spyOn(client, "raw")
|
||||
for (const r of responses) raw.mockResolvedValueOnce(r)
|
||||
return { client, raw }
|
||||
}
|
||||
|
||||
function calledUrl(raw: ReturnType<typeof clientReturning>["raw"], call = 0): URL {
|
||||
return new URL(raw.mock.calls[call][0], "http://x")
|
||||
}
|
||||
|
||||
describe("roots", () => {
|
||||
it("builds the application roots under apps.cozystack.io", () => {
|
||||
expect(metricsRoot(app)).toBe("/apis/apps.cozystack.io/v1alpha1/namespaces/tenant-a/postgreses/db/metrics/")
|
||||
expect(logsRoot(app)).toBe("/apis/apps.cozystack.io/v1alpha1/namespaces/tenant-a/postgreses/db/log/")
|
||||
})
|
||||
|
||||
it("builds the namespace roots under cozystack.io", () => {
|
||||
expect(metricsRoot(ns)).toBe("/apis/cozystack.io/v1alpha1/namespaces/tenant-a/metrics/")
|
||||
expect(logsRoot(ns)).toBe("/apis/cozystack.io/v1alpha1/namespaces/tenant-a/logs/")
|
||||
})
|
||||
})
|
||||
|
||||
describe("errorKindForStatus", () => {
|
||||
it.each([
|
||||
[400, "bad-request"],
|
||||
[401, "unauthorized"],
|
||||
[403, "forbidden"],
|
||||
[404, "not-available"],
|
||||
[422, "limit"],
|
||||
[429, "rate-limited"],
|
||||
[502, "store-unreachable"],
|
||||
[503, "unavailable"],
|
||||
[504, "timeout"],
|
||||
[500, "unknown"],
|
||||
])("maps %i to %s", (status, kind) => {
|
||||
expect(errorKindForStatus(status)).toBe(kind)
|
||||
})
|
||||
})
|
||||
|
||||
describe("queryRange", () => {
|
||||
it("GETs query_range under the root and returns the matrix", async () => {
|
||||
const body = { status: "success", data: { resultType: "matrix", result: [{ metric: { pod: "p" }, values: [[1, "2"]] }] } }
|
||||
const { client, raw } = clientReturning(Response.json(body))
|
||||
|
||||
const result = await queryRange(client, metricsRoot(app), { query: "up", start: 10, end: 20, step: 5 })
|
||||
|
||||
const url = calledUrl(raw)
|
||||
expect(url.pathname).toBe(`${metricsRoot(app)}api/v1/query_range`)
|
||||
expect(Object.fromEntries(url.searchParams)).toEqual({ query: "up", start: "10", end: "20", step: "5" })
|
||||
expect(result).toEqual(body.data.result)
|
||||
})
|
||||
|
||||
it("throws a ProxyError carrying the Prometheus error text", async () => {
|
||||
const { client } = clientReturning(
|
||||
Response.json({ status: "error", error: "range too long" }, { status: 422 }),
|
||||
)
|
||||
|
||||
const err = await queryRange(client, metricsRoot(app), { query: "up", start: 0, end: 1, step: 1 }).catch((e) => e)
|
||||
|
||||
expect(err).toBeInstanceOf(ProxyError)
|
||||
expect(err).toMatchObject({ kind: "limit", status: 422, message: "range too long" })
|
||||
})
|
||||
|
||||
it("treats a Kubernetes Status 403 as a cluster without the proxy route", async () => {
|
||||
const status = { kind: "Status", apiVersion: "v1", status: "Failure", reason: "Forbidden", code: 403, message: "logs.cozystack.io \"select\" is forbidden" }
|
||||
const { client } = clientReturning(Response.json(status, { status: 403 }))
|
||||
|
||||
const err = await queryRange(client, metricsRoot(ns), { query: "up", start: 0, end: 1, step: 1 }).catch((e) => e)
|
||||
|
||||
expect(err).toMatchObject({ kind: "not-available", status: 403 })
|
||||
})
|
||||
|
||||
it("keeps the proxy's own plain-text 403 as forbidden", async () => {
|
||||
const { client } = clientReturning(new Response("forbidden\n", { status: 403 }))
|
||||
|
||||
const err = await queryRange(client, metricsRoot(ns), { query: "up", start: 0, end: 1, step: 1 }).catch((e) => e)
|
||||
|
||||
expect(err).toMatchObject({ kind: "forbidden" })
|
||||
})
|
||||
|
||||
it("reports a fetch failure as a network error", async () => {
|
||||
const client = new K8sClient()
|
||||
vi.spyOn(client, "raw").mockRejectedValue(new TypeError("offline"))
|
||||
|
||||
const err = await queryRange(client, metricsRoot(app), { query: "up", start: 0, end: 1, step: 1 }).catch((e) => e)
|
||||
|
||||
expect(err).toMatchObject({ kind: "network" })
|
||||
})
|
||||
})
|
||||
|
||||
describe("queryLogs", () => {
|
||||
it("parses NDJSON lines and sorts them by _time ascending", async () => {
|
||||
const ndjson = [
|
||||
JSON.stringify({ _time: "2026-11-04T10:00:02Z", _msg: "b" }),
|
||||
"",
|
||||
JSON.stringify({ _time: "2026-11-04T10:00:01Z", _msg: "a" }),
|
||||
].join("\n")
|
||||
const { client, raw } = clientReturning(new Response(ndjson))
|
||||
|
||||
const entries = await queryLogs(client, logsRoot(ns), { query: "*", start: "s", end: "e", limit: 100 })
|
||||
|
||||
expect(calledUrl(raw).pathname).toBe(`${logsRoot(ns)}select/logsql/query`)
|
||||
expect(Object.fromEntries(calledUrl(raw).searchParams)).toEqual({ query: "*", start: "s", end: "e", limit: "100" })
|
||||
expect(entries.map((e) => e._msg)).toEqual(["a", "b"])
|
||||
})
|
||||
|
||||
it("keeps rows a pipe stripped of _time, in the order the store sent them", async () => {
|
||||
const ndjson = ['{"_msg":"visible message"}', '{"_msg":"second"}', "[1]", "null", "{torn"].join("\n")
|
||||
const { client } = clientReturning(new Response(ndjson))
|
||||
|
||||
const entries = await queryLogs(client, logsRoot(ns), { query: "* | fields _msg", start: "s", end: "e", limit: 100 })
|
||||
|
||||
expect(entries).toEqual([{ _msg: "visible message" }, { _msg: "second" }])
|
||||
})
|
||||
|
||||
it("keeps a plain-text error body as the message", async () => {
|
||||
const { client } = clientReturning(new Response("cannot parse query", { status: 400 }))
|
||||
|
||||
const err = await queryLogs(client, logsRoot(ns), { query: "(", start: "s", end: "e", limit: 1 }).catch((e) => e)
|
||||
|
||||
expect(err).toMatchObject({ kind: "bad-request", message: "cannot parse query" })
|
||||
})
|
||||
})
|
||||
|
||||
describe("fieldValues", () => {
|
||||
it("uses stream_field_values for stream fields and returns the values", async () => {
|
||||
const { client, raw } = clientReturning(
|
||||
Response.json({ values: [{ value: "pod-b", hits: 1 }, { value: "pod-a", hits: 9 }] }),
|
||||
)
|
||||
|
||||
const values = await fieldValues(client, logsRoot(app), {
|
||||
field: "kubernetes_pod_name",
|
||||
stream: true,
|
||||
query: "*",
|
||||
start: "s",
|
||||
end: "e",
|
||||
})
|
||||
|
||||
expect(calledUrl(raw).pathname).toBe(`${logsRoot(app)}select/logsql/stream_field_values`)
|
||||
expect(calledUrl(raw).searchParams.get("field")).toBe("kubernetes_pod_name")
|
||||
expect(values).toEqual(["pod-a", "pod-b"])
|
||||
})
|
||||
|
||||
it("uses field_values for other fields", async () => {
|
||||
const { client, raw } = clientReturning(Response.json({ values: [] }))
|
||||
|
||||
await fieldValues(client, logsRoot(app), { field: "level", stream: false, query: "*", start: "s", end: "e" })
|
||||
|
||||
expect(calledUrl(raw).pathname).toBe(`${logsRoot(app)}select/logsql/field_values`)
|
||||
})
|
||||
})
|
||||
|
||||
describe("tailLogs", () => {
|
||||
it("delivers every line as it arrives, including one split across chunks", async () => {
|
||||
const enc = new TextEncoder()
|
||||
const chunks = ['{"_time":"t1","_msg":"a"}\n{"_ti', 'me":"t2","_msg":"b"}\n']
|
||||
const body = new ReadableStream<Uint8Array>({
|
||||
start(c) {
|
||||
for (const ch of chunks) c.enqueue(enc.encode(ch))
|
||||
c.close()
|
||||
},
|
||||
})
|
||||
const { client, raw } = clientReturning(new Response(body))
|
||||
const seen: (string | undefined)[] = []
|
||||
|
||||
await tailLogs(client, logsRoot(ns), { query: "*", since: "t0" }, (e) => seen.push(e._msg))
|
||||
|
||||
expect(calledUrl(raw).pathname).toBe(`${logsRoot(ns)}select/logsql/tail`)
|
||||
expect(calledUrl(raw).searchParams.get("since")).toBe("t0")
|
||||
expect(seen).toEqual(["a", "b"])
|
||||
})
|
||||
|
||||
it("delivers a line without _time", async () => {
|
||||
const { client } = clientReturning(new Response('{"_msg":"untimed"}\n'))
|
||||
const seen: (string | undefined)[] = []
|
||||
|
||||
await tailLogs(client, logsRoot(ns), { query: "* | fields _msg" }, (e) => seen.push(e._msg))
|
||||
|
||||
expect(seen).toEqual(["untimed"])
|
||||
})
|
||||
|
||||
it("omits since on the first connect", async () => {
|
||||
const { client, raw } = clientReturning(new Response(""))
|
||||
|
||||
await tailLogs(client, logsRoot(ns), { query: "*" }, () => {})
|
||||
|
||||
expect(calledUrl(raw).searchParams.has("since")).toBe(false)
|
||||
})
|
||||
|
||||
it("throws a ProxyError when the tail is refused", async () => {
|
||||
const { client } = clientReturning(new Response("", { status: 403 }))
|
||||
|
||||
const err = await tailLogs(client, logsRoot(ns), { query: "*" }, () => {}).catch((e) => e)
|
||||
|
||||
expect(err).toMatchObject({ kind: "forbidden" })
|
||||
})
|
||||
|
||||
it("says the stream opened before any line arrives", async () => {
|
||||
const { client } = clientReturning(new Response(""))
|
||||
const onOpen = vi.fn()
|
||||
|
||||
await tailLogs(client, logsRoot(ns), { query: "*" }, () => {}, undefined, onOpen)
|
||||
|
||||
expect(onOpen).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
|
||||
it("does not say the stream opened when the tail is refused", async () => {
|
||||
const { client } = clientReturning(new Response("", { status: 429 }))
|
||||
const onOpen = vi.fn()
|
||||
|
||||
await tailLogs(client, logsRoot(ns), { query: "*" }, () => {}, undefined, onOpen).catch(() => {})
|
||||
|
||||
expect(onOpen).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
|
||||
describe("isScopeWide", () => {
|
||||
it("is true for answers that do not depend on the query", () => {
|
||||
expect(isScopeWide(new ProxyError("not-available", 404, ""))).toBe(true)
|
||||
expect(isScopeWide(new ProxyError("forbidden", 403, ""))).toBe(true)
|
||||
expect(isScopeWide(new ProxyError("limit", 422, ""))).toBe(false)
|
||||
expect(isScopeWide(new Error("x"))).toBe(false)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,228 @@
|
||||
import type { K8sClient } from "@cozystack/k8s-client"
|
||||
import { compareTime } from "./logs.ts"
|
||||
|
||||
export type ObservabilityScope =
|
||||
| { namespace: string }
|
||||
| { namespace: string; plural: string; name: string }
|
||||
|
||||
function appBase(scope: ObservabilityScope): string | null {
|
||||
if (!("plural" in scope)) return null
|
||||
return `/apis/apps.cozystack.io/v1alpha1/namespaces/${scope.namespace}/${scope.plural}/${scope.name}`
|
||||
}
|
||||
|
||||
export function metricsRoot(scope: ObservabilityScope): string {
|
||||
const app = appBase(scope)
|
||||
return app ? `${app}/metrics/` : `/apis/cozystack.io/v1alpha1/namespaces/${scope.namespace}/metrics/`
|
||||
}
|
||||
|
||||
export function logsRoot(scope: ObservabilityScope): string {
|
||||
const app = appBase(scope)
|
||||
return app ? `${app}/log/` : `/apis/cozystack.io/v1alpha1/namespaces/${scope.namespace}/logs/`
|
||||
}
|
||||
|
||||
export type ProxyErrorKind =
|
||||
| "bad-request"
|
||||
| "unauthorized"
|
||||
| "forbidden"
|
||||
| "not-available"
|
||||
| "limit"
|
||||
| "rate-limited"
|
||||
| "store-unreachable"
|
||||
| "unavailable"
|
||||
| "timeout"
|
||||
| "network"
|
||||
| "unknown"
|
||||
|
||||
const KIND_BY_STATUS: Record<number, ProxyErrorKind> = {
|
||||
400: "bad-request",
|
||||
401: "unauthorized",
|
||||
403: "forbidden",
|
||||
404: "not-available",
|
||||
422: "limit",
|
||||
429: "rate-limited",
|
||||
502: "store-unreachable",
|
||||
503: "unavailable",
|
||||
504: "timeout",
|
||||
}
|
||||
|
||||
export function errorKindForStatus(status: number): ProxyErrorKind {
|
||||
return KIND_BY_STATUS[status] ?? "unknown"
|
||||
}
|
||||
|
||||
export class ProxyError extends Error {
|
||||
kind: ProxyErrorKind
|
||||
status: number
|
||||
|
||||
constructor(kind: ProxyErrorKind, status: number, message: string) {
|
||||
super(message)
|
||||
this.name = "ProxyError"
|
||||
this.kind = kind
|
||||
this.status = status
|
||||
}
|
||||
}
|
||||
|
||||
// These answers hold for every query in the scope, so one notice replaces the whole view.
|
||||
export function isScopeWide(error: unknown): boolean {
|
||||
return (
|
||||
error instanceof ProxyError &&
|
||||
(error.kind === "not-available" || error.kind === "forbidden" || error.kind === "unauthorized")
|
||||
)
|
||||
}
|
||||
|
||||
// The proxy answers in plain text; a Kubernetes Status body means nginx sent
|
||||
// the path to kube-apiserver, which RBAC-denies or 404s a route it does not serve.
|
||||
function isKubernetesStatus(text: string): boolean {
|
||||
try {
|
||||
const parsed: unknown = JSON.parse(text)
|
||||
return !!parsed && typeof parsed === "object" && "kind" in parsed && parsed.kind === "Status"
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// Prometheus answers {"error": "..."}, VictoriaLogs and nginx answer plain text.
|
||||
function errorMessage(text: string, status: number): string {
|
||||
try {
|
||||
const parsed: unknown = JSON.parse(text)
|
||||
if (parsed && typeof parsed === "object" && "error" in parsed && typeof parsed.error === "string") {
|
||||
return parsed.error
|
||||
}
|
||||
} catch {
|
||||
// not JSON
|
||||
}
|
||||
const trimmed = text.trim()
|
||||
return trimmed && trimmed.length < 300 && !trimmed.startsWith("<") ? trimmed : `Server returned ${status}`
|
||||
}
|
||||
|
||||
async function proxyGet(
|
||||
client: K8sClient,
|
||||
path: string,
|
||||
params: Record<string, string | number | undefined>,
|
||||
signal?: AbortSignal,
|
||||
): Promise<Response> {
|
||||
const search = new URLSearchParams()
|
||||
for (const [k, v] of Object.entries(params)) if (v !== undefined) search.set(k, String(v))
|
||||
let res: Response
|
||||
try {
|
||||
res = await client.raw(`${path}?${search}`, { signal })
|
||||
} catch (err) {
|
||||
if (signal?.aborted) throw err
|
||||
throw new ProxyError("network", 0, "Unable to connect to the server")
|
||||
}
|
||||
if (!res.ok) {
|
||||
const text = await res.text().catch(() => "")
|
||||
const kind = isKubernetesStatus(text) ? "not-available" : errorKindForStatus(res.status)
|
||||
throw new ProxyError(kind, res.status, errorMessage(text, res.status))
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
export interface PromSeries {
|
||||
metric: Record<string, string>
|
||||
values: [number, string][]
|
||||
}
|
||||
|
||||
export interface RangeParams {
|
||||
query: string
|
||||
start: number
|
||||
end: number
|
||||
step: number
|
||||
}
|
||||
|
||||
export async function queryRange(
|
||||
client: K8sClient,
|
||||
root: string,
|
||||
params: RangeParams,
|
||||
signal?: AbortSignal,
|
||||
): Promise<PromSeries[]> {
|
||||
const res = await proxyGet(client, `${root}api/v1/query_range`, { ...params }, signal)
|
||||
const body = (await res.json()) as { data?: { result?: PromSeries[] } }
|
||||
return body.data?.result ?? []
|
||||
}
|
||||
|
||||
// A pipe such as `fields _msg` or `stats` can drop _time and _msg.
|
||||
export interface LogEntry {
|
||||
_time?: string
|
||||
_msg?: string
|
||||
_stream_id?: string
|
||||
[field: string]: string | undefined
|
||||
}
|
||||
|
||||
function parseLine(line: string): LogEntry | null {
|
||||
if (!line.trim()) return null
|
||||
try {
|
||||
const value: unknown = JSON.parse(line)
|
||||
if (value && typeof value === "object" && !Array.isArray(value)) return value as LogEntry
|
||||
} catch {
|
||||
// a torn or non-JSON line carries nothing to show
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
export interface LogsQueryParams {
|
||||
query: string
|
||||
start: string
|
||||
end: string
|
||||
limit: number
|
||||
}
|
||||
|
||||
export async function queryLogs(
|
||||
client: K8sClient,
|
||||
root: string,
|
||||
params: LogsQueryParams,
|
||||
signal?: AbortSignal,
|
||||
): Promise<LogEntry[]> {
|
||||
const res = await proxyGet(client, `${root}select/logsql/query`, { ...params }, signal)
|
||||
const entries = (await res.text()).split("\n").map(parseLine).filter((e) => e !== null)
|
||||
if (entries.some((e) => e._time === undefined)) return entries
|
||||
return entries.sort((a, b) => compareTime(a._time ?? "", b._time ?? ""))
|
||||
}
|
||||
|
||||
export interface FieldValuesParams {
|
||||
field: string
|
||||
stream: boolean
|
||||
query: string
|
||||
start: string
|
||||
end: string
|
||||
}
|
||||
|
||||
export async function fieldValues(
|
||||
client: K8sClient,
|
||||
root: string,
|
||||
{ stream, ...params }: FieldValuesParams,
|
||||
signal?: AbortSignal,
|
||||
): Promise<string[]> {
|
||||
const endpoint = stream ? "stream_field_values" : "field_values"
|
||||
const res = await proxyGet(client, `${root}select/logsql/${endpoint}`, { ...params }, signal)
|
||||
const body = (await res.json()) as { values?: { value: string }[] }
|
||||
return (body.values ?? []).map((v) => v.value).sort()
|
||||
}
|
||||
|
||||
export async function tailLogs(
|
||||
client: K8sClient,
|
||||
root: string,
|
||||
params: { query: string; since?: string },
|
||||
onEntry: (entry: LogEntry) => void,
|
||||
signal?: AbortSignal,
|
||||
onOpen?: () => void,
|
||||
): Promise<void> {
|
||||
const res = await proxyGet(client, `${root}select/logsql/tail`, params, signal)
|
||||
onOpen?.()
|
||||
if (!res.body) return
|
||||
const reader = res.body.getReader()
|
||||
const decoder = new TextDecoder()
|
||||
let buffer = ""
|
||||
for (;;) {
|
||||
const { done, value } = await reader.read()
|
||||
if (done) break
|
||||
buffer += decoder.decode(value, { stream: true })
|
||||
const lines = buffer.split("\n")
|
||||
buffer = lines.pop() ?? ""
|
||||
for (const line of lines) {
|
||||
const entry = parseLine(line)
|
||||
if (entry) onEntry(entry)
|
||||
}
|
||||
}
|
||||
const last = parseLine(buffer)
|
||||
if (last) onEntry(last)
|
||||
}
|
||||
+133
@@ -0,0 +1,133 @@
|
||||
import { describe, it, expect, vi } from "vitest"
|
||||
import { renderHook, waitFor } from "@testing-library/react"
|
||||
import type { ReactNode } from "react"
|
||||
import { K8sClient, K8sProvider } from "@cozystack/k8s-client"
|
||||
import { useLogTail } from "./use-log-tail.ts"
|
||||
|
||||
const line = (time: string, msg: string) => JSON.stringify({ _time: time, _msg: msg, _stream_id: "s" })
|
||||
|
||||
function ndjson(...lines: string[]): Response {
|
||||
return new Response(lines.map((l) => `${l}\n`).join(""))
|
||||
}
|
||||
|
||||
function openStream(): Response {
|
||||
return new Response(new ReadableStream<Uint8Array>({ start() {} }))
|
||||
}
|
||||
|
||||
function setup(...responses: Response[]) {
|
||||
const client = new K8sClient()
|
||||
const raw = vi.spyOn(client, "raw")
|
||||
for (const r of responses) raw.mockResolvedValueOnce(r)
|
||||
raw.mockImplementation(() => new Promise(() => {}))
|
||||
const wrapper = ({ children }: { children: ReactNode }) => <K8sProvider client={client}>{children}</K8sProvider>
|
||||
return { raw, wrapper }
|
||||
}
|
||||
|
||||
const T1 = "2026-11-04T10:00:01Z"
|
||||
const T2 = "2026-11-04T10:00:02Z"
|
||||
const T3 = "2026-11-04T10:00:03Z"
|
||||
|
||||
describe("useLogTail", () => {
|
||||
it("streams lines and resumes after a disconnect from the last _time without duplicates", async () => {
|
||||
const { raw, wrapper } = setup(ndjson(line(T1, "a"), line(T2, "b")), ndjson(line(T2, "b"), line(T3, "c")))
|
||||
|
||||
const { result } = renderHook(() => useLogTail("/root/", "*", 1, { cap: 100, retryDelayMs: 1 }), { wrapper })
|
||||
|
||||
await waitFor(() => expect(result.current.entries.map((e) => e._msg)).toEqual(["a", "b", "c"]))
|
||||
expect(new URL(raw.mock.calls[0][0], "http://x").searchParams.has("since")).toBe(false)
|
||||
expect(new URL(raw.mock.calls[1][0], "http://x").searchParams.get("since")).toBe(T2)
|
||||
})
|
||||
|
||||
it("stops and reports an error the proxy will repeat on every retry", async () => {
|
||||
const { raw, wrapper } = setup(new Response("", { status: 403 }))
|
||||
|
||||
const { result } = renderHook(() => useLogTail("/root/", "*", 1, { cap: 100, retryDelayMs: 1 }), { wrapper })
|
||||
|
||||
await waitFor(() => expect(result.current.state).toBe("stopped"))
|
||||
expect(result.current.error).toMatchObject({ kind: "forbidden" })
|
||||
expect(raw).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
|
||||
it("starts a new follow session with an empty buffer", async () => {
|
||||
const { wrapper } = setup(ndjson(line(T1, "first")), ndjson(line(T3, "second")))
|
||||
const { result, rerender } = renderHook(
|
||||
({ session }: { session: number | null }) => useLogTail("/root/", "*", session, { retryDelayMs: 60_000 }),
|
||||
{ wrapper, initialProps: { session: 1 as number | null } },
|
||||
)
|
||||
await waitFor(() => expect(result.current.entries.map((e) => e._msg)).toEqual(["first"]))
|
||||
|
||||
rerender({ session: null })
|
||||
rerender({ session: 2 })
|
||||
|
||||
await waitFor(() => expect(result.current.entries.map((e) => e._msg)).toEqual(["second"]))
|
||||
})
|
||||
|
||||
it("resumes from the newest line seen, not the last one received", async () => {
|
||||
const { raw, wrapper } = setup(ndjson(line(T3, "c"), line(T1, "a")), ndjson())
|
||||
|
||||
renderHook(() => useLogTail("/root/", "*", 1, { retryDelayMs: 1 }), { wrapper })
|
||||
|
||||
await waitFor(() => expect(raw.mock.calls.length).toBeGreaterThan(1))
|
||||
expect(new URL(raw.mock.calls[1][0], "http://x").searchParams.get("since")).toBe(T3)
|
||||
})
|
||||
|
||||
it("retries a tail refused for load instead of stopping", async () => {
|
||||
const { wrapper } = setup(new Response("too many concurrent requests", { status: 429 }), ndjson(line(T1, "a")))
|
||||
|
||||
const { result } = renderHook(() => useLogTail("/root/", "*", 1, { retryDelayMs: 1 }), { wrapper })
|
||||
|
||||
await waitFor(() => expect(result.current.entries.map((e) => e._msg)).toEqual(["a"]))
|
||||
})
|
||||
|
||||
it("goes live and clears a load warning once the stream opens, before any line", async () => {
|
||||
const { wrapper } = setup(new Response("too many concurrent requests", { status: 429 }), openStream())
|
||||
|
||||
const { result } = renderHook(() => useLogTail("/root/", "*", 1, { retryDelayMs: 1 }), { wrapper })
|
||||
|
||||
await waitFor(() => expect(result.current.state).toBe("live"))
|
||||
expect(result.current.error).toBeNull()
|
||||
})
|
||||
|
||||
it("reconnects after an idle close at the base delay, from when the quiet stream opened", async () => {
|
||||
const delays: number[] = []
|
||||
const realSetTimeout = globalThis.setTimeout
|
||||
const spy = vi.spyOn(globalThis, "setTimeout").mockImplementation(((fn: () => void, ms?: number) => {
|
||||
if (ms !== undefined && ms % 7 === 0 && ms > 0) delays.push(ms)
|
||||
return realSetTimeout(fn, ms)
|
||||
}) as typeof setTimeout)
|
||||
const refused = () => new Response("", { status: 429 })
|
||||
const { raw, wrapper } = setup(refused(), refused(), ndjson())
|
||||
|
||||
try {
|
||||
renderHook(() => useLogTail("/root/", "*", 1, { retryDelayMs: 7 }), { wrapper })
|
||||
await waitFor(() => expect(raw).toHaveBeenCalledTimes(4))
|
||||
} finally {
|
||||
spy.mockRestore()
|
||||
}
|
||||
|
||||
expect(delays).toEqual([7, 14, 7])
|
||||
expect(new URL(raw.mock.calls[2][0], "http://x").searchParams.has("since")).toBe(false)
|
||||
const since = new URL(raw.mock.calls[3][0], "http://x").searchParams.get("since")
|
||||
expect(since).not.toBeNull()
|
||||
expect(new Date(since!).toISOString()).toBe(since)
|
||||
})
|
||||
|
||||
it("does not connect while disabled", () => {
|
||||
const { raw, wrapper } = setup()
|
||||
|
||||
renderHook(() => useLogTail("/root/", "*", null), { wrapper })
|
||||
|
||||
expect(raw).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it("aborts the stream on unmount", async () => {
|
||||
const { raw, wrapper } = setup()
|
||||
const { unmount } = renderHook(() => useLogTail("/root/", "*", 1), { wrapper })
|
||||
await waitFor(() => expect(raw).toHaveBeenCalled())
|
||||
|
||||
unmount()
|
||||
|
||||
const init = raw.mock.calls[0][1] as RequestInit
|
||||
expect(init.signal?.aborted).toBe(true)
|
||||
})
|
||||
})
|
||||
+111
@@ -0,0 +1,111 @@
|
||||
import { useEffect, useState } from "react"
|
||||
import { useK8sClient } from "@cozystack/k8s-client"
|
||||
import { compareTime, mergeTail } from "./logs.ts"
|
||||
import { ProxyError, tailLogs, type LogEntry } from "./proxy.ts"
|
||||
|
||||
export type TailState = "connecting" | "live" | "reconnecting" | "stopped"
|
||||
|
||||
interface TailSnapshot {
|
||||
key: string
|
||||
entries: LogEntry[]
|
||||
state: TailState
|
||||
error: unknown
|
||||
}
|
||||
|
||||
const FLUSH_MS = 100
|
||||
const MAX_RETRY_MS = 30_000
|
||||
// Load refusals retry: the proxy caps rate and concurrent tails with 429 (422 in
|
||||
// its first builds), and a reconnect can race the previous tail's freed slot.
|
||||
const FATAL: ReadonlySet<string> = new Set(["bad-request", "unauthorized", "forbidden", "not-available"])
|
||||
|
||||
const sleep = (ms: number, signal: AbortSignal) =>
|
||||
new Promise<void>((resolve) => {
|
||||
const onAbort = () => {
|
||||
clearTimeout(t)
|
||||
resolve()
|
||||
}
|
||||
const t = setTimeout(() => {
|
||||
signal.removeEventListener("abort", onAbort)
|
||||
resolve()
|
||||
}, ms)
|
||||
signal.addEventListener("abort", onAbort, { once: true })
|
||||
})
|
||||
|
||||
// Each non-null session is one follow run with its own buffer; null stops following.
|
||||
export function useLogTail(
|
||||
root: string,
|
||||
query: string,
|
||||
session: number | null,
|
||||
{ cap = 1000, retryDelayMs = 2000 }: { cap?: number; retryDelayMs?: number } = {},
|
||||
): Omit<TailSnapshot, "key"> {
|
||||
const client = useK8sClient()
|
||||
const key = `${root}\n${query}\n${session}`
|
||||
const [snap, setSnap] = useState<TailSnapshot>({ key, entries: [], state: "connecting", error: null })
|
||||
|
||||
useEffect(() => {
|
||||
if (session === null) return
|
||||
const controller = new AbortController()
|
||||
const { signal } = controller
|
||||
let pending: LogEntry[] = []
|
||||
let timer: ReturnType<typeof setTimeout> | undefined
|
||||
let resumedFrom: string | undefined
|
||||
let lastTime: string | undefined
|
||||
|
||||
const update = (patch: Partial<TailSnapshot>, batch: LogEntry[] = [], from = resumedFrom) =>
|
||||
setSnap((prev) => {
|
||||
const base = prev.key === key ? prev : { key, entries: [], state: "connecting" as const, error: null }
|
||||
return { ...base, ...patch, entries: batch.length ? mergeTail(base.entries, batch, cap, from) : base.entries }
|
||||
})
|
||||
const flush = () => {
|
||||
clearTimeout(timer)
|
||||
timer = undefined
|
||||
if (pending.length === 0 || signal.aborted) return
|
||||
const batch = pending
|
||||
pending = []
|
||||
update({ state: "live", error: null }, batch)
|
||||
}
|
||||
|
||||
const run = async () => {
|
||||
let failures = 0
|
||||
let openedAt: string | undefined
|
||||
while (!signal.aborted) {
|
||||
resumedFrom = lastTime
|
||||
let delay = retryDelayMs
|
||||
try {
|
||||
await tailLogs(client, root, { query, since: lastTime ?? openedAt }, (entry) => {
|
||||
if (entry._time && (!lastTime || compareTime(entry._time, lastTime) > 0)) lastTime = entry._time
|
||||
pending.push(entry)
|
||||
timer ??= setTimeout(flush, FLUSH_MS)
|
||||
}, signal, () => {
|
||||
failures = 0
|
||||
// With no line yet, the next reconnect resumes from here so the gap is not lost.
|
||||
openedAt = new Date().toISOString()
|
||||
update({ state: "live", error: null })
|
||||
})
|
||||
flush()
|
||||
} catch (err) {
|
||||
flush()
|
||||
if (signal.aborted) return
|
||||
if (err instanceof ProxyError && FATAL.has(err.kind)) {
|
||||
update({ state: "stopped", error: err })
|
||||
return
|
||||
}
|
||||
update({ error: err })
|
||||
delay = Math.min(retryDelayMs * 2 ** failures, MAX_RETRY_MS)
|
||||
failures++
|
||||
}
|
||||
if (signal.aborted) return
|
||||
update({ state: "reconnecting" })
|
||||
await sleep(delay, signal)
|
||||
}
|
||||
}
|
||||
void run()
|
||||
return () => {
|
||||
controller.abort()
|
||||
clearTimeout(timer)
|
||||
}
|
||||
}, [client, root, query, session, key, cap, retryDelayMs])
|
||||
|
||||
const current = snap.key === key ? snap : { key, entries: [], state: "connecting" as const, error: null }
|
||||
return { entries: current.entries, state: current.state, error: current.error }
|
||||
}
|
||||
+50
@@ -70,4 +70,54 @@ describe("ConsolePage routing", () => {
|
||||
await waitFor(() => expect(client.list).toHaveBeenCalled())
|
||||
expect(screen.queryByText(/Cluster-scoped capacity/i)).toBeNull()
|
||||
})
|
||||
|
||||
it("serves the tenant logs page under the reserved -/ segment", async () => {
|
||||
renderWithK8sProvider(
|
||||
<TenantProvider>
|
||||
<ConsolePage />
|
||||
</TenantProvider>,
|
||||
{ client: makeClient(), initialRoute: "/-/observability/logs" },
|
||||
)
|
||||
|
||||
expect(await screen.findByText(/^Every application in/)).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it("leaves /logs to an application kind whose plural is logs", async () => {
|
||||
const client = makeClient()
|
||||
renderWithK8sProvider(
|
||||
<TenantProvider>
|
||||
<ConsolePage />
|
||||
</TenantProvider>,
|
||||
{ client, initialRoute: "/logs" },
|
||||
)
|
||||
|
||||
await waitFor(() => expect(client.list).toHaveBeenCalled())
|
||||
expect(screen.queryByText(/^Every application in/)).toBeNull()
|
||||
})
|
||||
|
||||
// No ApplicationDefinition plural or application name can be "-", so only
|
||||
// routes under it are safe from shadowing an application's own pages.
|
||||
it("leaves /observability/logs to an application named logs of kind observability", async () => {
|
||||
const client = makeClient()
|
||||
vi.mocked(client.list).mockImplementation(async (_g, _v, plural) => ({
|
||||
apiVersion: "v1",
|
||||
kind: `${plural}List`,
|
||||
metadata: {},
|
||||
items:
|
||||
plural === "tenantnamespaces"
|
||||
? [{ apiVersion: "core.cozystack.io/v1alpha1", kind: "TenantNamespace", metadata: { name: "tenant-root" } }]
|
||||
: [],
|
||||
}) as K8sList<unknown>)
|
||||
const get = vi.spyOn(client, "get").mockRejectedValue(new Error("not found"))
|
||||
renderWithK8sProvider(
|
||||
<TenantProvider>
|
||||
<ConsolePage />
|
||||
</TenantProvider>,
|
||||
{ client, initialRoute: "/observability/logs" },
|
||||
)
|
||||
|
||||
expect(await screen.findByText(/^Application/)).toHaveTextContent("Application logs not found.")
|
||||
expect(get).toHaveBeenCalledWith(expect.anything(), expect.anything(), "observability", "logs", expect.anything())
|
||||
expect(screen.queryByText(/^Every application in/)).toBeNull()
|
||||
})
|
||||
})
|
||||
@@ -15,6 +15,7 @@ import { MigrationSourceEditPage } from "./MigrationSourceEditPage.tsx"
|
||||
import { MigrationTaskCreatePage } from "./MigrationTaskCreatePage.tsx"
|
||||
import { MigrationTaskDetailPage } from "./MigrationTaskDetailPage.tsx"
|
||||
import { ApplicationOrderPage } from "./ApplicationOrderPage.tsx"
|
||||
import { LogsPage } from "./LogsPage.tsx"
|
||||
|
||||
export function ConsolePage() {
|
||||
return (
|
||||
@@ -96,6 +97,7 @@ export function ConsolePage() {
|
||||
path="migration/vmimporttasks/:name"
|
||||
element={<MigrationTaskDetailPage />}
|
||||
/>
|
||||
<Route path="-/observability/logs" element={<LogsPage />} />
|
||||
<Route path="new/:appName" element={<ApplicationOrderPage />} />
|
||||
<Route path=":plural/:name/edit" element={<ApplicationEditRoute />} />
|
||||
<Route path=":plural/:name/*" element={<ApplicationDetailPage />} />
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
import { describe, it, expect, vi } from "vitest"
|
||||
import { render, screen } from "@testing-library/react"
|
||||
|
||||
vi.mock("../lib/tenant-context.tsx", () => ({
|
||||
useTenantContext: () => ({ tenantNamespace: "tenant-a" }),
|
||||
}))
|
||||
vi.mock("../components/observability/LogsView.tsx", () => ({
|
||||
LogsView: ({ scope }: { scope: object }) => <pre data-testid="scope">{JSON.stringify(scope)}</pre>,
|
||||
}))
|
||||
|
||||
const { LogsPage } = await import("./LogsPage.tsx")
|
||||
|
||||
describe("LogsPage", () => {
|
||||
it("reads logs from the selected tenant's namespace root", () => {
|
||||
render(<LogsPage />)
|
||||
|
||||
expect(screen.getByRole("heading", { name: "Logs" })).toBeInTheDocument()
|
||||
expect(JSON.parse(screen.getByTestId("scope").textContent ?? "")).toEqual({ namespace: "tenant-a" })
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,15 @@
|
||||
import { useTenantContext } from "../lib/tenant-context.tsx"
|
||||
import { LogsView } from "../components/observability/LogsView.tsx"
|
||||
|
||||
export function LogsPage() {
|
||||
const { tenantNamespace } = useTenantContext()
|
||||
return (
|
||||
<div className="flex h-full flex-col">
|
||||
<div className="border-b border-slate-200 bg-white px-6 py-4">
|
||||
<h1 className="text-lg font-semibold text-slate-900">Logs</h1>
|
||||
<p className="text-xs text-slate-500">Every application in {tenantNamespace ?? "this tenant"}</p>
|
||||
</div>
|
||||
{tenantNamespace && <LogsView scope={{ namespace: tenantNamespace }} />}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
+113
@@ -0,0 +1,113 @@
|
||||
import { describe, it, expect, vi, beforeEach } from "vitest"
|
||||
import { act, render, screen, waitFor } from "@testing-library/react"
|
||||
import { createMemoryRouter, RouterProvider } from "react-router"
|
||||
import { K8sProvider } from "@cozystack/k8s-client"
|
||||
import { TenantContext, type TenantContextValue } from "../../lib/tenant-context.tsx"
|
||||
import type { ChartData } from "../../lib/observability/metrics.ts"
|
||||
|
||||
// Keep the consoles' and the chart's browser-only libraries out of jsdom; the
|
||||
// Logs and Metrics tabs, the page and the providers stay real.
|
||||
vi.mock("./VncTab.tsx", () => ({ VncTab: () => null }))
|
||||
vi.mock("./SerialTab.tsx", () => ({ SerialTab: () => null }))
|
||||
vi.mock("../../components/observability/TimeSeriesChart.tsx", () => ({
|
||||
TimeSeriesChart: ({ data }: { data: ChartData }) => (
|
||||
<div data-testid="chart">{data.series.map((s) => s.label).join(",")}</div>
|
||||
),
|
||||
}))
|
||||
|
||||
const { ApplicationDetailPage } = await import("./ApplicationDetailPage.tsx")
|
||||
|
||||
const NS = "tenant-test"
|
||||
const APPS = `/apis/apps.cozystack.io/v1alpha1/namespaces/${NS}/postgreses`
|
||||
|
||||
const tenant: TenantContextValue = {
|
||||
tenants: [],
|
||||
selectedTenant: "test",
|
||||
selectTenant: () => {},
|
||||
tenantNamespace: NS,
|
||||
isLoading: false,
|
||||
error: null,
|
||||
}
|
||||
|
||||
const definitions = {
|
||||
items: [{ metadata: { name: "postgres" }, spec: { application: { plural: "postgreses", kind: "Postgres" } } }],
|
||||
}
|
||||
|
||||
const instance = (name: string) => ({
|
||||
apiVersion: "apps.cozystack.io/v1alpha1",
|
||||
kind: "Postgres",
|
||||
metadata: { name, namespace: NS },
|
||||
})
|
||||
|
||||
const logLine = (name: string) =>
|
||||
`${JSON.stringify({ _time: "2026-11-04T10:00:01Z", _msg: `${name} is ready`, _stream_id: "s", kubernetes_pod_name: `${name}-0`, kubernetes_container_name: "postgres" })}\n`
|
||||
|
||||
const matrix = (name: string) =>
|
||||
Response.json({ status: "success", data: { resultType: "matrix", result: [{ metric: { pod: `${name}-0` }, values: [[1, "1"]] }] } })
|
||||
|
||||
// app-b's own GET never answers, so the page keeps the placeholder of app-a
|
||||
// that the production query defaults hand it; its proxy reads do answer.
|
||||
function handle(url: URL): Response | Promise<Response> {
|
||||
const path = url.pathname
|
||||
if (path.endsWith("/applicationdefinitions")) return Response.json(definitions)
|
||||
if (path === `${APPS}/app-a`) return Response.json(instance("app-a"))
|
||||
if (path === `${APPS}/app-b`) return new Promise(() => {})
|
||||
const app = path.match(/\/postgreses\/(app-[ab])\//)?.[1]
|
||||
if (app && path.endsWith("/select/logsql/query")) return new Response(logLine(app))
|
||||
if (app && path.endsWith("/stream_field_values")) return Response.json({ values: [] })
|
||||
if (app && path.includes("/metrics/api/v1/query_range")) return matrix(app)
|
||||
return Response.json({ items: [] })
|
||||
}
|
||||
|
||||
const fetchMock = vi.fn(async (input: RequestInfo | URL) => handle(new URL(String(input), "http://x")))
|
||||
|
||||
function renderAt(path: string) {
|
||||
const router = createMemoryRouter(
|
||||
[{ path: "/console/:plural/:name/*", element: <ApplicationDetailPage /> }],
|
||||
{ initialEntries: [path] },
|
||||
)
|
||||
// No queryClient: the provider's production defaults keep a query's previous
|
||||
// result while its key changes, which is what exposes the stale instance.
|
||||
render(
|
||||
<K8sProvider>
|
||||
<TenantContext.Provider value={tenant}>
|
||||
<RouterProvider router={router} />
|
||||
</TenantContext.Provider>
|
||||
</K8sProvider>,
|
||||
)
|
||||
return { go: (to: string) => act(() => router.navigate(to)) }
|
||||
}
|
||||
|
||||
const requested = (prefix: string) =>
|
||||
fetchMock.mock.calls.some(([input]) => new URL(String(input), "http://x").pathname.startsWith(prefix))
|
||||
|
||||
beforeEach(() => {
|
||||
vi.stubGlobal("fetch", fetchMock)
|
||||
fetchMock.mockClear()
|
||||
})
|
||||
|
||||
describe("ApplicationDetailPage observability while the next application loads", () => {
|
||||
it("shows the selected application's logs, not the previous one's", async () => {
|
||||
const { go } = renderAt("/console/postgreses/app-a/logs")
|
||||
expect(await screen.findByText("app-a is ready")).toBeInTheDocument()
|
||||
|
||||
await go("/console/postgreses/app-b/logs")
|
||||
|
||||
expect(screen.getByRole("heading", { name: "app-b" })).toBeInTheDocument()
|
||||
expect(screen.queryByText("app-a is ready")).toBeNull()
|
||||
await waitFor(() => expect(requested(`${APPS}/app-b/log/`)).toBe(true))
|
||||
expect(await screen.findByText("app-b is ready")).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it("shows the selected application's metrics, not the previous one's", async () => {
|
||||
const { go } = renderAt("/console/postgreses/app-a/metrics")
|
||||
await waitFor(() => expect(screen.getAllByTestId("chart")[0]).toHaveTextContent("app-a-0"))
|
||||
|
||||
await go("/console/postgreses/app-b/metrics")
|
||||
|
||||
expect(screen.getByRole("heading", { name: "app-b" })).toBeInTheDocument()
|
||||
expect(screen.queryAllByText("app-a-0", { exact: false })).toEqual([])
|
||||
await waitFor(() => expect(requested(`${APPS}/app-b/metrics/`)).toBe(true))
|
||||
await waitFor(() => expect(screen.getAllByTestId("chart")[0]).toHaveTextContent("app-b-0"))
|
||||
})
|
||||
})
|
||||
+31
@@ -59,6 +59,8 @@ vi.mock("./EventsTab.tsx", () => ({ EventsTab: () => null }))
|
||||
vi.mock("./VncTab.tsx", () => ({ VncTab: () => null }))
|
||||
vi.mock("./SerialTab.tsx", () => ({ SerialTab: () => null }))
|
||||
vi.mock("./VMPowerControls.tsx", () => ({ VMPowerControls: () => null }))
|
||||
vi.mock("./MetricsTab.tsx", () => ({ MetricsTab: () => null }))
|
||||
vi.mock("./LogsTab.tsx", () => ({ LogsTab: () => null }))
|
||||
|
||||
const { ApplicationDetailPage } = await import("./ApplicationDetailPage.tsx")
|
||||
|
||||
@@ -111,11 +113,40 @@ describe("ApplicationDetailPage tabs for a virtual machine", () => {
|
||||
"VNC",
|
||||
"Workloads",
|
||||
"Services",
|
||||
"Metrics",
|
||||
"Logs",
|
||||
"Events",
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe("ApplicationDetailPage observability tabs", () => {
|
||||
function showKind(kind: string, plural: string) {
|
||||
h.appKind = kind
|
||||
h.appPlural = plural
|
||||
h.get = {
|
||||
data: { kind, metadata: { name: "demo", namespace: "tenant-test" } },
|
||||
isLoading: false,
|
||||
error: undefined,
|
||||
}
|
||||
h.configMaps = []
|
||||
h.configMapsError = undefined
|
||||
h.tabLabels = []
|
||||
renderPage()
|
||||
}
|
||||
|
||||
it("offers Metrics and Logs on any application kind, ahead of Events", () => {
|
||||
showKind("Postgres", "postgreses")
|
||||
expect(h.tabLabels.slice(-4)).toEqual(["Secrets", "Metrics", "Logs", "Events"])
|
||||
})
|
||||
|
||||
it("leaves Metrics and Logs off a VM disk, which runs no containers", () => {
|
||||
showKind("VMDisk", "vmdisks")
|
||||
expect(h.tabLabels).not.toContain("Metrics")
|
||||
expect(h.tabLabels).not.toContain("Logs")
|
||||
})
|
||||
})
|
||||
|
||||
describe("ApplicationDetailPage configuration", () => {
|
||||
it("offers ConfigMaps declared by the application's resource map", () => {
|
||||
h.appKind = "FoundationDB"
|
||||
|
||||
+16
@@ -37,11 +37,14 @@ import { VncTab } from "./VncTab.tsx"
|
||||
// fetched only when a VM's console is opened. Every dashboard page reaches this
|
||||
// file, and most of them never touch a VM.
|
||||
const SerialTab = lazy(() => import("./SerialTab.tsx").then((m) => ({ default: m.SerialTab })))
|
||||
// Same for the charting library behind the Metrics tab.
|
||||
const MetricsTab = lazy(() => import("./MetricsTab.tsx").then((m) => ({ default: m.MetricsTab })))
|
||||
import { VMPowerControls } from "./VMPowerControls.tsx"
|
||||
import { useResourceBasePath } from "../../lib/portal.ts"
|
||||
import { useResourcePresence } from "./use-resource-presence.ts"
|
||||
import { useApplicationConfigMaps } from "./use-app-configmaps.ts"
|
||||
import { ConfigMapsTab } from "./ConfigMapsTab.tsx"
|
||||
import { LogsTab } from "./LogsTab.tsx"
|
||||
|
||||
export function ApplicationDetailPage() {
|
||||
const { plural, name } = useParams<{ plural: string; name: string }>()
|
||||
@@ -143,6 +146,8 @@ export function ApplicationDetailPage() {
|
||||
{ to: `${base}/vnc`, label: "VNC", end: false },
|
||||
{ to: `${base}/workloads`, label: "Workloads", end: false },
|
||||
{ to: `${base}/services`, label: "Services", end: false },
|
||||
{ to: `${base}/metrics`, label: "Metrics", end: false },
|
||||
{ to: `${base}/logs`, label: "Logs", end: false },
|
||||
{ to: `${base}/events`, label: "Events", end: false },
|
||||
)
|
||||
} else {
|
||||
@@ -163,6 +168,8 @@ export function ApplicationDetailPage() {
|
||||
}
|
||||
tabs.push(
|
||||
{ to: `${base}/secrets`, label: "Secrets", end: false },
|
||||
{ to: `${base}/metrics`, label: "Metrics", end: false },
|
||||
{ to: `${base}/logs`, label: "Logs", end: false },
|
||||
{ to: `${base}/events`, label: "Events", end: false },
|
||||
)
|
||||
}
|
||||
@@ -262,6 +269,15 @@ export function ApplicationDetailPage() {
|
||||
}
|
||||
/>
|
||||
<Route path="vnc" element={<VncTab ad={ad} instance={instance} />} />
|
||||
<Route
|
||||
path="metrics"
|
||||
element={
|
||||
<Suspense fallback={<Spinner />}>
|
||||
<MetricsTab ad={ad} name={name} />
|
||||
</Suspense>
|
||||
}
|
||||
/>
|
||||
<Route path="logs" element={<LogsTab ad={ad} name={name} />} />
|
||||
</Routes>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
import type { ApplicationDefinition } from "@cozystack/types"
|
||||
import { useTenantContext } from "../../lib/tenant-context.tsx"
|
||||
import { LogsView } from "../../components/observability/LogsView.tsx"
|
||||
|
||||
interface LogsTabProps {
|
||||
ad: ApplicationDefinition
|
||||
// The route's name: while the next application loads, the page still holds the previous instance.
|
||||
name: string
|
||||
}
|
||||
|
||||
export function LogsTab({ ad, name }: LogsTabProps) {
|
||||
const { tenantNamespace } = useTenantContext()
|
||||
const plural = ad.spec?.application.plural
|
||||
if (!tenantNamespace || !plural) return null
|
||||
return <LogsView scope={{ namespace: tenantNamespace, plural, name }} />
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
import type { ApplicationDefinition } from "@cozystack/types"
|
||||
import { useTenantContext } from "../../lib/tenant-context.tsx"
|
||||
import { MetricsView } from "../../components/observability/MetricsView.tsx"
|
||||
|
||||
interface MetricsTabProps {
|
||||
ad: ApplicationDefinition
|
||||
// The route's name: while the next application loads, the page still holds the previous instance.
|
||||
name: string
|
||||
}
|
||||
|
||||
export function MetricsTab({ ad, name }: MetricsTabProps) {
|
||||
const { tenantNamespace } = useTenantContext()
|
||||
const plural = ad.spec?.application.plural
|
||||
if (!tenantNamespace || !plural) return null
|
||||
return <MetricsView scope={{ namespace: tenantNamespace, plural, name }} />
|
||||
}
|
||||
+10
@@ -110,6 +110,16 @@ describe("useConsoleSidebarSections — admin areas moved out", () => {
|
||||
expect(findItem(result.current, "Imports")?.to).toBe("/console/migration/vmimporttasks")
|
||||
expect(result.current.some((s) => s.title === "Migration")).toBe(true)
|
||||
})
|
||||
|
||||
it("offers the tenant's namespace logs in an Observability group", async () => {
|
||||
const client = makeClient({ nodes: true, backupclasses: true })
|
||||
const { result } = renderHook(() => useConsoleSidebarSections(), {
|
||||
wrapper: makeWrapper(client),
|
||||
})
|
||||
await waitFor(() => expect(result.current.length).toBeGreaterThan(0))
|
||||
expect(findItem(result.current, "Logs")?.to).toBe("/console/-/observability/logs")
|
||||
expect(result.current.find((s) => s.title === "Observability")?.items).toHaveLength(1)
|
||||
})
|
||||
})
|
||||
|
||||
function appDef(name: string, kind: string, category: string, module = false) {
|
||||
|
||||
+7
-1
@@ -11,6 +11,7 @@ import {
|
||||
Layers,
|
||||
Network,
|
||||
Package,
|
||||
ScrollText,
|
||||
Plug,
|
||||
Server,
|
||||
ToyBrick,
|
||||
@@ -118,7 +119,12 @@ export function useConsoleSidebarSections(): SidebarSection[] {
|
||||
],
|
||||
}
|
||||
|
||||
return [...categorySections, backupsSection, migrationSection]
|
||||
const observabilitySection: SidebarSection = {
|
||||
title: "Observability",
|
||||
items: [{ label: "Logs", to: "/console/-/observability/logs", icon: ScrollText }],
|
||||
}
|
||||
|
||||
return [...categorySections, backupsSection, migrationSection, observabilitySection]
|
||||
}, [grouped])
|
||||
}
|
||||
|
||||
|
||||
@@ -50,14 +50,12 @@ export class K8sClient {
|
||||
}
|
||||
|
||||
private async request<T>(path: string, init?: RequestInit): Promise<T> {
|
||||
const headers: Record<string, string> = {
|
||||
"Content-Type": "application/json",
|
||||
...(init?.headers as Record<string, string>),
|
||||
}
|
||||
const headers = new Headers(init?.headers)
|
||||
if (!headers.has("Content-Type")) headers.set("Content-Type", "application/json")
|
||||
|
||||
if (this.getToken) {
|
||||
const token = await this.getToken()
|
||||
headers["Authorization"] = `Bearer ${token}`
|
||||
headers.set("Authorization", `Bearer ${token}`)
|
||||
}
|
||||
|
||||
let res: Response
|
||||
@@ -241,6 +239,22 @@ export class K8sClient {
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch a same-origin path with the client's credentials and 401 handling,
|
||||
* leaving status and body to the caller. For endpoints that do not speak
|
||||
* Kubernetes JSON, such as the observability proxy's streamed NDJSON.
|
||||
*/
|
||||
async raw(path: string, init?: RequestInit): Promise<Response> {
|
||||
const headers = new Headers(init?.headers)
|
||||
if (this.getToken) {
|
||||
headers.set("Authorization", `Bearer ${await this.getToken()}`)
|
||||
}
|
||||
const res = await fetch(`${this.baseUrl}${path}`, { ...init, headers })
|
||||
if (res.status === 401) this.handleUnauthorized()
|
||||
else if (res.ok) this.unauthorizedHandled = false
|
||||
return res
|
||||
}
|
||||
|
||||
getApiGroups(): Promise<APIGroupList> {
|
||||
return this.request<APIGroupList>("/apis")
|
||||
}
|
||||
|
||||
@@ -92,6 +92,9 @@ importers:
|
||||
simple-icons:
|
||||
specifier: ^16.22.0
|
||||
version: 16.22.0
|
||||
uplot:
|
||||
specifier: ^1.6.32
|
||||
version: 1.6.32
|
||||
devDependencies:
|
||||
'@tailwindcss/vite':
|
||||
specifier: ^4.2.2
|
||||
@@ -1773,6 +1776,9 @@ packages:
|
||||
peerDependencies:
|
||||
browserslist: '>= 4.21.0'
|
||||
|
||||
uplot@1.6.32:
|
||||
resolution: {integrity: sha512-KIMVnG68zvu5XXUbC4LQEPnhwOxBuLyW1AHtpm6IKTXImkbLgkMy+jabjLgSLMasNuGGzQm/ep3tOkyTxpiQIw==}
|
||||
|
||||
uri-js@4.4.1:
|
||||
resolution: {integrity: sha512-7rKUyy33Q1yc98pQ1DAmLtwX109F7TIfWlW1Ydo8Wl1ii1SeHieeh0HHfPeL2fMXK6z0s8ecKs9frCuLJvndBg==}
|
||||
|
||||
@@ -3405,6 +3411,8 @@ snapshots:
|
||||
escalade: 3.2.0
|
||||
picocolors: 1.1.1
|
||||
|
||||
uplot@1.6.32: {}
|
||||
|
||||
uri-js@4.4.1:
|
||||
dependencies:
|
||||
punycode: 2.3.1
|
||||
|
||||
Reference in new issue
Block a user