Skip to content

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

fix(trace-timeline): overflow and scroll behaviour (#6216)

marliessophiedaeaf0a98664
diff --git a/web/src/components/grouped-score-badge.tsx b/web/src/components/grouped-score-badge.tsx
index 76f8e31810..2db8b9e069 100644
--- a/web/src/components/grouped-score-badge.tsx
+++ b/web/src/components/grouped-score-badge.tsx
@@ -5,13 +5,69 @@ import {
HoverCardTrigger,
} from "@/src/components/ui/hover-card";
import { MessageCircleMoreIcon } from "lucide-react";
-
import { type APIScore, type LastUserScore } from "@langfuse/shared";
+const partitionScores = <T extends APIScore | LastUserScore>(
+ scores: Record<string, T[]>,
+ maxVisible?: number,
+) => {
+ const sortedScores = Object.entries(scores).sort(([a], [b]) =>
+ a < b ? -1 : 1,
+ );
+ if (!maxVisible) return { visibleScores: sortedScores, hiddenScores: [] };
+
+ const visibleScores = sortedScores.slice(0, maxVisible);
+ const hiddenScores = sortedScores.slice(maxVisible);
+ return { visibleScores, hiddenScores };
+};
+
+const ScoreGroupBadge = <T extends APIScore | LastUserScore>({
+ name,
+ scores,
+}: {
+ name: string;
+ scores: T[];
+}) => {
+ return (
+ <Badge
+ variant="tertiary"
+ key={name}
+ className="flex items-center gap-1 px-2.5 text-xs font-normal"
+ >
+ <div className="w-fit max-w-20 truncate" title={name}>
+ {name}:
+ </div>
+ <div className="flex items-center gap-1 text-nowrap">
+ {scores.map((s, i) => (
+ <span
+ key={i}
+ className="group/score ml-1 flex items-center gap-1 rounded-sm first:ml-0"
+ >
+ {s.stringValue ?? s.value?.toFixed(2) ?? ""}
+ {s.comment && (
+ <HoverCard>
+ <HoverCardTrigger className="inline-block">
+ <MessageCircleMoreIcon className="mb-[0.0625rem] !size-3" />
+ </HoverCardTrigger>
+ <HoverCardContent className="overflow-hidden whitespace-normal break-normal">
+ <p>{s.comment}</p>
+ </HoverCardContent>
+ </HoverCard>
+ )}
+ <span className="group-last/score:hidden">,</span>
+ </span>
+ ))}
+ </div>
+ </Badge>
+ );
+};
+
export const GroupedScoreBadges = <T extends APIScore | LastUserScore>({
scores,
+ maxVisible,
}: {
scores: T[];
+ maxVisible?: number;
}) => {
const groupedScores = scores.reduce<Record<string, T[]>>((acc, score) => {
if (!acc[score.name] || !Array.isArray(acc[score.name])) {
@@ -22,42 +78,35 @@ export const GroupedScoreBadges = <T extends APIScore | LastUserScore>({
return acc;
}, {});
+ const { visibleScores, hiddenScores } = partitionScores(
+ groupedScores,
+ maxVisible,
+ );
+
return (
<>
- {Object.entries(groupedScores)
- .sort(([a], [b]) => (a < b ? -1 : 1))
- .map(([name, scores]) => (
- <Badge
- variant="tertiary"
- key={name}
- className="flex items-center gap-1 px-2.5 text-xs font-normal"
- >
- <div className="w-fit max-w-20 truncate" title={name}>
- {name}:
- </div>
- <div className="flex items-center gap-1 text-nowrap">
- {scores.map((s, i) => (
- <span
- key={i}
- className="group/score ml-1 flex items-center gap-1 rounded-sm first:ml-0"
- >
- {s.stringValue ?? s.value?.toFixed(2) ?? ""}
- {s.comment && (
- <HoverCard>
- <HoverCardTrigger className="inline-block">
- <MessageCircleMoreIcon className="mb-[0.0625rem] !size-3" />
- </HoverCardTrigger>
- <HoverCardContent className="overflow-hidden whitespace-normal break-normal">
- <p>{s.comment}</p>
- </HoverCardContent>
- </HoverCard>
- )}
- <span className="group-last/score:hidden">,</span>
- </span>
+ {visibleScores.map(([name, scores]) => (
+ <ScoreGroupBadge key={name} name={name} scores={scores} />
+ ))}
+ {Boolean(hiddenScores.length) && (
+ <HoverCard>
+ <HoverCardTrigger className="inline-block rounded-sm">
+ <Badge
+ className="cursor-pointer px-1 text-xs font-medium"
+ variant="tertiary"
+ >
+ +{hiddenScores.length}
+ </Badge>
+ </HoverCardTrigger>
+ <HoverCardContent className="max-h-[300px] max-w-[200px] overflow-y-auto p-2">
+ <div className="flex flex-wrap gap-1">
+ {hiddenScores.map(([name, scores]) => (
+ <ScoreGroupBadge key={name} name={name} scores={scores} />
))}
</div>
- </Badge>
- ))}
+ </HoverCardContent>
+ </HoverCard>
+ )}
</>
);
};
diff --git a/web/src/components/trace/TraceTimelineView.tsx b/web/src/components/trace/TraceTimelineView.tsx
index 1597a4830d..2638a5f31b 100644
--- a/web/src/components/trace/TraceTimelineView.tsx
+++ b/web/src/components/trace/TraceTimelineView.tsx
@@ -6,7 +6,13 @@ import {
ObservationLevel,
type ObservationLevelType,
} from "@langfuse/shared";
-import React, { useEffect, useMemo, useRef, useState } from "react";
+import React, {
+ useEffect,
+ useMemo,
+ useRef,
+ useState,
+ useLayoutEffect,
+} from "react";
import { SimpleTreeView } from "@mui/x-tree-view/SimpleTreeView";
import { TreeItem } from "@mui/x-tree-view/TreeItem";
import type Decimal from "decimal.js";
@@ -93,7 +99,10 @@ function TreeItemInner({
className={cn("group my-0.5 flex w-full min-w-fit flex-row items-center")}
>
<div className="flex items-center" style={{ width: `${SCALE_WIDTH}px` }}>
- <div className={`relative w-[${SCALE_WIDTH}px] flex flex-row`}>
+ <div
+ className={`relative flex flex-row`}
+ style={{ width: `${SCALE_WIDTH}px` }}
+ >
{firstTokenTimeOffset ? (
<div
className={cn(
@@ -103,7 +112,6 @@ function TreeItemInner({
: "group-hover:ring group-hover:ring-tertiary",
)}
style={{ marginLeft: `${startOffset}px` }}
- title="First token"
>
<div
className={cn(
@@ -168,8 +176,8 @@ function TreeItemInner({
</span>
)}
{showScores && scores && scores.length > 0 && (
- <div className="flex max-h-8 flex-wrap gap-1 overflow-y-auto">
- <GroupedScoreBadges scores={scores} />
+ <div className="flex max-h-8 gap-1">
+ <GroupedScoreBadges scores={scores} maxVisible={3} />
</div>
)}
</div>
@@ -237,8 +245,8 @@ function TreeItemInner({
</span>
)}
{showScores && scores && scores.length > 0 && (
- <div className="flex max-h-8 flex-wrap gap-1 overflow-y-auto">
- <GroupedScoreBadges scores={scores} />
+ <div className="flex max-h-8 gap-1">
+ <GroupedScoreBadges scores={scores} maxVisible={3} />
</div>
)}
</div>
@@ -456,7 +464,7 @@ export function TraceTimelineView({
return () => {
window.removeEventListener("resize", handleResize);
};
- }, [parentRef]);
+ }, []);
const isAuthenticatedAndProjectMember =
useIsAuthenticatedAndProjectMember(projectId);
@@ -494,8 +502,26 @@ export function TraceTimelineView({
},
);
- if (!latency) return null;
+ const timeIndexRef = useRef<HTMLDivElement>(null);
+ const timelineContentRef = useRef<HTMLDivElement>(null);
+ const outerContainerRef = useRef<HTMLDivElement>(null);
+
+ const [contentWidth, setContentWidth] = useState(SCALE_WIDTH);
+ // Use a useLayoutEffect to measure the actual content width after rendering
+ useLayoutEffect(() => {
+ if (!timelineContentRef.current) return;
+
+ // Use scrollWidth which accounts for all content, including overflow
+ const scrollWidth = timelineContentRef.current.scrollWidth;
+
+ const newWidth = Math.max(SCALE_WIDTH, scrollWidth);
+ if (newWidth !== contentWidth) {
+ setContentWidth(newWidth);
+ }
+ }, [observations, expandedItems, contentWidth]);
+
+ if (!latency) return null;
const stepSize = calculateStepSize(latency, SCALE_WIDTH);
const totalScaleSpan = stepSize * (SCALE_WIDTH / STEP_SIZE);
@@ -504,130 +530,172 @@ export function TraceTimelineView({
return (
<div ref={parentRef} className="h-full w-full px-3">
- <div className="flex max-h-full flex-col" style={{ width: cardWidth }}>
- <div style={{ width: `${SCALE_WIDTH + 8}px` }} className="mb-2 ml-2">
+ <div className="relative flex max-h-full flex-col">
+ {/* Sticky time index section - positioned absolutely at the top */}
+ <div className="sticky top-0 z-20 bg-background">
<div
- className="relative mr-2 h-8"
- style={{ width: `${SCALE_WIDTH}px` }}
+ ref={timeIndexRef}
+ className="overflow-x-auto"
+ style={{
+ width: cardWidth,
+ scrollbarWidth: "none" /* Firefox */,
+ msOverflowStyle: "none" /* IE and Edge */,
+ WebkitOverflowScrolling: "touch",
+ }}
+ onScroll={(e) => {
+ if (outerContainerRef.current) {
+ outerContainerRef.current.scrollLeft =
+ e.currentTarget.scrollLeft;
+ }
+ }}
>
- {Array.from({ length: SCALE_WIDTH / STEP_SIZE + 1 }).map(
- (_, index) => {
- const step = stepSize * index;
- const isLastStep = index === SCALE_WIDTH / STEP_SIZE;
-
- return isLastStep ? (
- <span
- className="absolute -right-2 text-xs text-muted-foreground"
- key={index}
- >
- {step.toFixed(latency.toString().length >= 8 ? 0 : 2)}s
- </span>
- ) : (
- <div
- key={index}
- className="absolute h-full border border-l text-xs"
- style={{ left: `${index * STEP_SIZE}px` }}
- >
- <span className="absolute left-2 text-xs text-muted-foreground">
- {step.toFixed(2)}s
- </span>
- </div>
- );
- },
- )}
+ <div style={{ width: `${contentWidth}px` }}>
+ <div className="mb-2 ml-2">
+ <div
+ className="relative mr-2 h-8"
+ style={{ width: `${SCALE_WIDTH}px` }}
+ >
+ {Array.from({
+ length: Math.ceil(SCALE_WIDTH / STEP_SIZE) + 1,
+ }).map((_, index) => {
+ const step = stepSize * index;
+
+ return (
+ <div
+ key={index}
+ className="absolute h-full border border-l text-xs"
+ style={{ left: `${index * STEP_SIZE}px` }}
+ >
+ <span className="absolute left-2 text-xs text-muted-foreground">
+ {step.toFixed(2)}s
+ </span>
+ </div>
+ );
+ })}
+
+ {/* Add end marker if content exceeds scale */}
+ {contentWidth > SCALE_WIDTH && (
+ <div
+ className="absolute h-full border-r border-dashed"
+ style={{ left: `${SCALE_WIDTH}px` }}
+ />
+ )}
+ </div>
+ </div>
+ </div>
</div>
</div>
+
+ {/* Main content with scrolling */}
<div
- style={{ width: `${SCALE_WIDTH}px` }}
- className="overflow-y-auto overflow-x-hidden"
+ ref={outerContainerRef}
+ className="overflow-x-auto"
+ style={{ width: cardWidth }}
+ onScroll={(e) => {
+ if (timeIndexRef.current) {
+ timeIndexRef.current.scrollLeft = e.currentTarget.scrollLeft;
+ }
+ }}
>
- <SimpleTreeView
- expandedItems={expandedItems}
- onExpandedItemsChange={(_, itemIds) => setExpandedItems(itemIds)}
- itemChildrenIndentation={TREE_INDENTATION}
- expansionTrigger="iconContainer"
- >
- <TreeItem
- key={`trace-${id}`}
- itemId={`trace-${id}`}
- classes={{
- content: `!min-w-fit hover:!bg-background`,
- selected: "!bg-background !important",
- label: "!min-w-fit",
- iconContainer: "absolute left-3 top-1/2 z-10 -translate-y-1/2",
- }}
- onClick={(e) => {
- const isIconClick = (e.target as HTMLElement).closest(
- "svg.MuiSvgIcon-root",
- );
- if (!isIconClick) {
- setCurrentObservationId(null);
- }
- }}
- label={
- <TreeItemInner
- name={name}
- latency={latency}
- totalScaleSpan={totalScaleSpan}
- type="TRACE"
- hasChildren={!!nestedObservations.length}
- isSelected={currentObservationId === null}
- showMetrics={showMetrics}
- showScores={showScores}
- showComments={showComments}
- colorCodeMetrics={colorCodeMetrics}
- scores={traceScores}
- commentCount={traceCommentCounts.data?.get(id)}
- totalCost={totalCost}
- />
- }
+ <div style={{ width: `${contentWidth}px` }}>
+ {/* Main timeline content */}
+ <div
+ ref={timelineContentRef}
+ className="overflow-y-auto"
+ style={{ width: `${contentWidth}px` }}
>
- {Boolean(nestedObservations.length)
- ? nestedObservations.map((observation) => (
- <TraceTreeItem
- key={`observation-${observation.id}`}
- observation={observation}
- level={1}
- traceStartTime={nestedObservations[0].startTime}
+ <SimpleTreeView
+ expandedItems={expandedItems}
+ onExpandedItemsChange={(_, itemIds) =>
+ setExpandedItems(itemIds)
+ }
+ itemChildrenIndentation={TREE_INDENTATION}
+ expansionTrigger="iconContainer"
+ >
+ <TreeItem
+ key={`trace-${id}`}
+ itemId={`trace-${id}`}
+ classes={{
+ content: `!min-w-fit hover:!bg-background`,
+ selected: "!bg-background !important",
+ label: "!min-w-fit",
+ iconContainer:
+ "absolute left-3 top-1/2 z-10 -translate-y-1/2",
+ }}
+ onClick={(e) => {
+ const isIconClick = (e.target as HTMLElement).closest(
+ "svg.MuiSvgIcon-root",
+ );
+ if (!isIconClick) {
+ setCurrentObservationId(null);
+ }
+ }}
+ label={
+ <TreeItemInner
+ name={name}
+ latency={latency}
totalScaleSpan={totalScaleSpan}
- projectId={projectId}
- scores={scores}
- observations={observations}
- cardWidth={cardWidth}
- commentCounts={observationCommentCounts.data}
- currentObservationId={currentObservationId}
- setCurrentObservationId={setCurrentObservationId}
+ type="TRACE"
+ hasChildren={!!nestedObservations.length}
+ isSelected={currentObservationId === null}
showMetrics={showMetrics}
showScores={showScores}
showComments={showComments}
colorCodeMetrics={colorCodeMetrics}
- parentTotalDuration={totalDuration}
- parentTotalCost={totalCost}
+ scores={traceScores}
+ commentCount={traceCommentCounts.data?.get(id)}
+ totalCost={totalCost}
/>
- ))
- : null}
- </TreeItem>
- </SimpleTreeView>
-
- {minLevel && hiddenObservationsCount > 0 ? (
- <div className="flex items-center gap-1 p-2 py-4">
- <InfoIcon className="h-4 w-4 text-muted-foreground" />
- <span className="flex flex-row gap-1 text-sm text-muted-foreground">
- <p>
- {hiddenObservationsCount} observations below {minLevel} level
- are hidden.
- </p>
- {setMinLevel && (
- <p
- className="cursor-pointer underline"
- onClick={() => setMinLevel(ObservationLevel.DEBUG)}
- >
- Show all
- </p>
- )}
- </span>
+ }
+ >
+ {Boolean(nestedObservations.length)
+ ? nestedObservations.map((observation) => (
+ <TraceTreeItem
+ key={`observation-${observation.id}`}
+ observation={observation}
+ level={1}
+ traceStartTime={nestedObservations[0].startTime}
+ totalScaleSpan={totalScaleSpan}
+ projectId={projectId}
+ scores={scores}
+ observations={observations}
+ cardWidth={cardWidth}
+ commentCounts={observationCommentCounts.data}
+ currentObservationId={currentObservationId}
+ setCurrentObservationId={setCurrentObservationId}
+ showMetrics={showMetrics}
+ showScores={showScores}
+ showComments={showComments}
+ colorCodeMetrics={colorCodeMetrics}
+ parentTotalDuration={totalDuration}
+ parentTotalCost={totalCost}
+ />
+ ))
+ : null}
+ </TreeItem>
+ </SimpleTreeView>
+
+ {minLevel && hiddenObservationsCount > 0 ? (
+ <div className="flex items-center gap-1 p-2 py-4">
+ <InfoIcon className="h-4 w-4 text-muted-foreground" />
+ <span className="flex flex-row gap-1 text-sm text-muted-foreground">
+ <p>
+ {hiddenObservationsCount} observations below {minLevel}
+ level are hidden.
+ </p>
+ {setMinLevel && (
+ <p
+ className="cursor-pointer underline"
+ onClick={() => setMinLevel(ObservationLevel.DEBUG)}
+ >
+ Show all
+ </p>
+ )}
+ </span>
+ </div>
+ ) : null}
</div>
- ) : null}
+ </div>
</div>
</div>
</div>