Skip to content

Commit

Permalink
Tracing set via CloudFormation
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexandr Murashkin authored and Alexandr Murashkin committed Oct 13, 2017
1 parent 5b148dd commit 0efc8a0
Show file tree
Hide file tree
Showing 10 changed files with 188 additions and 125 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

Enables AWS X-Ray (https://aws.amazon.com/xray/) for the entire Serverless stack or individual functions.

**Update**: as of `2.0.0`, plugin uses Cloud Formation to update `TracingConfig` and no longer
makes additional AWS SDK calls.

Note: this plugin is currently **Beta**.

Note: tested to work well with `serverless@1.13.2`. Some older versions of `serverless`
Expand Down
26 changes: 18 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,23 @@ module.exports = class TracingConfigPlugin {
constructor(serverless, options) {
this.serverless = serverless;
this.options = options;
this.aws = serverless.getProvider('aws');
this.hooks = {
'after:deploy:deploy': this.processTracing.bind(this)
'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 => {
Expand All @@ -25,12 +34,13 @@ module.exports = class TracingConfigPlugin {
}

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}"`);
return this.options.noDeploy ? Promise.resolve(void 'noop') : this.aws.request('Lambda', 'updateFunctionConfiguration', {
FunctionName: functionName,
TracingConfig: {
Mode: isEnabled === true ? 'Active' : 'PassThrough'
}
});
this.functionResources[functionName].Properties.TracingConfig = {
Mode: isEnabled === true ? 'Active' : 'PassThrough'
};
}
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "serverless-plugin-tracing",
"version": "1.1.0",
"version": "2.0.0",
"description": "Enables AWS X-Ray for entire Serverless stack or individual functions",
"main": "index.js",
"engines": {
Expand Down
24 changes: 24 additions & 0 deletions test/cf-template-basic-enabled-both.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "The AWS CloudFormation template for this Serverless application",
"Resources": {
"MainLambdaFunction": {
"Type": "AWS::Lambda::Function",
"Properties": {
"FunctionName": "myService-test-mainFunction",
"TracingConfig": {
"Mode": "Active"
}
}
},
"HealthcheckLambdaFunction": {
"Type": "AWS::Lambda::Function",
"Properties": {
"FunctionName": "myService-test-healthcheck",
"TracingConfig": {
"Mode": "Active"
}
}
}
}
}
24 changes: 24 additions & 0 deletions test/cf-template-basic-enabled-one.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "The AWS CloudFormation template for this Serverless application",
"Resources": {
"MainLambdaFunction": {
"Type": "AWS::Lambda::Function",
"Properties": {
"FunctionName": "myService-test-mainFunction",
"TracingConfig": {
"Mode": "Active"
}
}
},
"HealthcheckLambdaFunction": {
"Type": "AWS::Lambda::Function",
"Properties": {
"FunctionName": "myService-test-healthcheck",
"TracingConfig": {
"Mode": "PassThrough"
}
}
}
}
}
18 changes: 18 additions & 0 deletions test/cf-template-basic.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "The AWS CloudFormation template for this Serverless application",
"Resources": {
"MainLambdaFunction": {
"Type": "AWS::Lambda::Function",
"Properties": {
"FunctionName": "myService-test-mainFunction"
}
},
"HealthcheckLambdaFunction": {
"Type": "AWS::Lambda::Function",
"Properties": {
"FunctionName": "myService-test-healthcheck"
}
}
}
}
21 changes: 21 additions & 0 deletions test/cf-template-custom-name-enabled.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "The AWS CloudFormation template for this Serverless application",
"Resources": {
"MainLambdaFunction": {
"Type": "AWS::Lambda::Function",
"Properties": {
"FunctionName": "custom-name",
"TracingConfig": {
"Mode": "Active"
}
}
},
"HealthcheckLambdaFunction": {
"Type": "AWS::Lambda::Function",
"Properties": {
"FunctionName": "myService-test-healthcheck"
}
}
}
}
21 changes: 21 additions & 0 deletions test/cf-template-custom-name-not-changed.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "The AWS CloudFormation template for this Serverless application",
"Resources": {
"MainLambdaFunction": {
"Type": "AWS::Lambda::Function",
"Properties": {
"FunctionName": "custom-name",
"TracingConfig": {
"Mode": "Active"
}
}
},
"HealthcheckLambdaFunction": {
"Type": "AWS::Lambda::Function",
"Properties": {
"FunctionName": "myService-test-healthcheck"
}
}
}
}
18 changes: 18 additions & 0 deletions test/cf-template-custom-name.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "The AWS CloudFormation template for this Serverless application",
"Resources": {
"MainLambdaFunction": {
"Type": "AWS::Lambda::Function",
"Properties": {
"FunctionName": "custom-name"
}
},
"HealthcheckLambdaFunction": {
"Type": "AWS::Lambda::Function",
"Properties": {
"FunctionName": "myService-test-healthcheck"
}
}
}
}
Loading

0 comments on commit 0efc8a0

Please sign in to comment.