Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
and3rson committed Dec 13, 2022
1 parent 8cd0cfb commit 5468c56
Show file tree
Hide file tree
Showing 4 changed files with 10,956 additions and 3,323 deletions.
120 changes: 64 additions & 56 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,63 +1,71 @@
'use strict';

const PLUGIN_NAME = 'serverless-plugin-tracing';
const PLUGIN_NAME = 'serverless-plugin-xray';

module.exports = class TracingConfigPlugin {
constructor(serverless, options) {
this.serverless = serverless;
this.options = options;
this.hooks = {
'package:compileEvents': this.processTracing.bind(this)
};

// Validate the schema, required in serverless 2.x and above
if (this.serverless.configSchemaHandler.defineCustomProperties) {
this.serverless.configSchemaHandler.defineCustomProperties({
type: 'object',
properties: {
[PLUGIN_NAME]: {
type: 'object',
properties: {
tracing: { type: 'boolean' }
}
}
}
});
}
}

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;
}
constructor(serverless, options) {
this.serverless = serverless;
this.options = options;
this.hooks = {
'package:compileEvents': this.processTracing.bind(this)
};

// Validate the schema, required in serverless 2.x and above
if (this.serverless.configSchemaHandler && this.serverless.configSchemaHandler.defineCustomProperties) {
this.serverless.configSchemaHandler.defineCustomProperties({
type: 'object',
properties: {
[PLUGIN_NAME]: {
type: 'object',
properties: {
tracing: { type: 'boolean' }
}
}
}
});
}
}

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 provider = service.provider || {};
const legacyTracing = (provider.tracing || false).toString() === 'true';

const custom = service.custom || {};
const pluginCustom = custom[PLUGIN_NAME] || {};
const tracing = (pluginCustom.tracing || false).toString() === 'true';
const providerLevelTracingEnabled = tracing || legacyTracing;

return Promise.all(Object.keys(service.functions).map(functionName => {
var func = service.functions[functionName];
var enabled = providerLevelTracingEnabled;
if (typeof func.tracing !== 'undefined') {
enabled = func.tracing.toString() === 'true';
}
return this.toggleTracing(func.name || `${service.service}-${stage}-${functionName}`, enabled);
}));
}

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;
}
return acc;
}, {});
const stage = this.options.stage;
const providerLevelTracingEnabled = (service.custom[PLUGIN_NAME].tracing === true || service.custom[PLUGIN_NAME].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'
};
}
this.serverless.cli.log(`Tracing ${isEnabled ? 'ENABLED' : 'DISABLED'} for function "${functionName}"`);
this.functionResources[functionName].Properties.TracingConfig = {
Mode: isEnabled === true ? 'Active' : 'PassThrough'
};
}
};
Loading

0 comments on commit 5468c56

Please sign in to comment.