-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP feat(billing): implement context aware json logger
refs #247
- Loading branch information
1 parent
fbab992
commit bacc119
Showing
16 changed files
with
296 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,5 @@ POSTGRES_DB_URI=postgres://postgres:[email protected]:5432/cloudmos-users | |
RPC_NODE_ENDPOINT=https://rpc.sandbox-01.aksh.pw:443 | ||
TRIAL_DEPLOYMENT_ALLOWANCE_AMOUNT=100000 | ||
TRIAL_FEES_ALLOWANCE_AMOUNT=100000 | ||
TRIAL_ALLOWANCE_DENOM=uakt | ||
TRIAL_ALLOWANCE_DENOM=uakt | ||
LOG_LEVEL=info |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import dotenv from "dotenv"; | ||
import { z } from "zod"; | ||
|
||
dotenv.config({ path: ".env.local" }); | ||
dotenv.config(); | ||
|
||
const envSchema = z.object({ | ||
LOG_LEVEL: z.enum(["fatal", "error", "warn", "info", "debug", "trace"]).optional().default("info") | ||
}); | ||
|
||
export const env = envSchema.parse(process.env); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { env } from "./env"; | ||
|
||
export const config = { | ||
...env | ||
}; | ||
export type CoreConfig = typeof config; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { container } from "tsyringe"; | ||
|
||
import { config } from "@src/core/config"; | ||
|
||
export const CORE_CONFIG = "CORE_CONFIG"; | ||
|
||
container.register(CORE_CONFIG, { useValue: config }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from "./PostgresProvider"; | ||
export * from "./configProvider"; |
11 changes: 11 additions & 0 deletions
11
apps/api/src/core/services/contextual-logger/ContextualLogger.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Logger } from "@src/core/services/logger/Logger"; | ||
import { RequestStorage } from "@src/core/services/request-storage/RequestStorage"; | ||
|
||
export class ContextualLogger extends Logger { | ||
protected get logger() { | ||
const context = RequestStorage.getContext(); | ||
const requestId = context?.get("requestId"); | ||
|
||
return requestId ? super.logger.child({ requestId }) : super.logger; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
apps/api/src/core/services/log-http-requests/logHttpRequests.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { Context, Next } from "hono"; | ||
|
||
import { ContextualLogger } from "@src/core/services/contextual-logger/ContextualLogger"; | ||
|
||
export const logHttpRequests = () => async (c: Context, next: Next) => { | ||
const logger = new ContextualLogger({ context: "HTTP" }); | ||
const timer = performance.now(); | ||
logger.info({ | ||
method: c.req.method, | ||
url: c.req.url | ||
}); | ||
|
||
await next(); | ||
|
||
logger.info({ | ||
status: c.res.status, | ||
duration: `${(performance.now() - timer).toFixed(3)}ms` | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import pino, { Bindings } from "pino"; | ||
|
||
import { env } from "@src/core/config/env"; | ||
|
||
export class Logger { | ||
private readonly pino: pino.Logger; | ||
|
||
protected get logger() { | ||
return this.pino; | ||
} | ||
|
||
constructor(bindings?: Bindings) { | ||
this.pino = pino({ level: env.LOG_LEVEL }); | ||
|
||
if (bindings) { | ||
this.pino = this.logger.child(bindings); | ||
} | ||
} | ||
|
||
info(message: any) { | ||
message = this.toLoggableInput(message); | ||
return this.logger.info(message); | ||
} | ||
|
||
error(message: any) { | ||
this.logger.error(this.toLoggableInput(message)); | ||
} | ||
|
||
warn(message: any) { | ||
return this.logger.warn(this.toLoggableInput(message)); | ||
} | ||
|
||
debug(message: any) { | ||
return this.logger.debug(this.toLoggableInput(message)); | ||
} | ||
|
||
private toLoggableInput(message: any) { | ||
if (message instanceof Error) { | ||
return message.stack; | ||
} | ||
|
||
return message; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,25 @@ | ||
import { DefaultLogger } from "drizzle-orm/logger"; | ||
import { DefaultLogger, LogWriter } from "drizzle-orm/logger"; | ||
import { drizzle } from "drizzle-orm/node-postgres"; | ||
import { migrate } from "drizzle-orm/node-postgres/migrator"; | ||
import { Pool } from "pg"; | ||
|
||
import * as billingSchemas from "@src/billing/model-schemas"; | ||
import { ContextualLogger } from "@src/core/services/contextual-logger/ContextualLogger"; | ||
import { env } from "@src/utils/env"; | ||
|
||
const pool = new Pool({ | ||
connectionString: env.UserDatabaseCS | ||
}); | ||
export const pgDatabase = drizzle(pool, { logger: new DefaultLogger(), schema: billingSchemas }); | ||
|
||
class ContextualLogWriter implements LogWriter { | ||
logger = new ContextualLogger({ context: "POSTGRES" }); | ||
|
||
write(message: string) { | ||
this.logger.debug({ query: message.replace(/^Query: /, "") }); | ||
} | ||
} | ||
|
||
export const pgDatabase = drizzle(pool, { logger: new DefaultLogger({ writer: new ContextualLogWriter() }), schema: billingSchemas }); | ||
export const migrateDb = () => migrate(pgDatabase, { migrationsFolder: "./drizzle" }); | ||
|
||
export type ApiPgSchema = typeof billingSchemas; |
31 changes: 31 additions & 0 deletions
31
apps/api/src/core/services/request-storage/RequestStorage.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { Context, Next } from "hono"; | ||
import { AsyncLocalStorage } from "node:async_hooks"; | ||
import { v4 as uuid } from "uuid"; | ||
|
||
export class RequestStorage { | ||
static CONTEXT_KEY = "CONTEXT"; | ||
|
||
static storage = new AsyncLocalStorage<Map<string, Context>>(); | ||
|
||
static middleware() { | ||
return async (c: Context, next: Next) => { | ||
const requestId = c.req.header("X-Request-Id") || uuid(); | ||
c.set("requestId", requestId); | ||
|
||
await this.runWithContext(c, next); | ||
}; | ||
} | ||
|
||
static async runWithContext(context: Context, cb: () => Promise<void>) { | ||
return await new Promise((resolve, reject) => { | ||
this.storage.run(new Map(), () => { | ||
this.storage.getStore().set(this.CONTEXT_KEY, context); | ||
cb().then(resolve).catch(reject); | ||
}); | ||
}); | ||
} | ||
|
||
static getContext() { | ||
return this.storage.getStore()?.get(this.CONTEXT_KEY); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.