Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(web): stop trace/observations tables crashing on unknown level values (#15538)

Nikita Kabardind662199757e3
diff --git a/web/src/components/level-colors.clienttest.ts b/web/src/components/level-colors.clienttest.ts
new file mode 100644
index 0000000000..52102fac6c
--- /dev/null
+++ b/web/src/components/level-colors.clienttest.ts
@@ -0,0 +1,50 @@
+import { getLevelColors, LevelColors } from "@/src/components/level-colors";
+
+describe("getLevelColors", () => {
+ it("returns the correct style for each known level", () => {
+ expect(getLevelColors("DEFAULT")).toEqual(LevelColors.DEFAULT);
+ expect(getLevelColors("DEBUG")).toEqual(LevelColors.DEBUG);
+ expect(getLevelColors("WARNING")).toEqual(LevelColors.WARNING);
+ expect(getLevelColors("ERROR")).toEqual(LevelColors.ERROR);
+ });
+
+ // Level is user-/OTel-controlled data (an open string, not a closed enum), so
+ // every one of these must return the neutral fallback rather than throw.
+ it.each([
+ ["INFO (OTel severity)", "INFO"],
+ ["lowercase info", "info"],
+ ["empty string", ""],
+ ["a made-up custom level", "SUPER_CRITICAL"],
+ // Object.prototype member names must NOT match (guard uses Object.hasOwn,
+ // not `in`, which would walk the prototype chain and return a Function).
+ ["toString", "toString"],
+ ["constructor", "constructor"],
+ ["valueOf", "valueOf"],
+ ["hasOwnProperty", "hasOwnProperty"],
+ ])(
+ "returns the neutral fallback for %s without throwing",
+ (_label, level) => {
+ expect(() => getLevelColors(level)).not.toThrow();
+ expect(getLevelColors(level)).toEqual({ text: "", bg: "" });
+ },
+ );
+
+ it.each([
+ ["null", null],
+ ["undefined", undefined],
+ ])(
+ "returns the neutral fallback for %s without throwing",
+ (_label, level) => {
+ expect(() => getLevelColors(level)).not.toThrow();
+ expect(getLevelColors(level)).toEqual({ text: "", bg: "" });
+ },
+ );
+
+ it("always returns an object exposing .bg and .text (safe to dereference)", () => {
+ for (const level of ["ERROR", "INFO", "", null, undefined]) {
+ const colors = getLevelColors(level);
+ expect(typeof colors.bg).toBe("string");
+ expect(typeof colors.text).toBe("string");
+ }
+ });
+});
diff --git a/web/src/components/level-colors.tsx b/web/src/components/level-colors.tsx
index 8208706a2d..9074985bc0 100644
--- a/web/src/components/level-colors.tsx
+++ b/web/src/components/level-colors.tsx
@@ -9,6 +9,28 @@ export const LevelColors = {
ERROR: { text: "text-dark-red", bg: "bg-light-red" },
};
+export type LevelColor = { text: string; bg: string };
+
+const NEUTRAL_LEVEL_COLOR: LevelColor = { text: "", bg: "" };
+
+/**
+ * Total color lookup for a level value. A level is user-/OTel-controlled data
+ * (an open string — e.g. an OTel severity like "INFO", lowercase "info", or a
+ * custom SDK level — not a closed enum), so `LevelColors[level]` may be
+ * `undefined`. Dereferencing `.bg`/`.text` on that undefined crashed the
+ * trace/observations tables (LFE-14567). Every color lookup must therefore be
+ * total: unknown or absent levels get a neutral (no color chip) fallback.
+ */
+export function getLevelColors(level: string | null | undefined): LevelColor {
+ // Own-property check (NOT `in`): `in` walks the prototype chain, so a level
+ // literally named "toString"/"constructor"/"valueOf" would otherwise match
+ // and return an inherited `Object.prototype` function instead of a
+ // `{ text, bg }` — breaking the "total" contract for that sliver of inputs.
+ return level != null && Object.hasOwn(LevelColors, level)
+ ? LevelColors[level as keyof typeof LevelColors]
+ : NEUTRAL_LEVEL_COLOR;
+}
+
// Colored bar per level value for the Status filter facet (LFE-10883):
// red = error, yellow = warning, green = the ok/default path, muted = debug.
const levelBarColors: Record<string, string> = {
diff --git a/web/src/components/table/use-cases/observations.tsx b/web/src/components/table/use-cases/observations.tsx
index a87834cabd..531a6596b3 100644
--- a/web/src/components/table/use-cases/observations.tsx
+++ b/web/src/components/table/use-cases/observations.tsx
@@ -51,7 +51,7 @@ import {
} from "@/src/components/table/loading-cells";
import { type LangfuseColumnDef } from "@/src/components/table/types";
import { cn } from "@/src/utils/tailwind";
-import { LevelColors } from "@/src/components/level-colors";
+import { getLevelColors } from "@/src/components/level-colors";
import { numberFormatter, usdFormatter } from "@/src/utils/numbers";
import { useOrderByState } from "@/src/features/orderBy/hooks/useOrderByState";
import { useRowHeightLocalStorage } from "@/src/components/table/data-table-row-height-switch";
@@ -791,8 +791,8 @@ export default function ObservationsTable({
<span
className={cn(
"rounded-sm p-0.5 text-xs",
- LevelColors[value].bg,
- LevelColors[value].text,
+ getLevelColors(value).bg,
+ getLevelColors(value).text,
)}
>
{value}
diff --git a/web/src/components/table/use-cases/traces.tsx b/web/src/components/table/use-cases/traces.tsx
index 7b5dddbd48..d0a11a6afd 100644
--- a/web/src/components/table/use-cases/traces.tsx
+++ b/web/src/components/table/use-cases/traces.tsx
@@ -30,7 +30,7 @@ import {
import { DeleteTraceButton } from "@/src/components/deleteButton";
import {
formatAsLabel,
- LevelColors,
+ getLevelColors,
LevelSymbols,
} from "@/src/components/level-colors";
import { cn } from "@/src/utils/tailwind";
@@ -1074,8 +1074,8 @@ export default function TracesTable({
<span
className={cn(
"rounded-sm p-0.5 text-xs",
- LevelColors[value].bg,
- LevelColors[value].text,
+ getLevelColors(value).bg,
+ getLevelColors(value).text,
)}
>
{value}
diff --git a/web/src/components/trace/components/SpanContent.tsx b/web/src/components/trace/components/SpanContent.tsx
index 0b8911ff81..8500df819b 100644
--- a/web/src/components/trace/components/SpanContent.tsx
+++ b/web/src/components/trace/components/SpanContent.tsx
@@ -19,7 +19,7 @@
import { type TreeNode } from "../lib/types";
import { GroupedScoreBadges } from "@/src/components/grouped-score-badge";
-import { LevelColors } from "@/src/components/level-colors";
+import { getLevelColors } from "@/src/components/level-colors";
import { CommentCountIcon } from "@/src/features/comments/CommentCountIcon";
import { cn } from "@/src/utils/tailwind";
import { formatIntervalSeconds } from "@/src/utils/dates";
@@ -153,8 +153,8 @@ export function SpanContent({
<span
className={cn(
"rounded-sm p-0.5 text-xs",
- LevelColors[node.level as keyof typeof LevelColors]?.bg,
- LevelColors[node.level as keyof typeof LevelColors]?.text,
+ getLevelColors(node.level).bg,
+ getLevelColors(node.level).text,
)}
>
{node.level}
diff --git a/web/src/features/events/components/EventsTable.tsx b/web/src/features/events/components/EventsTable.tsx
index 870c56dce5..855ffd1989 100644
--- a/web/src/features/events/components/EventsTable.tsx
+++ b/web/src/features/events/components/EventsTable.tsx
@@ -42,7 +42,7 @@ import {
import { type LangfuseColumnDef } from "@/src/components/table/types";
import { filterStateToQueryText } from "@/src/features/search-bar/lib/filter-state-to-query";
import { cn } from "@/src/utils/tailwind";
-import { LevelColors } from "@/src/components/level-colors";
+import { getLevelColors } from "@/src/components/level-colors";
import {
compactNumberFormatter,
numberFormatter,
@@ -1288,8 +1288,8 @@ export default function ObservationsEventsTable({
<span
className={cn(
"rounded-sm p-0.5 text-xs",
- LevelColors[value].bg,
- LevelColors[value].text,
+ getLevelColors(value).bg,
+ getLevelColors(value).text,
)}
>
{value}