diff --git a/.agents/skills/verify-toolkit/map/mcp-apps.md b/.agents/skills/verify-toolkit/map/mcp-apps.md index fa6374685..810feacd0 100644 --- a/.agents/skills/verify-toolkit/map/mcp-apps.md +++ b/.agents/skills/verify-toolkit/map/mcp-apps.md @@ -15,7 +15,10 @@ the host theme, and calls the tool again through the host. The goals are in - `view-theme` The view follows the host's light or dark theme. Proof: `drive`. - `range-roundtrip` The range picker calls `render_sentry_chart` through the host with the new period and redraws without remounting. Proof: `drive`. - `open-link` "Open in Sentry" asks the host to open the result's Sentry URL with `ui/open-link`. Proof: `drive`. -- `grouped` A `groupBy` result draws one series per top group, with a legend. Proof: `drive`. +- `grouped` A `groupBy` result draws one series per top group, with a legend that shows each group's short label and event count, and no peak, since one group's bucket would read as the chart's peak. Proof: `drive`. +- `top-by-volume` A grouped chart of a non-additive aggregate, such as p95 by release, plots the groups with the most events, not the sparse groups with the highest values. Proof: `drive`. +- `null-gaps` A bucket with no data is null in the result, not 0, and the view draws it as a gap; a bucket with no data on either side is a dot, and its tooltip row reads "no data". Proof: `drive`. +- `compact-result` The result for 5 series of 168 buckets (7 days at 1h) stays under 16 KiB, and its text content is the same JSON. Proof: `drive`. - `lifecycle` The view follows a `host-context-changed` theme change, shows a cancelled state on `ui/notifications/tool-cancelled`, and answers `ui/resource-teardown`. Proof: `drive`. - `default-csp` The view loads and runs under the spec's default CSP with no console errors, so it needs no `csp` domains. Proof: `drive`. @@ -31,12 +34,13 @@ Preconditions: - Launch from `../SKILL.md` has run on this checkout, and `$VERIFY_RUN` is set. - A Chromium build for playwright-core is installed. If the script cannot launch it, run `pnpm --filter @sentry/mcp-core exec playwright-core install chromium-headless-shell`. -- **Every `drive` claim: act as the host.** Run `node .agents/skills/verify-toolkit/scripts/mcp-apps-host.mjs --out "$VERIFY_RUN/mcp-apps"`. It starts the mock server, calls the tool, reads the view, and mounts it in headless Chromium inside a host page that uses `AppBridge`. Every line it prints, also in `checks.txt`, is `PASS`. The screenshots `chart-light.png`, `chart-dark.png`, `chart-range-7d.png`, and `chart-grouped-dark.png` show each state. `host-log.json` holds the view's `tools/call` and `ui/open-link` requests to the host. +- **Every `drive` claim: act as the host.** Run `node .agents/skills/verify-toolkit/scripts/mcp-apps-host.mjs --out "$VERIFY_RUN/mcp-apps"`. It starts the mock server, calls the tool, reads the view, and mounts it in headless Chromium inside a host page that uses `AppBridge`. Every line it prints, also in `checks.txt`, is `PASS`. Its grouped call is `p95(span.duration)` by `release` over 7 days at 1h. The screenshots `chart-light.png`, `chart-dark.png`, `chart-range-7d.png`, and `chart-grouped-dark.png` show each state. `host-log.json` holds the view's `tools/call` and `ui/open-link` requests to the host. ## Gotchas - The mock server answers from built packages. Rerun `pnpm run build` after changing mcp-core or the mocks, or the script drives stale code. - The view is generated. After changing `packages/mcp-core/chart-app/`, run `pnpm --filter @sentry/mcp-core generate-chart-app`, then build. +- For a grouped request, the mock returns releases in the order that the request's `sort` gives. Three releases are busy; five have 1 to 57 events, every one at 30s, so a sort by p95 picks those five and leaves most of their buckets null. - The host page sets the default CSP with a meta tag in `srcdoc`. Real hosts serve the view from a sandbox origin with CSP headers. ## Gaps diff --git a/.agents/skills/verify-toolkit/scripts/mcp-apps-host.mjs b/.agents/skills/verify-toolkit/scripts/mcp-apps-host.mjs index c322708d1..6f5df2238 100644 --- a/.agents/skills/verify-toolkit/scripts/mcp-apps-host.mjs +++ b/.agents/skills/verify-toolkit/scripts/mcp-apps-host.mjs @@ -3,8 +3,11 @@ // stdio server, call the tool, read its `ui://` view, and mount the view in a // headless browser through the ext-apps AppBridge, the way Claude or VS Code // does. Screenshots the chart in light and dark, after the range picker calls -// the tool again through the host, and split by an attribute. Writes evidence -// and one PASS or FAIL line per check to checks.txt in --out. See ../SKILL.md. +// the tool again through the host, and split by an attribute. The grouped +// chart is p95 by release over 7 days at 1h, where the mock has busy releases +// and sparse ones whose p95 is higher, as in getsentry/toolkit#2451. Writes +// evidence and one PASS or FAIL line per check to checks.txt in --out. See +// ../SKILL.md. import { appendFileSync, mkdirSync, writeFileSync } from "node:fs"; import { createRequire } from "node:module"; import { dirname, join, resolve } from "node:path"; @@ -54,7 +57,11 @@ const GROUPED_CHART = { yAxis: "p95(span.duration)", groupBy: "release", period: "7d", + interval: "1h", }; +// Largest structured result for GROUPED_CHART: 5 series of 168 buckets. The +// compact result is about 12 KB; one object per bucket was about 200 KB. +const GROUPED_BUDGET_BYTES = 16 * 1024; // The spec's default view CSP: no network, no eval. const DEFAULT_CSP = "default-src 'none'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; media-src 'self' data:; connect-src 'none'"; @@ -263,7 +270,7 @@ try { rangeCall?.params?.name === TOOL && rangeCall.params.arguments.period === "7d" && rangeResult?.result?.structuredContent?.timeRange?.statsPeriod === "7d", - `"${before}" -> view called ${TOOL} period=7d through the host, got ${rangeResult?.result?.structuredContent?.series?.[0]?.buckets?.length} buckets -> ${range}`, + `"${before}" -> view called ${TOOL} period=7d through the host, got ${rangeResult?.result?.structuredContent?.times?.length} buckets -> ${range}`, ); await frame.getByRole("button", { name: "Open in Sentry" }).click(); @@ -279,13 +286,60 @@ try { `ui/open-link ${openLink?.params?.url}`, ); + const groupedChart = groupedResult.structuredContent; + const groupedBytes = JSON.stringify(groupedChart).length; + check( + "compact-result", + groupedBytes < GROUPED_BUDGET_BYTES && + groupedResult.content[0]?.text?.length === groupedBytes, + `${groupedChart.series.length} series x ${groupedChart.times.length} buckets: structuredContent ${groupedBytes} bytes, text ${groupedResult.content[0]?.text?.length} bytes (budget ${GROUPED_BUDGET_BYTES})`, + ); + const counts = groupedChart.series.map((s) => s.count); + check( + "top-by-volume", + counts.length === 5 && + counts.slice(0, 3).every((count) => count > 1000) && + counts.every((count, i) => i === 0 || count <= counts[i - 1]), + `p95 by release picks the releases with the most events: ${groupedChart.series.map((s) => `${s.label.slice(0, 7)}=${s.count}`).join(", ")}`, + ); + await mount("dark", GROUPED_CHART, groupedResult); const legend = await frame.locator(".legend").innerText(); + const groupedStats = await frame.locator(".stats").innerText(); const grouped = await shot("chart-grouped-dark.png"); check( "grouped", - legend.split("\n").filter(Boolean).length === 3, - `top groups "${legend.replace(/\n/g, " | ")}" -> ${grouped}`, + legend.split("\n").filter(Boolean).length === 5 && + groupedChart.peak === null && + !groupedStats.includes("Peak"), + `top groups "${legend.replace(/\n/g, " | ")}", stats "${groupedStats.replace(/\n/g, " ")}", peak ${groupedChart.peak} -> ${grouped}`, + ); + + // The sparsest charted release has data in 4 of 168 buckets. Its empty + // buckets are null in the result and draw as gaps: 4 dots, no line to 0. + const sparse = groupedChart.series.at(-1); + const sparseColor = "#2ba1db"; + const dots = await frame + .locator(`svg circle[fill="${sparseColor}"][r="2.5"]`) + .count(); + const lines = await frame + .locator(`svg path[stroke="${sparseColor}"]`) + .count(); + const groupedBox = await frame.locator("svg").boundingBox(); + // The left edge of the plot, where the sparse release has no data. + await page.mouse.move( + groupedBox.x + 50, + groupedBox.y + groupedBox.height / 2, + ); + const gapTooltip = await frame.locator(".tooltip").innerText(); + const withData = sparse.values.filter((v) => v !== null).length; + check( + "null-gaps", + !sparse.values.includes(0) && + dots === withData && + lines === 0 && + gapTooltip.includes("no data"), + `${sparse.label.slice(0, 7)} has ${withData} of ${sparse.values.length} buckets with data and no 0s; drawn as ${dots} dots and ${lines} lines; tooltip at the left edge "${gapTooltip.replace(/\s+/g, " ")}"`, ); // The rest of the lifecycle: a theme change, a cancelled call, teardown. diff --git a/docs/evidence/mcp-apps-chart/chart-grouped-dark.png b/docs/evidence/mcp-apps-chart/chart-grouped-dark.png index 72831ff1e..165fc3e9b 100644 Binary files a/docs/evidence/mcp-apps-chart/chart-grouped-dark.png and b/docs/evidence/mcp-apps-chart/chart-grouped-dark.png differ diff --git a/docs/evidence/mcp-apps-chart/checks.txt b/docs/evidence/mcp-apps-chart/checks.txt index 5a6fcdf08..02ffeb793 100644 --- a/docs/evidence/mcp-apps-chart/checks.txt +++ b/docs/evidence/mcp-apps-chart/checks.txt @@ -1,9 +1,12 @@ -PASS mcp-apps#tool-meta: render_sentry_chart _meta.ui.resourceUri=ui://sentry/chart/v1.html, openai/outputTemplate=ui://sentry/chart/v1.html -PASS mcp-apps#view-resource: resources/read ui://sentry/chart/v1.html: text/html;profile=mcp-app, 16 KiB HTML +PASS mcp-apps#tool-meta: render_sentry_chart _meta.ui.resourceUri=ui://sentry/chart/v3.html, openai/outputTemplate=ui://sentry/chart/v3.html +PASS mcp-apps#view-resource: resources/read ui://sentry/chart/v3.html: text/html;profile=mcp-app, 17 KiB HTML PASS mcp-apps#view-renders: light chart with hover tooltip "Sep 10, 16:00 UTC count() 91" -> docs/evidence/mcp-apps-chart/chart-light.png PASS mcp-apps#view-theme: host theme light=rgb(255, 255, 255) dark=rgb(30, 26, 37) -> docs/evidence/mcp-apps-chart/chart-dark.png PASS mcp-apps#range-roundtrip: "errors · level:error · Last 24 hours" -> view called render_sentry_chart period=7d through the host, got 56 buckets -> docs/evidence/mcp-apps-chart/chart-range-7d.png PASS mcp-apps#open-link: ui/open-link https://sentry-mcp-evals.sentry.io/explore/discover/homepage/?dataset=errors&queryDataset=error-events&query=level%3Aerror&project=-1&field=count%28%29&sort=-count%28%29&statsPeriod=7d&mode=aggregate&yAxis=count%28%29 -PASS mcp-apps#grouped: top groups "frontend@2.4.1 | frontend@2.4.0 | frontend@2.3.9" -> docs/evidence/mcp-apps-chart/chart-grouped-dark.png +PASS mcp-apps#compact-result: 5 series x 168 buckets: structuredContent 12304 bytes, text 12304 bytes (budget 16384) +PASS mcp-apps#top-by-volume: p95 by release picks the releases with the most events: 3f9c2e1=20160, 8b41d07=10080, c05e9a3=3360, 1d7f4b2=57, e2a86c9=23 +PASS mcp-apps#grouped: top groups "3f9c2e1 · 20.2K events | 8b41d07 · 10.1K events | c05e9a3 · 3.4K events | 1d7f4b2 · 57 events | e2a86c9 · 23 events", stats "Interval 1h", peak null -> docs/evidence/mcp-apps-chart/chart-grouped-dark.png +PASS mcp-apps#null-gaps: e2a86c9 has 4 of 168 buckets with data and no 0s; drawn as 4 dots and 0 lines; tooltip at the left edge "Sep 4, 01:00 UTC 3f9c2e1 6.44s 8b41d07 7.59s c05e9a3 8.04s 1d7f4b2 no data e2a86c9 no data" PASS mcp-apps#lifecycle: host-context-changed -> light, tool-cancelled -> "The chart request was cancelled.", ui/resource-teardown -> {} PASS mcp-apps#default-csp: 0 console errors under the default CSP diff --git a/packages/mcp-core/chart-app/chart.test.ts b/packages/mcp-core/chart-app/chart.test.ts index 0cac024ad..cf44edde5 100644 --- a/packages/mcp-core/chart-app/chart.test.ts +++ b/packages/mcp-core/chart-app/chart.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from "vitest"; -import { scaleFraction, valueScale } from "./chart"; +import { scaleFraction, shortLabel, valueScale } from "./chart"; function plotted(values: number[]) { const scale = valueScale(values); @@ -46,3 +46,22 @@ describe("valueScale", () => { expect(valueScale([0, 0])).toEqual({ bottom: 0, top: 1, step: 1 }); }); }); + +describe("shortLabel", () => { + it.each([ + ["a commit SHA", "3f9c2e1a3f9c2e1a3f9c2e1a3f9c2e1a3f9c2e1a", "3f9c2e1"], + [ + "a SHA after a package", + "frontend@3f9c2e1a3f9c2e1a3f9c2e1a3f9c2e1a3f9c2e1a", + "frontend@3f9c2e1", + ], + ["a short version", "frontend@2.4.1", "frontend@2.4.1"], + [ + "a long value", + "/api/0/organizations/{organization_id_or_slug}/events/", + "/api/0/organizations/{organizat…", + ], + ])("shortens %s", (_, label, expected) => { + expect(shortLabel(label)).toBe(expected); + }); +}); diff --git a/packages/mcp-core/chart-app/chart.ts b/packages/mcp-core/chart-app/chart.ts index 071e16b01..1e977f157 100644 --- a/packages/mcp-core/chart-app/chart.ts +++ b/packages/mcp-core/chart-app/chart.ts @@ -1,12 +1,16 @@ /** * Draws a render_sentry_chart result as SVG: one area (or line, when there * are several series) per series, a y grid, time labels, a hatched band over - * buckets that are still filling, and a hover tooltip. Values come from an - * untrusted tool result, so text only ever goes in through textContent. + * buckets that are still filling, and a hover tooltip. A bucket with no data + * is a gap, not 0, and a bucket with data on neither side is a dot. Values + * come from an untrusted tool result, so text only ever goes in through + * textContent. */ import type { ChartResult } from "../src/tools/support/chart/schema"; const SVG_NS = "http://www.w3.org/2000/svg"; +/** A plotted bucket: x, y, and its index into `times`. */ +type Point = readonly [x: number, y: number, index: number]; const HEIGHT = 220; const MARGIN = { top: 8, right: 8, bottom: 22, left: 48 }; export const SERIES_COLORS = [ @@ -32,6 +36,16 @@ const compact = new Intl.NumberFormat(undefined, { maximumFractionDigits: 1, }); +export function formatCount(count: number): string { + return compact.format(count); +} + +/** A legend-sized label: a 40-character commit SHA becomes its short form. */ +export function shortLabel(label: string): string { + const short = label.replace(/\b([0-9a-f]{7})[0-9a-f]{33}\b/gi, "$1"); + return short.length > 32 ? `${short.slice(0, 31)}…` : short; +} + export function formatValue(value: number, chart: ChartResult): string { if (chart.valueType === "duration") { const ms = value * (DURATION_MS[chart.valueUnit ?? "millisecond"] ?? 1); @@ -137,12 +151,12 @@ export function drawChart( const width = Math.max(container.clientWidth, 280); const plotWidth = width - MARGIN.left - MARGIN.right; const plotHeight = HEIGHT - MARGIN.top - MARGIN.bottom; - const times = chart.series[0]?.buckets.map((b) => Date.parse(b.time)) ?? []; + const times = chart.times.map((time) => Date.parse(time)); const first = times[0] ?? 0; const last = times.at(-1) ?? first; const span = Math.max(last - first, 1); const scale = valueScale( - chart.series.flatMap((s) => s.buckets.map((b) => b.value)), + chart.series.flatMap((s) => s.values.filter((v) => v !== null)), ); const { bottom, top, step } = scale; const x = (time: number) => @@ -210,8 +224,8 @@ export function drawChart( } // Buckets still filling: a hatched band from the first one to the end. - const firstFilling = chart.series[0]?.buckets.findIndex((b) => b.incomplete); - if (firstFilling !== undefined && firstFilling >= 0) { + const firstFilling = chart.incomplete[0]; + if (firstFilling !== undefined) { const fillingStart = x(times[Math.max(firstFilling - 1, 0)] ?? last); svg( "rect", @@ -238,48 +252,57 @@ export function drawChart( ); const single = chart.series.length === 1; + const path = (pts: readonly Point[]) => + pts.map(([px, py], i) => `${i ? "L" : "M"}${px},${py}`).join(""); chart.series.forEach((series, index) => { const color = SERIES_COLORS[index % SERIES_COLORS.length] ?? "#7553ff"; - const points = series.buckets.map( - (b) => [x(Date.parse(b.time)), y(b.value)] as const, - ); - const split = series.buckets.findIndex((b) => b.incomplete); - const settled = split < 0 ? points : points.slice(0, Math.max(split, 1)); - const filling = split < 0 ? [] : points.slice(Math.max(split - 1, 0)); - const path = (pts: readonly (readonly [number, number])[]) => - pts.map(([px, py], i) => `${i ? "L" : "M"}${px},${py}`).join(""); - if (single && points.length > 1) { - svg( - "path", - { - d: `${path(points)}L${points.at(-1)?.[0]},${y(0)}L${points[0]?.[0]},${y(0)}Z`, - fill: color, - "fill-opacity": 0.16, - }, - root, - ); - } - svg( - "path", - { d: path(settled), fill: "none", stroke: color, "stroke-width": 2 }, - root, - ); - if (points.length === 1) { - const [px, py] = points[0] ?? [0, 0]; - svg("circle", { cx: px, cy: py, r: 4, fill: color }, root); + // Runs of buckets with data. A null bucket ends a run, so it draws as a + // gap instead of a drop to 0. + const runs: Point[][] = []; + let run: Point[] | null = null; + for (const [i, time] of times.entries()) { + const value = series.values[i]; + if (value === null || value === undefined) { + run = null; + continue; + } + if (!run) runs.push((run = [])); + run.push([x(time), y(value), i]); } - if (filling.length > 1) { - svg( - "path", - { - d: path(filling), - fill: "none", - stroke: color, - "stroke-width": 2, - "stroke-dasharray": "4 3", - }, - root, - ); + for (const pts of runs) { + const [px, py] = pts[0] ?? [0, 0]; + if (pts.length === 1) { + svg("circle", { cx: px, cy: py, r: 2.5, fill: color }, root); + continue; + } + if (single) { + svg( + "path", + { + d: `${path(pts)}L${pts.at(-1)?.[0]},${y(0)}L${px},${y(0)}Z`, + fill: color, + "fill-opacity": 0.16, + }, + root, + ); + } + const settled = + firstFilling === undefined + ? pts + : pts.filter(([, , i]) => i < firstFilling); + const filling = + firstFilling === undefined + ? [] + : pts.filter(([, , i]) => i >= firstFilling - 1); + const line = { fill: "none", stroke: color, "stroke-width": 2 }; + if (settled.length > 1) svg("path", { d: path(settled), ...line }, root); + if (filling.length > 1) { + svg( + "path", + { d: path(filling), ...line, "stroke-dasharray": "4 3" }, + root, + ); + } } }); @@ -325,15 +348,18 @@ export function drawChart( crosshair.setAttribute("visibility", "visible"); const timeRow = document.createElement("div"); timeRow.className = "time"; - const incomplete = chart.series[0]?.buckets[index]?.incomplete; + const incomplete = chart.incomplete.includes(index); timeRow.textContent = `${formatTooltipTime(time, format)}${incomplete ? " (still filling)" : ""}`; const rows = chart.series.map((series, s) => { - const bucket = series.buckets[index]; + const value = series.values[index] ?? null; const marker = markers[s]; - if (bucket && marker) { + if (marker) { marker.setAttribute("cx", String(px)); - marker.setAttribute("cy", String(y(bucket.value))); - marker.setAttribute("visibility", "visible"); + marker.setAttribute("cy", String(y(value ?? 0))); + marker.setAttribute( + "visibility", + value === null ? "hidden" : "visible", + ); } const row = document.createElement("div"); row.className = "row"; @@ -341,10 +367,11 @@ export function drawChart( swatch.className = "swatch"; swatch.style.background = SERIES_COLORS[s % SERIES_COLORS.length] ?? ""; const label = document.createElement("span"); - label.textContent = series.label; - const value = document.createElement("span"); - value.textContent = bucket ? formatValue(bucket.value, chart) : "–"; - row.append(swatch, label, value); + label.textContent = shortLabel(series.label); + const shown = document.createElement("span"); + shown.textContent = + value === null ? "no data" : formatValue(value, chart); + row.append(swatch, label, shown); return row; }); tooltip.replaceChildren(timeRow, ...rows); diff --git a/packages/mcp-core/chart-app/main.ts b/packages/mcp-core/chart-app/main.ts index 3194dc91a..df34115cc 100644 --- a/packages/mcp-core/chart-app/main.ts +++ b/packages/mcp-core/chart-app/main.ts @@ -12,9 +12,11 @@ import { } from "./bridge"; import { drawChart, + formatCount, formatTooltipTime, formatValue, SERIES_COLORS, + shortLabel, type TimeFormat, } from "./chart"; import { parseChartResult } from "./parse"; @@ -142,7 +144,9 @@ function render(): void { ` · ${rangeLabel(chart)}`, ); - const hasData = chart.series.some((series) => series.buckets.length > 0); + const hasData = chart.series.some((series) => + series.values.some((value) => value !== null), + ); if (view.kind === "ready") { state.className = "state"; state.hidden = hasData; @@ -158,7 +162,7 @@ function render(): void { items.push( stat( "Peak", - `${formatValue(chart.peak.value, chart)}${single ? "" : ` (${chart.peak.series})`} at ${formatTooltipTime(Date.parse(chart.peak.time), format)}`, + `${formatValue(chart.peak.value, chart)} at ${formatTooltipTime(Date.parse(chart.peak.time), format)}`, ), ); } @@ -173,18 +177,18 @@ function render(): void { const swatch = el("span", "swatch"); swatch.style.background = SERIES_COLORS[index % SERIES_COLORS.length] ?? ""; - const total = - series.total === null + // The event count shows when a series rests on a handful of events. + const count = + series.count === null ? "" - : ` · ${formatValue(series.total, chart)}`; - item.append(swatch, `${series.label}${total}`); + : ` · ${formatCount(series.count)} ${series.count === 1 ? "event" : "events"}`; + item.title = series.label; + item.append(swatch, `${shortLabel(series.label)}${count}`); return item; })), ); - const filling = chart.series.some((series) => - series.buckets.some((bucket) => bucket.incomplete), - ); + const filling = chart.incomplete.length > 0; const ingestion = chart.ingestion && chart.ingestion.status !== "healthy" ? ` Ingestion is ${chart.ingestion.status}.` diff --git a/packages/mcp-core/chart-app/parse.test.ts b/packages/mcp-core/chart-app/parse.test.ts index be2ec4b3c..112644cc0 100644 --- a/packages/mcp-core/chart-app/parse.test.ts +++ b/packages/mcp-core/chart-app/parse.test.ts @@ -13,20 +13,12 @@ const chart = { interval: "1h", timeRange: { statsPeriod: "24h", start: null, end: null }, url: "https://sentry-mcp-evals.sentry.io/explore/discover/homepage/", + times: ["2025-09-10T23:00:00.000Z", "2025-09-11T00:00:00.000Z"], + incomplete: [1], + outsideRetention: [], series: [ - { - label: "1.0.0", - total: 12, - buckets: [ - { - time: "2025-09-10T23:00:00.000Z", - value: 12, - incomplete: false, - outsideRetention: false, - }, - ], - }, - { label: "1.0.1", total: null, buckets: [] }, + { label: "1.0.0", count: 12, total: 12, values: [12, 0] }, + { label: "1.0.1", count: null, total: null, values: [null, null] }, ], peak: { series: "1.0.0", time: "2025-09-10T23:00:00.000Z", value: 12 }, ingestion: { @@ -46,24 +38,16 @@ const invalid: Mutation[] = [ ["an undefined groupBy", (c) => ({ ...c, groupBy: undefined })], ["no timeRange", (c) => ({ ...c, timeRange: null })], ["a series object", (c) => ({ ...c, series: {} })], + ["no times", ({ times: _, ...rest }) => rest], [ - "a string bucket value", - (c) => ({ - ...c, - series: [ - { - ...c.series[0], - buckets: [{ ...c.series[0]?.buckets[0], value: "12" }], - }, - ], - }), + "a string value", + (c) => ({ ...c, series: [{ ...c.series[0], values: ["12"] }] }), ], + ["a fractional bucket index", (c) => ({ ...c, incomplete: [0.5] })], + ["a negative bucket index", (c) => ({ ...c, outsideRetention: [-1] })], [ - "a bucket without incomplete", - (c) => ({ - ...c, - series: [{ ...c.series[0], buckets: [{ time: "t", value: 1 }] }], - }), + "a series without count", + (c) => ({ ...c, series: [{ label: "a", total: null, values: [] }] }), ], ["a peak without time", (c) => ({ ...c, peak: { series: "a", value: 1 } })], ["no ingestion", ({ ingestion: _, ...rest }) => rest], diff --git a/packages/mcp-core/chart-app/parse.ts b/packages/mcp-core/chart-app/parse.ts index 6867e2bc1..7cb4bd5d8 100644 --- a/packages/mcp-core/chart-app/parse.ts +++ b/packages/mcp-core/chart-app/parse.ts @@ -7,7 +7,6 @@ import type { ChartResult } from "../src/tools/support/chart/schema"; type Series = ChartResult["series"][number]; -type Bucket = Series["buckets"][number]; /** Thrown inside the parser; parseChartResult turns it into null. */ class Invalid extends Error {} @@ -29,11 +28,6 @@ function num(value: unknown): number { return value; } -function bool(value: unknown): boolean { - if (typeof value !== "boolean") throw new Invalid(); - return value; -} - function nullable(parse: (value: unknown) => T, value: unknown): T | null { return value === null ? null : parse(value); } @@ -43,22 +37,19 @@ function list(parse: (value: unknown) => T, value: unknown): T[] { return value.map(parse); } -function bucket(value: unknown): Bucket { - const b = record(value); - return { - time: str(b["time"]), - value: num(b["value"]), - incomplete: bool(b["incomplete"]), - outsideRetention: bool(b["outsideRetention"]), - }; +function index(value: unknown): number { + const n = num(value); + if (!Number.isInteger(n) || n < 0) throw new Invalid(); + return n; } function series(value: unknown): Series { const s = record(value); return { label: str(s["label"]), + count: nullable(num, s["count"]), total: nullable(num, s["total"]), - buckets: list(bucket, s["buckets"]), + values: list((v) => nullable(num, v), s["values"]), }; } @@ -99,6 +90,9 @@ export function parseChartResult(value: unknown): ChartResult | null { end: nullable(str, range["end"]), }, url: str(c["url"]), + times: list(str, c["times"]), + incomplete: list(index, c["incomplete"]), + outsideRetention: list(index, c["outsideRetention"]), series: list(series, c["series"]), peak: nullable(peak, c["peak"]), ingestion: nullable(ingestion, c["ingestion"]), diff --git a/packages/mcp-core/src/api-client/client.ts b/packages/mcp-core/src/api-client/client.ts index 7f848ce72..9c94f1ef1 100644 --- a/packages/mcp-core/src/api-client/client.ts +++ b/packages/mcp-core/src/api-client/client.ts @@ -1227,9 +1227,9 @@ export class SentryApiService { } } - // Add sort parameter for all queries + // Explore reads the aggregate table's sort from `aggregateSort`. if (sort) { - urlParams.set("sort", sort); + urlParams.set(isAggregateQuery ? "aggregateSort" : "sort", sort); } this.appendExplorerTimeParams(urlParams, params); @@ -4760,9 +4760,13 @@ export class SentryApiService { end?: string; /** * One series per value of `groupBy`, for the `limit` values with the - * highest `yAxis`, without an "Other" series. + * highest `rankBy` (default `yAxis`), without an "Other" series. A + * `rankBy` other than `yAxis` is fetched as a second yAxis, because + * Sentry only sorts top events by a selected column. */ - topEvents?: { groupBy: string; limit: number } | undefined; + topEvents?: + | { groupBy: string; limit: number; rankBy?: string | undefined } + | undefined; }, opts?: RequestOptions, ) { @@ -4771,9 +4775,11 @@ export class SentryApiService { queryParams.set("query", query); queryParams.set("yAxis", yAxis); if (topEvents) { + const rankBy = topEvents.rankBy ?? yAxis; + if (rankBy !== yAxis) queryParams.append("yAxis", rankBy); queryParams.set("topEvents", String(topEvents.limit)); queryParams.set("groupBy", topEvents.groupBy); - queryParams.set("sort", `-${yAxis}`); + queryParams.set("sort", `-${rankBy}`); queryParams.set("excludeOther", "1"); } // Omit interval to let Sentry pick a sensible bucket size for the range. diff --git a/packages/mcp-core/src/api-client/schema.ts b/packages/mcp-core/src/api-client/schema.ts index 473ed5552..babed40f4 100644 --- a/packages/mcp-core/src/api-client/schema.ts +++ b/packages/mcp-core/src/api-client/schema.ts @@ -2667,9 +2667,9 @@ export const EventsTimeSeriesValueSchema = z .passthrough(); /** - * Response from the events-timeseries endpoint. The MCP always requests a - * single yAxis, so `timeSeries` holds one series, or one per group with - * `topEvents`, each naming its group in `groupBy`. + * Response from the events-timeseries endpoint. `timeSeries` holds one series + * per yAxis, or with `topEvents` one per group and yAxis, each naming its + * group in `groupBy` and its aggregate in `yAxis`. */ export const EventsTimeSeriesResponseSchema = z .object({ diff --git a/packages/mcp-core/src/server.ui-resources.test.ts b/packages/mcp-core/src/server.ui-resources.test.ts index 6f118a803..6c063fb24 100644 --- a/packages/mcp-core/src/server.ui-resources.test.ts +++ b/packages/mcp-core/src/server.ui-resources.test.ts @@ -15,7 +15,7 @@ vi.mock("@sentry/core/server", () => ({ wrapMcpServerWithSentry: vi.fn((server) => server), })); -const CHART_URI = "ui://sentry/chart/v1.html"; +const CHART_URI = "ui://sentry/chart/v3.html"; const MCP_APP_MIME_TYPE = "text/html;profile=mcp-app"; async function connect(skills: Skill[], experimentalMode = true) { diff --git a/packages/mcp-core/src/tools/catalog/render-sentry-chart.test.ts b/packages/mcp-core/src/tools/catalog/render-sentry-chart.test.ts index de05aab3d..ff773efe1 100644 --- a/packages/mcp-core/src/tools/catalog/render-sentry-chart.test.ts +++ b/packages/mcp-core/src/tools/catalog/render-sentry-chart.test.ts @@ -1,6 +1,6 @@ import { mswServer } from "@sentry/mcp-server-mocks"; import { HttpResponse, http } from "msw"; -import { describe, expect, it } from "vitest"; +import { afterEach, describe, expect, it } from "vitest"; import { UserInputError } from "../../errors"; import { assertStructuredOnlyResult, @@ -34,6 +34,8 @@ const baseParams = { }; describe("render_sentry_chart", () => { + afterEach(() => mswServer.resetHandlers()); + it("returns every bucket of the series", async () => { const result = await renderSentryChart.handler(baseParams, context); @@ -42,8 +44,12 @@ describe("render_sentry_chart", () => { { "dataset": "errors", "groupBy": null, + "incomplete": [ + 3, + ], "ingestion": null, "interval": "15m", + "outsideRetention": [], "peak": { "series": "count()", "time": "2025-09-10T23:30:00.000Z", @@ -52,34 +58,15 @@ describe("render_sentry_chart", () => { "query": "level:error", "series": [ { - "buckets": [ - { - "incomplete": false, - "outsideRetention": false, - "time": "2025-09-10T23:00:00.000Z", - "value": 56, - }, - { - "incomplete": false, - "outsideRetention": false, - "time": "2025-09-10T23:15:00.000Z", - "value": 56, - }, - { - "incomplete": false, - "outsideRetention": false, - "time": "2025-09-10T23:30:00.000Z", - "value": 140, - }, - { - "incomplete": true, - "outsideRetention": false, - "time": "2025-09-10T23:45:00.000Z", - "value": 56, - }, - ], + "count": 308, "label": "count()", "total": 308, + "values": [ + 56, + 56, + 140, + 56, + ], }, ], "timeRange": { @@ -87,6 +74,12 @@ describe("render_sentry_chart", () => { "start": null, "statsPeriod": "1h", }, + "times": [ + "2025-09-10T23:00:00.000Z", + "2025-09-10T23:15:00.000Z", + "2025-09-10T23:30:00.000Z", + "2025-09-10T23:45:00.000Z", + ], "title": "count()", "url": "https://sentry-mcp-evals.sentry.io/explore/discover/homepage/?dataset=errors&queryDataset=error-events&query=level%3Aerror&project=-1&field=count%28%29&sort=-count%28%29&statsPeriod=1h&mode=aggregate&yAxis=count%28%29", "valueType": "integer", @@ -96,30 +89,33 @@ describe("render_sentry_chart", () => { `); }); - it("asks Sentry for the top 5 groups and labels each series by its value", async () => { + it("asks Sentry for the 5 groups with the most events, with each group's count", async () => { let captured: URLSearchParams | undefined; mswServer.use( http.get(TIMESERIES_URL, ({ request }) => { captured = new URL(request.url).searchParams; return HttpResponse.json({ - timeSeries: ["2.4.1", null].map((release, index) => ({ - yAxis: "p95(span.duration)", - groupBy: [{ key: "release", value: release }], - values: [ - { - timestamp: 1757545200000, - value: 120 - index, + timeSeries: ["2.4.1", null].flatMap((release, index) => + [ + ["p95(span.duration)", [120 - index, null]], + ["count()", [40 - index, 0]], + ].map(([yAxis, values]) => ({ + yAxis, + groupBy: [{ key: "release", value: release }], + values: (values as (number | null)[]).map((value, bucket) => ({ + timestamp: 1757545200000 + bucket * 3600000, + value, incomplete: false, + })), + meta: { + interval: 3600000, + valueType: yAxis === "count()" ? "integer" : "duration", + valueUnit: yAxis === "count()" ? null : "millisecond", + order: index, + isOther: false, }, - ], - meta: { - interval: 3600000, - valueType: "duration", - valueUnit: "millisecond", - order: index, - isOther: false, - }, - })), + })), + ), }); }), ); @@ -136,27 +132,102 @@ describe("render_sentry_chart", () => { context, ); + expect(captured?.getAll("yAxis")).toEqual([ + "p95(span.duration)", + "count()", + ]); expect(Object.fromEntries(captured ?? [])).toMatchObject({ dataset: "spans", - yAxis: "p95(span.duration)", topEvents: "5", groupBy: "release", - sort: "-p95(span.duration)", + sort: "-count()", excludeOther: "1", statsPeriod: "24h", }); const chart = getStructuredContent(result); expect(chart.title).toBe("p95(span.duration) by release"); expect(chart.valueUnit).toBe("millisecond"); - expect(chart.series.map(({ label, total }) => ({ label, total }))).toEqual([ - { label: "2.4.1", total: null }, - { label: "(none)", total: null }, + expect(chart.times).toEqual([ + "2025-09-10T23:00:00.000Z", + "2025-09-11T00:00:00.000Z", ]); - expect(chart.peak).toEqual({ - series: "2.4.1", - time: "2025-09-10T23:00:00.000Z", - value: 120, - }); + expect(chart.series).toEqual([ + { label: "2.4.1", count: 40, total: null, values: [120, null] }, + { label: "(none)", count: 39, total: null, values: [119, null] }, + ]); + expect(chart.peak).toBeNull(); + }); + + // The busy releases have steady traffic; the sparse ones have 1 to 57 + // events, all at the 30s pageload cap, so ranking by p95 would pick them. + it("charts the busiest releases, keeps empty buckets null, and stays small for 7d at 1h", async () => { + const chart = getStructuredContent( + await renderSentryChart.handler( + { + ...baseParams, + dataset: "spans", + query: "is_transaction:true", + yAxis: "p95(span.duration)", + groupBy: "release", + period: "7d", + interval: "1h", + }, + context, + ), + ); + + expect(chart.times).toHaveLength(168); + // "Open in Sentry" ranks the groups by event count too. + const sentryLink = new URL(chart.url); + expect(sentryLink.searchParams.get("aggregateSort")).toBe("-count()"); + expect(sentryLink.searchParams.getAll("aggregateField")).toEqual([ + JSON.stringify({ groupBy: "release" }), + JSON.stringify({ yAxes: ["p95(span.duration)", "count()"] }), + ]); + expect( + chart.series.map(({ label, count }) => [label.slice(0, 8), count]), + ).toEqual([ + ["3f9c2e1a", 20160], + ["8b41d07c", 10080], + ["c05e9a3f", 3360], + ["1d7f4b2e", 57], + ["e2a86c91", 23], + ]); + for (const series of chart.series) { + expect(series.values).toHaveLength(168); + expect(series.values).not.toContain(0); + } + const sparse = chart.series[4]?.values ?? []; + expect(sparse.filter((value) => value === null)).toHaveLength(164); + expect(chart.peak).toBeNull(); + // 5 series of 168 buckets is about 12 KB; one object per bucket, as + // before, came to about 200 KB. + expect(JSON.stringify(chart).length).toBeLessThan(16 * 1024); + }); + + it.each([ + [ + "the metric's own count for a trace metric", + "p95(value,http.request.duration,distribution,millisecond)", + "count(value,http.request.duration,distribution,millisecond)", + ], + ["the aggregate when it names no metric", "p95(value)", "p95(value)"], + ])("ranks metrics groups by %s", async (_, yAxis, rankBy) => { + let captured: URLSearchParams | undefined; + mswServer.use( + http.get(TIMESERIES_URL, ({ request }) => { + captured = new URL(request.url).searchParams; + return HttpResponse.json({ timeSeries: [] }); + }), + ); + + await renderSentryChart.handler( + { ...baseParams, dataset: "metrics", yAxis, groupBy: "release" }, + context, + ); + + expect(captured?.getAll("yAxis")).toEqual([...new Set([yAxis, rankBy])]); + expect(captured?.get("sort")).toBe(`-${rankBy}`); }); it.each([ diff --git a/packages/mcp-core/src/tools/catalog/render-sentry-chart.ts b/packages/mcp-core/src/tools/catalog/render-sentry-chart.ts index 9c2dbe634..441859193 100644 --- a/packages/mcp-core/src/tools/catalog/render-sentry-chart.ts +++ b/packages/mcp-core/src/tools/catalog/render-sentry-chart.ts @@ -19,7 +19,6 @@ import { renderSentryChartOutputSchema, } from "../support/chart/schema"; import { toStructuredIngestion } from "../support/ingestion"; -import { summarizeSeries } from "../support/search-events/formatters"; /** Top groups plotted when the chart is split by an attribute. */ const MAX_CHART_SERIES = 5; @@ -38,42 +37,118 @@ function formatInterval(ms: number): string { return `${ms / 1000}s`; } -function seriesLabel( - series: EventsTimeSeriesResponse["timeSeries"][number], -): string { +type TimeSeries = EventsTimeSeriesResponse["timeSeries"][number]; + +function seriesLabel(series: TimeSeries): string { if (!series.groupBy?.length) return series.yAxis; return series.groupBy .map(({ value }) => (value === null || value === "" ? "(none)" : value)) .join(", "); } -function toChartSeries( +/** + * The aggregate that counts the events behind `yAxis`, used to pick the top + * groups by volume. Ranking by `yAxis` itself would pick the groups with the + * most extreme value, such as a p95 from one event. Trace metric aggregates + * name their metric (`fn(value,name,type,unit)`), so its count must too; null + * when the metric cannot be read from `yAxis`. + */ +function countAggregate(dataset: string, yAxis: string): string | null { + if (dataset !== "metrics") return "count()"; + const args = /^\w+\((value,[^,()]+,[^,()]+,[^,()]+)\)$/.exec(yAxis)?.[1]; + return args ? `count(${args})` : null; +} + +const isCount = (yAxis: string) => /^count\(/i.test(yAxis); +const isAdditive = (yAxis: string) => isCount(yAxis) || /^sum\(/i.test(yAxis); + +/** A bucket value as a number, or null where the bucket has no data. */ +function bucketValue(raw: number | string | null | undefined): number | null { + if (raw === null || raw === undefined) return null; + // Date-typed aggregates such as max(timestamp) return ISO datetime strings. + const parsed = Number(raw); + const value = Number.isNaN(parsed) ? Date.parse(String(raw)) : parsed; + if (!Number.isFinite(value)) return null; + // Drops float noise such as 6234.500000001 to keep the result compact. + return Number.isInteger(value) ? value : Number(value.toPrecision(8)); +} + +function sum(values: readonly (number | null)[]): number { + return values.reduce((total, value) => total + (value ?? 0), 0); +} + +function indexesWhere(items: readonly T[], test: (item: T) => boolean) { + return items.flatMap((item, index) => (test(item) ? [index] : [])); +} + +/** + * Turns the response into one shared time axis and one value array per + * series. `countYAxis` names the series that counts each group's events, + * when the response has one. + */ +function toChart( response: EventsTimeSeriesResponse, yAxis: string, -): { series: ChartResult["series"]; peak: ChartResult["peak"] } { - let peak: ChartResult["peak"] = null; - const series = response.timeSeries.map((entry) => { + countYAxis: string | null, +): Pick< + ChartResult, + "times" | "incomplete" | "outsideRetention" | "series" | "peak" +> { + // Sentry returns top groups in rank order. + const plotted = response.timeSeries.filter((entry) => entry.yAxis === yAxis); + const counts = new Map( + response.timeSeries + .filter((entry) => countYAxis !== yAxis && entry.yAxis === countYAxis) + .map((entry) => [ + seriesLabel(entry), + sum(entry.values.map((b) => bucketValue(b.value))), + ]), + ); + const buckets = plotted[0]?.values ?? []; + const outsideRetention = (b: (typeof buckets)[number]) => + b.incomplete && b.incompleteReason === "OUTSIDE_RETENTION"; + const incomplete = indexesWhere( + buckets, + (b) => b.incomplete && !outsideRetention(b), + ); + const additive = isAdditive(yAxis); + const series = plotted.map((entry) => { const label = seriesLabel(entry); - const summary = summarizeSeries(entry, yAxis); - if (summary.peak && (!peak || summary.peak.value > peak.value)) { - peak = { - series: label, - time: new Date(summary.peak.timestamp).toISOString(), - value: summary.peak.value, - }; - } + const values = entry.values.map((b) => bucketValue(b.value)); + const total = additive ? sum(values) : null; return { label, - total: summary.total, - buckets: summary.points.map((point) => ({ - time: new Date(point.timestamp).toISOString(), - value: point.value, - incomplete: point.filling, - outsideRetention: point.outsideRetention, - })), + count: counts.get(label) ?? (isCount(yAxis) ? total : null), + total, + values, }; }); - return { series, peak }; + + // Only a single-series chart has a peak. Across groups, any one bucket + // would headline a single group, which reads as the chart's peak. + let peak: ChartResult["peak"] = null; + const only = series.length === 1 ? series[0] : undefined; + for (const [index, value] of (only?.values ?? []).entries()) { + const bucket = buckets[index]; + if (!only || value === null || !bucket || incomplete.includes(index)) { + continue; + } + if (!peak || value > peak.value) { + peak = { + series: only.label, + time: new Date(bucket.timestamp).toISOString(), + value, + }; + } + } + + return { + times: buckets.map((b) => new Date(b.timestamp).toISOString()), + incomplete, + outsideRetention: indexesWhere(buckets, outsideRetention), + series, + peak, + }; } export default defineTool({ @@ -85,7 +160,7 @@ export default defineTool({ skills: ["inspect", "triage", "seer"], requiredScopes: ["event:read"], description: - "Chart a count, rate, duration, or other aggregate of Sentry errors, logs, spans, metrics, or profiles over time, optionally split into the top 5 values of one attribute. Returns every bucket of each series, and MCP Apps hosts show it as an interactive chart. To find individual events, use the dataset search tools.", + "Chart a count, rate, duration, or other aggregate of Sentry errors, logs, spans, metrics, or profiles over time, optionally split by one attribute into the 5 values with the most events. Returns every bucket of each series, with each group's event count when split, and MCP Apps hosts show it as an interactive chart. To find individual events, use the dataset search tools.", inputSchema: { organizationSlug: ParamOrganizationSlug, regionUrl: ParamRegionUrl.nullable().default(null), @@ -111,7 +186,7 @@ export default defineTool({ .nullable() .default(null) .describe( - "Attribute to split by, such as `release` or `transaction`; plots its top 5 values. Not supported for profiles.", + "Attribute to split by, such as `release` or `transaction`; plots its 5 values with the most events. Not supported for profiles.", ), projectSlug: ParamProjectSlug.nullable().default(null), period: ParamPeriod.nullable() @@ -193,6 +268,13 @@ export default defineTool({ params.start !== null && params.end !== null ? { start: params.start, end: params.end } : { statsPeriod: params.period ?? DEFAULT_PERIOD }; + const countYAxis = params.groupBy + ? countAggregate(params.dataset, params.yAxis) + : null; + // "Open in Sentry" ranks groups the same way the chart does. + const rankBy = countYAxis ?? params.yAxis; + const linkAggregates = + rankBy === params.yAxis ? [params.yAxis] : [params.yAxis, rankBy]; const response = await apiService.getEventsTimeSeries({ organizationSlug, query: params.query, @@ -202,17 +284,22 @@ export default defineTool({ dataset: params.dataset, ...timeParams, topEvents: params.groupBy - ? { groupBy: params.groupBy, limit: MAX_CHART_SERIES } + ? { + groupBy: params.groupBy, + limit: MAX_CHART_SERIES, + rankBy: countYAxis ?? undefined, + } : undefined, }); - const meta = response.timeSeries[0]?.meta; + const meta = response.timeSeries.find( + (entry) => entry.yAxis === params.yAxis, + )?.meta; if (meta?.valueType === "string") { throw new UserInputError( `\`${params.yAxis}\` returns text, which cannot be charted. Use a numeric aggregate such as \`count()\`.`, ); } - const { series, peak } = toChartSeries(response, params.yAxis); return structuredResult({ title: @@ -240,16 +327,15 @@ export default defineTool({ projectId, params.dataset, // Discover reads group-by columns from `field`, so list it first. - params.groupBy ? [params.groupBy, params.yAxis] : [params.yAxis], - `-${params.yAxis}`, - [params.yAxis], + params.groupBy ? [params.groupBy, ...linkAggregates] : linkAggregates, + `-${rankBy}`, + linkAggregates, params.groupBy ? [params.groupBy] : [], timeParams.statsPeriod, timeParams.start, timeParams.end, ), - series, - peak, + ...toChart(response, params.yAxis, countYAxis), ingestion: toStructuredIngestion(response.meta?.ingestion), }); }, diff --git a/packages/mcp-core/src/tools/catalog/search-events.test.ts b/packages/mcp-core/src/tools/catalog/search-events.test.ts index bf66b3085..dc1d98d3e 100644 --- a/packages/mcp-core/src/tools/catalog/search-events.test.ts +++ b/packages/mcp-core/src/tools/catalog/search-events.test.ts @@ -4603,7 +4603,7 @@ describe("search_events", () => { | 2025-09-11 00:00 | 100 | **View these results in Sentry**: - https://test-org.sentry.io/explore/traces/?query=span.op%3Ahttp.client&project=42&aggregateField=%7B%22yAxes%22%3A%5B%22count%28%29%22%5D%7D&mode=aggregate&sort=-count%28%29&statsPeriod=24h&table=span + https://test-org.sentry.io/explore/traces/?query=span.op%3Ahttp.client&project=42&aggregateField=%7B%22yAxes%22%3A%5B%22count%28%29%22%5D%7D&mode=aggregate&aggregateSort=-count%28%29&statsPeriod=24h&table=span Please tell the user this dashboard link is available if they want to open the results in Sentry." `); } diff --git a/packages/mcp-core/src/tools/support/chart/schema.ts b/packages/mcp-core/src/tools/support/chart/schema.ts index 00034948e..848200887 100644 --- a/packages/mcp-core/src/tools/support/chart/schema.ts +++ b/packages/mcp-core/src/tools/support/chart/schema.ts @@ -5,18 +5,7 @@ import { z } from "zod"; import { ingestionSchema } from "../ingestion"; -const chartBucketSchema = z.object({ - time: z.string(), - value: z.number(), - incomplete: z - .boolean() - .describe("Data is still arriving, so the value may rise."), - outsideRetention: z - .boolean() - .describe( - "Starts before the retention window, so older data is missing and the value will not change.", - ), -}); +const bucketIndexesSchema = z.array(z.number().int().nonnegative()); export const renderSentryChartOutputSchema = z.object({ title: z.string(), @@ -36,26 +25,45 @@ export const renderSentryChartOutputSchema = z.object({ end: z.string().nullable(), }), url: z.string(), + times: z + .array(z.string()) + .describe("Start of each bucket, oldest first; shared by every series."), + incomplete: bucketIndexesSchema.describe( + "Indexes into `times` of buckets still receiving data, whose values may rise.", + ), + outsideRetention: bucketIndexesSchema.describe( + "Indexes into `times` of buckets that start before the retention window, so older data is missing and the value will not change.", + ), series: z .array( z.object({ label: z .string() .describe("The group's value, or the aggregate when ungrouped."), + count: z + .number() + .nullable() + .describe( + "Events behind this series; null when not measured. A small count means a noisy series.", + ), total: z .number() .nullable() .describe("Sum of all buckets; null for non-additive aggregates."), - buckets: z - .array(chartBucketSchema) - .describe("Every bucket, oldest first."), + values: z + .array(z.number().nullable()) + .describe( + "One value per entry of `times`; null where the bucket has no data.", + ), }), ) - .describe("One series, or one per top group, largest first."), + .describe("One series, or one per top group, most events first."), peak: z .object({ series: z.string(), time: z.string(), value: z.number() }) .nullable() - .describe("Highest bucket across series that is no longer receiving data."), + .describe( + "Highest bucket that is no longer receiving data; null for grouped charts.", + ), ingestion: ingestionSchema, }); diff --git a/packages/mcp-core/src/ui-resources.ts b/packages/mcp-core/src/ui-resources.ts index b2000609c..4c453c81e 100644 --- a/packages/mcp-core/src/ui-resources.ts +++ b/packages/mcp-core/src/ui-resources.ts @@ -54,13 +54,13 @@ export interface UiResource { /** The render_sentry_chart view. Built from chart-app/ by generate-chart-app. */ export const CHART_UI_RESOURCE: UiResource = { - uri: "ui://sentry/chart/v1.html", + uri: "ui://sentry/chart/v3.html", name: "sentry_chart", title: "Sentry Chart", description: "Interactive time series chart for render_sentry_chart results, with a range picker and a link to Sentry.", htmlSha256: - "c42d34d3aa082a33b4ab293b509dbe4b2dbde7edc4965033cffa8b920fc6e39f", + "5383b9d3a0f90695bd6811f4f4ed0a8c5a31edef6a3073c1e84990dd38ef5d39", // About 1.5x the current view, so a new dependency cannot slip in. budgetBytes: 24 * 1024, loadHtml: async () => diff --git a/packages/mcp-core/src/ui/chart-app.generated.ts b/packages/mcp-core/src/ui/chart-app.generated.ts index ff3682822..b939d0d03 100644 --- a/packages/mcp-core/src/ui/chart-app.generated.ts +++ b/packages/mcp-core/src/ui/chart-app.generated.ts @@ -1,2 +1,2 @@ // Generated by scripts/generate-chart-app.ts from chart-app/. Do not edit. -export const CHART_APP_HTML = "Sentry chart"; +export const CHART_APP_HTML = "Sentry chart"; diff --git a/packages/mcp-server-mocks/src/events-timeseries.ts b/packages/mcp-server-mocks/src/events-timeseries.ts index 608d748c4..79fdb1ea9 100644 --- a/packages/mcp-server-mocks/src/events-timeseries.ts +++ b/packages/mcp-server-mocks/src/events-timeseries.ts @@ -32,12 +32,84 @@ function parseDuration(value: string | null): number | undefined { return match && unit ? Number(match[1]) * (UNIT_MS[unit] ?? 0) : undefined; } -const GROUP_VALUES = ["frontend@2.4.1", "frontend@2.4.0", "frontend@2.3.9"]; +/** + * Releases for grouped requests, busiest first. The first three carry steady + * traffic. The rest have a handful of events each, every one at the 30s + * pageload cap, so their p95 is the highest and almost every bucket is empty. + * Ranking groups by p95 picks the sparse ones; ranking by count picks the + * busy ones. + */ +interface Release { + name: string; + /** Events per bucket for a busy release; total events for a sparse one. */ + events: number; + /** Typical duration in ms. */ + duration: number; + /** Buckets with events, for a sparse release; busy ones fill every bucket. */ + hits?: number; +} + +const sha = (seed: string) => seed.repeat(40).slice(0, 40); + +const MOCK_RELEASES: Release[] = [ + { name: sha("3f9c2e1a"), events: 120, duration: 6200 }, + { name: sha("8b41d07c"), events: 60, duration: 6800 }, + { name: sha("c05e9a3f"), events: 20, duration: 7400 }, + { name: sha("1d7f4b2e"), events: 57, duration: 30_000, hits: 6 }, + { name: sha("e2a86c91"), events: 23, duration: 30_000, hits: 4 }, + { name: sha("94b3f5d0"), events: 7, duration: 30_000, hits: 3 }, + { name: sha("5a0e17b8"), events: 3, duration: 30_000, hits: 2 }, + { name: sha("f61c2d49"), events: 1, duration: 30_000, hits: 1 }, +]; + +/** Events a release has in bucket `index` of `bucketCount`. */ +function releaseEvents( + release: Release, + releaseIndex: number, + index: number, + bucketCount: number, +): number { + if (release.hits === undefined) { + const wave = Math.sin((index / bucketCount) * Math.PI * 4 + releaseIndex); + return Math.round(release.events * (1 + 0.3 * wave)); + } + const hits = Math.min(release.hits, bucketCount); + for (let hit = 0; hit < hits; hit++) { + const at = Math.floor(((hit + 0.5) / hits) * bucketCount + releaseIndex); + if (at % bucketCount !== index) continue; + const base = Math.floor(release.events / hits); + return base + (hit < release.events % hits ? 1 : 0); + } + return 0; +} + +const isCount = (yAxis: string) => yAxis.startsWith("count("); +const isDuration = (yAxis: string) => /duration/.test(yAxis); + +/** + * One release's bucket value: its event count for `count()`, otherwise a + * duration-like value, or null for a bucket with no events, as Sentry + * returns for percentiles and averages. + */ +function releaseValue( + release: Release, + releaseIndex: number, + yAxis: string, + index: number, + bucketCount: number, +): number | null { + const events = releaseEvents(release, releaseIndex, index, bucketCount); + if (isCount(yAxis)) return events; + if (events === 0) return null; + if (release.hits !== undefined) return release.duration; + // Unrounded, as Sentry returns percentiles. + return release.duration * (1 + 0.12 * Math.sin(index / 3 + releaseIndex)); +} export function eventsTimeSeriesResponse(request: Request): Response { const params = new URL(request.url).searchParams; - const yAxis = params.get("yAxis"); - if (!params.get("dataset") || !yAxis) { + const yAxes = params.getAll("yAxis"); + if (!params.get("dataset") || yAxes.length === 0) { return HttpResponse.json( { detail: "dataset and yAxis are required" }, { status: 400 }, @@ -45,12 +117,20 @@ export function eventsTimeSeriesResponse(request: Request): Response { } const topEvents = params.get("topEvents"); const groupBy = params.get("groupBy"); - if (topEvents && (!groupBy || !params.get("sort"))) { + const sort = params.get("sort"); + if (topEvents && (!groupBy || !sort)) { return HttpResponse.json( { detail: "groupBy is a required parameter when doing topEvents" }, { status: 400 }, ); } + const sortBy = sort?.replace(/^-/, ""); + if (topEvents && !yAxes.includes(sortBy ?? "")) { + return HttpResponse.json( + { detail: `Cannot sort by ${sortBy}: it is not a selected column` }, + { status: 400 }, + ); + } const start = params.get("start"); const end = params.get("end"); @@ -67,33 +147,66 @@ export function eventsTimeSeriesResponse(request: Request): Response { DEFAULT_INTERVALS.find(([limit]) => range <= limit)?.[1] ?? 6 * HOUR; const bucketCount = Math.max(1, Math.round(range / interval)); + const bucket = (index: number, value: number | null) => { + const isLast = index === bucketCount - 1; + return { + timestamp: rangeEnd - (bucketCount - index) * interval, + value, + incomplete: isLast, + ...(isLast && { incompleteReason: "INGESTION_PENDING" }), + }; + }; + const meta = (yAxis: string) => ({ + interval, + valueType: isDuration(yAxis) && !isCount(yAxis) ? "duration" : "integer", + valueUnit: isDuration(yAxis) && !isCount(yAxis) ? "millisecond" : null, + }); + + if (!groupBy) { + const timeSeries = yAxes.map((yAxis) => ({ + yAxis, + values: Array.from({ length: bucketCount }, (_, index) => { + const wave = Math.sin((index / bucketCount) * Math.PI * 4); + const spike = index === Math.floor(bucketCount * 0.7) ? 2.5 : 1; + const base = isDuration(yAxis) ? 180 : 40; + return bucket(index, Math.round(base * (1.4 + wave) * spike)); + }), + meta: meta(yAxis), + })); + return HttpResponse.json({ + timeSeries, + meta: { start: rangeStart, end: rangeEnd }, + }); + } - const groups = groupBy - ? GROUP_VALUES.slice(0, Number(topEvents) || GROUP_VALUES.length) - : [null]; - const isDuration = /duration/.test(yAxis); - const timeSeries = groups.map((group, groupIndex) => ({ - yAxis, - ...(group !== null && { groupBy: [{ key: groupBy, value: group }] }), - values: Array.from({ length: bucketCount }, (_, index) => { - const wave = Math.sin((index / bucketCount) * Math.PI * 4 + groupIndex); - const spike = index === Math.floor(bucketCount * 0.7) ? 2.5 : 1; - const base = (isDuration ? 180 : 40) / (groupIndex + 1); - const isLast = index === bucketCount - 1; - return { - timestamp: rangeEnd - (bucketCount - index) * interval, - value: Math.round(base * (1.4 + wave) * spike), - incomplete: isLast, - ...(isLast && { incompleteReason: "INGESTION_PENDING" }), - }; - }), - meta: { - interval, - valueType: isDuration ? "duration" : "integer", - valueUnit: isDuration ? "millisecond" : null, - ...(group !== null && { order: groupIndex, isOther: false }), - }, - })); + // Rank as Sentry does for `sort=-`: by total events for a + // count, otherwise by the release's typical value. + const rank = (release: Release, index: number) => + isCount(sortBy ?? "") + ? Array.from({ length: bucketCount }, (_, i) => + releaseEvents(release, index, i, bucketCount), + ).reduce((sum, events) => sum + events, 0) + : release.duration; + const ranked = MOCK_RELEASES.map((release, index) => ({ + release, + index, + rank: rank(release, index), + })) + .sort((a, b) => b.rank - a.rank) + .slice(0, Number(topEvents) || MOCK_RELEASES.length); + const timeSeries = ranked.flatMap(({ release, index: releaseIndex }, order) => + yAxes.map((yAxis) => ({ + yAxis, + groupBy: [{ key: groupBy, value: release.name }], + values: Array.from({ length: bucketCount }, (_, index) => + bucket( + index, + releaseValue(release, releaseIndex, yAxis, index, bucketCount), + ), + ), + meta: { ...meta(yAxis), order, isOther: false }, + })), + ); return HttpResponse.json({ timeSeries,