Skip to content

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

feat(trace-detail-ui): filter by minObservationlvl (#5039)

Hassieb Pakzadcb6f95dc9382
diff --git a/web/src/components/trace/ObservationTree.tsx b/web/src/components/trace/ObservationTree.tsx
index 2def1295db..6a1c7ea65a 100644
--- a/web/src/components/trace/ObservationTree.tsx
+++ b/web/src/components/trace/ObservationTree.tsx
@@ -1,6 +1,11 @@
import { type NestedObservation } from "@/src/utils/types";
import { cn } from "@/src/utils/tailwind";
-import { type APIScore, type Trace, type $Enums } from "@langfuse/shared";
+import {
+ type APIScore,
+ type Trace,
+ type $Enums,
+ type ObservationLevel,
+} from "@langfuse/shared";
import { GroupedScoreBadges } from "@/src/components/grouped-score-badge";
import { Fragment, useMemo } from "react";
import { type ObservationReturnType } from "@/src/server/api/routers/traces";
@@ -39,10 +44,11 @@ export const ObservationTree = ({
traceCommentCounts?: Map<string, number>;
className?: string;
showExpandControls?: boolean;
+ minLevel?: ObservationLevel;
}) => {
const nestedObservations = useMemo(
- () => nestObservations(props.observations),
- [props.observations],
+ () => nestObservations(props.observations, props.minLevel),
+ [props.observations, props.minLevel],
);
const totalCost = useMemo(() => {
return calculateDisplayTotalCost({
diff --git a/web/src/components/trace/index.tsx b/web/src/components/trace/index.tsx
index bfb8940035..73b46384cc 100644
--- a/web/src/components/trace/index.tsx
+++ b/web/src/components/trace/index.tsx
@@ -5,6 +5,14 @@ import { TracePreview } from "./TracePreview";
import Header from "@/src/components/layouts/header";
import { Badge } from "@/src/components/ui/badge";
+import {
+ Select,
+ SelectContent,
+ SelectItem,
+ SelectTrigger,
+ SelectLabel,
+ SelectGroup,
+} from "@/src/components/ui/select";
import { AggUsageBadge } from "@/src/components/token-usage-badge";
import { StringParam, useQueryParam, withDefault } from "use-query-params";
import { PublishTraceSwitch } from "@/src/components/publish-object-switch";
@@ -22,6 +30,7 @@ import {
Award,
ChevronsDownUp,
ChevronsUpDown,
+ FilterIcon,
ListTree,
Network,
Percent,
@@ -31,7 +40,7 @@ import { useCallback, useState } from "react";
import { DeleteButton } from "@/src/components/deleteButton";
import { usePostHogClientCapture } from "@/src/features/posthog-analytics/usePostHogClientCapture";
import { TraceTimelineView } from "@/src/components/trace/TraceTimelineView";
-import { type APIScore } from "@langfuse/shared";
+import { type APIScore, ObservationLevel } from "@langfuse/shared";
import { FullScreenPage } from "@/src/components/layouts/full-screen-page";
import { calculateDisplayTotalCost } from "@/src/components/trace/lib/helpers";
import { useIsAuthenticatedAndProjectMember } from "@/src/features/auth/hooks";
@@ -74,6 +83,9 @@ export function Trace(props: {
[],
);
+ const [minObservationLevel, setMinObservationLevel] =
+ useState<ObservationLevel>(ObservationLevel.DEFAULT);
+
const isAuthenticatedAndProjectMember = useIsAuthenticatedAndProjectMember(
props.projectId,
);
@@ -223,6 +235,36 @@ export function Trace(props: {
>
<Percent className="h-4 w-4" />
</Toggle>
+ <Select
+ onValueChange={(v: ObservationLevel) => setMinObservationLevel(v)}
+ defaultValue={ObservationLevel.DEFAULT}
+ >
+ <SelectTrigger
+ hideDownIcon
+ className="focus:ring-none h-auto w-auto border-none px-0 py-0 text-sm focus:outline-none focus:ring-0 focus:ring-offset-0"
+ >
+ <Toggle
+ pressed={colorCodeMetricsOnObservationTree}
+ onPressedChange={(e) => setColorCodeMetricsOnObservationTree(e)}
+ size="xs"
+ title="Color code metrics (>50% yellow, >75% red)"
+ >
+ <FilterIcon className="h-4 w-4" />
+ </Toggle>
+ </SelectTrigger>
+ <SelectContent>
+ <SelectGroup>
+ <SelectLabel className="py-1 text-sm font-semibold">
+ Min. Level
+ </SelectLabel>
+ {Object.values(ObservationLevel).map((level) => (
+ <SelectItem value={level} key={level}>
+ {level}
+ </SelectItem>
+ ))}
+ </SelectGroup>
+ </SelectContent>
+ </Select>
</div>
<ObservationTree
@@ -240,6 +282,7 @@ export function Trace(props: {
colorCodeMetrics={colorCodeMetricsOnObservationTree}
observationCommentCounts={observationCommentCounts.data}
traceCommentCounts={traceCommentCounts.data}
+ minLevel={minObservationLevel}
className="flex w-full flex-col overflow-y-auto"
/>
</div>
diff --git a/web/src/components/trace/lib/helpers.ts b/web/src/components/trace/lib/helpers.ts
index dc77a72655..60bcfb87d3 100644
--- a/web/src/components/trace/lib/helpers.ts
+++ b/web/src/components/trace/lib/helpers.ts
@@ -1,4 +1,4 @@
-import { ObservationType } from "@langfuse/shared";
+import { ObservationLevel, ObservationType } from "@langfuse/shared";
import { type NestedObservation } from "@/src/utils/types";
import { type ObservationReturnType } from "@/src/server/api/routers/traces";
import Decimal from "decimal.js";
@@ -14,11 +14,17 @@ export const treeItemColors: Map<TreeItemType, string> = new Map([
export function nestObservations(
list: ObservationReturnType[],
+ minLevel?: ObservationLevel,
): NestedObservation[] {
if (list.length === 0) return [];
- // Data prep: Remove parentObservationId attribute from observations if the id does not exist in the list of observations
- const mutableList = list.map((o) => ({ ...o }));
+ // Data prep:
+ // - Filter for observations with minimum level
+ // - Remove parentObservationId attribute from observations if the id does not exist in the list of observations
+ const mutableList = list.filter((o) =>
+ getObservationLevels(minLevel).includes(o.level),
+ );
+
mutableList.forEach((observation) => {
if (
observation.parentObservationId &&
@@ -114,7 +120,9 @@ export function calculateDisplayTotalCost(p: {
curr.calculatedOutputCost ?? new Decimal(0),
),
)
- : curr.calculatedInputCost ?? curr.calculatedOutputCost ?? undefined;
+ : (curr.calculatedInputCost ??
+ curr.calculatedOutputCost ??
+ undefined);
}
if (!curr.calculatedTotalCost) return prev;
@@ -129,3 +137,18 @@ export function calculateDisplayTotalCost(p: {
return totalCost;
}
+
+function getObservationLevels(minLevel: ObservationLevel | undefined) {
+ const ascendingLevels = [
+ ObservationLevel.DEBUG,
+ ObservationLevel.DEFAULT,
+ ObservationLevel.WARNING,
+ ObservationLevel.ERROR,
+ ];
+
+ if (!minLevel) return ascendingLevels;
+
+ const minLevelIndex = ascendingLevels.indexOf(minLevel);
+
+ return ascendingLevels.slice(minLevelIndex);
+}
diff --git a/web/src/components/ui/select.tsx b/web/src/components/ui/select.tsx
index 7e9b168169..92dbec3ea3 100644
--- a/web/src/components/ui/select.tsx
+++ b/web/src/components/ui/select.tsx
@@ -14,7 +14,9 @@ const SelectValue = SelectPrimitive.Value;
const SelectTrigger = React.forwardRef<
React.ElementRef<typeof SelectPrimitive.Trigger>,
- React.ComponentPropsWithoutRef<typeof SelectPrimitive.Trigger>
+ React.ComponentPropsWithoutRef<typeof SelectPrimitive.Trigger> & {
+ hideDownIcon?: boolean;
+ }
>(({ className, children, ...props }, ref) => (
<SelectPrimitive.Trigger
ref={ref}
@@ -25,9 +27,11 @@ const SelectTrigger = React.forwardRef<
{...props}
>
{children}
- <SelectPrimitive.Icon asChild>
- <ChevronDown className="h-4 w-4 opacity-50" />
- </SelectPrimitive.Icon>
+ {props.hideDownIcon ? null : (
+ <SelectPrimitive.Icon asChild>
+ <ChevronDown className="h-4 w-4 opacity-50" />
+ </SelectPrimitive.Icon>
+ )}
</SelectPrimitive.Trigger>
));
SelectTrigger.displayName = SelectPrimitive.Trigger.displayName;