@rebasepro/server 0.33.2 → 0.34.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{GCSStorageController-kCv5MH2B.js → GCSStorageController-DKv7oDo7.js} +3 -3
- package/dist/{GCSStorageController-kCv5MH2B.js.map → GCSStorageController-DKv7oDo7.js.map} +1 -1
- package/dist/{S3StorageController-DqK533Sl.js → S3StorageController-CIQ2_mC5.js} +3 -3
- package/dist/{S3StorageController-DqK533Sl.js.map → S3StorageController-CIQ2_mC5.js.map} +1 -1
- package/dist/{access-CRQBlEZY.js → access-BdsIXo_L.js} +4 -4
- package/dist/{access-CRQBlEZY.js.map → access-BdsIXo_L.js.map} +1 -1
- package/dist/api/rest/api-generator.d.ts +11 -1
- package/dist/auth/auth-hooks.d.ts +1 -1
- package/dist/auth/builtin-auth-adapter.d.ts +2 -0
- package/dist/auth/capabilities.d.ts +12 -0
- package/dist/auth/platform-signin-routes.d.ts +85 -0
- package/dist/auth/platform-token.d.ts +33 -0
- package/dist/auth/routes.d.ts +8 -0
- package/dist/auth/seed-admin.d.ts +2 -0
- package/dist/{auth-BanB_pZj.js → auth-CHOmnYwu.js} +358 -12
- package/dist/auth-CHOmnYwu.js.map +1 -0
- package/dist/{backup-CVNce2jz.js → backup-M37yPYpz.js} +6 -6
- package/dist/{backup-CVNce2jz.js.map → backup-M37yPYpz.js.map} +1 -1
- package/dist/boot/bundle.d.ts +12 -0
- package/dist/boot/env.d.ts +1 -0
- package/dist/{branch-routes-aLDaGOD1.js → branch-routes-Cl-Oa3CL.js} +2 -2
- package/dist/{branch-routes-aLDaGOD1.js.map → branch-routes-Cl-Oa3CL.js.map} +1 -1
- package/dist/{contract-routes-a4ZbN1h3.js → contract-routes-Dkxl-2B_.js} +3 -3
- package/dist/{contract-routes-a4ZbN1h3.js.map → contract-routes-Dkxl-2B_.js.map} +1 -1
- package/dist/{cron-loader-CWhyA5eR.js → cron-loader-BWahenzQ.js} +2 -2
- package/dist/{cron-loader-CWhyA5eR.js.map → cron-loader-BWahenzQ.js.map} +1 -1
- package/dist/{cron-routes-CMGAz1my.js → cron-routes-Cq6O3oPR.js} +4 -4
- package/dist/{cron-routes-CMGAz1my.js.map → cron-routes-Cq6O3oPR.js.map} +1 -1
- package/dist/{cron-scheduler-DQBdkkfb.js → cron-scheduler-B25NuV7o.js} +2 -2
- package/dist/{cron-scheduler-DQBdkkfb.js.map → cron-scheduler-B25NuV7o.js.map} +1 -1
- package/dist/{cron-store-CjUydRyp.js → cron-store-B1IStnKM.js} +2 -2
- package/dist/{cron-store-CjUydRyp.js.map → cron-store-B1IStnKM.js.map} +1 -1
- package/dist/dist-DDppJiwg.js +19354 -0
- package/dist/dist-DDppJiwg.js.map +1 -0
- package/dist/email/link-base.d.ts +21 -0
- package/dist/email/types.d.ts +4 -0
- package/dist/{filter-dialect-nsQ2ozPY.js → filter-dialect-DgWkoXt7.js} +2 -2
- package/dist/{filter-dialect-nsQ2ozPY.js.map → filter-dialect-DgWkoXt7.js.map} +1 -1
- package/dist/{function-loader-Cihj_r86.js → function-loader-C23HVFx8.js} +2 -2
- package/dist/{function-loader-Cihj_r86.js.map → function-loader-C23HVFx8.js.map} +1 -1
- package/dist/{function-routes-CWaldgEj.js → function-routes-DBKL2azw.js} +2 -2
- package/dist/{function-routes-CWaldgEj.js.map → function-routes-DBKL2azw.js.map} +1 -1
- package/dist/{history-recorder-B8TJMEfO.js → history-recorder-C5gixsU3.js} +16 -3
- package/dist/history-recorder-C5gixsU3.js.map +1 -0
- package/dist/history-store-c_cm9R3w.js +459 -0
- package/dist/history-store-c_cm9R3w.js.map +1 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.es.js +273 -236
- package/dist/index.es.js.map +1 -1
- package/dist/{jobs-IJX3fE3X.js → jobs-uqvEGTfs.js} +2 -2
- package/dist/{jobs-IJX3fE3X.js.map → jobs-uqvEGTfs.js.map} +1 -1
- package/dist/{jwt-BuEprb39.js → jwt-B_5zx-7y.js} +3 -3
- package/dist/{jwt-BuEprb39.js.map → jwt-B_5zx-7y.js.map} +1 -1
- package/dist/{keys-C8gpPIOG.js → keys-C0g4vwP5.js} +2 -2
- package/dist/{keys-C8gpPIOG.js.map → keys-C0g4vwP5.js.map} +1 -1
- package/dist/{load-schema-BvRxAuaD.js → load-schema-aNynBztw.js} +2 -2
- package/dist/{load-schema-BvRxAuaD.js.map → load-schema-aNynBztw.js.map} +1 -1
- package/dist/{logs-routes-BAq2ZxuY.js → logs-routes-D2NGM5Pa.js} +2 -2
- package/dist/{logs-routes-BAq2ZxuY.js.map → logs-routes-D2NGM5Pa.js.map} +1 -1
- package/dist/metrics/history-recorder.d.ts +10 -1
- package/dist/metrics/history-store.d.ts +122 -7
- package/dist/metrics/index.d.ts +24 -2
- package/dist/{offline-ET6xI1ZO-CIdB1ilf.js → offline-ET6xI1ZO-Du75S8J1.js} +4 -4
- package/dist/{offline-ET6xI1ZO-CIdB1ilf.js.map → offline-ET6xI1ZO-Du75S8J1.js.map} +1 -1
- package/dist/{offline-store-DtmBwmKE-CzyvC_Zj.js → offline-store-DtmBwmKE-CEakGPWn.js} +3 -3
- package/dist/{offline-store-DtmBwmKE-CzyvC_Zj.js.map → offline-store-DtmBwmKE-CEakGPWn.js.map} +1 -1
- package/dist/{openapi-generator-CCLtAJ2s.js → openapi-generator-B7EbCjhN.js} +2 -2
- package/dist/{openapi-generator-CCLtAJ2s.js.map → openapi-generator-B7EbCjhN.js.map} +1 -1
- package/dist/{project-root-CuCOXvbk.js → project-root-BTQAiPki.js} +2 -2
- package/dist/{project-root-CuCOXvbk.js.map → project-root-BTQAiPki.js.map} +1 -1
- package/dist/{proxy-DUUXXoQo.js → proxy-0fGXJapt.js} +2 -2
- package/dist/{proxy-DUUXXoQo.js.map → proxy-0fGXJapt.js.map} +1 -1
- package/dist/{query-parser-C9l1eFov.js → query-parser-DwERE0rm.js} +3 -3
- package/dist/{query-parser-C9l1eFov.js.map → query-parser-DwERE0rm.js.map} +1 -1
- package/dist/{request-timeout-DOu8ItLQ.js → request-timeout-DdbFS6ef.js} +3 -3
- package/dist/{request-timeout-DOu8ItLQ.js.map → request-timeout-DdbFS6ef.js.map} +1 -1
- package/dist/{rls-simulator-routes-DIAjffhq.js → rls-simulator-routes-Cjv8yY1V.js} +3 -3
- package/dist/{rls-simulator-routes-DIAjffhq.js.map → rls-simulator-routes-Cjv8yY1V.js.map} +1 -1
- package/dist/{schema-editor-routes-qWGWBPi0.js → schema-editor-routes-CwdoGYKu.js} +4 -4
- package/dist/{schema-editor-routes-qWGWBPi0.js.map → schema-editor-routes-CwdoGYKu.js.map} +1 -1
- package/dist/{schema-tables-BneB2KZ7.js → schema-tables-DUThMxen.js} +3 -3
- package/dist/{schema-tables-BneB2KZ7.js.map → schema-tables-DUThMxen.js.map} +1 -1
- package/dist/{schema-tables-routes-BBZJK8_S.js → schema-tables-routes-Ch52xLqF.js} +4 -4
- package/dist/{schema-tables-routes-BBZJK8_S.js.map → schema-tables-routes-Ch52xLqF.js.map} +1 -1
- package/dist/{securityRuleToConditions-Cj5ZR7TS.js → securityRuleToConditions-DXPY_Z-x.js} +2 -2
- package/dist/{securityRuleToConditions-Cj5ZR7TS.js.map → securityRuleToConditions-DXPY_Z-x.js.map} +1 -1
- package/dist/{selection-BB8ejqa2.js → selection-BiTJ-Tiy.js} +2 -2
- package/dist/{selection-BB8ejqa2.js.map → selection-BiTJ-Tiy.js.map} +1 -1
- package/dist/{sql-export-routes-CPXI3qZ7.js → sql-export-routes-BYt7G2v-.js} +2 -2
- package/dist/{sql-export-routes-CPXI3qZ7.js.map → sql-export-routes-BYt7G2v-.js.map} +1 -1
- package/dist/{src-BdPKCs9u.js → src--EBfCtYm.js} +2 -2
- package/dist/src--EBfCtYm.js.map +1 -0
- package/dist/storage/routes.d.ts +6 -0
- package/dist/{table-source-editor-D3m_VMwN.js → table-source-editor-P5zp7lOn.js} +3 -3
- package/dist/{table-source-editor-D3m_VMwN.js.map → table-source-editor-P5zp7lOn.js.map} +1 -1
- package/package.json +5 -5
- package/dist/auth-BanB_pZj.js.map +0 -1
- package/dist/dist-C1vNURUy.js +0 -19354
- package/dist/dist-C1vNURUy.js.map +0 -1
- package/dist/history-recorder-B8TJMEfO.js.map +0 -1
- package/dist/history-store-DzrJeEKv.js +0 -233
- package/dist/history-store-DzrJeEKv.js.map +0 -1
- package/dist/src-BdPKCs9u.js.map +0 -1
|
@@ -2,7 +2,7 @@ import { createRequire as __rebaseCreateRequire } from "module";
|
|
|
2
2
|
import __rebaseProcess from "process";
|
|
3
3
|
globalThis.process ??= __rebaseProcess;
|
|
4
4
|
__rebaseCreateRequire(import.meta.url);
|
|
5
|
-
import {
|
|
5
|
+
import { zn as __exportAll } from "./src--EBfCtYm.js";
|
|
6
6
|
import { n as addLogSink } from "./logger-SigQ2oZJ.js";
|
|
7
7
|
import { i as errorHandler, t as ApiError } from "./errors-NVbAQU25.js";
|
|
8
8
|
import { Hono } from "hono";
|
|
@@ -380,4 +380,4 @@ createLogsRoutes();
|
|
|
380
380
|
//#endregion
|
|
381
381
|
export { logs_routes_exports as n, logMiddleware as t };
|
|
382
382
|
|
|
383
|
-
//# sourceMappingURL=logs-routes-
|
|
383
|
+
//# sourceMappingURL=logs-routes-D2NGM5Pa.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"logs-routes-BAq2ZxuY.js","names":[],"sources":["../src/api/logs-routes.ts"],"sourcesContent":["import { Hono } from \"hono\";\nimport type { MiddlewareHandler } from \"hono\";\nimport { streamSSE } from \"hono/streaming\";\nimport type { HonoEnv } from \"./types\";\nimport { ApiError, errorHandler } from \"./errors\";\nimport { addLogSink } from \"../utils/logger\";\n\nexport interface LogEntry {\n id: string;\n timestamp: string;\n level: \"debug\" | \"info\" | \"warn\" | \"error\";\n source: \"api\" | \"auth\" | \"storage\" | \"realtime\" | \"system\";\n message: string;\n metadata?: Record<string, unknown>;\n}\n\n/** What a caller can narrow the log by, in either direction (query or stream). */\nexport interface LogFilterOptions {\n level?: string;\n source?: string;\n search?: string;\n since?: string;\n}\n\n/**\n * A filter with the search term already lowercased.\n *\n * The distinction matters on the stream path: `query()` lowercases once and then\n * scans, but a subscriber tests one entry at a time and would otherwise redo the\n * same `toLowerCase()` on every request the server handles.\n */\ntype NormalizedFilter = LogFilterOptions;\n\nfunction normalizeFilter(options: LogFilterOptions): NormalizedFilter {\n return { ...options,\n search: options.search?.toLowerCase() };\n}\n\n/**\n * Whether one entry belongs in a filtered view.\n *\n * Shared by the query and the stream on purpose: two copies of this would drift,\n * and the failure that produces is invisible — a tail that quietly shows a\n * different set of lines than the snapshot it started from.\n */\nfunction matchesFilter(entry: LogEntry, filter: NormalizedFilter): boolean {\n if (filter.level && entry.level !== filter.level) return false;\n if (filter.source && entry.source !== filter.source) return false;\n if (filter.search && !entry.message.toLowerCase().includes(filter.search)) return false;\n if (filter.since && entry.timestamp < filter.since) return false;\n return true;\n}\n\n/** Notified for every entry pushed, in push order. */\nexport type LogListener = (entry: LogEntry) => void;\n\nclass LogRingBuffer {\n private buffer: LogEntry[] = [];\n private maxSize: number;\n private idCounter = 0;\n private listeners = new Set<LogListener>();\n\n constructor(maxSize = 10000) {\n this.maxSize = maxSize;\n }\n\n push(entry: Omit<LogEntry, \"id\">): void {\n const id = `log_${++this.idCounter}`;\n const stored: LogEntry = { ...entry,\n id };\n this.buffer.push(stored);\n if (this.buffer.length > this.maxSize) {\n this.buffer.shift();\n }\n // This runs on the request hot path, so a listener must never be able to\n // take the request down with it: a tail that throws loses its own tail,\n // not the response the log line was describing.\n for (const listener of this.listeners) {\n try {\n listener(stored);\n } catch {\n /* a broken tail is not the request's problem */\n }\n }\n }\n\n /**\n * Follow the buffer. Returns the unsubscribe — call it, always: a listener\n * left behind holds its whole closure, and on this class that closure is a\n * pending-entry array.\n *\n * A listener must not log. It is called from inside `push`, so anything that\n * reaches `addLog` from here recurses until the stack gives out.\n */\n subscribe(listener: LogListener): () => void {\n this.listeners.add(listener);\n return () => {\n this.listeners.delete(listener);\n };\n }\n\n query(options: LogFilterOptions & {\n limit?: number;\n offset?: number;\n }): { entries: LogEntry[]; total: number } {\n const filter = normalizeFilter(options);\n const filtered = this.buffer.filter(e => matchesFilter(e, filter));\n\n // Newest first\n const sorted = [...filtered].reverse();\n const total = sorted.length;\n const limit = options.limit || 100;\n const offset = options.offset || 0;\n\n return {\n entries: sorted.slice(offset, offset + limit),\n total\n };\n }\n\n getLatest(count = 50): LogEntry[] {\n return this.buffer.slice(-count).reverse();\n }\n}\n\n// Global singleton\nexport const logBuffer = new LogRingBuffer();\n\n/**\n * Which process this log belongs to.\n *\n * The ring is this process's memory, and nothing gathers the rings of a\n * deployment's other replicas or of its split runtime roles. So a reader is\n * told whose log it is: `HOSTNAME` is the pod name on Kubernetes and the\n * container id under Docker; the pid tells two local processes apart.\n */\nexport function logInstanceName(): string {\n return process.env.HOSTNAME?.trim() || `pid-${process.pid}`;\n}\n\n/** Add a log entry */\nexport function addLog(\n level: LogEntry[\"level\"],\n source: LogEntry[\"source\"],\n message: string,\n metadata?: Record<string, unknown>\n): void {\n logBuffer.push({\n timestamp: new Date().toISOString(),\n level,\n source,\n message,\n metadata\n });\n}\n\nexport interface LogMiddlewareOptions {\n /**\n * Paths this sink ignores, matched exactly against `c.req.path`.\n *\n * For requests whose only reason to exist is to read the log. Recording\n * those makes the reader the loudest thing in its own output, and on a quiet\n * server it is also the thing evicting real entries out of the ring.\n */\n ignorePaths?: string[];\n /**\n * Where the API is mounted (`/api`). The segment after it names the\n * subsystem a request is filed under — see {@link sourceForRequestPath}.\n * Unset, the first segment of the path is read.\n */\n basePath?: string;\n}\n\n/** The first path segment under the base path that files a request under a source other than `api`. */\nconst SOURCE_BY_SEGMENT: Readonly<Record<string, LogEntry[\"source\"]>> = {\n auth: \"auth\",\n oauth: \"auth\",\n storage: \"storage\"\n};\n\n/**\n * The `source` of a request entry: the subsystem its path addresses.\n *\n * `/api/auth/*` and the MCP authorization server under `/api/oauth/*` are\n * `auth`, `/api/storage/*` is `storage`, and everything else is `api`. The\n * Logs Explorer's Source filter matches this field, so a sign-in filed as\n * `api` was invisible under Auth. The realtime socket is not an HTTP request\n * and is not recorded here.\n *\n * Exported for its test.\n */\nexport function sourceForRequestPath(path: string, basePath = \"\"): LogEntry[\"source\"] {\n const base = basePath.replace(/\\/+$/, \"\");\n if (base && path !== base && !path.startsWith(`${base}/`)) return \"api\";\n const segment = path.slice(base.length).split(\"/\").find(part => part !== \"\")?.toLowerCase();\n return (segment !== undefined && Object.prototype.hasOwnProperty.call(SOURCE_BY_SEGMENT, segment))\n ? SOURCE_BY_SEGMENT[segment]\n : \"api\";\n}\n\n/** Hono middleware to log API requests */\nexport function logMiddleware(options: LogMiddlewareOptions = {}): MiddlewareHandler<HonoEnv> {\n const ignored = new Set(options.ignorePaths ?? []);\n return async (c, next) => {\n const start = Date.now();\n await next();\n if (ignored.has(c.req.path)) return;\n const duration = Date.now() - start;\n const reqId = c.get(\"requestId\");\n // Every request used to be recorded at `info`, whatever it answered, so\n // the Logs Explorer's level filter could not find a single failure: a\n // 500 sat at the same level as the 200 above it, in a wall of them.\n const status = c.res.status;\n const level: LogEntry[\"level\"] = status >= 500 ? \"error\" : status >= 400 ? \"warn\" : \"info\";\n // What the error handler answered, so the failure is on the entry\n // rather than in a stdout line the panel cannot see. See\n // `HonoEnv.Variables.errorSummary`.\n const failure = c.get(\"errorSummary\");\n addLog(\n level,\n sourceForRequestPath(c.req.path, options.basePath),\n `${c.req.method} ${c.req.path} ${status} ${duration}ms`\n + (failure ? ` — ${failure.code}: ${failure.message}` : \"\"),\n {\n method: c.req.method,\n path: c.req.path,\n status,\n duration,\n ...(reqId && { requestId: reqId }),\n ...(c.get(\"collection\") && { collection: c.get(\"collection\") }),\n ...(failure && { errorCode: failure.code, errorMessage: failure.message })\n }\n );\n };\n}\n\n/**\n * Prefixes the server writes at the head of a log message, and the `source`\n * each one belongs to.\n *\n * The ring's `source` is a closed set the Studio filters on, and the messages\n * carry their origin as a bracketed prefix — `[API]`, `[Auth]`, `[functions]`,\n * `[schema]`. Matching them is what makes a teed line filterable beside the\n * request entries rather than a heap under \"system\".\n */\nconst SOURCE_BY_PREFIX: Array<[RegExp, LogEntry[\"source\"]]> = [\n [/^\\[?(api|rest)\\b/i, \"api\"],\n [/^\\[?auth\\b/i, \"auth\"],\n [/^\\[?(storage|s3|gcs)\\b/i, \"storage\"],\n [/^\\[?(realtime|ws|websocket|cdc)\\b/i, \"realtime\"]\n];\n\n/**\n * The `source` for a teed line, from whatever prefix it carries.\n *\n * Exported for its test: the mapping is a string match on wording somebody else\n * writes, which is the shape of check that stops working without failing.\n */\nexport function sourceForMessage(message: string): LogEntry[\"source\"] {\n // The level emoji comes first on several call sites (`⚠️ [API] …`), so the\n // prefix is whatever is inside the first bracket, wherever that is.\n const bracketed = message.match(/\\[([^\\]]{1,32})\\]/);\n const candidate = bracketed?.[1] ?? message;\n for (const [pattern, source] of SOURCE_BY_PREFIX) {\n if (pattern.test(candidate)) return source;\n }\n return \"system\";\n}\n\n/**\n * Feed the Logs Explorer everything the server says at warn and above.\n *\n * The ring used to be filled by `logMiddleware` alone, so the panel showed a\n * wall of `GET /api/data/posts 200 4ms` and not one of the errors, warnings or\n * boot diagnoses being written to stdout at the same moment. A function that\n * threw was the sharpest case: the request entry said `500` and the reason\n * existed only in a terminal the person looking at the panel does not have.\n *\n * Warn and above, deliberately. `info` is where the steady-state chatter lives,\n * and a 10,000-entry ring filled with it evicts the lines somebody opened the\n * panel to find.\n *\n * Idempotent: called from `createLogsRoutes`, which a split deployment may\n * reach more than once.\n */\nlet detachLoggerTee: (() => void) | undefined;\nexport function teeLoggerIntoLogBuffer(): () => void {\n if (detachLoggerTee) return detachLoggerTee;\n const detach = addLogSink((level, message, data) => {\n if (level !== \"warn\" && level !== \"error\") return;\n // `requestLogger` writes this one to stdout for every request, and\n // `logMiddleware` has already recorded the same request here with the\n // fields this panel renders. Teeing it too would double every failure.\n if (message === \"request\") return;\n addLog(level, sourceForMessage(message), message, Object.keys(data).length > 0 ? data : undefined);\n });\n detachLoggerTee = () => {\n detach();\n detachLoggerTee = undefined;\n };\n return detachLoggerTee;\n}\n\n/**\n * How long entries accumulate before a batch goes out.\n *\n * Not zero, and that is the point. A busy server logs faster than a browser can\n * render, and one SSE frame per line would hand the client a re-render per\n * request served — worse than the 3s poll this replaces, precisely when the logs\n * are worth watching. Coalescing keeps the frame rate bounded by the window\n * rather than by traffic, and 250ms still reads as \"live\" to a person.\n */\nconst STREAM_FLUSH_MS = 250;\n\n/**\n * Idle gap after which the stream sends a comment line.\n *\n * A silent SSE connection is indistinguishable from a dead one to everything in\n * between — proxies, load balancers and laptop NICs all reap idle sockets, and a\n * server with nothing to say is the normal state here.\n */\nconst STREAM_HEARTBEAT_MS = 25_000;\n\n/**\n * Entries a single connection will hold between flushes.\n *\n * This is a *rate* ceiling, not just a memory bound, and that is easy to get\n * wrong: nothing drains `pending` between flushes, so the most a connection can\n * carry losslessly is `maxPending` per `flushMs` — here 2000 per 250ms, or 8000\n * entries a second. Above that the oldest pending entries go and the client is\n * told how many, whatever speed it is reading at.\n *\n * It was 500, which put that ceiling at 2000/s. A healthy reader on a loopback\n * socket lost 85% of a 20k burst to it — the cap fired on the server's own\n * coalescing window rather than on any slowness at the client, which is a drop\n * notice that says nothing true about why. 8000/s is past what one Node process\n * serves, so reaching it now means genuinely more log than a person can be shown.\n *\n * The memory this bounds is the copy a *stalled* reader causes: roughly 2000\n * entries, a few MB, per stuck connection.\n */\nconst STREAM_MAX_PENDING = 2000;\n\n/**\n * The largest window any of these routes will hand back.\n *\n * The ring buffer holds 10,000 entries, so asking for more than all of it is a\n * mistake worth naming rather than silently clamping — and a clamped answer is\n * indistinguishable from \"that is all there is\".\n */\nconst LOG_WINDOW_MAX = 10_000;\n\n/**\n * The stream's timings, injectable only so they can be tested.\n *\n * The defaults above are the contract and nothing in production passes this. A\n * heartbeat is a 25-second wait to observe, and a suite that cannot observe it is\n * a suite where the keepalive can rot — which surfaces as \"the tail dies after a\n * few minutes behind the load balancer\", months later, on someone else's cluster.\n */\nexport interface LogStreamTiming {\n flushMs?: number;\n heartbeatMs?: number;\n maxPending?: number;\n}\n\n/** What ends the stream from the server's side. */\nexport interface LogsRoutesOptions {\n /**\n * Aborted when the backend shuts down. Every open stream ends then, rather\n * than when its client happens to leave: `server.close()` waits on every\n * open connection, so one Studio → Logs tab held a SIGTERM for the whole\n * force timeout — longer than Cloud Run's or `docker stop`'s grace period,\n * which killed the process before the pool was closed.\n */\n closeSignal?: AbortSignal;\n}\n\nexport function createLogsRoutes(timing: LogStreamTiming = {}, options: LogsRoutesOptions = {}): Hono<HonoEnv> {\n // Here rather than at module load: the ring exists whether or not anything\n // reads it, but there is no reason to fill it on a process that serves no\n // logs surface. Idempotent, so the repeated calls a split deployment makes\n // do not stack up sinks.\n teeLoggerIntoLogBuffer();\n\n const flushMs = timing.flushMs ?? STREAM_FLUSH_MS;\n const heartbeatMs = timing.heartbeatMs ?? STREAM_HEARTBEAT_MS;\n const maxPending = timing.maxPending ?? STREAM_MAX_PENDING;\n\n const app = new Hono<HonoEnv>();\n // Its own, like every other router here: nothing registers one on the host\n // app — not `boot.ts`, not the scaffolded backend, not the eject template —\n // so a router that throws without this answers Hono's default 500 in plain\n // text, outside the `{ error: { code, message } }` envelope the rest of the\n // API keeps to.\n app.onError(errorHandler);\n\n /**\n * A window into the ring buffer, or a 400 saying why not.\n *\n * `parseInt` was the whole of it before, and every malformed value failed\n * differently and silently: `?limit=abc` fell through to the default,\n * `?limit=-5` sliced an empty window and answered 200 with no entries, and\n * `?count=abc` made `slice(-NaN)` return the *entire* buffer. Three ways to\n * be wrong, none of them visible to the caller. The data plane refuses the\n * same input with a 400 — see `resolveListLimitParam`.\n *\n * `min` is 1 for a size and 0 for an offset: `?offset=0` is the first page,\n * which is where every pager starts, and it was a 400.\n */\n const window = (raw: string | undefined, what: string, max: number, min = 1): number | undefined => {\n if (raw === undefined || raw.trim() === \"\") return undefined;\n const parsed = Number(raw.trim());\n if (!Number.isInteger(parsed) || parsed < min || parsed > max) {\n throw new ApiError(\n 400,\n \"INVALID_PARAM\",\n `Invalid \\`${what}\\`: ${raw}. Expected a whole number between ${min} and ${max}.`,\n undefined,\n true\n );\n }\n return parsed;\n };\n\n // GET /api/logs — Query logs\n app.get(\"/\", (c) => {\n const query = c.req.query();\n const result = logBuffer.query({\n level: query.level,\n source: query.source,\n search: query.search,\n limit: window(query.limit, \"limit\", LOG_WINDOW_MAX),\n offset: window(query.offset, \"offset\", Number.MAX_SAFE_INTEGER, 0),\n since: query.since\n });\n return c.json(result);\n });\n\n // GET /api/logs/latest — Get latest logs (for real-time)\n app.get(\"/latest\", (c) => {\n const count = window(c.req.query(\"count\"), \"count\", LOG_WINDOW_MAX) ?? 50;\n return c.json({ entries: logBuffer.getLatest(count) });\n });\n\n // GET /api/logs/stream — tail the buffer over SSE.\n //\n // The Logs Explorer used to poll this router every 3 seconds, which cost a\n // request per client per 3s to say \"nothing happened\" and still showed each\n // line up to 3s late. Here the buffer pushes instead, so an idle server is an\n // idle socket.\n //\n // Events:\n // snapshot {entries, total, instance}\n // the filtered window, oldest-first, at open,\n // and the process whose ring it is\n // append {entries, dropped} entries since the last frame, oldest-first\n // `: ping` comment, keepalive only\n //\n // Snapshot and appends come down the same connection deliberately. A client\n // that fetched its backlog separately would race the subscription — entries\n // logged between the two calls belong to neither — and closing that race from\n // the outside needs an id cursor and dedupe on every frame.\n app.get(\"/stream\", (c) => {\n const query = c.req.query();\n const filter = normalizeFilter({\n level: query.level,\n source: query.source,\n search: query.search\n });\n const limit = window(query.limit, \"limit\", LOG_WINDOW_MAX) ?? 200;\n\n // Reverse proxies buffer text responses by default, which turns a live\n // tail into nothing at all until the buffer fills. nginx (and the ingress\n // in front of the managed runtime) reads this header; everything else\n // ignores it. The rest of the SSE headers are set by `streamSSE`.\n c.header(\"X-Accel-Buffering\", \"no\");\n\n return streamSSE(c, async (stream) => {\n let pending: LogEntry[] = [];\n let dropped = 0;\n\n // Subscribe *before* reading the backlog, with nothing awaited\n // between the two. Both are synchronous, so the two halves meet\n // exactly: an entry logged after the query but before the\n // subscription would otherwise be in neither, and that gap is the one\n // thing this route exists to close.\n const unsubscribe = logBuffer.subscribe(entry => {\n if (!matchesFilter(entry, filter)) return;\n if (pending.length >= maxPending) {\n pending.shift();\n dropped++;\n }\n pending.push(entry);\n });\n const snapshot = logBuffer.query({ ...filter,\n limit });\n\n // A client that goes away has to end this handler, or the\n // subscription outlives the socket. `streamSSE` only wires the\n // request signal through on old Bun, so do it here and let\n // `stream.aborted` be the one condition the loop tests.\n //\n // The `aborted` check is not belt-and-braces. A listener added to an\n // already-aborted signal is never called, so a client that leaves\n // during the snapshot write — a fast navigation, or a reconnect storm\n // against a restarting server — would leave this handler with no way\n // to learn it had gone: a subscriber and a flush loop, per attempt,\n // for the life of the process.\n const abortOnDisconnect = () => {\n if (!stream.closed) stream.abort();\n };\n c.req.raw.signal.addEventListener(\"abort\", abortOnDisconnect, { once: true });\n if (c.req.raw.signal.aborted) abortOnDisconnect();\n // The server going away ends it the same way, for the same reason\n // the client going away does — and a stream opened after shutdown\n // began still sends its snapshot, then ends.\n const closeSignal = options.closeSignal;\n closeSignal?.addEventListener(\"abort\", abortOnDisconnect, { once: true });\n if (closeSignal?.aborted) abortOnDisconnect();\n\n try {\n await stream.writeSSE({\n event: \"snapshot\",\n // The view tails like a terminal; both frames are oldest-first\n // so the client only ever appends.\n data: JSON.stringify({\n entries: snapshot.entries.slice().reverse(),\n total: snapshot.total,\n instance: logInstanceName()\n })\n });\n\n let idleMs = 0;\n while (!stream.aborted && !stream.closed) {\n await stream.sleep(flushMs);\n if (stream.aborted || stream.closed) break;\n\n if (pending.length === 0) {\n idleMs += flushMs;\n if (idleMs >= heartbeatMs) {\n await stream.write(\": ping\\n\\n\");\n idleMs = 0;\n }\n continue;\n }\n\n const entries = pending;\n const lost = dropped;\n pending = [];\n dropped = 0;\n idleMs = 0;\n await stream.writeSSE({\n event: \"append\",\n data: JSON.stringify(lost > 0 ? { entries,\n dropped: lost } : { entries })\n });\n }\n } finally {\n unsubscribe();\n c.req.raw.signal.removeEventListener(\"abort\", abortOnDisconnect);\n closeSignal?.removeEventListener(\"abort\", abortOnDisconnect);\n }\n });\n });\n\n return app;\n}\n\nexport default createLogsRoutes();\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAiCA,SAAS,gBAAgB,SAA6C;CAClE,OAAO;EAAE,GAAG;EACR,QAAQ,QAAQ,QAAQ,YAAY;CAAE;AAC9C;;;;;;;;AASA,SAAS,cAAc,OAAiB,QAAmC;CACvE,IAAI,OAAO,SAAS,MAAM,UAAU,OAAO,OAAO,OAAO;CACzD,IAAI,OAAO,UAAU,MAAM,WAAW,OAAO,QAAQ,OAAO;CAC5D,IAAI,OAAO,UAAU,CAAC,MAAM,QAAQ,YAAY,CAAC,CAAC,SAAS,OAAO,MAAM,GAAG,OAAO;CAClF,IAAI,OAAO,SAAS,MAAM,YAAY,OAAO,OAAO,OAAO;CAC3D,OAAO;AACX;AAKA,IAAM,gBAAN,MAAoB;CAChB,SAA6B,CAAC;CAC9B;CACA,YAAoB;CACpB,4BAAoB,IAAI,IAAiB;CAEzC,YAAY,UAAU,KAAO;EACzB,KAAK,UAAU;CACnB;CAEA,KAAK,OAAmC;EACpC,MAAM,KAAK,OAAO,EAAE,KAAK;EACzB,MAAM,SAAmB;GAAE,GAAG;GAC1B;EAAG;EACP,KAAK,OAAO,KAAK,MAAM;EACvB,IAAI,KAAK,OAAO,SAAS,KAAK,SAC1B,KAAK,OAAO,MAAM;EAKtB,KAAK,MAAM,YAAY,KAAK,WACxB,IAAI;GACA,SAAS,MAAM;EACnB,QAAQ,CAER;CAER;;;;;;;;;CAUA,UAAU,UAAmC;EACzC,KAAK,UAAU,IAAI,QAAQ;EAC3B,aAAa;GACT,KAAK,UAAU,OAAO,QAAQ;EAClC;CACJ;CAEA,MAAM,SAGqC;EACvC,MAAM,SAAS,gBAAgB,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AAO,SAAS;MAChB,UAAU,gBAAgB;KAC9B,CAAC;IACL,CAAC;IAED,IAAI,SAAS;IACb,OAAO,CAAC,OAAO,WAAW,CAAC,OAAO,QAAQ;KACtC,MAAM,OAAO,MAAM,OAAO;KAC1B,IAAI,OAAO,WAAW,OAAO,QAAQ;KAErC,IAAI,QAAQ,WAAW,GAAG;MACtB,UAAU;MACV,IAAI,UAAU,aAAa;OACvB,MAAM,OAAO,MAAM,YAAY;OAC/B,SAAS;MACb;MACA;KACJ;KAEA,MAAM,UAAU;KAChB,MAAM,OAAO;KACb,UAAU,CAAC;KACX,UAAU;KACV,SAAS;KACT,MAAM,OAAO,SAAS;MAClB,OAAO;MACP,MAAM,KAAK,UAAU,OAAO,IAAI;OAAE;OAC9B,SAAS;MAAK,IAAI,EAAE,QAAQ,CAAC;KACrC,CAAC;IACL;GACJ,UAAU;IACN,YAAY;IACZ,EAAE,IAAI,IAAI,OAAO,oBAAoB,SAAS,iBAAiB;IAC/D,aAAa,oBAAoB,SAAS,iBAAiB;GAC/D;EACJ,CAAC;CACL,CAAC;CAED,OAAO;AACX;AAEe,iBAAiB"}
|
|
1
|
+
{"version":3,"file":"logs-routes-D2NGM5Pa.js","names":[],"sources":["../src/api/logs-routes.ts"],"sourcesContent":["import { Hono } from \"hono\";\nimport type { MiddlewareHandler } from \"hono\";\nimport { streamSSE } from \"hono/streaming\";\nimport type { HonoEnv } from \"./types\";\nimport { ApiError, errorHandler } from \"./errors\";\nimport { addLogSink } from \"../utils/logger\";\n\nexport interface LogEntry {\n id: string;\n timestamp: string;\n level: \"debug\" | \"info\" | \"warn\" | \"error\";\n source: \"api\" | \"auth\" | \"storage\" | \"realtime\" | \"system\";\n message: string;\n metadata?: Record<string, unknown>;\n}\n\n/** What a caller can narrow the log by, in either direction (query or stream). */\nexport interface LogFilterOptions {\n level?: string;\n source?: string;\n search?: string;\n since?: string;\n}\n\n/**\n * A filter with the search term already lowercased.\n *\n * The distinction matters on the stream path: `query()` lowercases once and then\n * scans, but a subscriber tests one entry at a time and would otherwise redo the\n * same `toLowerCase()` on every request the server handles.\n */\ntype NormalizedFilter = LogFilterOptions;\n\nfunction normalizeFilter(options: LogFilterOptions): NormalizedFilter {\n return { ...options,\n search: options.search?.toLowerCase() };\n}\n\n/**\n * Whether one entry belongs in a filtered view.\n *\n * Shared by the query and the stream on purpose: two copies of this would drift,\n * and the failure that produces is invisible — a tail that quietly shows a\n * different set of lines than the snapshot it started from.\n */\nfunction matchesFilter(entry: LogEntry, filter: NormalizedFilter): boolean {\n if (filter.level && entry.level !== filter.level) return false;\n if (filter.source && entry.source !== filter.source) return false;\n if (filter.search && !entry.message.toLowerCase().includes(filter.search)) return false;\n if (filter.since && entry.timestamp < filter.since) return false;\n return true;\n}\n\n/** Notified for every entry pushed, in push order. */\nexport type LogListener = (entry: LogEntry) => void;\n\nclass LogRingBuffer {\n private buffer: LogEntry[] = [];\n private maxSize: number;\n private idCounter = 0;\n private listeners = new Set<LogListener>();\n\n constructor(maxSize = 10000) {\n this.maxSize = maxSize;\n }\n\n push(entry: Omit<LogEntry, \"id\">): void {\n const id = `log_${++this.idCounter}`;\n const stored: LogEntry = { ...entry,\n id };\n this.buffer.push(stored);\n if (this.buffer.length > this.maxSize) {\n this.buffer.shift();\n }\n // This runs on the request hot path, so a listener must never be able to\n // take the request down with it: a tail that throws loses its own tail,\n // not the response the log line was describing.\n for (const listener of this.listeners) {\n try {\n listener(stored);\n } catch {\n /* a broken tail is not the request's problem */\n }\n }\n }\n\n /**\n * Follow the buffer. Returns the unsubscribe — call it, always: a listener\n * left behind holds its whole closure, and on this class that closure is a\n * pending-entry array.\n *\n * A listener must not log. It is called from inside `push`, so anything that\n * reaches `addLog` from here recurses until the stack gives out.\n */\n subscribe(listener: LogListener): () => void {\n this.listeners.add(listener);\n return () => {\n this.listeners.delete(listener);\n };\n }\n\n query(options: LogFilterOptions & {\n limit?: number;\n offset?: number;\n }): { entries: LogEntry[]; total: number } {\n const filter = normalizeFilter(options);\n const filtered = this.buffer.filter(e => matchesFilter(e, filter));\n\n // Newest first\n const sorted = [...filtered].reverse();\n const total = sorted.length;\n const limit = options.limit || 100;\n const offset = options.offset || 0;\n\n return {\n entries: sorted.slice(offset, offset + limit),\n total\n };\n }\n\n getLatest(count = 50): LogEntry[] {\n return this.buffer.slice(-count).reverse();\n }\n}\n\n// Global singleton\nexport const logBuffer = new LogRingBuffer();\n\n/**\n * Which process this log belongs to.\n *\n * The ring is this process's memory, and nothing gathers the rings of a\n * deployment's other replicas or of its split runtime roles. So a reader is\n * told whose log it is: `HOSTNAME` is the pod name on Kubernetes and the\n * container id under Docker; the pid tells two local processes apart.\n */\nexport function logInstanceName(): string {\n return process.env.HOSTNAME?.trim() || `pid-${process.pid}`;\n}\n\n/** Add a log entry */\nexport function addLog(\n level: LogEntry[\"level\"],\n source: LogEntry[\"source\"],\n message: string,\n metadata?: Record<string, unknown>\n): void {\n logBuffer.push({\n timestamp: new Date().toISOString(),\n level,\n source,\n message,\n metadata\n });\n}\n\nexport interface LogMiddlewareOptions {\n /**\n * Paths this sink ignores, matched exactly against `c.req.path`.\n *\n * For requests whose only reason to exist is to read the log. Recording\n * those makes the reader the loudest thing in its own output, and on a quiet\n * server it is also the thing evicting real entries out of the ring.\n */\n ignorePaths?: string[];\n /**\n * Where the API is mounted (`/api`). The segment after it names the\n * subsystem a request is filed under — see {@link sourceForRequestPath}.\n * Unset, the first segment of the path is read.\n */\n basePath?: string;\n}\n\n/** The first path segment under the base path that files a request under a source other than `api`. */\nconst SOURCE_BY_SEGMENT: Readonly<Record<string, LogEntry[\"source\"]>> = {\n auth: \"auth\",\n oauth: \"auth\",\n storage: \"storage\"\n};\n\n/**\n * The `source` of a request entry: the subsystem its path addresses.\n *\n * `/api/auth/*` and the MCP authorization server under `/api/oauth/*` are\n * `auth`, `/api/storage/*` is `storage`, and everything else is `api`. The\n * Logs Explorer's Source filter matches this field, so a sign-in filed as\n * `api` was invisible under Auth. The realtime socket is not an HTTP request\n * and is not recorded here.\n *\n * Exported for its test.\n */\nexport function sourceForRequestPath(path: string, basePath = \"\"): LogEntry[\"source\"] {\n const base = basePath.replace(/\\/+$/, \"\");\n if (base && path !== base && !path.startsWith(`${base}/`)) return \"api\";\n const segment = path.slice(base.length).split(\"/\").find(part => part !== \"\")?.toLowerCase();\n return (segment !== undefined && Object.prototype.hasOwnProperty.call(SOURCE_BY_SEGMENT, segment))\n ? SOURCE_BY_SEGMENT[segment]\n : \"api\";\n}\n\n/** Hono middleware to log API requests */\nexport function logMiddleware(options: LogMiddlewareOptions = {}): MiddlewareHandler<HonoEnv> {\n const ignored = new Set(options.ignorePaths ?? []);\n return async (c, next) => {\n const start = Date.now();\n await next();\n if (ignored.has(c.req.path)) return;\n const duration = Date.now() - start;\n const reqId = c.get(\"requestId\");\n // Every request used to be recorded at `info`, whatever it answered, so\n // the Logs Explorer's level filter could not find a single failure: a\n // 500 sat at the same level as the 200 above it, in a wall of them.\n const status = c.res.status;\n const level: LogEntry[\"level\"] = status >= 500 ? \"error\" : status >= 400 ? \"warn\" : \"info\";\n // What the error handler answered, so the failure is on the entry\n // rather than in a stdout line the panel cannot see. See\n // `HonoEnv.Variables.errorSummary`.\n const failure = c.get(\"errorSummary\");\n addLog(\n level,\n sourceForRequestPath(c.req.path, options.basePath),\n `${c.req.method} ${c.req.path} ${status} ${duration}ms`\n + (failure ? ` — ${failure.code}: ${failure.message}` : \"\"),\n {\n method: c.req.method,\n path: c.req.path,\n status,\n duration,\n ...(reqId && { requestId: reqId }),\n ...(c.get(\"collection\") && { collection: c.get(\"collection\") }),\n ...(failure && { errorCode: failure.code, errorMessage: failure.message })\n }\n );\n };\n}\n\n/**\n * Prefixes the server writes at the head of a log message, and the `source`\n * each one belongs to.\n *\n * The ring's `source` is a closed set the Studio filters on, and the messages\n * carry their origin as a bracketed prefix — `[API]`, `[Auth]`, `[functions]`,\n * `[schema]`. Matching them is what makes a teed line filterable beside the\n * request entries rather than a heap under \"system\".\n */\nconst SOURCE_BY_PREFIX: Array<[RegExp, LogEntry[\"source\"]]> = [\n [/^\\[?(api|rest)\\b/i, \"api\"],\n [/^\\[?auth\\b/i, \"auth\"],\n [/^\\[?(storage|s3|gcs)\\b/i, \"storage\"],\n [/^\\[?(realtime|ws|websocket|cdc)\\b/i, \"realtime\"]\n];\n\n/**\n * The `source` for a teed line, from whatever prefix it carries.\n *\n * Exported for its test: the mapping is a string match on wording somebody else\n * writes, which is the shape of check that stops working without failing.\n */\nexport function sourceForMessage(message: string): LogEntry[\"source\"] {\n // The level emoji comes first on several call sites (`⚠️ [API] …`), so the\n // prefix is whatever is inside the first bracket, wherever that is.\n const bracketed = message.match(/\\[([^\\]]{1,32})\\]/);\n const candidate = bracketed?.[1] ?? message;\n for (const [pattern, source] of SOURCE_BY_PREFIX) {\n if (pattern.test(candidate)) return source;\n }\n return \"system\";\n}\n\n/**\n * Feed the Logs Explorer everything the server says at warn and above.\n *\n * The ring used to be filled by `logMiddleware` alone, so the panel showed a\n * wall of `GET /api/data/posts 200 4ms` and not one of the errors, warnings or\n * boot diagnoses being written to stdout at the same moment. A function that\n * threw was the sharpest case: the request entry said `500` and the reason\n * existed only in a terminal the person looking at the panel does not have.\n *\n * Warn and above, deliberately. `info` is where the steady-state chatter lives,\n * and a 10,000-entry ring filled with it evicts the lines somebody opened the\n * panel to find.\n *\n * Idempotent: called from `createLogsRoutes`, which a split deployment may\n * reach more than once.\n */\nlet detachLoggerTee: (() => void) | undefined;\nexport function teeLoggerIntoLogBuffer(): () => void {\n if (detachLoggerTee) return detachLoggerTee;\n const detach = addLogSink((level, message, data) => {\n if (level !== \"warn\" && level !== \"error\") return;\n // `requestLogger` writes this one to stdout for every request, and\n // `logMiddleware` has already recorded the same request here with the\n // fields this panel renders. Teeing it too would double every failure.\n if (message === \"request\") return;\n addLog(level, sourceForMessage(message), message, Object.keys(data).length > 0 ? data : undefined);\n });\n detachLoggerTee = () => {\n detach();\n detachLoggerTee = undefined;\n };\n return detachLoggerTee;\n}\n\n/**\n * How long entries accumulate before a batch goes out.\n *\n * Not zero, and that is the point. A busy server logs faster than a browser can\n * render, and one SSE frame per line would hand the client a re-render per\n * request served — worse than the 3s poll this replaces, precisely when the logs\n * are worth watching. Coalescing keeps the frame rate bounded by the window\n * rather than by traffic, and 250ms still reads as \"live\" to a person.\n */\nconst STREAM_FLUSH_MS = 250;\n\n/**\n * Idle gap after which the stream sends a comment line.\n *\n * A silent SSE connection is indistinguishable from a dead one to everything in\n * between — proxies, load balancers and laptop NICs all reap idle sockets, and a\n * server with nothing to say is the normal state here.\n */\nconst STREAM_HEARTBEAT_MS = 25_000;\n\n/**\n * Entries a single connection will hold between flushes.\n *\n * This is a *rate* ceiling, not just a memory bound, and that is easy to get\n * wrong: nothing drains `pending` between flushes, so the most a connection can\n * carry losslessly is `maxPending` per `flushMs` — here 2000 per 250ms, or 8000\n * entries a second. Above that the oldest pending entries go and the client is\n * told how many, whatever speed it is reading at.\n *\n * It was 500, which put that ceiling at 2000/s. A healthy reader on a loopback\n * socket lost 85% of a 20k burst to it — the cap fired on the server's own\n * coalescing window rather than on any slowness at the client, which is a drop\n * notice that says nothing true about why. 8000/s is past what one Node process\n * serves, so reaching it now means genuinely more log than a person can be shown.\n *\n * The memory this bounds is the copy a *stalled* reader causes: roughly 2000\n * entries, a few MB, per stuck connection.\n */\nconst STREAM_MAX_PENDING = 2000;\n\n/**\n * The largest window any of these routes will hand back.\n *\n * The ring buffer holds 10,000 entries, so asking for more than all of it is a\n * mistake worth naming rather than silently clamping — and a clamped answer is\n * indistinguishable from \"that is all there is\".\n */\nconst LOG_WINDOW_MAX = 10_000;\n\n/**\n * The stream's timings, injectable only so they can be tested.\n *\n * The defaults above are the contract and nothing in production passes this. A\n * heartbeat is a 25-second wait to observe, and a suite that cannot observe it is\n * a suite where the keepalive can rot — which surfaces as \"the tail dies after a\n * few minutes behind the load balancer\", months later, on someone else's cluster.\n */\nexport interface LogStreamTiming {\n flushMs?: number;\n heartbeatMs?: number;\n maxPending?: number;\n}\n\n/** What ends the stream from the server's side. */\nexport interface LogsRoutesOptions {\n /**\n * Aborted when the backend shuts down. Every open stream ends then, rather\n * than when its client happens to leave: `server.close()` waits on every\n * open connection, so one Studio → Logs tab held a SIGTERM for the whole\n * force timeout — longer than Cloud Run's or `docker stop`'s grace period,\n * which killed the process before the pool was closed.\n */\n closeSignal?: AbortSignal;\n}\n\nexport function createLogsRoutes(timing: LogStreamTiming = {}, options: LogsRoutesOptions = {}): Hono<HonoEnv> {\n // Here rather than at module load: the ring exists whether or not anything\n // reads it, but there is no reason to fill it on a process that serves no\n // logs surface. Idempotent, so the repeated calls a split deployment makes\n // do not stack up sinks.\n teeLoggerIntoLogBuffer();\n\n const flushMs = timing.flushMs ?? STREAM_FLUSH_MS;\n const heartbeatMs = timing.heartbeatMs ?? STREAM_HEARTBEAT_MS;\n const maxPending = timing.maxPending ?? STREAM_MAX_PENDING;\n\n const app = new Hono<HonoEnv>();\n // Its own, like every other router here: nothing registers one on the host\n // app — not `boot.ts`, not the scaffolded backend, not the eject template —\n // so a router that throws without this answers Hono's default 500 in plain\n // text, outside the `{ error: { code, message } }` envelope the rest of the\n // API keeps to.\n app.onError(errorHandler);\n\n /**\n * A window into the ring buffer, or a 400 saying why not.\n *\n * `parseInt` was the whole of it before, and every malformed value failed\n * differently and silently: `?limit=abc` fell through to the default,\n * `?limit=-5` sliced an empty window and answered 200 with no entries, and\n * `?count=abc` made `slice(-NaN)` return the *entire* buffer. Three ways to\n * be wrong, none of them visible to the caller. The data plane refuses the\n * same input with a 400 — see `resolveListLimitParam`.\n *\n * `min` is 1 for a size and 0 for an offset: `?offset=0` is the first page,\n * which is where every pager starts, and it was a 400.\n */\n const window = (raw: string | undefined, what: string, max: number, min = 1): number | undefined => {\n if (raw === undefined || raw.trim() === \"\") return undefined;\n const parsed = Number(raw.trim());\n if (!Number.isInteger(parsed) || parsed < min || parsed > max) {\n throw new ApiError(\n 400,\n \"INVALID_PARAM\",\n `Invalid \\`${what}\\`: ${raw}. Expected a whole number between ${min} and ${max}.`,\n undefined,\n true\n );\n }\n return parsed;\n };\n\n // GET /api/logs — Query logs\n app.get(\"/\", (c) => {\n const query = c.req.query();\n const result = logBuffer.query({\n level: query.level,\n source: query.source,\n search: query.search,\n limit: window(query.limit, \"limit\", LOG_WINDOW_MAX),\n offset: window(query.offset, \"offset\", Number.MAX_SAFE_INTEGER, 0),\n since: query.since\n });\n return c.json(result);\n });\n\n // GET /api/logs/latest — Get latest logs (for real-time)\n app.get(\"/latest\", (c) => {\n const count = window(c.req.query(\"count\"), \"count\", LOG_WINDOW_MAX) ?? 50;\n return c.json({ entries: logBuffer.getLatest(count) });\n });\n\n // GET /api/logs/stream — tail the buffer over SSE.\n //\n // The Logs Explorer used to poll this router every 3 seconds, which cost a\n // request per client per 3s to say \"nothing happened\" and still showed each\n // line up to 3s late. Here the buffer pushes instead, so an idle server is an\n // idle socket.\n //\n // Events:\n // snapshot {entries, total, instance}\n // the filtered window, oldest-first, at open,\n // and the process whose ring it is\n // append {entries, dropped} entries since the last frame, oldest-first\n // `: ping` comment, keepalive only\n //\n // Snapshot and appends come down the same connection deliberately. A client\n // that fetched its backlog separately would race the subscription — entries\n // logged between the two calls belong to neither — and closing that race from\n // the outside needs an id cursor and dedupe on every frame.\n app.get(\"/stream\", (c) => {\n const query = c.req.query();\n const filter = normalizeFilter({\n level: query.level,\n source: query.source,\n search: query.search\n });\n const limit = window(query.limit, \"limit\", LOG_WINDOW_MAX) ?? 200;\n\n // Reverse proxies buffer text responses by default, which turns a live\n // tail into nothing at all until the buffer fills. nginx (and the ingress\n // in front of the managed runtime) reads this header; everything else\n // ignores it. The rest of the SSE headers are set by `streamSSE`.\n c.header(\"X-Accel-Buffering\", \"no\");\n\n return streamSSE(c, async (stream) => {\n let pending: LogEntry[] = [];\n let dropped = 0;\n\n // Subscribe *before* reading the backlog, with nothing awaited\n // between the two. Both are synchronous, so the two halves meet\n // exactly: an entry logged after the query but before the\n // subscription would otherwise be in neither, and that gap is the one\n // thing this route exists to close.\n const unsubscribe = logBuffer.subscribe(entry => {\n if (!matchesFilter(entry, filter)) return;\n if (pending.length >= maxPending) {\n pending.shift();\n dropped++;\n }\n pending.push(entry);\n });\n const snapshot = logBuffer.query({ ...filter,\n limit });\n\n // A client that goes away has to end this handler, or the\n // subscription outlives the socket. `streamSSE` only wires the\n // request signal through on old Bun, so do it here and let\n // `stream.aborted` be the one condition the loop tests.\n //\n // The `aborted` check is not belt-and-braces. A listener added to an\n // already-aborted signal is never called, so a client that leaves\n // during the snapshot write — a fast navigation, or a reconnect storm\n // against a restarting server — would leave this handler with no way\n // to learn it had gone: a subscriber and a flush loop, per attempt,\n // for the life of the process.\n const abortOnDisconnect = () => {\n if (!stream.closed) stream.abort();\n };\n c.req.raw.signal.addEventListener(\"abort\", abortOnDisconnect, { once: true });\n if (c.req.raw.signal.aborted) abortOnDisconnect();\n // The server going away ends it the same way, for the same reason\n // the client going away does — and a stream opened after shutdown\n // began still sends its snapshot, then ends.\n const closeSignal = options.closeSignal;\n closeSignal?.addEventListener(\"abort\", abortOnDisconnect, { once: true });\n if (closeSignal?.aborted) abortOnDisconnect();\n\n try {\n await stream.writeSSE({\n event: \"snapshot\",\n // The view tails like a terminal; both frames are oldest-first\n // so the client only ever appends.\n data: JSON.stringify({\n entries: snapshot.entries.slice().reverse(),\n total: snapshot.total,\n instance: logInstanceName()\n })\n });\n\n let idleMs = 0;\n while (!stream.aborted && !stream.closed) {\n await stream.sleep(flushMs);\n if (stream.aborted || stream.closed) break;\n\n if (pending.length === 0) {\n idleMs += flushMs;\n if (idleMs >= heartbeatMs) {\n await stream.write(\": ping\\n\\n\");\n idleMs = 0;\n }\n continue;\n }\n\n const entries = pending;\n const lost = dropped;\n pending = [];\n dropped = 0;\n idleMs = 0;\n await stream.writeSSE({\n event: \"append\",\n data: JSON.stringify(lost > 0 ? { entries,\n dropped: lost } : { entries })\n });\n }\n } finally {\n unsubscribe();\n c.req.raw.signal.removeEventListener(\"abort\", abortOnDisconnect);\n closeSignal?.removeEventListener(\"abort\", abortOnDisconnect);\n }\n });\n });\n\n return app;\n}\n\nexport default createLogsRoutes();\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAiCA,SAAS,gBAAgB,SAA6C;CAClE,OAAO;EAAE,GAAG;EACR,QAAQ,QAAQ,QAAQ,YAAY;CAAE;AAC9C;;;;;;;;AASA,SAAS,cAAc,OAAiB,QAAmC;CACvE,IAAI,OAAO,SAAS,MAAM,UAAU,OAAO,OAAO,OAAO;CACzD,IAAI,OAAO,UAAU,MAAM,WAAW,OAAO,QAAQ,OAAO;CAC5D,IAAI,OAAO,UAAU,CAAC,MAAM,QAAQ,YAAY,CAAC,CAAC,SAAS,OAAO,MAAM,GAAG,OAAO;CAClF,IAAI,OAAO,SAAS,MAAM,YAAY,OAAO,OAAO,OAAO;CAC3D,OAAO;AACX;AAKA,IAAM,gBAAN,MAAoB;CAChB,SAA6B,CAAC;CAC9B;CACA,YAAoB;CACpB,4BAAoB,IAAI,IAAiB;CAEzC,YAAY,UAAU,KAAO;EACzB,KAAK,UAAU;CACnB;CAEA,KAAK,OAAmC;EACpC,MAAM,KAAK,OAAO,EAAE,KAAK;EACzB,MAAM,SAAmB;GAAE,GAAG;GAC1B;EAAG;EACP,KAAK,OAAO,KAAK,MAAM;EACvB,IAAI,KAAK,OAAO,SAAS,KAAK,SAC1B,KAAK,OAAO,MAAM;EAKtB,KAAK,MAAM,YAAY,KAAK,WACxB,IAAI;GACA,SAAS,MAAM;EACnB,QAAQ,CAER;CAER;;;;;;;;;CAUA,UAAU,UAAmC;EACzC,KAAK,UAAU,IAAI,QAAQ;EAC3B,aAAa;GACT,KAAK,UAAU,OAAO,QAAQ;EAClC;CACJ;CAEA,MAAM,SAGqC;EACvC,MAAM,SAAS,gBAAgB,OAAO;EAItC,MAAM,SAAS,CAAC,GAHC,KAAK,OAAO,QAAO,MAAK,cAAc,GAAG,MAAM,CAG7C,CAAQ,CAAC,CAAC,QAAQ;EACr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AQ;KACtC,MAAM,OAAO,MAAM,OAAO;KAC1B,IAAI,OAAO,WAAW,OAAO,QAAQ;KAErC,IAAI,QAAQ,WAAW,GAAG;MACtB,UAAU;MACV,IAAI,UAAU,aAAa;OACvB,MAAM,OAAO,MAAM,YAAY;OAC/B,SAAS;MACb;MACA;KACJ;KAEA,MAAM,UAAU;KAChB,MAAM,OAAO;KACb,UAAU,CAAC;KACX,UAAU;KACV,SAAS;KACT,MAAM,OAAO,SAAS;MAClB,OAAO;MACP,MAAM,KAAK,UAAU,OAAO,IAAI;OAAE;OAC9B,SAAS;MAAK,IAAI,EAAE,QAAQ,CAAC;KACrC,CAAC;IACL;GACJ,UAAU;IACN,YAAY;IACZ,EAAE,IAAI,IAAI,OAAO,oBAAoB,SAAS,iBAAiB;IAC/D,aAAa,oBAAoB,SAAS,iBAAiB;GAC/D;EACJ,CAAC;CACL,CAAC;CAED,OAAO;AACX;AAEe,iBAAiB"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type DataDriver } from "@rebasepro/types";
|
|
2
|
-
import { type MetricSeries, type SeriesPoint } from "./history-store.js";
|
|
2
|
+
import { type HistoryBucket, type MetricSeries, type SeriesPoint, type SurfaceTotals } from "./history-store.js";
|
|
3
3
|
export interface MetricsHistory {
|
|
4
4
|
/** Create the table and sweep what has aged out. */
|
|
5
5
|
ensure(): Promise<void>;
|
|
@@ -7,6 +7,15 @@ export interface MetricsHistory {
|
|
|
7
7
|
start(): () => void;
|
|
8
8
|
/** Read one series, for the route and for anything else that asks. */
|
|
9
9
|
read(series: MetricSeries, sinceMinutes: number): Promise<SeriesPoint[]>;
|
|
10
|
+
/** Read several series at once, bucketed — the `/metrics/history/batch` route. */
|
|
11
|
+
readBatch(series: MetricSeries[], minutes: number, bucket: HistoryBucket): Promise<Record<string, SeriesPoint[]>>;
|
|
12
|
+
/**
|
|
13
|
+
* Where the sampler reads request and error counts from: the request
|
|
14
|
+
* middleware's registry, which this half does not own. Until it is set the
|
|
15
|
+
* sampler records no request history — a backend booted without metrics
|
|
16
|
+
* middleware has no counts to record.
|
|
17
|
+
*/
|
|
18
|
+
countRequestsFrom(read: () => SurfaceTotals): void;
|
|
10
19
|
}
|
|
11
20
|
/**
|
|
12
21
|
* History for this deployment, or `undefined` when it cannot have any.
|
|
@@ -37,15 +37,48 @@ export declare const SAMPLE_INTERVAL_MS = 60000;
|
|
|
37
37
|
* one signal here that says whether the process is *healthy* rather than how
|
|
38
38
|
* much it is using, and it has no counter semantics to get wrong.
|
|
39
39
|
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
40
|
+
* Requests and errors are NOT the registry's cumulative counters, which reset
|
|
41
|
+
* on restart and are not monotonic when summed across replicas — a rolling
|
|
42
|
+
* deploy would draw a cliff that looks like traffic collapsing. They are
|
|
43
|
+
* per-minute deltas per surface, written by `recordRequestDeltas`; see
|
|
44
|
+
* {@link REQUEST_DELTA_SERIES}. Every name here has a writer: `sampleSelf` for
|
|
45
|
+
* the first three, `recordRequestDeltas` for the rest.
|
|
46
46
|
*/
|
|
47
|
-
export declare const METRIC_SERIES: readonly ["cpu_millicores", "memory_bytes", "event_loop_delay_ms"];
|
|
47
|
+
export declare const METRIC_SERIES: readonly ["cpu_millicores", "memory_bytes", "event_loop_delay_ms", "requests_data", "errors_data", "requests_auth", "errors_auth", "requests_storage", "errors_storage", "requests_functions", "errors_functions", "requests_app", "errors_app"];
|
|
48
48
|
export type MetricSeries = (typeof METRIC_SERIES)[number];
|
|
49
|
+
/**
|
|
50
|
+
* The surfaces whose requests are recorded over time.
|
|
51
|
+
*
|
|
52
|
+
* `admin`, `meta` and `other` are left out on purpose: they are the console and
|
|
53
|
+
* tooling reading the backend, and on a quiet project they are most of the
|
|
54
|
+
* traffic — a chart of them would be a chart of somebody looking at the chart.
|
|
55
|
+
*/
|
|
56
|
+
export declare const RECORDED_SURFACES: readonly ["data", "auth", "storage", "functions", "app"];
|
|
57
|
+
export type RecordedSurface = (typeof RECORDED_SURFACES)[number];
|
|
58
|
+
/**
|
|
59
|
+
* The delta series: "requests this instance handled in this minute".
|
|
60
|
+
*
|
|
61
|
+
* ## Why deltas and not the counters
|
|
62
|
+
*
|
|
63
|
+
* The registry's request counters are cumulative and reset when the process
|
|
64
|
+
* does, and a resetting counter summed across replicas is not monotonic: a
|
|
65
|
+
* rolling deploy draws a cliff that looks like traffic collapsing. A delta has
|
|
66
|
+
* none of that. It never goes backwards, a restart is simply a new process
|
|
67
|
+
* whose first delta is its own count since boot, and the sum of deltas across
|
|
68
|
+
* replicas is the deployment's true figure for the minute.
|
|
69
|
+
*
|
|
70
|
+
* A delta of zero is NOT written — a quiet project would otherwise write ten
|
|
71
|
+
* zero rows a minute per instance for nothing — so a read fills zeros back in,
|
|
72
|
+
* but only for minutes some instance was alive (see {@link readSeriesBatch}).
|
|
73
|
+
*/
|
|
74
|
+
export declare const REQUEST_DELTA_SERIES: readonly MetricSeries[];
|
|
75
|
+
/** Whether a series is a per-minute delta, zero-filled on read. */
|
|
76
|
+
export declare function isDeltaSeries(series: MetricSeries): boolean;
|
|
77
|
+
/**
|
|
78
|
+
* The series every live instance writes every tick, and so the one a read uses
|
|
79
|
+
* to tell "running and quiet" (a zero) from "not running" (a gap).
|
|
80
|
+
*/
|
|
81
|
+
export declare const LIVENESS_SERIES: MetricSeries;
|
|
49
82
|
export interface MetricSample {
|
|
50
83
|
at: Date;
|
|
51
84
|
series: MetricSeries;
|
|
@@ -108,6 +141,8 @@ eventLoopDelayMs?: number): {
|
|
|
108
141
|
* fortnight of them is well under a million.
|
|
109
142
|
*/
|
|
110
143
|
export declare function recordSamples(exec: Exec, samples: Omit<MetricSample, "at">[], instance: string, at?: Date): Promise<void>;
|
|
144
|
+
/** How a series combines across replicas. Exported for the route's tests. */
|
|
145
|
+
export declare function combineOf(series: MetricSeries): "sum" | "avg";
|
|
111
146
|
export interface SeriesPoint {
|
|
112
147
|
at: string;
|
|
113
148
|
/** The deployment's figure, combined per `COMBINE`. */
|
|
@@ -129,3 +164,83 @@ export interface SeriesPoint {
|
|
|
129
164
|
* a line chart alone cannot tell them apart.
|
|
130
165
|
*/
|
|
131
166
|
export declare function readSeries(exec: Exec, series: MetricSeries, sinceMinutes: number): Promise<SeriesPoint[]>;
|
|
167
|
+
/** Cumulative requests and errors per recorded surface, as the registry holds them. */
|
|
168
|
+
export type SurfaceTotals = Partial<Record<RecordedSurface, {
|
|
169
|
+
requests: number;
|
|
170
|
+
errors: number;
|
|
171
|
+
}>>;
|
|
172
|
+
/** The previous cumulative count per delta series — the recorder's cursor. */
|
|
173
|
+
export type DeltaCursor = Partial<Record<MetricSeries, number>>;
|
|
174
|
+
/**
|
|
175
|
+
* Turn the registry's cumulative counts into this minute's deltas.
|
|
176
|
+
*
|
|
177
|
+
* Pure: the cursor is passed in and the caller decides when to advance it. A
|
|
178
|
+
* series with no previous reading — the first tick after boot — contributes its
|
|
179
|
+
* whole count since boot. A count LOWER than the cursor can only mean the
|
|
180
|
+
* counter restarted, and the honest delta is then the new count itself, never a
|
|
181
|
+
* negative. Zero deltas are left out: nothing is written for them.
|
|
182
|
+
*/
|
|
183
|
+
export declare function requestDeltas(totals: SurfaceTotals, cursor: DeltaCursor): {
|
|
184
|
+
series: MetricSeries;
|
|
185
|
+
value: number;
|
|
186
|
+
cumulative: number;
|
|
187
|
+
}[];
|
|
188
|
+
/**
|
|
189
|
+
* Write this tick's request deltas and return the advanced cursor.
|
|
190
|
+
*
|
|
191
|
+
* Each series' cursor moves only when ITS row was written. A failed write
|
|
192
|
+
* leaves it where it was, so the next tick's delta covers both minutes: nothing
|
|
193
|
+
* is lost and nothing is counted twice. Per series rather than per tick,
|
|
194
|
+
* because the rows are separate statements and a failure halfway through must
|
|
195
|
+
* not re-count the half that landed.
|
|
196
|
+
*
|
|
197
|
+
* The upsert ADDS rather than replaces. Two writes for one minute from one
|
|
198
|
+
* instance — two ticks drifting into the same minute, or a container restart
|
|
199
|
+
* that keeps its hostname — are both real traffic, and replacing would drop
|
|
200
|
+
* the first.
|
|
201
|
+
*/
|
|
202
|
+
export declare function recordRequestDeltas(exec: Exec, totals: SurfaceTotals, cursor: DeltaCursor, instance: string, at?: Date): Promise<{
|
|
203
|
+
cursor: DeltaCursor;
|
|
204
|
+
failed: unknown[];
|
|
205
|
+
}>;
|
|
206
|
+
/** The bucket sizes the batch read accepts, in minutes. */
|
|
207
|
+
export declare const HISTORY_BUCKETS: readonly [1, 5, 30, 180];
|
|
208
|
+
export type HistoryBucket = (typeof HISTORY_BUCKETS)[number];
|
|
209
|
+
/** The widest window a read may ask for, in minutes: the retention. */
|
|
210
|
+
export declare const MAX_HISTORY_MINUTES = 20160;
|
|
211
|
+
/** How many series one batch read may name. */
|
|
212
|
+
export declare const MAX_BATCH_SERIES = 16;
|
|
213
|
+
/**
|
|
214
|
+
* Read several series over a window, bucketed, oldest first.
|
|
215
|
+
*
|
|
216
|
+
* ## The window
|
|
217
|
+
*
|
|
218
|
+
* Buckets are aligned to the epoch and the window ends at the start of the
|
|
219
|
+
* CURRENT bucket, which is excluded for the same reason {@link readSeries}
|
|
220
|
+
* excludes the current minute: it is still being written. The window then
|
|
221
|
+
* reaches back `minutes`, so a 60-minute read at one-minute buckets is at most
|
|
222
|
+
* sixty complete points, and a day at thirty is at most forty-eight.
|
|
223
|
+
*
|
|
224
|
+
* ## Gaps and zeros
|
|
225
|
+
*
|
|
226
|
+
* A delta series stores no zeros, so its read fills them in — but only for a
|
|
227
|
+
* minute in which some instance was alive, which is a minute with a
|
|
228
|
+
* {@link LIVENESS_SERIES} row. A minute nobody was running is absent: a gap on
|
|
229
|
+
* a chart, not a zero, because "the app was down" and "the app was quiet" are
|
|
230
|
+
* different answers. `instances` on a delta point is the number of live
|
|
231
|
+
* instances.
|
|
232
|
+
*
|
|
233
|
+
* ## Within a bucket
|
|
234
|
+
*
|
|
235
|
+
* - a delta series is summed over its minutes: requests in the bucket;
|
|
236
|
+
* - an `avg` series (event-loop delay) is the mean of its per-minute combined
|
|
237
|
+
* values;
|
|
238
|
+
* - a `sum` gauge (CPU, memory) is ALSO the mean of its per-minute combined
|
|
239
|
+
* values. Summing thirty minutes of memory would report thirty times the
|
|
240
|
+
* memory; a gauge is a level, not an amount that accumulates.
|
|
241
|
+
*
|
|
242
|
+
* `instances` is the maximum over the bucket. A bucket with no live minute is
|
|
243
|
+
* absent. Every requested series is a key in the result, an empty array when
|
|
244
|
+
* nothing was recorded in the window.
|
|
245
|
+
*/
|
|
246
|
+
export declare function readSeriesBatch(exec: Exec, series: readonly MetricSeries[], minutes: number, bucket: HistoryBucket): Promise<Record<string, SeriesPoint[]>>;
|
package/dist/metrics/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Hono } from "hono";
|
|
2
2
|
import type { MiddlewareHandler } from "hono";
|
|
3
|
-
import { type MetricSeries, type SeriesPoint } from "./history-store.js";
|
|
3
|
+
import { type HistoryBucket, type MetricSeries, type SeriesPoint, type SurfaceTotals } from "./history-store.js";
|
|
4
4
|
import type { HonoEnv } from "../api/types.js";
|
|
5
5
|
/**
|
|
6
6
|
* Runtime metrics, in Prometheus text format.
|
|
@@ -31,6 +31,17 @@ export type MetricSurface = "data" | "auth" | "storage" | "functions" | "admin"
|
|
|
31
31
|
| "other";
|
|
32
32
|
/** Whether a path is the platform looking in, rather than a request served. */
|
|
33
33
|
export declare function isUncountedPath(pathname: string): boolean;
|
|
34
|
+
/**
|
|
35
|
+
* Whether a request's `status` label counts as an error.
|
|
36
|
+
*
|
|
37
|
+
* A 5xx and nothing else: the server failed. A 4xx is the caller's mistake and
|
|
38
|
+
* a working API answers plenty of them. This is THE definition — the stored
|
|
39
|
+
* per-minute error series use it, and the control plane's rollup of
|
|
40
|
+
* `rebase_requests_total` (`metrics-rollup.ts` in the cloud console) applies
|
|
41
|
+
* the same rule to the same label, which a test on each side pins to one table
|
|
42
|
+
* of statuses.
|
|
43
|
+
*/
|
|
44
|
+
export declare function isErrorStatus(status: string | undefined): boolean;
|
|
34
45
|
/**
|
|
35
46
|
* How long a query waited for a pooled database connection, in milliseconds —
|
|
36
47
|
* opening one included, when the pool had to. Labelled by data source. See
|
|
@@ -63,6 +74,12 @@ export declare class MetricsRegistry {
|
|
|
63
74
|
private static namedKey;
|
|
64
75
|
private static splitNamedKey;
|
|
65
76
|
recordRequest(labels: Record<string, string>, durationMs: number): void;
|
|
77
|
+
/**
|
|
78
|
+
* Requests and errors since this process started, summed per recorded
|
|
79
|
+
* surface. Read by the history sampler to compute per-minute deltas; the
|
|
80
|
+
* surfaces it does not record (`admin`, `meta`, `other`) are left out.
|
|
81
|
+
*/
|
|
82
|
+
requestTotalsBySurface(): SurfaceTotals;
|
|
66
83
|
incrementCounter(name: string, labels?: Record<string, string>, by?: number): void;
|
|
67
84
|
/** Set a counter whose running total is kept elsewhere. See {@link onRender}. */
|
|
68
85
|
setCounter(name: string, value: number, labels?: Record<string, string>): void;
|
|
@@ -129,4 +146,9 @@ export declare function createMetricsRoutes(registry: MetricsRegistry, token?: s
|
|
|
129
146
|
* route then answers 501 rather than pretending an empty chart is a quiet
|
|
130
147
|
* period.
|
|
131
148
|
*/
|
|
132
|
-
history?: (series: MetricSeries, sinceMinutes: number) => Promise<SeriesPoint[]
|
|
149
|
+
history?: (series: MetricSeries, sinceMinutes: number) => Promise<SeriesPoint[]>,
|
|
150
|
+
/**
|
|
151
|
+
* Reads several stored series at once, bucketed. Absent under the same
|
|
152
|
+
* conditions as `history`, with the same 501.
|
|
153
|
+
*/
|
|
154
|
+
historyBatch?: (series: MetricSeries[], minutes: number, bucket: HistoryBucket) => Promise<Record<string, SeriesPoint[]>>): Hono<HonoEnv>;
|
|
@@ -2,10 +2,10 @@ import { createRequire as __rebaseCreateRequire } from "module";
|
|
|
2
2
|
import __rebaseProcess from "process";
|
|
3
3
|
globalThis.process ??= __rebaseProcess;
|
|
4
4
|
__rebaseCreateRequire(import.meta.url);
|
|
5
|
-
import { An as
|
|
5
|
+
import { An as parseCaseFoldedFieldPath, Fn as RebaseClientError, In as isUnsupported, Nn as toCanonicalOp, Pn as RebaseApiError, U as hasFieldOperation, jn as parseRelationAggregateSort } from "./src--EBfCtYm.js";
|
|
6
6
|
import { n as splitSearchTerms } from "./strings.es-k0kjzQBh.js";
|
|
7
|
-
import { a as byIdRead, d as etagOf, f as withETag, i as buildQueryString, n as MemoryOfflineStore, o as createMutationId, p as unref, r as SDKQueryBuilder, t as IndexedDBOfflineStore, u as invalidBulkBody } from "./offline-store-DtmBwmKE-
|
|
8
|
-
import { _ as normalizeOrderBy, c as collectAllPages, l as paginateFind, u as resolveFindWindow } from "./filter-dialect-
|
|
7
|
+
import { a as byIdRead, d as etagOf, f as withETag, i as buildQueryString, n as MemoryOfflineStore, o as createMutationId, p as unref, r as SDKQueryBuilder, t as IndexedDBOfflineStore, u as invalidBulkBody } from "./offline-store-DtmBwmKE-CEakGPWn.js";
|
|
8
|
+
import { _ as normalizeOrderBy, c as collectAllPages, l as paginateFind, u as resolveFindWindow } from "./filter-dialect-DgWkoXt7.js";
|
|
9
9
|
//#region ../client/dist/offline-ET6xI1ZO.js
|
|
10
10
|
/**
|
|
11
11
|
* Rows and queued writes through the offline store.
|
|
@@ -3611,4 +3611,4 @@ var OfflineManager = class {
|
|
|
3611
3611
|
//#endregion
|
|
3612
3612
|
export { OfflineManager };
|
|
3613
3613
|
|
|
3614
|
-
//# sourceMappingURL=offline-ET6xI1ZO-
|
|
3614
|
+
//# sourceMappingURL=offline-ET6xI1ZO-Du75S8J1.js.map
|