mirror of
https://github.com/cozystack/cozystack.git
synced 2026-11-22 12:43:32 +09:00
fix(dashboard): show log rows a pipe stripped of their timestamp
A LogsQL pipe such as `fields _msg` or `stats` returns rows without _time, and the console dropped every one of them, so a successful query read as "no lines matched". Such rows are now kept in the order the store sent them, shown without a timestamp, and never deduplicated on a tail resume. A row without _msg shows its remaining fields instead of an empty line. Assisted-by: LLM Signed-off-by: ZverGuy <maximbel2003@gmail.com>
This commit is contained in:
1 parent
2a473086cf
commit
1e1fdb740c
8 files changed
+86
-15
No files matched your search
+12
@@ -29,6 +29,18 @@ describe("LogLines", () => {
|
||||
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")
|
||||
|
||||
+26
-7
@@ -3,6 +3,22 @@ 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)
|
||||
@@ -29,13 +45,16 @@ export function LogLines({ entries, placeholder }: { entries: LogEntry[]; placeh
|
||||
}}
|
||||
className="h-full overflow-auto p-3"
|
||||
>
|
||||
{entries.map((e, i) => (
|
||||
<li key={`${e._stream_id}-${e._time}-${i}`} className="whitespace-pre-wrap break-all">
|
||||
<span className="text-slate-400">{new Date(e._time).toLocaleTimeString()}</span>{" "}
|
||||
<span className="text-sky-300">{`${e[POD_FIELD] ?? ""}/${e[CONTAINER_FIELD] ?? ""}`}</span>{" "}
|
||||
{e._msg}
|
||||
</li>
|
||||
))}
|
||||
{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>
|
||||
|
||||
+13
@@ -199,6 +199,19 @@ describe("LogsView", () => {
|
||||
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("names the source pod and container of each line", async () => {
|
||||
const { client } = clientWith(defaultHandler)
|
||||
|
||||
|
||||
+6
@@ -57,6 +57,12 @@ describe("mergeTail", () => {
|
||||
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("does not deduplicate outside a resume", () => {
|
||||
const out = mergeTail([e("t1", "a")], [e("t1", "a")], 10)
|
||||
|
||||
|
||||
@@ -61,12 +61,13 @@ export function compareTime(a: string, b: string): number {
|
||||
const entryKey = (e: LogEntry) => `${e._stream_id ?? ""}\u0000${e._time}`
|
||||
|
||||
// (_stream_id, _time) is not unique in VictoriaLogs, so dedup applies only to
|
||||
// lines a resumed tail replays at or before the resume point.
|
||||
// lines a resumed tail replays at or before the resume point, and never to
|
||||
// lines a pipe stripped of _time.
|
||||
export function mergeTail(buffer: LogEntry[], incoming: LogEntry[], cap: number, resumedFrom?: string): LogEntry[] {
|
||||
let fresh = incoming
|
||||
if (resumedFrom) {
|
||||
const seen = new Set(buffer.map(entryKey))
|
||||
fresh = incoming.filter((e) => compareTime(e._time, resumedFrom) > 0 || !seen.has(entryKey(e)))
|
||||
fresh = incoming.filter((e) => e._time === undefined || compareTime(e._time, resumedFrom) > 0 || !seen.has(entryKey(e)))
|
||||
}
|
||||
const merged = buffer.concat(fresh)
|
||||
return merged.length > cap ? merged.slice(merged.length - cap) : merged
|
||||
|
||||
+19
-1
@@ -122,6 +122,15 @@ describe("queryLogs", () => {
|
||||
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 }))
|
||||
|
||||
@@ -170,7 +179,7 @@ describe("tailLogs", () => {
|
||||
},
|
||||
})
|
||||
const { client, raw } = clientReturning(new Response(body))
|
||||
const seen: string[] = []
|
||||
const seen: (string | undefined)[] = []
|
||||
|
||||
await tailLogs(client, logsRoot(ns), { query: "*", since: "t0" }, (e) => seen.push(e._msg))
|
||||
|
||||
@@ -179,6 +188,15 @@ describe("tailLogs", () => {
|
||||
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(""))
|
||||
|
||||
|
||||
+6
-4
@@ -140,9 +140,10 @@ export async function queryRange(
|
||||
return body.data?.result ?? []
|
||||
}
|
||||
|
||||
// A pipe such as `fields _msg` or `stats` can drop _time and _msg.
|
||||
export interface LogEntry {
|
||||
_time: string
|
||||
_msg: string
|
||||
_time?: string
|
||||
_msg?: string
|
||||
_stream_id?: string
|
||||
[field: string]: string | undefined
|
||||
}
|
||||
@@ -151,7 +152,7 @@ function parseLine(line: string): LogEntry | null {
|
||||
if (!line.trim()) return null
|
||||
try {
|
||||
const value: unknown = JSON.parse(line)
|
||||
if (value && typeof value === "object" && "_time" in value) return value as LogEntry
|
||||
if (value && typeof value === "object" && !Array.isArray(value)) return value as LogEntry
|
||||
} catch {
|
||||
// a torn or non-JSON line carries nothing to show
|
||||
}
|
||||
@@ -173,7 +174,8 @@ export async function queryLogs(
|
||||
): 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)
|
||||
return entries.sort((a, b) => compareTime(a._time, b._time))
|
||||
if (entries.some((e) => e._time === undefined)) return entries
|
||||
return entries.sort((a, b) => compareTime(a._time ?? "", b._time ?? ""))
|
||||
}
|
||||
|
||||
export interface FieldValuesParams {
|
||||
|
||||
+1
-1
@@ -73,7 +73,7 @@ export function useLogTail(
|
||||
let delay = retryDelayMs
|
||||
try {
|
||||
await tailLogs(client, root, { query, since: lastTime ?? openedAt }, (entry) => {
|
||||
if (!lastTime || compareTime(entry._time, lastTime) > 0) lastTime = entry._time
|
||||
if (entry._time && (!lastTime || compareTime(entry._time, lastTime) > 0)) lastTime = entry._time
|
||||
pending.push(entry)
|
||||
timer ??= setTimeout(flush, FLUSH_MS)
|
||||
}, signal, () => {
|
||||
|
||||
Reference in new issue
Block a user