Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Hide process UUID in log viewer if there is only one process (#67152)

Rodrigo López Datod3850dc62814
diff --git a/e2e/test/scenarios/admin/tools/tools.cy.spec.ts b/e2e/test/scenarios/admin/tools/tools.cy.spec.ts
index f0b1dfe3ee4..df0e9a0ce6e 100644
--- a/e2e/test/scenarios/admin/tools/tools.cy.spec.ts
+++ b/e2e/test/scenarios/admin/tools/tools.cy.spec.ts
@@ -321,6 +321,7 @@ describe("scenarios > admin > tools > logs", () => {
};
const log2 = {
...log1,
+ process_uuid: "9da436dc-d79c-42f9-89e3-322c22cd0cd3",
timestamp: "2024-01-10T21:21:58.598Z",
level: "ERROR",
};
@@ -343,7 +344,7 @@ describe("scenarios > admin > tools > logs", () => {
"equal",
[
`[e7774ef2-42ab-43de-89f7-d6de9fdc624f] ${formatTimestamp(log1.timestamp)} DEBUG metabase.server.middleware.log message`,
- `[e7774ef2-42ab-43de-89f7-d6de9fdc624f] ${formatTimestamp(log2.timestamp)} ERROR metabase.server.middleware.log message`,
+ `[9da436dc-d79c-42f9-89e3-322c22cd0cd3] ${formatTimestamp(log2.timestamp)} ERROR metabase.server.middleware.log message`,
].join("\n"),
);
});
@@ -353,7 +354,7 @@ describe("scenarios > admin > tools > logs", () => {
cy.button(/Download/).click();
cy.readFile("cypress/downloads/logs.txt").should(
"equal",
- `[e7774ef2-42ab-43de-89f7-d6de9fdc624f] ${formatTimestamp(log2.timestamp)} ERROR metabase.server.middleware.log message`,
+ `[9da436dc-d79c-42f9-89e3-322c22cd0cd3] ${formatTimestamp(log2.timestamp)} ERROR metabase.server.middleware.log message`,
);
});
diff --git a/frontend/src/metabase/admin/tools/components/Logs/Logs.tsx b/frontend/src/metabase/admin/tools/components/Logs/Logs.tsx
index bb789506706..4267946b608 100644
--- a/frontend/src/metabase/admin/tools/components/Logs/Logs.tsx
+++ b/frontend/src/metabase/admin/tools/components/Logs/Logs.tsx
@@ -18,8 +18,8 @@ import { openSaveDialog } from "metabase/utils/dom";
import { LogsContainer, LogsContent } from "./Logs.styled";
import { usePollingLogsQuery, useTailLogs } from "./hooks";
import {
+ createLogFormatter,
filterLogs,
- formatLog,
getAllProcessUUIDs,
urlStateConfig,
} from "./utils";
@@ -47,14 +47,18 @@ const LogsBase = ({
const { loaded, error, logs } = usePollingLogsQuery(pollingDurationMs);
const processUUIDs = useMemo(() => getAllProcessUUIDs(logs), [logs]);
const filteredLogs = useMemo(
- () => filterLogs(logs, { process, query }),
- [logs, process, query],
+ () => filterLogs(logs, { process, query }, processUUIDs),
+ [logs, process, query, processUUIDs],
);
const hasAnyLogs = logs.length > 0;
const { scrollRef, onScroll, refollow } = useTailLogs(filteredLogs);
+ const formatter = useMemo(
+ () => createLogFormatter(process, processUUIDs),
+ [process, processUUIDs],
+ );
const logText = useMemo(
- () => filteredLogs.map(formatLog).join("\n"),
- [filteredLogs],
+ () => filteredLogs.map(formatter).join("\n"),
+ [filteredLogs, formatter],
);
const displayLogs = useMemo(() => {
diff --git a/frontend/src/metabase/admin/tools/components/Logs/Logs.unit.spec.tsx b/frontend/src/metabase/admin/tools/components/Logs/Logs.unit.spec.tsx
index 7f5267ef29c..1af7d0385d8 100644
--- a/frontend/src/metabase/admin/tools/components/Logs/Logs.unit.spec.tsx
+++ b/frontend/src/metabase/admin/tools/components/Logs/Logs.unit.spec.tsx
@@ -63,10 +63,9 @@ describe("Logs", () => {
it("should call UtilApi.logs every 1 second", async () => {
fetchMock.get("path:/api/logger/logs", []);
setup();
- await waitFor(() => [
- expect(screen.getByTestId("loading-indicator")).toBeInTheDocument(),
- expect(utilSpy).toHaveBeenCalledTimes(1),
- ]);
+ await waitFor(() => {
+ expect(utilSpy).toHaveBeenCalledTimes(1);
+ });
});
it("should skip calls to UtilsApi.logs if last request is still in-flight", async () => {
@@ -74,10 +73,9 @@ describe("Logs", () => {
let resolve: any;
utilSpy.mockReturnValueOnce(new Promise((res) => (resolve = res)));
setup();
- await waitFor(() => [
- expect(screen.getByTestId("loading-indicator")).toBeInTheDocument(),
- expect(utilSpy).toHaveBeenCalledTimes(1),
- ]);
+ await waitFor(() => {
+ expect(utilSpy).toHaveBeenCalledTimes(1);
+ });
act(() => {
jest.advanceTimersByTime(DEFAULT_POLLING_DURATION_MS + 100);
});
@@ -86,9 +84,7 @@ describe("Logs", () => {
resolve([log]);
});
await waitFor(() => {
- expect(
- screen.getByText(new RegExp(log.process_uuid)),
- ).toBeInTheDocument();
+ expect(screen.getByText(new RegExp(log.fqns))).toBeInTheDocument();
});
act(() => {
jest.advanceTimersByTime(DEFAULT_POLLING_DURATION_MS + 100);
@@ -131,9 +127,7 @@ describe("Logs", () => {
search: `?query=${log.fqns}`,
}),
});
- expect(
- await screen.findByText(new RegExp(log.process_uuid)),
- ).toBeInTheDocument();
+ expect(await screen.findByText(new RegExp(log.fqns))).toBeInTheDocument();
expect(screen.getByRole("button", { name: /Download/ })).toBeEnabled();
});
@@ -141,9 +135,7 @@ describe("Logs", () => {
fetchMock.get("path:/api/logger/logs", [log]);
setup();
await waitFor(() => {
- expect(
- screen.getByText(new RegExp(log.process_uuid)),
- ).toBeInTheDocument();
+ expect(screen.getByText(new RegExp(log.fqns))).toBeInTheDocument();
});
expect(screen.getByRole("button", { name: /Download/ })).toBeEnabled();
});
@@ -166,9 +158,7 @@ describe("Logs", () => {
it("should stop polling on unmount", async () => {
fetchMock.get("path:/api/logger/logs", [log]);
const { unmount } = setup();
- expect(
- await screen.findByText(new RegExp(log.process_uuid)),
- ).toBeInTheDocument();
+ expect(await screen.findByText(new RegExp(log.fqns))).toBeInTheDocument();
unmount();
act(() => {
diff --git a/frontend/src/metabase/admin/tools/components/Logs/LogsViewer.tsx b/frontend/src/metabase/admin/tools/components/Logs/LogsViewer.tsx
index b73dc256e1b..27617c185fb 100644
--- a/frontend/src/metabase/admin/tools/components/Logs/LogsViewer.tsx
+++ b/frontend/src/metabase/admin/tools/components/Logs/LogsViewer.tsx
@@ -6,7 +6,7 @@ import { AnsiLogs } from "metabase/common/components/AnsiLogs";
import type { Log } from "metabase-types/api";
import { LogsContent } from "./Logs.styled";
-import { formatLog } from "./utils";
+import { createLogFormatter, getAllProcessUUIDs } from "./utils";
type LogsViewerProps = React.ComponentPropsWithoutRef<"div"> & {
logs: Log[];
@@ -14,7 +14,11 @@ type LogsViewerProps = React.ComponentPropsWithoutRef<"div"> & {
// TODO(egorgrushin): use it in frontend/src/metabase/admin/tools/components/Logs/Logs.tsx
export const LogsViewer = ({ logs, ...rest }: LogsViewerProps) => {
- const logText = useMemo(() => logs.map(formatLog).join("\n"), [logs]);
+ const logText = useMemo(() => {
+ const processUUIDs = getAllProcessUUIDs(logs);
+ const formatLog = createLogFormatter("ALL", processUUIDs);
+ return logs.map(formatLog).join("\n");
+ }, [logs]);
const displayLogs = useMemo(() => reactAnsiStyle(React, logText), [logText]);
return (
diff --git a/frontend/src/metabase/admin/tools/components/Logs/utils.ts b/frontend/src/metabase/admin/tools/components/Logs/utils.ts
index 39604c4bd12..81f1305b917 100644
--- a/frontend/src/metabase/admin/tools/components/Logs/utils.ts
+++ b/frontend/src/metabase/admin/tools/components/Logs/utils.ts
@@ -73,15 +73,20 @@ export function hasLog(logs: Log[], targetLog: Log): boolean {
return _.findLastIndex(logs, targetLog) > -1;
}
-export function filterLogs(logs: Log[], { process, query }: UrlState) {
+export function filterLogs(
+ logs: Log[],
+ { process, query }: UrlState,
+ processUUIDs: string[] = [],
+) {
const lowerCaseQuery = query ? query.toLowerCase() : "";
+ const formatLog = createLogFormatter(process, processUUIDs);
return logs.filter((log) => {
- const formattedLog = formatLog(log).join("\n");
-
const matchesProcessFilter =
!process || process === "ALL" || log.process_uuid === process;
- const matchesQueryFilter = formattedLog
+ // only search on text visible to the user to avoid matching hidden data (e.g. process UUID)
+ const matchesQueryFilter = formatLog(log)
+ .join("\n")
.toLowerCase()
.includes(lowerCaseQuery);
@@ -99,11 +104,15 @@ export function getAllProcessUUIDs(logs: Log[]) {
const formatTs = (ts: string) => dayjs(ts).format();
const memoedFormatTs = _.memoize(formatTs);
-export function formatLog(log: Log) {
- const timestamp = memoedFormatTs(log.timestamp);
- const uuid = log.process_uuid || "---";
- return [
- `[${uuid}] ${timestamp} ${log.level} ${log.fqns} ${log.msg}`,
- ...(log.exception || []),
- ];
-}
+export const createLogFormatter =
+ (process: string, processUUIDs: string[]) => (log: Log) => {
+ const timestamp = memoedFormatTs(log.timestamp);
+ const uuid =
+ process === "ALL" && processUUIDs.length > 1
+ ? `[${log.process_uuid}]`
+ : undefined;
+ return [
+ [uuid, timestamp, log.level, log.fqns, log.msg].filter(Boolean).join(" "),
+ ...(log.exception || []),
+ ];
+ };