kopia lustrzana https://github.com/transitive-bullshit/chatgpt-api
pull/715/head
rodzic
3efbed9d23
commit
c967d92255
|
@ -31,7 +31,7 @@
|
||||||
"dev": "tsup --watch",
|
"dev": "tsup --watch",
|
||||||
"clean": "del dist",
|
"clean": "del dist",
|
||||||
"test": "run-s test:*",
|
"test": "run-s test:*",
|
||||||
"test:lint": "eslint src",
|
"test:lint": "eslint .",
|
||||||
"test:typecheck": "tsc --noEmit",
|
"test:typecheck": "tsc --noEmit",
|
||||||
"test:unit": "vitest run"
|
"test:unit": "vitest run"
|
||||||
},
|
},
|
||||||
|
|
|
@ -57,11 +57,13 @@ export function pinoLevelToGcpSeverity(
|
||||||
export function getGcpLoggingTimestamp() {
|
export function getGcpLoggingTimestamp() {
|
||||||
const seconds = Date.now() / 1000
|
const seconds = Date.now() / 1000
|
||||||
const secondsRounded = Math.floor(seconds)
|
const secondsRounded = Math.floor(seconds)
|
||||||
|
|
||||||
// The following line is 2x as fast as seconds % 1000
|
// The following line is 2x as fast as seconds % 1000
|
||||||
// Uses Math.round, not Math.floor due to JS floating point...
|
// Uses Math.round, not Math.floor due to JS floating point...
|
||||||
// eg for a Date.now()=1713024754120
|
// eg for a Date.now()=1713024754120
|
||||||
// (seconds-secondsRounded)*1000 => 119.99988555908203
|
// (seconds-secondsRounded)*1000 => 119.99988555908203
|
||||||
const millis = Math.round((seconds - secondsRounded) * 1000)
|
const millis = Math.round((seconds - secondsRounded) * 1000)
|
||||||
|
|
||||||
return `,"timestamp":{"seconds":${secondsRounded},"nanos":${millis}000000}`
|
return `,"timestamp":{"seconds":${secondsRounded},"nanos":${millis}000000}`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -86,10 +88,12 @@ export function formatGcpLogObject(
|
||||||
entry['logging.googleapis.com/trace'] = entry.trace_id
|
entry['logging.googleapis.com/trace'] = entry.trace_id
|
||||||
delete entry.trace_id
|
delete entry.trace_id
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((entry.span_id as string | undefined)?.length) {
|
if ((entry.span_id as string | undefined)?.length) {
|
||||||
entry['logging.googleapis.com/spanId'] = entry.span_id
|
entry['logging.googleapis.com/spanId'] = entry.span_id
|
||||||
delete entry.span_id
|
delete entry.span_id
|
||||||
}
|
}
|
||||||
|
|
||||||
// Trace flags is a bit field even though there is one on defined bit,
|
// 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.
|
// so lets convert it to an int and test against a bitmask.
|
||||||
// @see https://www.w3.org/TR/trace-context/#trace-flags
|
// @see https://www.w3.org/TR/trace-context/#trace-flags
|
||||||
|
|
|
@ -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')
|
|
@ -4,8 +4,8 @@ import { createMiddleware } from 'hono/factory'
|
||||||
import { HTTPException } from 'hono/http-exception'
|
import { HTTPException } from 'hono/http-exception'
|
||||||
|
|
||||||
import type { AuthenticatedEnv } from '@/lib/types'
|
import type { AuthenticatedEnv } from '@/lib/types'
|
||||||
|
import { HttpError } from '@/lib/errors'
|
||||||
import { HttpError } from '../errors'
|
import { logger } from '@/lib/logger'
|
||||||
|
|
||||||
export const errorHandler = createMiddleware<AuthenticatedEnv>(
|
export const errorHandler = createMiddleware<AuthenticatedEnv>(
|
||||||
async function errorHandlerMiddleware(ctx, next) {
|
async function errorHandlerMiddleware(ctx, next) {
|
||||||
|
@ -28,7 +28,7 @@ export const errorHandler = createMiddleware<AuthenticatedEnv>(
|
||||||
}
|
}
|
||||||
|
|
||||||
if (status >= 500) {
|
if (status >= 500) {
|
||||||
console.error('http error', status, message)
|
logger.error({ err, status, message })
|
||||||
Sentry.captureException(err)
|
Sentry.captureException(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
export * from './access-logger'
|
||||||
export * from './authenticate'
|
export * from './authenticate'
|
||||||
export * from './error-handler'
|
export * from './error-handler'
|
||||||
export * from './me'
|
export * from './me'
|
||||||
|
|
|
@ -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)
|
||||||
|
}
|
|
@ -16,6 +16,7 @@ export const app = new Hono()
|
||||||
|
|
||||||
app.use(sentry())
|
app.use(sentry())
|
||||||
app.use(compress())
|
app.use(compress())
|
||||||
|
app.use(middleware.accessLogger)
|
||||||
app.use(middleware.responseTime)
|
app.use(middleware.responseTime)
|
||||||
app.use(middleware.errorHandler)
|
app.use(middleware.errorHandler)
|
||||||
app.use(cors())
|
app.use(cors())
|
||||||
|
|
Ładowanie…
Reference in New Issue