refactor(Console): Report logs with opt out option

This commit is contained in:
Mariusz Nowak 2022-03-31 18:46:18 +02:00 committed by Mariusz Nowak
parent 62af0eab9b
commit ababd90f09
3 changed files with 47 additions and 2 deletions

View File

@ -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;
});
}),

View File

@ -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: {},

View File

@ -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;