Skip to content

Commit e958870

Browse files
jahoomaclaude
andcommitted
Log waiting room time-series metrics each admission tick
Emit queueDepth and activeCount every 15s with metric=freebuff_waiting_room so the waiting line length and concurrent admitted users can be charted over time from the log stream. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 3989559 commit e958870

File tree

3 files changed

+38
-14
lines changed

3 files changed

+38
-14
lines changed

web/src/server/free-session/__tests__/admission.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ function makeAdmissionDeps(overrides: Partial<AdmissionDeps> = {}): AdmissionDep
1515
calls,
1616
sweepExpired: async () => 0,
1717
queueDepth: async () => 0,
18+
activeCount: async () => 0,
1819
getFireworksHealth: async () => 'healthy',
1920
admitFromQueue: async ({ getFireworksHealth }) => {
2021
calls.admit += 1

web/src/server/free-session/admission.ts

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
isWaitingRoomEnabled,
66
} from './config'
77
import { getFireworksHealth } from './fireworks-health'
8-
import { admitFromQueue, queueDepth, sweepExpired } from './store'
8+
import { activeCount, admitFromQueue, queueDepth, sweepExpired } from './store'
99

1010
import type { FireworksHealth } from './fireworks-health'
1111

@@ -14,6 +14,7 @@ import { logger } from '@/util/logger'
1414
export interface AdmissionDeps {
1515
sweepExpired: (now: Date, graceMs: number) => Promise<number>
1616
queueDepth: () => Promise<number>
17+
activeCount: () => Promise<number>
1718
admitFromQueue: (params: {
1819
sessionLengthMs: number
1920
now: Date
@@ -29,6 +30,7 @@ export interface AdmissionDeps {
2930
const defaultDeps: AdmissionDeps = {
3031
sweepExpired,
3132
queueDepth,
33+
activeCount,
3234
admitFromQueue,
3335
// FREEBUFF_DEV_FORCE_ADMIT lets local `dev:freebuff` drive the full
3436
// waiting-room → admitted → ended flow without a real upstream.
@@ -48,6 +50,7 @@ export interface AdmissionTickResult {
4850
expired: number
4951
admitted: number
5052
queueDepth: number
53+
activeCount: number
5154
skipped: FireworksHealth | null
5255
}
5356

@@ -77,8 +80,17 @@ export async function runAdmissionTick(
7780
getFireworksHealth: deps.getFireworksHealth,
7881
})
7982

80-
const depth = await deps.queueDepth()
81-
return { expired, admitted: admitted.length, queueDepth: depth, skipped }
83+
const [depth, active] = await Promise.all([
84+
deps.queueDepth(),
85+
deps.activeCount(),
86+
])
87+
return {
88+
expired,
89+
admitted: admitted.length,
90+
queueDepth: depth,
91+
activeCount: active,
92+
skipped,
93+
}
8294
}
8395

8496
let interval: ReturnType<typeof setInterval> | null = null
@@ -89,17 +101,20 @@ function runTick() {
89101
inFlight = true
90102
runAdmissionTick()
91103
.then((result) => {
92-
if (result.admitted > 0 || result.expired > 0 || result.skipped !== null) {
93-
logger.info(
94-
{
95-
admitted: result.admitted,
96-
expired: result.expired,
97-
queueDepth: result.queueDepth,
98-
skipped: result.skipped,
99-
},
100-
'[FreeSessionAdmission] tick',
101-
)
102-
}
104+
// Emit every tick so queueDepth/activeCount form a continuous time-series
105+
// that can be charted over time. metric=freebuff_waiting_room makes it
106+
// filterable in the log aggregator.
107+
logger.info(
108+
{
109+
metric: 'freebuff_waiting_room',
110+
admitted: result.admitted,
111+
expired: result.expired,
112+
queueDepth: result.queueDepth,
113+
activeCount: result.activeCount,
114+
skipped: result.skipped,
115+
},
116+
'[FreeSessionAdmission] tick',
117+
)
103118
})
104119
.catch((error) => {
105120
logger.warn(

web/src/server/free-session/store.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,14 @@ export async function queueDepth(): Promise<number> {
108108
return Number(rows[0]?.n ?? 0)
109109
}
110110

111+
export async function activeCount(): Promise<number> {
112+
const rows = await db
113+
.select({ n: count() })
114+
.from(schema.freeSession)
115+
.where(eq(schema.freeSession.status, 'active'))
116+
return Number(rows[0]?.n ?? 0)
117+
}
118+
111119
export async function queuePositionFor(params: {
112120
userId: string
113121
queuedAt: Date

0 commit comments

Comments
 (0)