Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
22 changes: 16 additions & 6 deletions packages/ops/src/usage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ const QUERY = `query Usage($a: String!, $s: Date!, $e: Date!) {
sum { requests errors cpuTimeUs responseBodySize } dimensions { scriptName date }
}
di: durableObjectsInvocationsAdaptiveGroups(limit: 10000, filter: { date_geq: $s, date_leq: $e }) {
sum { requests errors responseBodySize } dimensions { scriptName date }
sum { requests errors responseBodySize } dimensions { scriptName namespaceId date }
}
dp: durableObjectsPeriodicGroups(limit: 10000, filter: { date_geq: $s, date_leq: $e }) {
sum { activeTime cpuTime inboundWebsocketMsgCount outboundWebsocketMsgCount } dimensions { namespaceId date }
Expand All @@ -138,19 +138,26 @@ const QUERY = `query Usage($a: String!, $s: Date!, $e: Date!) {

interface Raw {
w: { sum: { requests: number; errors: number; cpuTimeUs: number; responseBodySize: number }; dimensions: { scriptName: string; date: string } }[];
di: { sum: { requests: number; errors: number; responseBodySize: number }; dimensions: { scriptName: string; date: string } }[];
di: { sum: { requests: number; errors: number; responseBodySize: number }; dimensions: { scriptName: string; namespaceId?: string; date: string } }[];
dp: {
sum: { activeTime: number; cpuTime: number; inboundWebsocketMsgCount: number; outboundWebsocketMsgCount: number };
dimensions: { namespaceId: string; date: string };
}[];
}

/**
* Workers renamed since they first ran. Cloudflare keeps reporting some of a
* renamed worker's analytics under its first name, so both names count as one.
*/
export const RENAMED: Record<string, string> = { 'workspace-relay': 'relay', 'workspace-ops': 'operations' };

/** Turns the three analytics groups into per-day usage, keyed by namespace id in `labels`. */
export function fromRaw(raw: Raw, labels: Record<string, string> = {}): DayUsage[] {
const days = new Map<string, Map<string, FunctionUsage>>();
const get = (date: string, kind: FunctionUsage['kind'], name: string): FunctionUsage => {
let d = days.get(date);
if (!d) days.set(date, (d = new Map()));
name = RENAMED[name] ?? name;
const key = `${kind}:${name}`;
let f = d.get(key);
if (!f) d.set(key, (f = { kind, name, requests: 0, errors: 0, cpuMs: 0, gbSeconds: 0, wsIn: 0, wsOut: 0, bytesOut: 0 }));
Expand All @@ -163,9 +170,12 @@ export function fromRaw(raw: Raw, labels: Record<string, string> = {}): DayUsage
f.cpuMs += r.sum.cpuTimeUs / 1000;
f.bytesOut += r.sum.responseBodySize;
}
// Requests and response bytes are counted by script; duration and messages
// by namespace. A namespace is shown under its script's name: from `labels`,
// or the one script with Durable Objects when there is only one.
// Requests and response bytes are reported with both the script and the
// namespace; duration and messages with the namespace alone. So each
// namespace is shown under the script its requests name, and `labels`
// only names a namespace that had no requests in the period.
const scriptOf = new Map<string, string>();
for (const r of raw.di) if (r.dimensions.namespaceId) scriptOf.set(r.dimensions.namespaceId, r.dimensions.scriptName);
const doScripts = new Set(raw.di.map((r) => r.dimensions.scriptName));
const only = doScripts.size === 1 ? [...doScripts][0]! : null;
for (const r of raw.di) {
Expand All @@ -176,7 +186,7 @@ export function fromRaw(raw: Raw, labels: Record<string, string> = {}): DayUsage
}
for (const r of raw.dp) {
const id = r.dimensions.namespaceId;
const f = get(r.dimensions.date, 'durable-object', labels[id] ?? only ?? `namespace ${id.slice(0, 8)}`);
const f = get(r.dimensions.date, 'durable-object', scriptOf.get(id) ?? labels[id] ?? only ?? `namespace ${id.slice(0, 8)}`);
f.cpuMs += r.sum.cpuTime / 1000;
f.gbSeconds += (r.sum.activeTime / 1_000_000) * GB_PER_OBJECT;
f.wsIn += r.sum.inboundWebsocketMsgCount;
Expand Down
6 changes: 0 additions & 6 deletions packages/ops/src/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,7 @@ const SERVICES: Record<string, { label: string; role: string }> = {
label: 'Relay',
role: "Pairs two devices that can't reach each other directly and passes their encrypted traffic, unread.",
},
// Cloudflare's analytics keep reporting a renamed worker under its first name.
'workspace-relay': {
label: 'Relay',
role: "Pairs two devices that can't reach each other directly and passes their encrypted traffic, unread.",
},
operations: { label: 'This dashboard', role: 'This page.' },
'workspace-ops': { label: 'This dashboard', role: 'This page.' },
__unknown__: {
label: 'Not tied to a service',
role: "Requests Cloudflare couldn't attribute to a worker, such as calls to an address while a worker was being replaced.",
Expand Down
15 changes: 15 additions & 0 deletions packages/ops/tests/usage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,18 @@ test('the worker answers once with every view’s numbers, and the page draws ea
assert.match(((await none.json()) as { error: string }).error, /no Cloudflare API token/);
});


test('a namespace is one service with the worker its requests name, whatever its label says', () => {
const days = fromRaw(
{
w: [{ sum: { requests: 10, errors: 0, cpuTimeUs: 5000, responseBodySize: 100 }, dimensions: { scriptName: 'relay', date: '2026-11-22' } }],
di: [{ sum: { requests: 900, errors: 3, responseBodySize: 4000 }, dimensions: { scriptName: 'relay', namespaceId: 'ns1', date: '2026-11-22' } }],
dp: [{ sum: { activeTime: 8_000_000, cpuTime: 1000, inboundWebsocketMsgCount: 5, outboundWebsocketMsgCount: 7 }, dimensions: { namespaceId: 'ns1', date: '2026-11-22' } }],
},
// A label left from before the rename: the namespace's own requests name its worker.
{ ns1: 'some-old-name' },
);
const rooms = sumByFunction(days).filter((f) => f.kind === 'durable-object');
assert.equal(rooms.length, 1);
assert.deepEqual({ name: rooms[0]!.name, requests: rooms[0]!.requests, errors: rooms[0]!.errors, wsOut: rooms[0]!.wsOut, gbSeconds: rooms[0]!.gbSeconds }, { name: 'relay', requests: 900, errors: 3, wsOut: 7, gbSeconds: 1 });
});
Loading