From ababd90f098f922a92a79ca7bdb00a3dfe112c6b Mon Sep 17 00:00:00 2001 From: Mariusz Nowak Date: Thu, 31 Mar 2022 18:46:18 +0200 Subject: [PATCH] refactor(Console): Report logs with opt out option --- lib/classes/console.js | 6 +++++- lib/config-schema.js | 13 +++++++++++- test/unit/lib/classes/console.test.js | 30 +++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/lib/classes/console.js b/lib/classes/console.js index 814ef3c50..a77da72d7 100644 --- a/lib/classes/console.js +++ b/lib/classes/console.js @@ -40,9 +40,9 @@ class Console { } async initialize() { + const { configurationInput: configuration } = this.serverless; this.isEnabled = (() => { const { - configurationInput: configuration, processedInput: { commands, options }, } = this.serverless; if (!_.get(configuration, 'console')) return false; @@ -88,6 +88,7 @@ class Console { this.service = this.serverless.service.service; this.stage = this.provider.getStage(); this.region = this.provider.getRegion(); + this.shouldDisableLogsCollection = configuration.console.disableLogsCollection; this.otelIngestionUrl = (() => { if (process.env.SLS_CONSOLE_OTEL_INGESTION_URL) { return process.env.SLS_CONSOLE_OTEL_INGESTION_URL; @@ -366,6 +367,9 @@ Object.defineProperties( AWS_LAMBDA_EXEC_WRAPPER: '/opt/otel-extension/internal/exec-wrapper.sh', }; if (process.env.SLS_OTEL_LAYER_DEV_BUILD) result.DEBUG_SLS_OTEL_LAYER = '1'; + if (!this.shouldDisableLogsCollection) { + result.SLS_OTEL_REPORT_LOGS_URL = `${this.otelIngestionUrl}/v1/logs`; + } return result; }); }), diff --git a/lib/config-schema.js b/lib/config-schema.js index 74e73d228..dca49872e 100644 --- a/lib/config-schema.js +++ b/lib/config-schema.js @@ -15,7 +15,18 @@ const schema = { * The default is `warn`, and will be set to `error` in v2 */ configValidationMode: { enum: ['error', 'warn', 'off'] }, - console: { type: 'boolean' }, + console: { + anyOf: [ + { type: 'boolean' }, + { + type: 'object', + properties: { + disableLogsCollection: { type: 'boolean' }, + }, + additionalProperties: false, + }, + ], + }, custom: { type: 'object', properties: {}, diff --git a/test/unit/lib/classes/console.test.js b/test/unit/lib/classes/console.test.js index eb6253906..e976284ba 100644 --- a/test/unit/lib/classes/console.test.js +++ b/test/unit/lib/classes/console.test.js @@ -182,6 +182,7 @@ describe('test/unit/lib/classes/console.test.js', () => { for (const fnVariables of fnVariablesList) { expect(fnVariables).to.have.property('SLS_OTEL_REPORT_REQUEST_HEADERS'); expect(fnVariables).to.have.property('SLS_OTEL_REPORT_METRICS_URL'); + expect(fnVariables).to.have.property('SLS_OTEL_REPORT_LOGS_URL'); expect(fnVariables).to.have.property('AWS_LAMBDA_EXEC_WRAPPER'); } @@ -267,6 +268,35 @@ describe('test/unit/lib/classes/console.test.js', () => { }); }); + describe('disable logs collection', () => { + it('should not setup report logs url', async () => { + const fetchStub = createFetchStub().stub; + const { cfTemplate, awsNaming } = await runServerless({ + fixture: 'function', + command: 'package', + configExt: { + console: { disableLogsCollection: true }, + org: 'testorg', + }, + modulesCacheStub: { + [getRequire(path.dirname(require.resolve('@serverless/dashboard-plugin'))).resolve( + '@serverless/platform-client' + )]: { ServerlessSDK: ServerlessSDKMock }, + [require.resolve('node-fetch')]: fetchStub, + }, + awsRequestStubMap: createAwsRequestStubMap(), + env: { SERVERLESS_ACCESS_KEY: 'dummy' }, + }); + + const fnVariables = + cfTemplate.Resources[awsNaming.getLambdaLogicalId('basic')].Properties.Environment + .Variables; + expect(fnVariables).to.have.property('SLS_OTEL_REPORT_REQUEST_HEADERS'); + expect(fnVariables).to.not.have.property('SLS_OTEL_REPORT_LOGS_URL'); + expect(fnVariables).to.have.property('AWS_LAMBDA_EXEC_WRAPPER'); + }); + }); + describe('package for custom deployment bucket', () => { let cfTemplate; let awsNaming;