-
Notifications
You must be signed in to change notification settings - Fork 1
feat: replace LogTape with evlog for wide-event logging #142
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 |
|---|---|---|
| @@ -1,18 +1,7 @@ | ||
| import { buildLogger, LoggerCategory } from "@init/observability/logger" | ||
| import { createLogger } from "@init/observability/logger" | ||
| import { buildDrain } from "@init/observability/logger/drains" | ||
| import { singleton } from "@init/utils/singleton" | ||
|
|
||
| export const logger = singleton("logger:api", () => | ||
| buildLogger( | ||
| [ | ||
| LoggerCategory.DEFAULT, | ||
| LoggerCategory.EMAIL, | ||
| LoggerCategory.LOGTAPE, | ||
| LoggerCategory.HONO, | ||
| LoggerCategory.DRIZZLE_ORM, | ||
| LoggerCategory.INNGEST, | ||
| ], | ||
| { async: true } | ||
| ) | ||
| ) | ||
| export const drain = singleton("drain:api", () => buildDrain()) | ||
|
|
||
| export { LoggerCategory } from "@init/observability/logger" | ||
| export const logger = singleton("logger:api", () => createLogger({ drain, service: "api" })) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,22 +1,22 @@ | ||
| import type { Database } from "@init/db/client" | ||
| import type { KeyValue } from "@init/kv/client" | ||
| import type { LoggerVariables } from "@init/observability/logger/hono" | ||
| import type { DeepMerge } from "@init/utils/type" | ||
| import type { Files } from "files-sdk" | ||
| import type { Auth, Session } from "#shared/auth.ts" | ||
| import type { Locale } from "#shared/internationalization/runtime.js" | ||
| import type { logger } from "#shared/logger.ts" | ||
|
|
||
| type AppLogger = typeof logger | ||
|
|
||
| export type AppContext = { | ||
| Variables: { | ||
| auth: Auth | ||
| db: Database | ||
| files: Files | ||
| kv: KeyValue | ||
| language: Locale | ||
| logger: AppLogger | ||
| export type AppContext = DeepMerge< | ||
| LoggerVariables, | ||
| { | ||
| Variables: { | ||
| auth: Auth | ||
| db: Database | ||
| files: Files | ||
| kv: KeyValue | ||
| language: Locale | ||
| } | ||
| } | ||
| } | ||
| > | ||
|
|
||
| export type AuthenticatedAppContext = DeepMerge<AppContext, { Variables: { session: Session } }> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,9 @@ | ||
| import { buildLogger, LoggerCategory } from "@init/observability/logger" | ||
| import { hasWindow } from "@init/utils/env" | ||
| import { createLogger } from "@init/observability/logger" | ||
| import { singleton } from "@init/utils/singleton" | ||
|
|
||
| export const logger = singleton("logger:app", () => | ||
| buildLogger([LoggerCategory.DEFAULT], { | ||
| async: !hasWindow, | ||
| createLogger({ | ||
| isDevelopment: import.meta.env.DEV, | ||
| service: "app", | ||
| }) | ||
| ) |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,27 +1,48 @@ | ||||||||||||||||||||||||||
| import crypto from "node:crypto" | ||||||||||||||||||||||||||
| import { database } from "@init/db/client" | ||||||||||||||||||||||||||
| import { createRequestLogger } from "@init/observability/logger" | ||||||||||||||||||||||||||
| import { createCsrfMiddleware, createMiddleware } from "@tanstack/react-start" | ||||||||||||||||||||||||||
| import { logger } from "#shared/logger.ts" | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| export const withCsrf = createCsrfMiddleware({ | ||||||||||||||||||||||||||
| filter: (context) => context.handlerType === "serverFn", | ||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||
| * Opens one wide event per server request (SSR and server function calls) and emits it with the | ||||||||||||||||||||||||||
| * response status. Handlers and server functions can add context through `context.log`. | ||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||
| export const withWideEvent = createMiddleware({ type: "request" }).server( | ||||||||||||||||||||||||||
| async ({ request, pathname, next, handlerType, serverFnMeta }) => { | ||||||||||||||||||||||||||
| const log = createRequestLogger({ method: request.method, path: pathname }) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| log.set({ | ||||||||||||||||||||||||||
| handlerType, | ||||||||||||||||||||||||||
| ...(serverFnMeta | ||||||||||||||||||||||||||
| ? { serverFn: { filename: serverFnMeta.filename, name: serverFnMeta.name } } | ||||||||||||||||||||||||||
| : {}), | ||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||
| const result = await next({ context: { log } }) | ||||||||||||||||||||||||||
| log.set({ status: result.response.status }) | ||||||||||||||||||||||||||
| log.emit() | ||||||||||||||||||||||||||
|
Comment on lines
+27
to
+29
Contributor
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.
Deriving the level from the status closes the gap without touching the catch, which is still reachable on the SSR path.
Suggested change
|
||||||||||||||||||||||||||
| return result | ||||||||||||||||||||||||||
| } catch (error) { | ||||||||||||||||||||||||||
| log.error(error instanceof Error ? error : String(error)) | ||||||||||||||||||||||||||
| log.emit() | ||||||||||||||||||||||||||
| throw error | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| export const withRequestId = createMiddleware().server(({ next }) => | ||||||||||||||||||||||||||
| next({ context: { requestId: crypto.randomUUID() } }) | ||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| export const withLogger = createMiddleware() | ||||||||||||||||||||||||||
| .middleware([withRequestId]) | ||||||||||||||||||||||||||
| .server(({ next, context }) => | ||||||||||||||||||||||||||
| next({ | ||||||||||||||||||||||||||
| context: { | ||||||||||||||||||||||||||
| logger: logger.getChild("server-function").with({ | ||||||||||||||||||||||||||
| requestId: context.requestId, | ||||||||||||||||||||||||||
| }), | ||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||
| .server(({ next }) => next({ context: { logger } })) | ||||||||||||||||||||||||||
|
Contributor
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.
Technical details# Server functions log through the ambient logger, bypassing the wide event
## Affected sites
- `apps/app/src/shared/server/middleware.ts:44` — `withLogger` reduced to `next({ context: { logger } })`, handing over the process-wide singleton.
- `apps/app/src/shared/server/middleware.ts:38-40` — `withRequestId`'s `crypto.randomUUID()` is no longer consumed by anything now that `withLogger` dropped its `.with({ requestId })` call.
- `apps/app/src/shared/server/functions.ts:4` — `publicFunction` composes `[withLogger, withDatabase]`, so `context.logger` is what every server function actually receives.
- `apps/app/src/shared/server/middleware.ts:12-15` — the JSDoc says "Handlers and server functions can add context through `context.log`", but request-middleware context does not reach `beforeLoad`/`loader` (TanStack/router#6395) and this app defines no route `server.handlers`, so server functions are the only possible consumer — and they are wired to the other logger.
## Required outcome
- One logger is reachable from server functions, and writes to it land on the request's wide event.
- No middleware remains whose only output nothing reads.
## Suggested approach
Dropping `withLogger` and `withRequestId` and letting server functions read `context.log` from `withWideEvent` is the smallest change; `withLogger` could alternatively be kept as a compatibility alias that forwards `context.log`. Either way the JSDoc should say plainly that loaders and `beforeLoad` do not receive this context.
## Open questions for the human
- Is the ambient `logger` still wanted in server-function context for anything that should deliberately escape the request event? |
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| export const withDatabase = createMiddleware().server(({ next }) => | ||||||||||||||||||||||||||
| next({ context: { database: database() } }) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
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.
parseErrorreturns a plainError'smessageverbatim (dist/runtime/utils/parseError.mjs:if (error instanceof Error) return { message: error.message, … }), so every unhandled exception now echoes its internal text to the caller — Postgres errors carrying the failing statement, S3 SDK errors carrying bucket and endpoint, upstreamfetchfailures carrying internal hostnames. The oldc.text("Internal Server Error", 500)disclosed none of that.HTTPExceptions already short-circuit above, so the structured body only helps for errors that were never givenwhy/fixin the first place.Technical details
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.
Corrigido: streak que começou DEPOIS do último sucesso restampa
escalation_asked_at, então o sweep de retirada não cancela mais a pergunta que o refresh acabou de reescrever.🤖 Addressed by Claude Code