-
Notifications
You must be signed in to change notification settings - Fork 2
feat: add structured relay health endpoints #17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -74,6 +74,63 @@ function json(data: unknown, status = 200): Response { | |
| }) | ||
| } | ||
|
|
||
| const RELAY_VERSION = '1.0.0' | ||
| const HEALTH_STARTED_AT_KEY = '_health:started-at' | ||
|
|
||
| async function getStartedAt(queue: KVNamespace, now: number): Promise<number> { | ||
| const value = await queue.get(HEALTH_STARTED_AT_KEY) | ||
| const startedAt = Number(value) | ||
|
|
||
| if (Number.isFinite(startedAt) && startedAt > 0) { | ||
| return startedAt | ||
| } | ||
|
|
||
| await queue.put(HEALTH_STARTED_AT_KEY, String(now)) | ||
| return now | ||
| } | ||
|
|
||
| async function countKeys(queue: KVNamespace, prefix: string): Promise<number> { | ||
| let count = 0 | ||
| let cursor: string | undefined | ||
|
|
||
| do { | ||
| const page = await queue.list({ prefix, cursor }) | ||
| count += page.keys.length | ||
| cursor = page.list_complete ? undefined : page.cursor | ||
| } while (cursor) | ||
|
|
||
| return count | ||
| } | ||
|
|
||
| async function handleHealth(env: Env): Promise<Response> { | ||
| const now = Date.now() | ||
|
|
||
| try { | ||
| const [startedAt, messageCount, unsendCount] = await Promise.all([ | ||
| getStartedAt(env.LINE_QUEUE, now), | ||
| countKeys(env.LINE_QUEUE, 'msg:'), | ||
| countKeys(env.LINE_QUEUE, 'unsend:'), | ||
| ]) | ||
|
|
||
| return json({ | ||
| service: 'line-relay', | ||
| version: RELAY_VERSION, | ||
| status: 'ok', | ||
| uptime_seconds: Math.floor((now - startedAt) / 1000), | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Naming nit: Because Consider renaming to |
||
| queue_depth: messageCount + unsendCount, | ||
| timestamp: new Date(now).toISOString(), | ||
| }) | ||
| } catch (error) { | ||
| console.error('[health] failed', error) | ||
| return json({ | ||
| service: 'line-relay', | ||
| version: RELAY_VERSION, | ||
| status: 'degraded', | ||
| timestamp: new Date(now).toISOString(), | ||
| }, 503) | ||
| } | ||
| } | ||
|
|
||
| // ── Handlers ─────────────────────────────────────────────── | ||
|
|
||
| async function handleWebhook(request: Request, env: Env): Promise<Response> { | ||
|
|
@@ -150,6 +207,9 @@ async function handleGetMessages(request: Request, env: Env): Promise<Response> | |
| const messages: QueuedMessage[] = [] | ||
|
|
||
| for (const key of allKeys.keys) { | ||
| if (!key.name.startsWith('msg:') && !key.name.startsWith('unsend:')) { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good defensive guard. The unfiltered The WhatsApp relay doesn't need this because its |
||
| continue | ||
| } | ||
| const val = await env.LINE_QUEUE.get(key.name) | ||
| if (val) { | ||
| try { | ||
|
|
@@ -267,7 +327,7 @@ export default { | |
| const path = url.pathname | ||
|
|
||
| if (path === '/health' || path === '/') { | ||
| return new Response('ok') | ||
| return handleHealth(env) | ||
| } | ||
|
|
||
| if (path === '/webhook' && request.method === 'POST') { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Performance consideration: This paginated walk runs on every
GET /health(andGET /too). KVlist()returns up to 1000 keys per page, so if the queue accumulates messages, each health check becomes O(n/1000) KV reads.If monitoring polls every 10–30 s this could add meaningful latency and KV read charges.
Two lighter alternatives:
_meta:queue-depthkey on enqueue (handleWebhook) and decrement on delete (handleDeleteMessages). Health check becomes a singleget().Same pattern applies to the WhatsApp relay's identical
countKeys.