Skip to content
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

Skip newrelic in local and stop throwing when env vars are missing #124

Merged
merged 5 commits into from Apr 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 37 additions & 10 deletions __tests__/newrelic.tests.js
@@ -1,4 +1,21 @@
const newrelic = require( '../src/newrelic/' );
const Transport = require( 'winston-transport' );

class TestTransport extends Transport {
mjangda marked this conversation as resolved.
Show resolved Hide resolved
constructor( opts ) {
super( opts );
this.logs = [];
this.errors = [];
}

log( info ) {
this.logs.push( info );
}

error( info ) {
this.errors.push( info );
}
}

describe( 'src/newrelic', () => {
const OLD_ENV_VARS = process.env;
Expand All @@ -13,29 +30,39 @@ describe( 'src/newrelic', () => {

afterEach( () => {
process.env = OLD_ENV_VARS;
process.env.VIP_GO_APP_ID = 123; // Adding an ID to mimick VIP Go
} );

describe( 'Environment variables are missing', () => {
it( 'Should skip if local development environment', () => {
process.env.VIP_GO_APP_ID = '';
const transport = new TestTransport();
newrelic( { logger: transport } );

expect( transport.logs[ 0 ] ).toMatch( 'skipping New Relic' );
} );

it( 'Should fail if NEW_RELIC_NO_CONFIG_FILE is not set', () => {
expect( () => {
newrelic();
} ).toThrowError( /NEW_RELIC_NO_CONFIG_FILE/ );
const transport = new TestTransport();
newrelic( { logger: transport } );

expect( transport.errors[ 0 ] ).toMatch( 'NEW_RELIC_NO_CONFIG_FILE' );
} );

it( 'Should fail if NEW_RELIC_NO_CONFIG_FILE is set to false', () => {
process.env.NEW_RELIC_NO_CONFIG_FILE = false;
const transport = new TestTransport();
newrelic( { logger: transport } );

expect( () => {
newrelic();
} ).toThrowError( /NEW_RELIC_NO_CONFIG_FILE/ );
expect( transport.errors[ 0 ] ).toMatch( 'NEW_RELIC_NO_CONFIG_FILE' );
} );

it( 'Should fail if NEW_RELIC_LICENSE_KEY is not set', () => {
process.env.NEW_RELIC_NO_CONFIG_FILE = true;
const transport = new TestTransport();
newrelic( { logger: transport } );

expect( () => {
newrelic();
} ).toThrowError( /NEW_RELIC_LICENSE_KEY/ );
expect( transport.errors[ 0 ] ).toMatch( 'NEW_RELIC_LICENSE_KEY' );
} );
} );

Expand All @@ -53,7 +80,7 @@ describe( 'src/newrelic', () => {
} ).toThrowError( /could not be imported/ );
} );

it( 'Should return newrelic module when config correct set', () => {
it( 'Should return newrelic module when config is correctly set', () => {
process.env.NEW_RELIC_NO_CONFIG_FILE = true;
process.env.NEW_RELIC_LICENSE_KEY = 'ABC';
const returnedValue = newrelic();
Expand Down
16 changes: 13 additions & 3 deletions src/newrelic/index.js
Expand Up @@ -2,13 +2,23 @@ module.exports = ( { logger = console } = {} ) => {
const licenseKey = process.env.NEW_RELIC_LICENSE_KEY;
const noConfig = process.env.NEW_RELIC_NO_CONFIG_FILE === 'true';

const isLocal = ! process.env.VIP_GO_APP_ID;

if ( isLocal ) {
logger.log( 'Local development, skipping New Relic initialization...' );
return;
}

if ( ! noConfig ) {
throw new Error( `An environment variable is missing
or not set to true: NEW_RELIC_NO_CONFIG_FILE` );
logger.error( `An environment variable is missing
or not set to true: NEW_RELIC_NO_CONFIG_FILE. Skipping New Relic initialization...` );
return;
}

if ( ! licenseKey ) {
throw new Error( 'An environment variable is missing: NEW_RELIC_LICENSE_KEY' );
logger.error( `An environment variable is missing:
NEW_RELIC_LICENSE_KEY. Skipping New Relic initialization...` );
return;
}

logger.info( 'Importing New Relic library...' );
Expand Down