Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
802 changes: 794 additions & 8 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"build/public"
],
"scripts": {
"dev": "wrangler dev src/index.ts",
"uninstall-workerd-unsupported-packages": "npm uninstall winston winston-loki prom-client --no-save",
"dev": "npm run dev:workerd",
"dev:node": "tsx src/start-server.ts",
"dev:workerd": "wrangler dev src/index.ts",
"deploy": "wrangler deploy --minify src/index.ts",
Expand Down Expand Up @@ -53,6 +54,9 @@
"hono": "^4.6.10",
"jose": "^6.0.11",
"patch-package": "^8.0.0",
"prom-client": "^15.1.3",
"winston": "^3.18.3",
"winston-loki": "^6.1.3",
"ws": "^8.18.0",
"zod": "^3.22.4"
},
Expand Down
7 changes: 7 additions & 0 deletions src/apm/console/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { createLogger, transports, format } from 'winston';

export const ConsoleLogger = createLogger({
transports: [new transports.Console()],
format: format.combine(format.simple(), format.colorize()),
level: 'debug',
});
12 changes: 12 additions & 0 deletions src/apm/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Environment } from '../utils/env.js';

let _logger: any;

if (Environment().APM_LOGGER === 'loki') {
const { LokiLogger } = await import('./loki/logger.js');
_logger = LokiLogger;
} else {
_logger = console;
}

export const logger = _logger;
21 changes: 21 additions & 0 deletions src/apm/loki/envConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Environment } from '../../utils/env';

const requiredEnvVars = ['NODE_ENV', 'SERVICE_NAME', 'LOKI_AUTH', 'LOKI_HOST'];

export const loadAndValidateEnv = () => {
const env = Environment({}) as Record<string, string>;
requiredEnvVars.forEach((varName) => {
if (!env[varName]) {
console.error(`Missing required environment variable: ${varName}`);
process.exit(1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 Security & Stability

Issue: Using process.exit(1) in library code can cause the entire application to crash abruptly without proper cleanup. This is especially problematic in serverless environments.

Fix: Throw a specific error that can be caught and handled by the application's error handling mechanism, allowing for graceful degradation.

Impact: Improves application stability and allows for better error handling

Suggested change
process.exit(1);
throw new Error(`Missing required environment variable: ${varName}`);

}
});

return {
NODE_ENV: env.NODE_ENV!,
SERVICE_NAME: env.SERVICE_NAME!,
LOKI_AUTH: env.LOKI_AUTH!,
LOKI_HOST: env.LOKI_HOST!,
LOKI_PUSH_ENABLED: env.LOKI_PUSH_ENABLED!,
};
};
35 changes: 35 additions & 0 deletions src/apm/loki/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const { loadAndValidateEnv } = await import('./envConfig.js');

let LokiLogger: any;

try {
const { createLogger, transports, format } = await import('winston');
const LokiTransport = await import('winston-loki');

const envVars = loadAndValidateEnv();

LokiLogger = createLogger({
transports: [
...(envVars.LOKI_PUSH_ENABLED === 'true'
? [
new LokiTransport({
host: envVars.LOKI_HOST,
basicAuth: envVars.LOKI_AUTH,
labels: { app: envVars.SERVICE_NAME, env: envVars.NODE_ENV },
json: true,
format: format.json(),
replaceTimestamp: true,
onConnectionError: (err) => console.error(err),
}),
]
: []),
new transports.Console({
format: format.combine(format.simple(), format.colorize()),
}),
],
});
} catch (error) {
LokiLogger = null;
}

export { LokiLogger };
26 changes: 26 additions & 0 deletions src/apm/prometheus/envConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Environment } from '../../utils/env';

const requiredEnvVars = [
'NODE_ENV',
'SERVICE_NAME',
'PROMETHEUS_GATEWAY_URL',
'PROMETHEUS_GATEWAY_AUTH',
];

export const loadAndValidateEnv = (): { [key: string]: string } => {
const env = Environment({}) as Record<string, string>;
requiredEnvVars.forEach((varName) => {
if (!env[varName]) {
console.error(`Missing required environment variable: ${varName}`);
process.exit(1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 Security & Stability

Issue: Using process.exit(1) in library code can cause the entire application to crash abruptly without proper cleanup. This is especially problematic in serverless environments.

Fix: Throw a specific error that can be caught and handled by the application's error handling mechanism, allowing for graceful degradation.

Impact: Improves application stability and allows for better error handling

Suggested change
process.exit(1);
throw new Error(`Missing required environment variable: ${varName}`);

}
});

return {
NODE_ENV: env.NODE_ENV!,
SERVICE_NAME: env.SERVICE_NAME!,
PROMETHEUS_GATEWAY_URL: env.PROMETHEUS_GATEWAY_URL!,
PROMETHEUS_GATEWAY_AUTH: env.PROMETHEUS_GATEWAY_AUTH!,
PROMETHEUS_PUSH_ENABLED: env.PROMETHEUS_PUSH_ENABLED!,
};
};
Loading