Skip to content

Commit

Permalink
Add the ability to exclude functions from being instrumented (#135)
Browse files Browse the repository at this point in the history
* adding the ability to exclude a function from being instrumented

* fixing formatting

* updating documentation

* Update README.md

Updating the readme based on suggestion.

* Update README.md

Fixing a typo.

* Addressing issues. additional cleanup

* Update serverless/README.md - fixing grammar.

Co-authored-by: Rey Abolofia <[email protected]>

---------

Co-authored-by: Rey Abolofia <[email protected]>
  • Loading branch information
TophrC-dd and purple4reina authored Nov 14, 2024
1 parent a972b84 commit d8f9502
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 3 deletions.
4 changes: 3 additions & 1 deletion serverless/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,9 @@ To further configure your plugin, use the following custom parameters:
| `nodeLayerVersion` | Version of the Node.js Lambda layer to install, such as "29". Required if you are deploying at least one Lambda function written in Node.js and `addLayers` is true. Find the latest version number from [https://github.com/DataDog/datadog-lambda-js/releases][6]. |
| `dotnetLayerVersion` | Version of the .NET Lambda layer to install, such as "14". Required if you are deploying at least one Lambda function written in .NET and `addLayers` is true. Find the latest version number from [https://github.com/DataDog/dd-trace-dotnet-aws-lambda-layer/releases][9].
| `javaLayerVersion` | Version of the Java Lambda layer to install, such as "12". Required if you are deploying at least one Lambda function written in Java and `addLayers` is true. Find the latest version number from [https://github.com/DataDog/datadog-lambda-java/releases][10].
| `extensionLayerVersion` | Version of the Datadog Lambda Extension layer to install, such as "5". When `extensionLayerVersion` is set, `apiKey` (or if encrypted, `apiKMSKey` or `apiKeySecretArn`) needs to be set as well. While using `extensionLayerVersion` do not set `forwarderArn`. Learn more about the Lambda extension [here][8]. |
| `extensionLayerVersion` | Version of the Datadog Lambda Extension layer to install, such as "5". When `extensionLayerVersion` is set, `apiKey` (or if encrypted, `apiKMSKey` or `apiKeySecretArn`) needs to be set as well. |
| `addExtension` | Whether to add the Lambda extension layer to the Lambda function. Defaults to false. When true, the extension layer is applied which handles sending logs, traces and custom metrics. The parameter `extensionLayerVersion` must be set. While using `extensionLayerVersion` do not set `forwarderArn`. To learn more, read the [Datadog Lambda Extension][8] documentation. |
| `exclude` | When set, the macro will ignore specified lambda functions. Use this if there are any functions that do not require datadog instrumentation. Defaults to []. |
| `forwarderArn` | When set, the plugin will automatically subscribe the functions' log groups to the Datadog Forwarder. Alternatively, you can define the log subscription using the [AWS::Logs::SubscriptionFilter][7] resource. **Note**: The 'FunctionName' property must be defined for functions that are deployed for the first time because the macro needs the function name to create the log groups and subscription filters. 'FunctionName' must NOT contain any CloudFormation functions, such as `!Sub`. |
| `stackName` | The name of the CloudFormation stack being deployed. Only required when a `forwarderArn` is provided and Lambda functions are dynamically named (when the `FunctionName` property isn't provided for a Lambda). For more information on how to add this parameter for SAM and CDK, see the examples below. |
| `flushMetricsToLogs` | Send custom metrics via logs with the Datadog Forwarder Lambda function (recommended). Defaults to `true`. When set to `false`, the Datadog API key must be defined using `apiKey` (or if encrypted, `apiKMSKey` or `apiKeySecretArn`). |
Expand Down
4 changes: 4 additions & 0 deletions serverless/src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ export interface Configuration {
// Optionally set by customer using `sam deploy --parameter-overrides DDGitData="$(git rev-parse HEAD),$(git config --get remote.origin.url)"`
// The customer template takes in the DDGitData override param and passes that to this macro's gitData param
gitData?: string;
// When set, the list of strings will be evalutated when processing each lambda function. if the string matches that function will not be instrumented by the macro.
exclude?: string[];
// When set, the lambda's payload will be captured within the incoming trace.
captureLambdaPayload: boolean;
// Cold Start Tracing is enabled by default
enableColdStartTracing?: boolean;
Expand Down Expand Up @@ -112,6 +115,7 @@ const ddApmFlushDeadlineMillisecondsEnvVar = "DD_APM_FLUSH_DEADLINE_MILLISECONDS
export const defaultConfiguration: Configuration = {
addLayers: true,
addExtension: false,
exclude: [],
flushMetricsToLogs: true,
logLevel: undefined,
site: "datadoghq.com",
Expand Down
5 changes: 4 additions & 1 deletion serverless/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@ export const handler = async (event: InputEvent, _: any) => {
};
}

const lambdas = findLambdas(resources, event.templateParameterValues);
// find lambdas then filter out the lamba's that are flagged to be excluded from instrumentation.
const lambdas = findLambdas(resources, event.templateParameterValues).filter(
(lambda) => !config.exclude?.includes(lambda.key),
);
log.debug(`Lambda resources found: ${JSON.stringify(lambdas)}`);

log.debug("Setting environment variables for Lambda function resources");
Expand Down
3 changes: 3 additions & 0 deletions serverless/test/env.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ describe("getConfig", () => {
expect(config).toEqual({
addLayers: true,
addExtension: false,
exclude: [],
flushMetricsToLogs: true,
site: "my-site",
enableXrayTracing: false,
Expand Down Expand Up @@ -64,6 +65,7 @@ describe("getConfig", () => {
expect(config).toEqual({
addLayers: true,
addExtension: false,
exclude: [],
flushMetricsToLogs: false,
logLevel: undefined,
site: "datadoghq.com",
Expand Down Expand Up @@ -92,6 +94,7 @@ describe("getConfig", () => {
expect(config).toEqual({
addLayers: true,
addExtension: false,
exclude: [],
flushMetricsToLogs: false,
site: "my-site",
enableXrayTracing: false,
Expand Down
12 changes: 11 additions & 1 deletion serverless/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ describe("Macro", () => {
]);
});

it("add only the extension layer only setting the extension layer version", async () => {
it("add only the extension layer by only setting the extension layer version", async () => {
const params = { addLayers: false, extensionLayerVersion: 6, apiKey: "abc123" };
const inputEvent = mockInputEvent(params, {});
const output = await handler(inputEvent, {});
Expand Down Expand Up @@ -231,6 +231,16 @@ describe("Macro", () => {
expect.stringMatching(/arn:aws-us-gov:lambda:us-gov-east-1:002406178527:layer:Datadog-Node12-x:25/),
]);
});

it("Excluding a lambda function from being instrumented", async () => {
const params = { exclude: [LAMBDA_KEY], nodeLayerVersion: 32, extensionLayerVersion: 32, apiKey: "testtest" };
const inputEvent = mockInputEvent(params, {}); // Use default configuration
const output = await handler(inputEvent, {});
const lambdaProperties: FunctionProperties = output.fragment.Resources[LAMBDA_KEY].Properties;

expect(lambdaProperties.Layers).toBeUndefined();
expect(lambdaProperties.Handler).toBe("app.handler");
});
});

describe("tracing", () => {
Expand Down

0 comments on commit d8f9502

Please sign in to comment.