🪢 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(monitors): deep-link alerts to the traces/observations table for the breaching window (LFE-11022) (#15192)
Nikita Kabardin6db1f2408abf
diff --git a/packages/shared/src/features/monitors/processor/buildMonitorAlertSlackMessage.test.ts b/packages/shared/src/features/monitors/processor/buildMonitorAlertSlackMessage.test.tsindex 55abac5e5b..0d4d02b40b 100644--- a/packages/shared/src/features/monitors/processor/buildMonitorAlertSlackMessage.test.ts+++ b/packages/shared/src/features/monitors/processor/buildMonitorAlertSlackMessage.test.ts@@ -84,4 +84,73 @@ describe("buildMonitorAlertSlackMessage", () => {expect(inner.some((b: any) => b.type === "actions")).toBe(false);expect(inner.some((b: any) => b.type === "context")).toBe(true);});++ it("adds a 'View observations' button to the windowed data table when dataPermalink is set (observations view)", () => {+ const dataPermalink =+ "https://cloud.langfuse.com/project/proj_01/observations?dateRange=1779450930000-1779451230000";+ const { attachments } = buildMonitorAlertSlackMessage({+ ...mockMonitorAlert,+ dataPermalink,+ });+ const actions = attachments![0].blocks!.find(+ (b: any) => b.type === "actions",+ );+ expect(actions.elements).toHaveLength(2);+ // Config button kept.+ expect(actions.elements[0]).toMatchObject({+ text: { text: "View in Langfuse" },+ url: mockMonitorAlert.permalink,+ });+ // Windowed data-table button added.+ expect(actions.elements[1]).toMatchObject({+ text: { text: "View observations" },+ url: dataPermalink,+ });+ });++ it("labels the data button 'View traces' for a scores view", () => {+ const dataPermalink =+ "https://cloud.langfuse.com/project/proj_01/traces?dateRange=1779450930000-1779451230000";+ const { attachments } = buildMonitorAlertSlackMessage({+ ...mockMonitorAlert,+ view: "scores-numeric",+ dataPermalink,+ });+ const actions = attachments![0].blocks!.find(+ (b: any) => b.type === "actions",+ );+ expect(actions.elements).toHaveLength(2);+ expect(actions.elements[1]).toMatchObject({+ text: { text: "View traces" },+ url: dataPermalink,+ });+ });++ it("keeps only the config button when dataPermalink is absent", () => {+ const { attachments } = buildMonitorAlertSlackMessage(mockMonitorAlert);+ const actions = attachments![0].blocks!.find(+ (b: any) => b.type === "actions",+ );+ expect(actions.elements).toHaveLength(1);+ expect(actions.elements[0].text.text).toBe("View in Langfuse");+ });++ // buildAlert only sets dataPermalink for breaching severities, so recovery+ // (OK) and NO_DATA notifications arrive here with dataPermalink undefined and+ // must render no "View traces" button — recovery messages stay unchanged.+ it.each(["OK", "NO_DATA"] as const)(+ "renders no secondary data button for a %s (non-breach) alert",+ (severity) => {+ const { attachments } = buildMonitorAlertSlackMessage({+ ...mockMonitorAlert,+ severity,+ dataPermalink: undefined,+ });+ const actions = attachments![0].blocks!.find(+ (b: any) => b.type === "actions",+ );+ expect(actions.elements).toHaveLength(1);+ expect(actions.elements[0].text.text).toBe("View in Langfuse");+ },+ );});diff --git a/packages/shared/src/features/monitors/processor/buildMonitorAlertSlackMessage.ts b/packages/shared/src/features/monitors/processor/buildMonitorAlertSlackMessage.tsindex 2410f79a17..00789ed15d 100644--- a/packages/shared/src/features/monitors/processor/buildMonitorAlertSlackMessage.ts+++ b/packages/shared/src/features/monitors/processor/buildMonitorAlertSlackMessage.ts@@ -22,5 +22,8 @@ export function buildMonitorAlertSlackMessage(body: alert.message.body,timestamp: alert.timestamp,url: alert.permalink,+ secondaryUrl: alert.dataPermalink,+ secondaryLabel:+ alert.view === "observations" ? "View observations" : "View traces",});}diff --git a/packages/shared/src/features/monitors/processor/buildPermalink.test.ts b/packages/shared/src/features/monitors/processor/buildPermalink.test.tsindex 9ee35238b0..41c2cc9048 100644--- a/packages/shared/src/features/monitors/processor/buildPermalink.test.ts+++ b/packages/shared/src/features/monitors/processor/buildPermalink.test.ts@@ -5,7 +5,11 @@ const envMock = vi.hoisted(() => ({}));vi.mock("../../../env", () => envMock);-import { buildPermalink } from "./processor";+import {+ buildDataWindowPermalink,+ buildPermalink,+ isBreaching,+} from "./processor";describe("buildPermalink", () => {beforeEach(() => {@@ -23,3 +27,58 @@ describe("buildPermalink", () => {);});});++describe("buildDataWindowPermalink", () => {+ const from = new Date("2026-05-18T11:55:30.000Z"); // 1779450930000+ const to = new Date("2026-05-18T12:00:30.000Z"); // 1779451230000++ beforeEach(() => {+ envMock.env.NEXTAUTH_URL = undefined;+ });++ it("returns undefined when NEXTAUTH_URL is unset (self-hosted)", () => {+ expect(+ buildDataWindowPermalink("proj_01", "observations", from, to),+ ).toBeUndefined();+ });++ it("links an observations monitor to the observations table windowed by dateRange", () => {+ envMock.env.NEXTAUTH_URL = "https://cloud.langfuse.com";+ expect(buildDataWindowPermalink("proj_01", "observations", from, to)).toBe(+ `https://cloud.langfuse.com/project/proj_01/observations?dateRange=${from.getTime()}-${to.getTime()}`,+ );+ });++ it.each(["scores-numeric", "scores-categorical"] as const)(+ "links a %s monitor to the traces table windowed by dateRange",+ (view) => {+ envMock.env.NEXTAUTH_URL = "https://cloud.langfuse.com";+ expect(buildDataWindowPermalink("proj_01", view, from, to)).toBe(+ `https://cloud.langfuse.com/project/proj_01/traces?dateRange=${from.getTime()}-${to.getTime()}`,+ );+ },+ );++ it("strips a trailing slash on NEXTAUTH_URL", () => {+ envMock.env.NEXTAUTH_URL = "https://cloud.langfuse.com/";+ expect(buildDataWindowPermalink("proj_01", "observations", from, to)).toBe(+ `https://cloud.langfuse.com/project/proj_01/observations?dateRange=${from.getTime()}-${to.getTime()}`,+ );+ });+});++describe("isBreaching", () => {+ it.each(["ALERT", "WARNING"] as const)(+ "treats %s as a breach (gets a data-window link)",+ (severity) => {+ expect(isBreaching(severity)).toBe(true);+ },+ );++ it.each(["OK", "NO_DATA", "UNKNOWN", "PAUSED"] as const)(+ "treats %s as a non-breach (no data-window link)",+ (severity) => {+ expect(isBreaching(severity)).toBe(false);+ },+ );+});diff --git a/packages/shared/src/features/monitors/processor/processor.ts b/packages/shared/src/features/monitors/processor/processor.tsindex ee3f8d9256..f4653f9310 100644--- a/packages/shared/src/features/monitors/processor/processor.ts+++ b/packages/shared/src/features/monitors/processor/processor.ts@@ -28,7 +28,9 @@ import {MonitorSeveritySchema,MonitorStatusSchema,type MonitorAlert,+ type MonitorSeverity,type MonitorWindow,+ type MonitorView,type Monitor,} from "../types";import { applyStateMachine, type MonitorCompletion } from "./applyStateMachine";@@ -382,6 +384,17 @@ function buildAlert(args: {fromTimestamp,toTimestamp,permalink: buildPermalink(prev.projectId, prev.id),+ // Only a threshold-cross (ALERT/WARNING) links to the breaching data window;+ // recovery (OK) and lifecycle states (NO_DATA/UNKNOWN/PAUSED) would point at+ // a recovered/empty window, so they carry no data link.+ dataPermalink: isBreaching(next.severity)+ ? buildDataWindowPermalink(+ prev.projectId,+ prev.view,+ fromTimestamp,+ toTimestamp,+ )+ : undefined,message: renderAlertMessage({ monitor: prev, completion: next }),view: prev.view,filters: prev.filters,@@ -399,6 +412,37 @@ export function buildPermalink(return `${base}/project/${projectId}/monitors/${monitorId}`;}+/** isBreaching returns true for the threshold-cross severities (ALERT/WARNING) whose alert should deep-link to the breaching data window; OK (recovery) and the lifecycle states (NO_DATA/UNKNOWN/PAUSED) return false. */+export function isBreaching(severity: MonitorSeverity): boolean {+ return (+ severity === MonitorSeveritySchema.enum.ALERT ||+ severity === MonitorSeveritySchema.enum.WARNING+ );+}++/**+ * buildDataWindowPermalink composes the absolute Langfuse data-table URL scoped+ * to the breaching evaluation window, or undefined when NEXTAUTH_URL is unset.+ *+ * The window is encoded as the table's custom `?dateRange=<fromMs>-<toMs>`+ * param (absolute epoch-ms range; the client date-range parser accepts custom+ * ranges directly, bypassing the preset gating). An `observations` monitor+ * links to the observations table; every other view (scores-*) links to the+ * traces table, which is the row-level data users expect to inspect.+ */+export function buildDataWindowPermalink(+ projectId: string,+ view: MonitorView,+ fromTimestamp: Date,+ toTimestamp: Date,+): string | undefined {+ if (!env.NEXTAUTH_URL) return undefined;+ const base = env.NEXTAUTH_URL.replace(/\/$/, "");+ const table = view === "observations" ? "observations" : "traces";+ const dateRange = `${fromTimestamp.getTime()}-${toTimestamp.getTime()}`;+ return `${base}/project/${projectId}/${table}?dateRange=${dateRange}`;+}+/** toMonitorWebhookInputs fans an alert out to one webhook input per matched automation. */function toMonitorWebhookInputs(args: {alert: MonitorAlert;diff --git a/packages/shared/src/features/monitors/types.test.ts b/packages/shared/src/features/monitors/types.test.tsindex dd4192435d..266228405a 100644--- a/packages/shared/src/features/monitors/types.test.ts+++ b/packages/shared/src/features/monitors/types.test.ts@@ -212,6 +212,33 @@ describe("MonitorAlertSchema", () => {expect(MonitorAlertSchema.safeParse(withoutPermalink).success).toBe(true);});+ it("round-trips an optional dataPermalink", () => {+ const withDataPermalink = {+ ...validAlert,+ dataPermalink:+ "https://cloud.langfuse.com/project/proj_01/traces?dateRange=1779450930000-1779451230000",+ };+ const result = MonitorAlertSchema.safeParse(withDataPermalink);+ expect(result.success).toBe(true);+ if (result.success) {+ expect(result.data.dataPermalink).toBe(withDataPermalink.dataPermalink);+ }+ });++ it("accepts an alert without dataPermalink (backward-compatible in-flight message)", () => {+ // validAlert already omits dataPermalink.+ expect(MonitorAlertSchema.safeParse(validAlert).success).toBe(true);+ });++ it("rejects a relative (path-only) dataPermalink", () => {+ expect(+ MonitorAlertSchema.safeParse({+ ...validAlert,+ dataPermalink: "/project/proj_01/traces?dateRange=1-2",+ }).success,+ ).toBe(false);+ });+it("rejects a relative (path-only) permalink", () => {expect(MonitorAlertSchema.safeParse({diff --git a/packages/shared/src/features/monitors/types.ts b/packages/shared/src/features/monitors/types.tsindex 0f90a17c50..4ccc1f58b0 100644--- a/packages/shared/src/features/monitors/types.ts+++ b/packages/shared/src/features/monitors/types.ts@@ -299,6 +299,8 @@ export const MonitorAlertSchema = z.object({monitorId: z.string(),projectId: z.string(),permalink: z.url().optional(),+ /** dataPermalink deep-links to the data table scoped to the breaching window. Optional so in-flight webhook messages without it still parse. */+ dataPermalink: z.url().optional(),message: z.object({ title: z.string(), body: z.string() }),severity: MonitorSeveritySchema,timestamp: z.coerce.date(),diff --git a/packages/shared/src/server/services/buildColoredAttachmentSlackMessage.test.ts b/packages/shared/src/server/services/buildColoredAttachmentSlackMessage.test.tsindex f6dd9e9df5..c8d92f3318 100644--- a/packages/shared/src/server/services/buildColoredAttachmentSlackMessage.test.ts+++ b/packages/shared/src/server/services/buildColoredAttachmentSlackMessage.test.ts@@ -39,4 +39,40 @@ describe("buildColoredAttachmentSlackMessage", () => {expect(serialized).not.toContain("View in Langfuse");expect(serialized).toContain("*Something failed*");});++ it("renders a labeled secondary button after the primary when secondaryUrl is set", () => {+ const blocks = buildColoredAttachmentSlackMessage({+ ...base,+ secondaryUrl: "https://cloud.langfuse.com/y?dateRange=1-2",+ secondaryLabel: "View traces",+ }).attachments![0].blocks!;+ const actions = blocks.find((b: any) => b.type === "actions");+ expect(actions.elements).toHaveLength(2);+ expect(actions.elements[0]).toMatchObject({+ type: "button",+ text: { text: "View in Langfuse" },+ url: base.url,+ });+ expect(actions.elements[1]).toMatchObject({+ type: "button",+ text: { text: "View traces" },+ url: "https://cloud.langfuse.com/y?dateRange=1-2",+ });+ });++ it("defaults the secondary button label to 'View data'", () => {+ const blocks = buildColoredAttachmentSlackMessage({+ ...base,+ secondaryUrl: "https://cloud.langfuse.com/y?dateRange=1-2",+ }).attachments![0].blocks!;+ const actions = blocks.find((b: any) => b.type === "actions");+ expect(actions.elements[1].text.text).toBe("View data");+ });++ it("omits the secondary button when secondaryUrl is absent", () => {+ const blocks =+ buildColoredAttachmentSlackMessage(base).attachments![0].blocks!;+ const actions = blocks.find((b: any) => b.type === "actions");+ expect(actions.elements).toHaveLength(1);+ });});diff --git a/packages/shared/src/server/services/buildColoredAttachmentSlackMessage.ts b/packages/shared/src/server/services/buildColoredAttachmentSlackMessage.tsindex 6370b07f84..6f02d48419 100644--- a/packages/shared/src/server/services/buildColoredAttachmentSlackMessage.ts+++ b/packages/shared/src/server/services/buildColoredAttachmentSlackMessage.ts@@ -16,6 +16,14 @@ export function buildColoredAttachmentSlackMessage(args: {url?: string;/** contextText is an optional extra context element rendered next to the timestamp. */contextText?: string;+ /**+ * secondaryUrl, when set alongside `url`, renders a second action button+ * after "View in Langfuse" (e.g. a monitor alert's link to the breaching+ * data). Absent by default so project notifications stay single-button.+ */+ secondaryUrl?: string;+ /** secondaryLabel is the second button's label; defaults to "View data". */+ secondaryLabel?: string;}): SlackMessage {const { color } = args;const title = escapeSlackMrkdwn(args.title);@@ -60,6 +68,19 @@ export function buildColoredAttachmentSlackMessage(args: {},url: args.url,},+ ...(args.secondaryUrl+ ? [+ {+ type: "button",+ text: {+ type: "plain_text",+ text: args.secondaryLabel ?? "View data",+ emoji: true,+ },+ url: args.secondaryUrl,+ },+ ]+ : []),],},]