Skip to content

Commit

Permalink
feat: Implement cloud logging and error reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
gnarea committed Jan 29, 2021
1 parent 96a4a6c commit 0fce77c
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 0 deletions.
9 changes: 9 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
"@relaycorp/cogrpc": "^1.3.1",
"@relaycorp/keystore-vault": "^1.2.1",
"@relaycorp/object-storage": "^1.3.2",
"@relaycorp/pino-cloud": "^1.0.0",
"@relaycorp/relaynet-core": "^1.42.3",
"@relaycorp/relaynet-pohttp": "^1.6.0",
"@typegoose/typegoose": "^7.4.8",
Expand Down
78 changes: 78 additions & 0 deletions src/utilities/logging.spec.ts
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());
});
});
13 changes: 13 additions & 0 deletions src/utilities/logging.ts
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 });
}

0 comments on commit 0fce77c

Please sign in to comment.