Skip to content

Commit

Permalink
feat: configurable log setting (#30)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Blankll authored Jan 11, 2025
1 parent 96c4ac8 commit 1bddf92
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 84 deletions.
1 change: 1 addition & 0 deletions src/parser/functionParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ export const parseFunction = (functions?: {
timeout: func.timeout,
environment: func.environment,
code: func.code,
log: func.log,
}));
};
77 changes: 43 additions & 34 deletions src/stack/rosStack/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { isEmpty } from 'lodash';
import * as ossDeployment from '@alicloud/ros-cdk-ossdeployment';
import * as ros from '@alicloud/ros-cdk-core';
import * as sls from '@alicloud/ros-cdk-sls';
import { RosFunction } from '@alicloud/ros-cdk-fc3/lib/fc3.generated';

export const resolveFunctions = (
scope: ros.Construct,
Expand All @@ -22,34 +23,44 @@ export const resolveFunctions = (
if (isEmpty(functions)) {
return undefined;
}
const slsService = new sls.Project(
scope,
`${service}_sls`,
{ name: `${service}-sls`, tags: replaceReference(tags, context) },
true,
);
let logConfig: RosFunction.LogConfigProperty | undefined = undefined;

const slsLogstore = new sls.Logstore(
scope,
`${service}_sls_logstore`,
{
logstoreName: `${service}-sls-logstore`,
projectName: slsService.attrName,
ttl: 7,
},
true,
);
const enableLog = functions?.some(({ log }) => log);
if (enableLog) {
const slsService = new sls.Project(
scope,
`${service}_sls`,
{ name: `${service}-sls`, tags: replaceReference(tags, context) },
true,
);

new sls.Index(
scope,
`${service}_sls_index`,
{
projectName: slsService.attrName,
logstoreName: slsLogstore.attrLogstoreName,
fullTextIndex: { enable: true },
},
true,
);
const slsLogstore = new sls.Logstore(
scope,
`${service}_sls_logstore`,
{
logstoreName: `${service}-sls-logstore`,
projectName: slsService.attrName,
ttl: 7,
},
true,
);

new sls.Index(
scope,
`${service}_sls_index`,
{
projectName: slsService.attrName,
logstoreName: slsLogstore.attrLogstoreName,
fullTextIndex: { enable: true },
},
true,
);
logConfig = {
project: slsLogstore.attrProjectName,
logstore: slsLogstore.attrLogstoreName,
enableRequestMetrics: true,
};
}

const fileSources = functions
?.filter(({ code }) => readCodeSize(code) > CODE_ZIP_SIZE_LIMIT)
Expand Down Expand Up @@ -102,17 +113,15 @@ export const resolveFunctions = (
timeout: replaceReference(fnc.timeout, context),
environmentVariables: replaceReference(fnc.environment, context),
code,
logConfig: {
project: slsLogstore.attrProjectName,
logstore: slsLogstore.attrLogstoreName,
enableRequestMetrics: true,
},
logConfig,
},
true,
);
fcn.addRosDependency(`${service}_sls`);
fcn.addRosDependency(`${service}_sls_logstore`);
fcn.addRosDependency(`${service}_sls_index`);
if (enableLog) {
fcn.addRosDependency(`${service}_sls`);
fcn.addRosDependency(`${service}_sls_logstore`);
fcn.addRosDependency(`${service}_sls_index`);
}

if (storeInBucket) {
fcn.addRosDependency(`${service}_artifacts_code_deployment`);
Expand Down
1 change: 1 addition & 0 deletions src/types/domains/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export type FunctionRaw = {
code: string;
memory: number;
timeout: number;
log?: boolean;
environment?: {
[key: string]: string;
};
Expand Down
2 changes: 2 additions & 0 deletions src/validator/functionSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export const functionSchema = {
patternProperties: {
'.*': {
type: 'object',
required: ['name', 'runtime', 'handler', 'code'],
properties: {
name: { type: 'string' },
runtime: {
Expand All @@ -29,6 +30,7 @@ export const functionSchema = {
code: { type: 'string' },
memory: { type: 'number' },
timeout: { type: 'number' },
log: { type: 'boolean' },
environment: {
type: 'object',
additionalProperties: {
Expand Down
53 changes: 3 additions & 50 deletions tests/fixtures/deployFixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export const oneFcOneGatewayIac = {
code: 'tests/fixtures/artifacts/artifact.zip',
memory: 128,
timeout: 10,
log: true,
environment: {
NODE_ENV: 'production',
},
Expand Down Expand Up @@ -473,66 +474,16 @@ export const minimumRos = {
ROSTemplateFormatVersion: '2015-09-01',
Resources: {
hello_fn: {
DependsOn: [
'my-demo-minimum-service_sls',
'my-demo-minimum-service_sls_logstore',
'my-demo-minimum-service_sls_index',
],
Properties: {
Code: {
ZipFile: 'resolved-code',
},
FunctionName: 'hello_fn',
Handler: 'index.handler',
LogConfig: {
EnableRequestMetrics: true,
Logstore: {
'Fn::GetAtt': ['my-demo-minimum-service_sls_logstore', 'LogstoreName'],
},
Project: {
'Fn::GetAtt': ['my-demo-minimum-service_sls_logstore', 'ProjectName'],
},
},
Runtime: 'nodejs18',
},
Type: 'ALIYUN::FC3::Function',
},
'my-demo-minimum-service_sls': {
Properties: {
Name: 'my-demo-minimum-service-sls',
},
Type: 'ALIYUN::SLS::Project',
},
'my-demo-minimum-service_sls_index': {
Properties: {
FullTextIndex: {
Enable: true,
},
LogReduce: false,
LogstoreName: {
'Fn::GetAtt': ['my-demo-minimum-service_sls_logstore', 'LogstoreName'],
},
ProjectName: {
'Fn::GetAtt': ['my-demo-minimum-service_sls', 'Name'],
},
},
Type: 'ALIYUN::SLS::Index',
},
'my-demo-minimum-service_sls_logstore': {
Properties: {
AppendMeta: false,
AutoSplit: false,
EnableTracking: false,
LogstoreName: 'my-demo-minimum-service-sls-logstore',
PreserveStorage: false,
ProjectName: {
'Fn::GetAtt': ['my-demo-minimum-service_sls', 'Name'],
},
ShardCount: 2,
TTL: 7,
},
Type: 'ALIYUN::SLS::Logstore',
},
},
};

Expand Down Expand Up @@ -561,6 +512,7 @@ export const oneFcIac = {
code: 'tests/fixtures/artifacts/artifact.zip',
memory: 128,
timeout: 10,
log: true,
environment: {
NODE_ENV: 'production',
},
Expand Down Expand Up @@ -697,6 +649,7 @@ export const oneFcIacWithStage = {
code: 'tests/fixtures/artifacts/artifact.zip',
memory: 128,
timeout: 10,
log: true,
environment: {
NODE_ENV: '${stages.node_env}',
},
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/serverless-insight.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ functions:
code: tests/fixtures/artifacts/artifact.zip
memory: 512
timeout: 10
log: true
environment:
NODE_ENV: ${stages.node_env}
TEST_VAR: ${vars.testv}
Expand Down

0 comments on commit 1bddf92

Please sign in to comment.