-
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.
feat: Implement cloud logging and error reporting
Part of relaycorp/cloud-gateway#10
- Loading branch information
Showing
4 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { getPinoOptions } from '@relaycorp/pino-cloud'; | ||
import { EnvVarError } from 'env-var'; | ||
import pino from 'pino'; | ||
|
||
import { configureMockEnvVars, getMockInstance } from '../services/_test_utils'; | ||
import { makeLogger } from './logging'; | ||
|
||
const REQUIRED_ENV_VARS = { | ||
GATEWAY_VERSION: '1.0.1', | ||
}; | ||
const mockEnvVars = configureMockEnvVars(REQUIRED_ENV_VARS); | ||
|
||
const COMPONENT = 'the-component'; | ||
|
||
jest.mock('@relaycorp/pino-cloud', () => ({ | ||
getPinoOptions: jest.fn().mockReturnValue({}), | ||
})); | ||
|
||
describe('makeLogger', () => { | ||
test('Log level should be info if LOG_LEVEL env var is absent', () => { | ||
mockEnvVars(REQUIRED_ENV_VARS); | ||
|
||
const logger = makeLogger(COMPONENT); | ||
|
||
expect(logger).toHaveProperty('level', 'info'); | ||
}); | ||
|
||
test('Log level in LOG_LEVEL env var should be honoured if present', () => { | ||
const loglevel = 'debug'; | ||
mockEnvVars({ ...REQUIRED_ENV_VARS, LOG_LEVEL: loglevel }); | ||
|
||
const logger = makeLogger(COMPONENT); | ||
|
||
expect(logger).toHaveProperty('level', loglevel); | ||
}); | ||
|
||
test('Log level in LOG_LEVEL env var should be lower-cased if present', () => { | ||
const loglevel = 'DEBUG'; | ||
mockEnvVars({ ...REQUIRED_ENV_VARS, LOG_LEVEL: loglevel }); | ||
|
||
const logger = makeLogger(COMPONENT); | ||
|
||
expect(logger).toHaveProperty('level', loglevel.toLowerCase()); | ||
}); | ||
|
||
test('GATEWAY_VERSION env var should be required', () => { | ||
mockEnvVars({ ...REQUIRED_ENV_VARS, GATEWAY_VERSION: undefined }); | ||
|
||
expect(() => makeLogger(COMPONENT)).toThrowWithMessage(EnvVarError, /GATEWAY_VERSION/); | ||
}); | ||
|
||
test('Cloud logging options should be used', () => { | ||
const messageKey = 'foo'; | ||
getMockInstance(getPinoOptions).mockReturnValue({ messageKey }); | ||
const logger = makeLogger(COMPONENT); | ||
|
||
expect(logger[pino.symbols.messageKeySym as any]).toEqual(messageKey); | ||
expect(getPinoOptions).toBeCalledWith(undefined, { | ||
name: COMPONENT, | ||
version: REQUIRED_ENV_VARS.GATEWAY_VERSION, | ||
}); | ||
}); | ||
|
||
test('LOG_TARGET env var should be honoured if present', () => { | ||
const loggingTarget = 'the-logging-target'; | ||
mockEnvVars({ ...REQUIRED_ENV_VARS, LOG_TARGET: loggingTarget }); | ||
|
||
makeLogger(COMPONENT); | ||
|
||
expect(getPinoOptions).toBeCalledWith(loggingTarget, expect.anything()); | ||
}); | ||
|
||
test('Logging target should be unset if LOG_TARGET env var is absent', () => { | ||
makeLogger(COMPONENT); | ||
|
||
expect(getPinoOptions).toBeCalledWith(undefined, expect.anything()); | ||
}); | ||
}); |
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,13 @@ | ||
import { getPinoOptions, LoggingTarget } from '@relaycorp/pino-cloud'; | ||
import { get as getEnvVar } from 'env-var'; | ||
import pino, { Level, Logger } from 'pino'; | ||
|
||
export function makeLogger(component: string): Logger { | ||
const logTarget = getEnvVar('LOG_TARGET').asString(); | ||
const gatewayVersion = getEnvVar('GATEWAY_VERSION').required().asString(); | ||
const appContext = { name: component, version: gatewayVersion }; | ||
const cloudPinoOptions = getPinoOptions(logTarget as LoggingTarget, appContext); | ||
|
||
const logLevel = getEnvVar('LOG_LEVEL').default('info').asString().toLowerCase() as Level; | ||
return pino({ ...cloudPinoOptions, level: logLevel }); | ||
} |