Skip to content

Commit

Permalink
fix: Using console log/error for fallback logging (pre-3.x, tests)
Browse files Browse the repository at this point in the history
Signed-off-by: mrickard <[email protected]>
  • Loading branch information
mrickard committed Apr 13, 2022
1 parent c8f49e4 commit 54f340b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 23 deletions.
47 changes: 25 additions & 22 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import * as Serverless from "serverless";
import { fetchLicenseKey, nerdgraphFetch } from "./api";
import Integration from "./integration";
import { waitForStatus } from "./utils";
// tslint:disable-next-line
import * as backupLogs from "@serverless/utils/log";

const DEFAULT_FILTER_PATTERNS = [
"REPORT",
Expand All @@ -21,6 +19,13 @@ const enum JavaHandler {
handleStreamsRequest = "handleStreamsRequest",
}

// The plugin uses only these log levels:
const logShim = {
error: console.error, // tslint:disable-line
warning: console.log, // tslint:disable-line
notice: console.log, // tslint:disable-line
};

export default class NewRelicLambdaLayerPlugin {
public serverless: Serverless;
public options: Serverless.Options;
Expand All @@ -38,7 +43,7 @@ export default class NewRelicLambdaLayerPlugin {
this.serverless = serverless;
this.options = options;
// The run-from-lib method used by the test can't supply a log object, so this is a fallback:
this.log = logParam && logParam.log ? logParam.log : backupLogs.log;
this.log = logParam && logParam.log ? logParam.log : logShim;
this.awsProvider = this.serverless.getProvider("aws") as any;
this.licenseKey = null;
this.managedSecretConfigured = false;
Expand Down Expand Up @@ -227,18 +232,16 @@ https://blog.newrelic.com/product-news/aws-lambda-extensions-integrations/
public async run() {
const version = this.serverless.getVersion();
if (semver.lt(version, "1.34.0")) {
// tslint:disable-next-line
console.error(
this.log.error(
`Serverless ${version} does not support layers. Please upgrade to >=1.34.0.`
);
return;
} else if (semver.lt(version, "3.0.0")) {
// tslint:disable-next-line
console.error(
`Serverless 3.0.0 changed its logging interface, and is incompatible with Serverless ${version}.
This plugin requires Serverless 3.x. Please upgrade Serverless to >=3.0.0, or use version 2.4.1 of this plugin.`
);
return;
this.log.warning(`
The Serverless logging interface changed with the release of 3.x. This plugin is compatible with Serverless 3,
but may not be fully compatible with Serverless ${version}. If you have trouble deploying, we recommend that you
either upgrade Serverless to >=3.0.0, or use version 2.4.1 of this plugin.
`);
}

let plugins = _.get(this.serverless, "service.plugins", []);
Expand Down Expand Up @@ -386,7 +389,7 @@ https://blog.newrelic.com/product-news/aws-lambda-extensions-integrations/
this.log.notice(`Adding NewRelic layer to ${funcName}`);

if (!this.region) {
this.log.warn("No AWS region specified for NewRelic layer; skipping.");
this.log.warning("No AWS region specified for NewRelic layer; skipping.");
return;
}

Expand All @@ -405,7 +408,7 @@ https://blog.newrelic.com/product-news/aws-lambda-extensions-integrations/
} = funcDef;

if (!this.config.accountId && !environment.NEW_RELIC_ACCOUNT_ID) {
this.log.warn(
this.log.warning(
`No New Relic Account ID specified for "${funcName}"; skipping.`
);
return;
Expand All @@ -427,7 +430,7 @@ https://blog.newrelic.com/product-news/aws-lambda-extensions-integrations/
typeof runtime !== "string" ||
(wrappableRuntime && !this.config.enableExtension)
) {
this.log.warn(
this.log.warning(
`Unsupported runtime "${runtime}" for NewRelic layer; skipping.`
);
return;
Expand All @@ -451,7 +454,7 @@ https://blog.newrelic.com/product-news/aws-lambda-extensions-integrations/

// Note: This is if the user specifies a layer in their serverless.yml
if (newRelicLayers.length) {
this.log.warn(
this.log.warning(
`Function "${funcName}" already specifies an NewRelic layer; skipping.`
);
} else {
Expand Down Expand Up @@ -539,7 +542,7 @@ https://blog.newrelic.com/product-news/aws-lambda-extensions-integrations/
return false;
}

this.log.warn(
this.log.warning(
`Skipping plugin serverless-newrelic-lambda-layers for stage ${this.stage}`
);

Expand All @@ -554,14 +557,14 @@ https://blog.newrelic.com/product-news/aws-lambda-extensions-integrations/
_.isArray(include) &&
include.indexOf(funcName) === -1
) {
this.log.warn(
this.log.warning(
`Excluded function ${funcName}; is not part of include; skipping`
);
return true;
}

if (_.isArray(exclude) && exclude.indexOf(funcName) !== -1) {
this.log.warn(`Excluded function ${funcName}; skipping`);
this.log.warning(`Excluded function ${funcName}; skipping`);
return true;
}

Expand Down Expand Up @@ -616,7 +619,7 @@ https://blog.newrelic.com/product-news/aws-lambda-extensions-integrations/
!compatibleLayers ||
(compatibleLayers.length < 1 && architecture)
) {
this.log.warn(
this.log.warning(
`${architecture} is not yet supported by New Relic layers for ${runtime} in ${this.region}. Skipping.`
);
return false;
Expand Down Expand Up @@ -686,7 +689,7 @@ https://blog.newrelic.com/product-news/aws-lambda-extensions-integrations/
this.log.error(
`Could not find a \`${logIngestionFunctionName}\` function installed.`
);
this.log.warn(
this.log.warning(
"Details about setup requirements are available here: https://docs.newrelic.com/docs/serverless-function-monitoring/aws-lambda-monitoring/get-started/enable-new-relic-monitoring-aws-lambda#enable-process"
);
if (err.providerError) {
Expand Down Expand Up @@ -722,7 +725,7 @@ https://blog.newrelic.com/product-news/aws-lambda-extensions-integrations/
);

if (competingFilters.length) {
this.log.warn(
this.log.warning(
"WARNING: Found a log subscription filter that was not installed by New Relic. This may prevent the New Relic log subscription filter from being installed. If you know you don't need this log subscription filter, you should first remove it and rerun this command. If your organization requires this log subscription filter, please contact New Relic at [email protected] for assistance with getting the AWS log subscription filter limit increased."
);
}
Expand Down Expand Up @@ -823,7 +826,7 @@ https://blog.newrelic.com/product-news/aws-lambda-extensions-integrations/
this
);
} catch (err) {
this.log.warn(
this.log.warning(
"Unable to create newrelic-log-ingestion function. Please verify that required environment variables have been set."
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export default class Integration {
});

if (match.length < 1) {
this.log.warn(
this.log.warning(
"No New Relic AWS Lambda integration found for this New Relic linked account and aws account."
);

Expand Down

0 comments on commit 54f340b

Please sign in to comment.