Skip to content

Commit 1bddf92

Browse files
authored
feat: configurable log setting (#30)
feat: configurable log setting disable the sls log by default, enable it through config it explicitly: `log: true` Refs: #5 #28 Signed-off-by: seven <zilisheng1996@gmail.com>
1 parent 96c4ac8 commit 1bddf92

File tree

6 files changed

+51
-84
lines changed

6 files changed

+51
-84
lines changed

src/parser/functionParser.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@ export const parseFunction = (functions?: {
1616
timeout: func.timeout,
1717
environment: func.environment,
1818
code: func.code,
19+
log: func.log,
1920
}));
2021
};

src/stack/rosStack/function.ts

Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { isEmpty } from 'lodash';
1111
import * as ossDeployment from '@alicloud/ros-cdk-ossdeployment';
1212
import * as ros from '@alicloud/ros-cdk-core';
1313
import * as sls from '@alicloud/ros-cdk-sls';
14+
import { RosFunction } from '@alicloud/ros-cdk-fc3/lib/fc3.generated';
1415

1516
export const resolveFunctions = (
1617
scope: ros.Construct,
@@ -22,34 +23,44 @@ export const resolveFunctions = (
2223
if (isEmpty(functions)) {
2324
return undefined;
2425
}
25-
const slsService = new sls.Project(
26-
scope,
27-
`${service}_sls`,
28-
{ name: `${service}-sls`, tags: replaceReference(tags, context) },
29-
true,
30-
);
26+
let logConfig: RosFunction.LogConfigProperty | undefined = undefined;
3127

32-
const slsLogstore = new sls.Logstore(
33-
scope,
34-
`${service}_sls_logstore`,
35-
{
36-
logstoreName: `${service}-sls-logstore`,
37-
projectName: slsService.attrName,
38-
ttl: 7,
39-
},
40-
true,
41-
);
28+
const enableLog = functions?.some(({ log }) => log);
29+
if (enableLog) {
30+
const slsService = new sls.Project(
31+
scope,
32+
`${service}_sls`,
33+
{ name: `${service}-sls`, tags: replaceReference(tags, context) },
34+
true,
35+
);
4236

43-
new sls.Index(
44-
scope,
45-
`${service}_sls_index`,
46-
{
47-
projectName: slsService.attrName,
48-
logstoreName: slsLogstore.attrLogstoreName,
49-
fullTextIndex: { enable: true },
50-
},
51-
true,
52-
);
37+
const slsLogstore = new sls.Logstore(
38+
scope,
39+
`${service}_sls_logstore`,
40+
{
41+
logstoreName: `${service}-sls-logstore`,
42+
projectName: slsService.attrName,
43+
ttl: 7,
44+
},
45+
true,
46+
);
47+
48+
new sls.Index(
49+
scope,
50+
`${service}_sls_index`,
51+
{
52+
projectName: slsService.attrName,
53+
logstoreName: slsLogstore.attrLogstoreName,
54+
fullTextIndex: { enable: true },
55+
},
56+
true,
57+
);
58+
logConfig = {
59+
project: slsLogstore.attrProjectName,
60+
logstore: slsLogstore.attrLogstoreName,
61+
enableRequestMetrics: true,
62+
};
63+
}
5364

5465
const fileSources = functions
5566
?.filter(({ code }) => readCodeSize(code) > CODE_ZIP_SIZE_LIMIT)
@@ -102,17 +113,15 @@ export const resolveFunctions = (
102113
timeout: replaceReference(fnc.timeout, context),
103114
environmentVariables: replaceReference(fnc.environment, context),
104115
code,
105-
logConfig: {
106-
project: slsLogstore.attrProjectName,
107-
logstore: slsLogstore.attrLogstoreName,
108-
enableRequestMetrics: true,
109-
},
116+
logConfig,
110117
},
111118
true,
112119
);
113-
fcn.addRosDependency(`${service}_sls`);
114-
fcn.addRosDependency(`${service}_sls_logstore`);
115-
fcn.addRosDependency(`${service}_sls_index`);
120+
if (enableLog) {
121+
fcn.addRosDependency(`${service}_sls`);
122+
fcn.addRosDependency(`${service}_sls_logstore`);
123+
fcn.addRosDependency(`${service}_sls_index`);
124+
}
116125

117126
if (storeInBucket) {
118127
fcn.addRosDependency(`${service}_artifacts_code_deployment`);

src/types/domains/function.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export type FunctionRaw = {
55
code: string;
66
memory: number;
77
timeout: number;
8+
log?: boolean;
89
environment?: {
910
[key: string]: string;
1011
};

src/validator/functionSchema.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export const functionSchema = {
44
patternProperties: {
55
'.*': {
66
type: 'object',
7+
required: ['name', 'runtime', 'handler', 'code'],
78
properties: {
89
name: { type: 'string' },
910
runtime: {
@@ -29,6 +30,7 @@ export const functionSchema = {
2930
code: { type: 'string' },
3031
memory: { type: 'number' },
3132
timeout: { type: 'number' },
33+
log: { type: 'boolean' },
3234
environment: {
3335
type: 'object',
3436
additionalProperties: {

tests/fixtures/deployFixture.ts

Lines changed: 3 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export const oneFcOneGatewayIac = {
2727
code: 'tests/fixtures/artifacts/artifact.zip',
2828
memory: 128,
2929
timeout: 10,
30+
log: true,
3031
environment: {
3132
NODE_ENV: 'production',
3233
},
@@ -473,66 +474,16 @@ export const minimumRos = {
473474
ROSTemplateFormatVersion: '2015-09-01',
474475
Resources: {
475476
hello_fn: {
476-
DependsOn: [
477-
'my-demo-minimum-service_sls',
478-
'my-demo-minimum-service_sls_logstore',
479-
'my-demo-minimum-service_sls_index',
480-
],
481477
Properties: {
482478
Code: {
483479
ZipFile: 'resolved-code',
484480
},
485481
FunctionName: 'hello_fn',
486482
Handler: 'index.handler',
487-
LogConfig: {
488-
EnableRequestMetrics: true,
489-
Logstore: {
490-
'Fn::GetAtt': ['my-demo-minimum-service_sls_logstore', 'LogstoreName'],
491-
},
492-
Project: {
493-
'Fn::GetAtt': ['my-demo-minimum-service_sls_logstore', 'ProjectName'],
494-
},
495-
},
496483
Runtime: 'nodejs18',
497484
},
498485
Type: 'ALIYUN::FC3::Function',
499486
},
500-
'my-demo-minimum-service_sls': {
501-
Properties: {
502-
Name: 'my-demo-minimum-service-sls',
503-
},
504-
Type: 'ALIYUN::SLS::Project',
505-
},
506-
'my-demo-minimum-service_sls_index': {
507-
Properties: {
508-
FullTextIndex: {
509-
Enable: true,
510-
},
511-
LogReduce: false,
512-
LogstoreName: {
513-
'Fn::GetAtt': ['my-demo-minimum-service_sls_logstore', 'LogstoreName'],
514-
},
515-
ProjectName: {
516-
'Fn::GetAtt': ['my-demo-minimum-service_sls', 'Name'],
517-
},
518-
},
519-
Type: 'ALIYUN::SLS::Index',
520-
},
521-
'my-demo-minimum-service_sls_logstore': {
522-
Properties: {
523-
AppendMeta: false,
524-
AutoSplit: false,
525-
EnableTracking: false,
526-
LogstoreName: 'my-demo-minimum-service-sls-logstore',
527-
PreserveStorage: false,
528-
ProjectName: {
529-
'Fn::GetAtt': ['my-demo-minimum-service_sls', 'Name'],
530-
},
531-
ShardCount: 2,
532-
TTL: 7,
533-
},
534-
Type: 'ALIYUN::SLS::Logstore',
535-
},
536487
},
537488
};
538489

@@ -561,6 +512,7 @@ export const oneFcIac = {
561512
code: 'tests/fixtures/artifacts/artifact.zip',
562513
memory: 128,
563514
timeout: 10,
515+
log: true,
564516
environment: {
565517
NODE_ENV: 'production',
566518
},
@@ -697,6 +649,7 @@ export const oneFcIacWithStage = {
697649
code: 'tests/fixtures/artifacts/artifact.zip',
698650
memory: 128,
699651
timeout: 10,
652+
log: true,
700653
environment: {
701654
NODE_ENV: '${stages.node_env}',
702655
},

tests/fixtures/serverless-insight.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ functions:
3131
code: tests/fixtures/artifacts/artifact.zip
3232
memory: 512
3333
timeout: 10
34+
log: true
3435
environment:
3536
NODE_ENV: ${stages.node_env}
3637
TEST_VAR: ${vars.testv}

0 commit comments

Comments
 (0)