Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
9099ff2
feat(metrics-pipeline): generic Redis-stream to ClickHouse metrics pi…
ericallam Jul 2, 2026
ce924cd
feat(clickhouse): queue metrics tables and read queries
ericallam Jul 2, 2026
d86c584
feat(run-engine): emit queue depth, throughput, and scheduling-delay …
ericallam Jul 2, 2026
173cf79
feat(webapp): queue metrics ingestion, admin controls, and emission s…
ericallam Jul 2, 2026
1ab4747
feat(tsql): opt-in gap-fill for time-bucketed series
ericallam Jul 2, 2026
7cd4648
feat(webapp): Queues dashboard and per-org metrics UI flag
ericallam Jul 2, 2026
864bb08
chore(webapp): add server-changes note for queue metrics
ericallam Jul 2, 2026
72e503d
chore: apply oxfmt formatting
ericallam Jul 3, 2026
54775a1
chore: use import type for type-only imports
ericallam Jul 3, 2026
5ed0838
fix(tsql): avoid polynomial backtracking in ORDER BY direction strip
ericallam Jul 3, 2026
0c642e9
fix(tsql): strip ORDER BY direction without a backtracking regex
ericallam Jul 3, 2026
d1cb3fa
fix(clickhouse): remove semicolons from queue metrics migration comments
ericallam Jul 3, 2026
b6c626d
test(clickhouse): rewrite queue metrics test for cumulative counters
ericallam Jul 3, 2026
e08ee4f
fix(metrics-pipeline): use BigInt order keys and namespaced odometer …
ericallam Jul 3, 2026
4d3ca7b
fix(clickhouse): filter zero waits from quantile view and accept stri…
ericallam Jul 3, 2026
41f857e
fix(webapp): fail open on queue metrics and honor sparkline total ove…
ericallam Jul 3, 2026
193ff71
test(run-engine): import describe from vitest in run-queue metrics test
ericallam Jul 3, 2026
45e978d
fix(tsql): skip gap-fill on descending bucket order
ericallam Jul 3, 2026
31fb262
fix(metrics-pipeline): widen order_key packing factor to 1e6
ericallam Jul 3, 2026
5aa2259
feat(webapp,clickhouse): standard time filter for queue metrics pages
ericallam Jul 4, 2026
7235f69
fix(tsql): register the deltaSumTimestampMerge aggregate
ericallam Jul 4, 2026
84ecb0d
chore(webapp): use shared primitives on the admin queue metrics page
ericallam Jul 4, 2026
9412bf5
feat(webapp): house style hero charts on the queues list
ericallam Jul 4, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .server-changes/queue-metrics-dashboard.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
area: webapp
type: feature
---

Queue metrics and health on the Queues page: per-queue depth, throughput, concurrency, throttling, and scheduling-delay charts, plus a per-queue detail view. Off by default; enabled per organization.
8 changes: 6 additions & 2 deletions apps/webapp/app/components/primitives/UsageSparkline.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ export type UsageSparklineProps = {
color?: string;
/** Unit shown in the tooltip (e.g. calls, tokens). */
unitLabel?: UnitLabel;
/** Trailing scalar shown after the chart. Defaults to the sum of buckets (override for gauges, e.g. peak). */
total?: number;
/** Format the trailing total. Defaults to `toLocaleString`. */
formatTotal?: (total: number) => string;
/** Class for the trailing total label. */
Expand All @@ -44,14 +46,16 @@ export function UsageSparkline({
bucketIntervalMs,
color = "#3B82F6",
unitLabel = { singular: "call", plural: "calls" },
total: totalOverride,
formatTotal,
totalClassName = "text-blue-400",
}: UsageSparklineProps) {
if (!data || data.every((v) => v === 0)) {
const hasTotalOverride = totalOverride !== undefined;
if (!data || data.length === 0 || (data.every((v) => v === 0) && !hasTotalOverride)) {
return <span className="text-text-dimmed">–</span>;
}

const total = data.reduce((a, b) => a + b, 0);
const total = totalOverride ?? data.reduce((a, b) => a + b, 0);
const max = Math.max(...data);

// Map each bucket to a dated point so the tooltip can show the window it
Expand Down
2 changes: 1 addition & 1 deletion apps/webapp/app/components/primitives/charts/Chart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ const ChartTooltipContent = React.forwardRef<
)}
<div
className={cn(
"flex flex-1 justify-between leading-none",
"flex flex-1 justify-between gap-3 leading-none",
nestLabel ? "items-end" : "items-center"
)}
>
Expand Down
53 changes: 52 additions & 1 deletion apps/webapp/app/components/primitives/charts/ChartLine.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
CartesianGrid,
Line,
LineChart,
ReferenceLine,
XAxis,
YAxis,
type XAxisProps,
Expand Down Expand Up @@ -48,12 +49,38 @@ export type ChartLineRendererProps = {
tooltipLabelFormatter?: (label: string, payload: any[]) => string;
/** Optional formatter for numeric tooltip values (e.g. bytes, duration) */
tooltipValueFormatter?: (value: number) => string;
/** Draw a dot at each data point. Defaults to true; turn off for dense/compact charts. */
showDots?: boolean;
/** Horizontal reference lines (e.g. limits); the y-domain extends to include them. */
referenceLines?: Array<{ y: number; label?: string; color?: string }>;
/** Width injected by ResponsiveContainer */
width?: number;
/** Height injected by ResponsiveContainer */
height?: number;
};

/** Reference-line label: right-aligned just below the line (recharts injects viewBox). */
function ReferenceLineLabel({
viewBox,
value,
}: {
viewBox?: { x: number; y: number; width: number };
value: string;
}) {
if (!viewBox) return null;
return (
<text
x={viewBox.x + viewBox.width - 4}
y={viewBox.y + 12}
textAnchor="end"
fill="#878C99"
fontSize={10}
>
{value}
</text>
);
}

/**
* Line chart renderer for the compound component system.
* Must be used within a Chart.Root.
Expand All @@ -73,6 +100,8 @@ export function ChartLineRenderer({
stacked = false,
tooltipLabelFormatter,
tooltipValueFormatter,
showDots = true,
referenceLines,
width,
height,
}: ChartLineRendererProps) {
Expand Down Expand Up @@ -176,6 +205,17 @@ export function ChartLineRenderer({
labelFormatter={tooltipLabelFormatter}
/>
{/* Note: Legend is now rendered by ChartRoot outside the chart container */}
{referenceLines?.map((line) => (
<ReferenceLine
key={`ref-${line.y}-${line.label ?? ""}`}
y={line.y}
stroke={line.color ?? "#4D525B"}
strokeDasharray="4 4"
strokeWidth={1}
ifOverflow="extendDomain"
label={line.label ? <ReferenceLineLabel value={line.label} /> : undefined}
/>
))}
{visibleSeries.map((key) => (
<Area
key={key}
Expand Down Expand Up @@ -222,14 +262,25 @@ export function ChartLineRenderer({
labelFormatter={tooltipLabelFormatter}
/>
{/* Note: Legend is now rendered by ChartRoot outside the chart container */}
{referenceLines?.map((line) => (
<ReferenceLine
key={`ref-${line.y}-${line.label ?? ""}`}
y={line.y}
stroke={line.color ?? "#4D525B"}
strokeDasharray="4 4"
strokeWidth={1}
ifOverflow="extendDomain"
label={line.label ? <ReferenceLineLabel value={line.label} /> : undefined}
/>
))}
{visibleSeries.map((key) => (
<Line
key={key}
dataKey={key}
type={lineType}
stroke={config[key]?.color}
strokeWidth={1}
dot={{ r: 1.5, fill: config[key]?.color, strokeWidth: 0 }}
dot={showDots ? { r: 1.5, fill: config[key]?.color, strokeWidth: 0 } : false}
activeDot={{ r: 4 }}
isAnimationActive={false}
/>
Expand Down
3 changes: 3 additions & 0 deletions apps/webapp/app/entry.server.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import * as Worker from "~/services/worker.server";
import { initMollifierDrainerWorker } from "~/v3/mollifierDrainerWorker.server";
import { initMollifierStaleSweepWorker } from "~/v3/mollifierStaleSweepWorker.server";
import { initBillingLimitWorker } from "~/v3/billingLimitWorker.server";
import { initQueueMetricsConsumer, initQueueMetricsEmitter } from "~/v3/queueMetrics.server";
import { bootstrap } from "./bootstrap";
import { LocaleContextProvider } from "./components/primitives/LocaleProvider";
import type { OperatingSystemPlatform } from "./components/primitives/OperatingSystemProvider";
Expand Down Expand Up @@ -234,6 +235,8 @@ Worker.init().catch((error) => {
initMollifierDrainerWorker();
initMollifierStaleSweepWorker();
initBillingLimitWorker();
initQueueMetricsEmitter();
initQueueMetricsConsumer();

bootstrap().catch((error) => {
logError(error);
Expand Down
22 changes: 22 additions & 0 deletions apps/webapp/app/env.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,28 @@ const EnvironmentSchema = z
RUN_ENGINE_REUSE_SNAPSHOT_COUNT: z.coerce.number().int().default(0),
RUN_ENGINE_MAXIMUM_ENV_COUNT: z.coerce.number().int().optional(),
RUN_ENGINE_RUN_QUEUE_SHARD_COUNT: z.coerce.number().int().default(4),
// Queue metrics ingestion (Redis Stream -> ClickHouse). The runtime on/off is the
// `queue_metrics:enabled` Redis key; these gate emitter construction + consumer boot.
QUEUE_METRICS_EMIT_ENABLED: z.string().default("0"),
QUEUE_METRICS_CONSUMER_ENABLED: z.string().default("0"),
QUEUE_METRICS_STREAM_SHARD_COUNT: z.coerce.number().int().default(4),
QUEUE_METRICS_CONSUMER_BATCH_SIZE: z.coerce.number().int().default(1000),
// Counter stream (exact counts, loss-intolerant). Unset host => the run-queue Redis;
// set it to a dedicated instance so counter backlog never competes with the run queue.
QUEUE_METRICS_REDIS_HOST: z.string().optional(),
QUEUE_METRICS_REDIS_PORT: z.coerce.number().optional(),
QUEUE_METRICS_REDIS_USERNAME: z.string().optional(),
QUEUE_METRICS_REDIS_PASSWORD: z.string().optional(),
QUEUE_METRICS_REDIS_TLS_DISABLED: z.string().default(process.env.REDIS_TLS_DISABLED ?? "false"),
QUEUE_METRICS_COUNTER_STREAM_MAXLEN: z.coerce.number().int().default(8_000_000),
// TTL (seconds) on the per-(queue,op) cumulative odometer key, refreshed on every write.
// Idle-past-TTL queues purge and self-heal (restart from 1) on return; default 7 days.
QUEUE_METRICS_COUNTER_ODOMETER_TTL_SECONDS: z.coerce.number().int().default(604_800),
// Per-env distinct queue_name cap (0 = unlimited); overflow maps to "__overflow__".
QUEUE_METRICS_MAX_QUEUE_NAMES_PER_ENV: z.coerce.number().int().default(1000),
// Fraction (0..1) of ops that emit a gauge; counters are never sampled. Dial below 1
// only if EngineCPU is too high in slow-path-heavy regions (hurts low-traffic queues).
QUEUE_METRICS_GAUGE_SAMPLE_RATE: z.coerce.number().min(0).max(1).default(1),
RUN_ENGINE_WORKER_SHUTDOWN_TIMEOUT_MS: z.coerce.number().int().default(60_000),
RUN_ENGINE_RETRY_WARM_START_THRESHOLD_MS: z.coerce.number().int().default(30_000),
RUN_ENGINE_PROCESS_WORKER_QUEUE_DEBOUNCE_MS: z.coerce.number().int().default(200),
Expand Down
109 changes: 109 additions & 0 deletions apps/webapp/app/hooks/useMetricResourceQuery.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import { useCallback, useEffect, useRef, useState } from "react";
import { useInterval } from "./useInterval";

export type MetricResourceRow = Record<string, number | string | null>;

type MetricResourceResponse =
| { success: true; data: { rows: MetricResourceRow[] } }
| { success: false; error: string };

export type MetricResourceTimeRange = {
period: string | null;
from: string | null;
to: string | null;
};

export type MetricResourceQueryOptions = {
organizationId: string;
projectId: string;
environmentId: string;
timeRange: MetricResourceTimeRange;
defaultPeriod: string;
queues?: string[];
fillGaps?: boolean;
refreshIntervalMs?: number;
};

/**
* Client-fetch a TRQL query from the metric resource route (like the dashboard
* widgets): own loading state, interval + on-focus refresh, abort on change/unmount.
*/
export function useMetricResourceQuery(query: string, opts: MetricResourceQueryOptions) {
const [rows, setRows] = useState<MetricResourceRow[] | null>(null);
const [isLoading, setIsLoading] = useState(true);
const [failed, setFailed] = useState(false);
const abortRef = useRef<AbortController | null>(null);

const {
organizationId,
projectId,
environmentId,
defaultPeriod,
fillGaps,
refreshIntervalMs = 60_000,
} = opts;
const { period, from, to } = opts.timeRange;
const queuesKey = opts.queues && opts.queues.length > 0 ? opts.queues.join(",") : undefined;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Comma-joined queue names round-tripped through split(",") can misparse names containing a literal comma.

queuesKey is joined with "," purely to give useCallback a stable primitive dependency, but it's then split back into the array sent in the request body. A queue name containing a comma would be incorrectly split into extra entries, corrupting the queues scope sent to /resources/metric.

🐛 Proposed fix
-        ...(queuesKey !== undefined ? { queues: queuesKey.split(",") } : {}),
+        ...(opts.queues && opts.queues.length > 0 ? { queues: opts.queues } : {}),

Also applies to: 63-67


const load = useCallback(() => {
abortRef.current?.abort();
const controller = new AbortController();
abortRef.current = controller;
setIsLoading(true);
fetch("/resources/metric", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
query,
scope: "environment",
period: period ?? (from || to ? null : defaultPeriod),
from,
to,
fillGaps: !!fillGaps,
organizationId,
projectId,
environmentId,
...(queuesKey !== undefined ? { queues: queuesKey.split(",") } : {}),
}),
signal: controller.signal,
})
.then((res) => res.json() as Promise<MetricResourceResponse>)
.then((data) => {
if (controller.signal.aborted) return;
if (data.success) {
setRows(data.data.rows);
setFailed(false);
} else {
setFailed(true);
}
setIsLoading(false);
})
.catch((error) => {
if (error instanceof DOMException && error.name === "AbortError") return;
if (!controller.signal.aborted) {
setFailed(true);
setIsLoading(false);
}
});
}, [
query,
period,
from,
to,
defaultPeriod,
fillGaps,
organizationId,
projectId,
environmentId,
queuesKey,
]);

useEffect(() => {
load();
return () => abortRef.current?.abort();
}, [load]);

useInterval({ interval: refreshIntervalMs, onLoad: false, onFocus: true, callback: load });

return { rows: rows ?? [], isLoading, showLoading: isLoading && !rows, failed };
}
Loading
Loading