pull/715/head
Travis Fischer 2025-04-25 05:37:04 +07:00
rodzic 3efbed9d23
commit c967d92255
7 zmienionych plików z 25 dodań i 4 usunięć

Wyświetl plik

@ -31,7 +31,7 @@
"dev": "tsup --watch",
"clean": "del dist",
"test": "run-s test:*",
"test:lint": "eslint src",
"test:lint": "eslint .",
"test:typecheck": "tsc --noEmit",
"test:unit": "vitest run"
},

Wyświetl plik

@ -57,11 +57,13 @@ export function pinoLevelToGcpSeverity(
export function getGcpLoggingTimestamp() {
const seconds = Date.now() / 1000
const secondsRounded = Math.floor(seconds)
// The following line is 2x as fast as seconds % 1000
// Uses Math.round, not Math.floor due to JS floating point...
// eg for a Date.now()=1713024754120
// (seconds-secondsRounded)*1000 => 119.99988555908203
const millis = Math.round((seconds - secondsRounded) * 1000)
return `,"timestamp":{"seconds":${secondsRounded},"nanos":${millis}000000}`
}
@ -86,10 +88,12 @@ export function formatGcpLogObject(
entry['logging.googleapis.com/trace'] = entry.trace_id
delete entry.trace_id
}
if ((entry.span_id as string | undefined)?.length) {
entry['logging.googleapis.com/spanId'] = entry.span_id
delete entry.span_id
}
// Trace flags is a bit field even though there is one on defined bit,
// so lets convert it to an int and test against a bitmask.
// @see https://www.w3.org/TR/trace-context/#trace-flags

Wyświetl plik

@ -0,0 +1,7 @@
import { logger as honoLogger } from 'hono/logger'
import { logger } from '@/lib/logger'
import { unless } from './unless'
export const accessLogger = unless(honoLogger(logger.trace), '/v1/health')

Wyświetl plik

@ -4,8 +4,8 @@ import { createMiddleware } from 'hono/factory'
import { HTTPException } from 'hono/http-exception'
import type { AuthenticatedEnv } from '@/lib/types'
import { HttpError } from '../errors'
import { HttpError } from '@/lib/errors'
import { logger } from '@/lib/logger'
export const errorHandler = createMiddleware<AuthenticatedEnv>(
async function errorHandlerMiddleware(ctx, next) {
@ -28,7 +28,7 @@ export const errorHandler = createMiddleware<AuthenticatedEnv>(
}
if (status >= 500) {
console.error('http error', status, message)
logger.error({ err, status, message })
Sentry.captureException(err)
}

Wyświetl plik

@ -1,3 +1,4 @@
export * from './access-logger'
export * from './authenticate'
export * from './error-handler'
export * from './me'

Wyświetl plik

@ -0,0 +1,8 @@
import type { MiddlewareHandler } from 'hono'
export const unless =
(mw: MiddlewareHandler, ...excluded: string[]): MiddlewareHandler =>
async (c, next) => {
if (excluded.includes(c.req.path)) return next()
return mw(c, next)
}

Wyświetl plik

@ -16,6 +16,7 @@ export const app = new Hono()
app.use(sentry())
app.use(compress())
app.use(middleware.accessLogger)
app.use(middleware.responseTime)
app.use(middleware.errorHandler)
app.use(cors())