-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Remove express-prom-bundle, puppeteer-extra, and puppeteer-extra-pl…
…ugin-stealth dependencies * Update dependencies * Update README * Use "observability" instead of "logger" * Improve and fix Express logging and metrics * Allow Pino logging level to be configurable
- Loading branch information
1 parent
43c3194
commit 6237832
Showing
16 changed files
with
922 additions
and
1,052 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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -1,21 +1,32 @@ | ||
import assert from "node:assert"; | ||
import type { LevelWithSilent } from "pino"; | ||
|
||
import assert from "node:assert/strict"; | ||
|
||
const { env } = process; | ||
|
||
const required = (name: string) => { | ||
const value = env[name]; | ||
// region assert | ||
assert(value !== undefined, `Environment variable ${name} is required`); | ||
// endregion | ||
return value; | ||
}; | ||
|
||
export default { | ||
BotGuildId: env.BOT_GUILD_ID, | ||
DiscordToken: env.DISCORD_TOKEN ?? assert.fail(), | ||
EnableCarsized: env.ENABLE_CARSIZED === `${true}`, | ||
DiscordToken: required("DISCORD_TOKEN"), | ||
EnableCarsized: env.ENABLE_CARSIZED === true.toString(), | ||
ExpressPort: Number(env.EXPRESS_PORT ?? 8080), | ||
PinoLevel: (env.PINO_LEVEL ?? "info") as LevelWithSilent, | ||
PostgresqlDatabase: env.POSTGRESQL_DATABASE ?? "db", | ||
PostgresqlHost: env.POSTGRESQL_HOST ?? "postgres", | ||
PostgresqlPassword: env.POSTGRESQL_PASSWORD ?? "password", | ||
PostgresqlPort: Number(env.POSTGRESQL_PORT ?? 5432), | ||
PostgresqlUser: env.POSTGRESQL_USER ?? "user", | ||
ProjectName: env.PROJECT_NAME ?? "Pedestrian", | ||
RedisCluster: env.REDIS_CLUSTER === `${true}`, | ||
RedisCluster: env.REDIS_CLUSTER === true.toString(), | ||
RedisHost: env.REDIS_HOST ?? "redis", | ||
RedisPassword: env.REDIS_PASSWORD, | ||
RedisPort: Number(env.REDIS_PORT ?? 6379), | ||
RedisUsername: env.REDIS_USERNAME, | ||
YoutubeApiKey: env.YOUTUBE_API_KEY ?? assert.fail(), | ||
YoutubeApiKey: required("YOUTUBE_API_KEY"), | ||
} as const; |
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,26 +1,92 @@ | ||
import type { Request } from "express"; | ||
import type { ServerResponse } from "node:http"; | ||
|
||
import express from "express"; | ||
import promBundle from "express-prom-bundle"; | ||
import pinoBundle from "pino-http"; | ||
import { randomUUID } from "node:crypto"; | ||
import pinoBundle, { startTime } from "pino-http"; | ||
import { Histogram, register } from "prom-client"; | ||
|
||
import loggerFactory from "./logger"; | ||
import * as observability from "./observability"; | ||
|
||
const server = express(); | ||
|
||
// region Logger and Metrics | ||
const logger = loggerFactory(module); | ||
|
||
server.use( | ||
promBundle({ | ||
includeMethod: true, | ||
includePath: true, | ||
}), | ||
); | ||
|
||
server.use( | ||
pinoBundle({ | ||
logger, | ||
}), | ||
); | ||
const httpRequestDuration = new Histogram({ | ||
help: "HTTP request duration in seconds", | ||
labelNames: ["method", "path", "statusCode"], | ||
name: "http_request_duration_seconds", | ||
}); | ||
|
||
const observe = ( | ||
request: Request, | ||
response: ServerResponse, | ||
responseTime: number, | ||
) => { | ||
const route: unknown = request.route; | ||
let path: string | undefined; | ||
|
||
if (typeof route === "object" && route !== null && "path" in route) { | ||
const { path: routePath } = route; | ||
if (typeof routePath === "string") path = routePath; | ||
} | ||
|
||
const { method } = request; | ||
const { statusCode } = response; | ||
|
||
const labels = { method, path, statusCode }; | ||
httpRequestDuration.observe(labels, responseTime / 1000); | ||
}; | ||
|
||
const logger = pinoBundle<Request>({ | ||
customAttributeKeys: { | ||
err: "error", | ||
req: "request", | ||
reqId: "correlationId", | ||
res: "response", | ||
}, | ||
customErrorMessage: (request, response) => { | ||
const responseTime = Date.now() - response[startTime]; | ||
observe(request, response, responseTime); | ||
return `Request errored in ${responseTime}ms`; | ||
}, | ||
customLogLevel: (request, { statusCode }) => { | ||
if (statusCode >= 500) return "error"; | ||
else if (statusCode >= 400) return "warn"; | ||
else return "info"; | ||
}, | ||
customSuccessMessage: (request, response, responseTime) => { | ||
observe(request, response, responseTime); | ||
return `Request succeeded in ${responseTime}ms`; | ||
}, | ||
genReqId: observability.correlationId, | ||
logger: observability.logger(module), | ||
}); | ||
|
||
server.use((request, response, next) => { | ||
response[startTime] = Date.now(); | ||
|
||
let correlationId = request.headers["X-Request-Id"]; | ||
if (Array.isArray(correlationId)) correlationId = correlationId[0]; | ||
correlationId ??= randomUUID(); | ||
|
||
response.setHeader("X-Request-Id", correlationId); | ||
observability.correlate({ correlationId }, () => { | ||
logger(request, response, next); | ||
}); | ||
}); | ||
|
||
server.get("/metrics", (request, response, next) => { | ||
register | ||
.metrics() | ||
.then((metrics) => { | ||
response.setHeader("Content-Type", register.contentType); | ||
response.send(metrics); | ||
|
||
const { log } = response; | ||
log.debug(`Global register metrics:\n${metrics}`); | ||
}) | ||
.catch(next); | ||
}); | ||
// endregion | ||
|
||
export default server; |
Oops, something went wrong.