-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathindex.js
46 lines (43 loc) · 1.89 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
'use strict';
module.exports = class TracingConfigPlugin {
constructor(serverless, options) {
this.serverless = serverless;
this.options = options;
this.hooks = {
'package:compileEvents': this.processTracing.bind(this)
};
}
processTracing() {
const service = this.serverless.service;
this.functionResources = Object.keys(service.provider.compiledCloudFormationTemplate.Resources)
.reduce((acc, resourceId) => {
const resource = service.provider.compiledCloudFormationTemplate.Resources[resourceId];
if (resource.Type === "AWS::Lambda::Function") {
if (resource.Properties) {
acc[resource.Properties.FunctionName] = resource;
}
}
return acc;
}, {});
const stage = this.options.stage;
const providerLevelTracingEnabled = (service.provider.tracing === true || service.provider.tracing === 'true');
return Promise.all(Object.keys(service.functions).map(functionName => {
return this.toggleTracing(
service.functions[functionName].name || `${service.service}-${stage}-${functionName}`,
service.functions[functionName].tracing === true
|| service.functions[functionName].tracing === 'true'
|| (providerLevelTracingEnabled && (service.functions[functionName].tracing !== false && service.functions[functionName].tracing !== 'false'))
);
}));
}
toggleTracing(functionName, isEnabled) {
if (!this.functionResources[functionName]) {
this.serverless.cli.log(`Tracing NOT SET for function "${functionName}" as couldn't find it in Cloud Formation template`);
return;
}
this.serverless.cli.log(`Tracing ${isEnabled ? 'ENABLED' : 'DISABLED'} for function "${functionName}"`);
this.functionResources[functionName].Properties.TracingConfig = {
Mode: isEnabled === true ? 'Active' : 'PassThrough'
};
}
};