🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
feat(web): instrument trace playback controls in PostHog (#16362)
Nikita Kabardin54c45adb5b7f
diff --git a/web/src/features/posthog-analytics/usePostHogClientCapture.ts b/web/src/features/posthog-analytics/usePostHogClientCapture.tsindex 7d65d42578..43c2c1b0c4 100644--- a/web/src/features/posthog-analytics/usePostHogClientCapture.ts+++ b/web/src/features/posthog-analytics/usePostHogClientCapture.ts@@ -42,6 +42,13 @@ export const events = {// Fired from the tree, timeline, graph, and search-result click handlers;// `source` says which surface drove the navigation."node_selected",+ // Trace playhead transport in the navigation header (and the overflow+ // menu on a narrow panel). Distinguishes play vs pause vs stop; `viewMode`+ // is tree vs timeline at click time; `observationCount` is the loaded+ // trace size. Metadata only — never a trace/observation id.+ "playback_play",+ "playback_pause",+ "playback_stop",// Download from the large-string IO fallback (LFE-10991): a top-level// string over the render limit is shown as a bounded preview + download// instead of the full Pretty/JSON viewer. Measures how often users hit it.diff --git a/web/src/features/traces/components/PlaybackControls.clienttest.tsx b/web/src/features/traces/components/PlaybackControls.clienttest.tsxnew file mode 100644index 0000000000..a7d0644b61--- /dev/null+++ b/web/src/features/traces/components/PlaybackControls.clienttest.tsx@@ -0,0 +1,179 @@+import { fireEvent, render, screen } from "@testing-library/react";+import { beforeEach, describe, expect, it, vi } from "vitest";+import { PlaybackControls } from "./PlaybackControls";++const h = vi.hoisted(() => ({+ capture: vi.fn(),+ play: vi.fn(),+ pause: vi.fn(),+ stop: vi.fn(),+ isPlaying: false,+ showPlayhead: false,+ viewMode: null as string | null,+ observationCount: 42,+ dimensions: {+ isV4: true,+ traceContext: "fullscreen" as "fullscreen" | "peek" | "annotation",+ },+}));++vi.mock("@/src/features/posthog-analytics/usePostHogClientCapture", () => ({+ usePostHogClientCapture: () => h.capture,+}));++vi.mock("@/src/features/traces/hooks/useTraceAnalyticsDimensions", () => ({+ useTraceAnalyticsDimensions: () => h.dimensions,+}));++vi.mock("use-query-params", async () => {+ const actual = await vi.importActual("use-query-params");+ return {+ ...actual,+ useQueryParam: () => [h.viewMode, vi.fn()] as const,+ };+});++vi.mock("@/src/features/traces/contexts/TraceDataContext", () => ({+ useTraceData: () => ({+ traceDuration: 10,+ observations: Array.from({ length: h.observationCount }, (_, i) => ({+ id: `obs-${i}`,+ })),+ }),+}));++vi.mock("@/src/features/traces/contexts/TraceGraphDataContext", () => ({+ useTraceGraphData: () => ({+ isGraphViewAvailable: true,+ isLoading: false,+ }),+}));++vi.mock("@/src/features/traces/contexts/ViewPreferencesContext", () => ({+ useViewPreferences: () => ({ showGraph: true }),+}));++vi.mock("@/src/features/traces/contexts/SearchContext", () => ({+ useSearch: () => ({ searchQuery: "" }),+}));++vi.mock("@/src/features/traces/contexts/PlayheadContext", () => ({+ usePlayhead: () => ({+ play: h.play,+ pause: h.pause,+ stop: h.stop,+ getPlayheadSec: () => 0,+ subscribePosition: () => () => {},+ }),+ useIsPlaying: () => h.isPlaying,+ useShowPlayhead: () => h.showPlayhead,+}));++const payloadKeys = (call: unknown[]) =>+ call[1] && typeof call[1] === "object" ? Object.keys(call[1] as object) : [];++describe("PlaybackControls analytics", () => {+ beforeEach(() => {+ h.capture.mockClear();+ h.play.mockClear();+ h.pause.mockClear();+ h.stop.mockClear();+ h.isPlaying = false;+ h.showPlayhead = false;+ h.viewMode = null;+ h.observationCount = 42;+ h.dimensions = { isV4: true, traceContext: "fullscreen" };+ });++ it("captures play from tree with observation count, once", () => {+ render(<PlaybackControls />);++ fireEvent.click(+ screen.getByRole("button", { name: "Play trace over time" }),+ );++ expect(h.play).toHaveBeenCalledTimes(1);+ expect(h.capture).toHaveBeenCalledTimes(1);+ expect(h.capture).toHaveBeenCalledWith("trace_detail:playback_play", {+ viewMode: "tree",+ observationCount: 42,+ isV4: true,+ traceContext: "fullscreen",+ });+ expect(payloadKeys(h.capture.mock.calls[0])).toEqual([+ "viewMode",+ "observationCount",+ "isV4",+ "traceContext",+ ]);+ });++ it("captures play from timeline, not tree", () => {+ h.viewMode = "timeline";+ h.observationCount = 7;+ h.dimensions = { isV4: false, traceContext: "peek" };+ render(<PlaybackControls />);++ fireEvent.click(+ screen.getByRole("button", { name: "Play trace over time" }),+ );++ expect(h.capture).toHaveBeenCalledWith("trace_detail:playback_play", {+ viewMode: "timeline",+ observationCount: 7,+ isV4: false,+ traceContext: "peek",+ });+ });++ it("captures pause (not play) when the transport is already running", () => {+ h.isPlaying = true;+ render(<PlaybackControls />);++ fireEvent.click(screen.getByRole("button", { name: "Pause playback" }));++ expect(h.pause).toHaveBeenCalledTimes(1);+ expect(h.play).not.toHaveBeenCalled();+ expect(h.capture).toHaveBeenCalledTimes(1);+ expect(h.capture).toHaveBeenCalledWith("trace_detail:playback_pause", {+ viewMode: "tree",+ observationCount: 42,+ isV4: true,+ traceContext: "fullscreen",+ });+ });++ it("captures stop with the same dimensions as play", () => {+ h.viewMode = "timeline";+ h.showPlayhead = true;+ render(<PlaybackControls />);++ fireEvent.click(screen.getByRole("button", { name: "Stop playback" }));++ expect(h.stop).toHaveBeenCalledTimes(1);+ expect(h.capture).toHaveBeenCalledTimes(1);+ expect(h.capture).toHaveBeenCalledWith("trace_detail:playback_stop", {+ viewMode: "timeline",+ observationCount: 42,+ isV4: true,+ traceContext: "fullscreen",+ });+ });++ it("does not capture stop before a playhead is placed", () => {+ render(<PlaybackControls />);++ const stopButton = screen.getByRole("button", { name: "Stop playback" });+ expect(stopButton).toBeDisabled();+ fireEvent.click(stopButton);++ expect(h.stop).not.toHaveBeenCalled();+ expect(h.capture).not.toHaveBeenCalled();+ });++ it("does not capture when the store pauses without a click", () => {+ render(<PlaybackControls />);+ h.pause();+ expect(h.capture).not.toHaveBeenCalled();+ });+});diff --git a/web/src/features/traces/components/PlaybackControls.tsx b/web/src/features/traces/components/PlaybackControls.tsxindex a82d192a2f..98151eb525 100644--- a/web/src/features/traces/components/PlaybackControls.tsx+++ b/web/src/features/traces/components/PlaybackControls.tsx@@ -30,6 +30,8 @@ import { useTraceData } from "@/src/features/traces/contexts/TraceDataContext";import { useTraceGraphData } from "@/src/features/traces/contexts/TraceGraphDataContext";import { useSearch } from "@/src/features/traces/contexts/SearchContext";import { useViewPreferences } from "@/src/features/traces/contexts/ViewPreferencesContext";+import { usePostHogClientCapture } from "@/src/features/posthog-analytics/usePostHogClientCapture";+import { useTraceAnalyticsDimensions } from "@/src/features/traces/hooks/useTraceAnalyticsDimensions";// A 22px ring around the ~28px (h-7) button; 2px stroke reads at this size.const RING_SIZE = 22;@@ -68,12 +70,57 @@ export function useHasPlayback(): boolean {return traceDuration > 0 && (hasPlaybackSurface || showPlayhead);}+/**+ * Shared play/pause/stop handlers for the header buttons and the overflow+ * menu. Capture at this click seam (not the store's play/pause/stop): auto-pause+ * at end-of-trace must not look like a user stop, and a programmatic reset+ * must not inflate usage.+ */+function usePlaybackClickHandlers() {+ const capture = usePostHogClientCapture();+ const analyticsDimensions = useTraceAnalyticsDimensions();+ const { observations } = useTraceData();+ const [viewMode] = useQueryParam("view", StringParam);+ const { play, pause, stop } = usePlayhead();+ const isPlaying = useIsPlaying();+ const showPlayhead = useShowPlayhead();++ // Tree is stored as a null query param; anything else is still tree.+ const props = {+ viewMode:+ viewMode === "timeline" ? ("timeline" as const) : ("tree" as const),+ observationCount: observations.length,+ ...analyticsDimensions,+ };++ return {+ isPlaying,+ showPlayhead,+ handlePlayPause: () => {+ if (isPlaying) {+ capture("trace_detail:playback_pause", props);+ pause();+ } else {+ capture("trace_detail:playback_play", props);+ play();+ }+ },+ handleStop: () => {+ // Menu already disables Stop until a playhead exists; the header+ // button must do the same or idle clicks inflate playback_stop.+ if (!showPlayhead) return;+ capture("trace_detail:playback_stop", props);+ stop();+ },+ };+}+export function PlaybackControls() {const { traceDuration } = useTraceData();- const { play, pause, getPlayheadSec, subscribePosition, stop } =- usePlayhead();- const isPlaying = useIsPlaying();+ const { getPlayheadSec, subscribePosition } = usePlayhead();const hasPlayback = useHasPlayback();+ const { isPlaying, showPlayhead, handlePlayPause, handleStop } =+ usePlaybackClickHandlers();const ringRef = useRef<SVGCircleElement>(null);// Fill the ring to the current playhead fraction; update imperatively as the@@ -97,7 +144,7 @@ export function PlaybackControls() {type="button"variant="ghost"size="icon"- onClick={isPlaying ? pause : play}+ onClick={handlePlayPause}title={isPlaying ? "Pause playback" : "Play trace over time"}aria-label={isPlaying ? "Pause playback" : "Play trace over time"}className="relative h-7 w-7"@@ -141,7 +188,8 @@ export function PlaybackControls() {type="button"variant="ghost"size="icon"- onClick={stop}+ onClick={handleStop}+ disabled={!showPlayhead}title="Stop playback"aria-label="Stop playback"className="h-7 w-7"@@ -160,16 +208,15 @@ export function PlaybackControls() {* stays reachable while a playhead is placed.*/export function PlaybackMenuItems() {- const { play, pause, stop } = usePlayhead();- const isPlaying = useIsPlaying();- const showPlayhead = useShowPlayhead();const hasPlayback = useHasPlayback();+ const { isPlaying, showPlayhead, handlePlayPause, handleStop } =+ usePlaybackClickHandlers();if (!hasPlayback) return null;return (<>- <DropdownMenuItem onSelect={() => (isPlaying ? pause() : play())}>+ <DropdownMenuItem onSelect={handlePlayPause}>{isPlaying ? (<Pause className="mr-2 h-3.5 w-3.5" />) : (@@ -177,7 +224,7 @@ export function PlaybackMenuItems() {)}{isPlaying ? "Pause playback" : "Play trace over time"}</DropdownMenuItem>- <DropdownMenuItem onSelect={() => stop()} disabled={!showPlayhead}>+ <DropdownMenuItem onSelect={handleStop} disabled={!showPlayhead}><Square className="mr-2 h-3 w-3" />Stop playback</DropdownMenuItem>