-
Notifications
You must be signed in to change notification settings - Fork 779
Add performance monitoring (prometheus for metrics and winston for logging) #1394
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 4 commits
914769d
cc36895
2733675
c8b018f
1ece8e8
e8e2eb4
2a73170
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| 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', | ||
| }); |
| 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; | ||
| 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); | ||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔴 Security & Stability Issue: Using 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
|
||||||||
| } | ||||||||
| }); | ||||||||
|
|
||||||||
| 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!, | ||||||||
| }; | ||||||||
| }; | ||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| const { loadAndValidateEnv } = await import('./envConfig.js'); | ||
|
|
||
| let LokiLogger: any; | ||
narengogi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| 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 }; | ||
| 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); | ||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔴 Security & Stability Issue: Using 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
|
||||||||
| } | ||||||||
| }); | ||||||||
|
|
||||||||
| 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!, | ||||||||
| }; | ||||||||
| }; | ||||||||
Uh oh!
There was an error while loading. Please reload this page.