Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: move setTags() to a separate file #322

Merged
merged 1 commit into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 5 additions & 22 deletions src/datadog-lambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
applyExtensionLayer,
} from "./index";
import { LambdaFunction } from "./interfaces";
import { setTags } from "./tag";

const versionJson = require("../version.json");

Expand Down Expand Up @@ -132,7 +133,7 @@ export class DatadogLambda extends Construct {

applyEnvVariables(extractedLambdaFunctions, baseProps);
setDDEnvVariables(extractedLambdaFunctions, this.props);
setTags(extractedLambdaFunctions, this.props);
setTagsForFunctions(extractedLambdaFunctions, this.props);

this.transport.applyEnvVars(extractedLambdaFunctions);

Expand Down Expand Up @@ -178,28 +179,10 @@ export function addCdkConstructVersionTag(lambdaFunctions: lambda.Function[]): v
});
}

function setTags(lambdaFunctions: lambda.Function[], props: DatadogLambdaProps): void {
log.debug(`Adding datadog tags`);
lambdaFunctions.forEach((functionName) => {
function setTagsForFunctions(lambdaFunctions: lambda.Function[], props: DatadogLambdaProps): void {
lambdaFunctions.forEach((lambdaFunction) => {
if (props.forwarderArn) {
if (props.env) {
Tags.of(functionName).add(TagKeys.ENV, props.env);
}
if (props.service) {
Tags.of(functionName).add(TagKeys.SERVICE, props.service);
}
if (props.version) {
Tags.of(functionName).add(TagKeys.VERSION, props.version);
}
if (props.tags) {
const tagsArray = props.tags.split(",");
tagsArray.forEach((tag: string) => {
const [key, value] = tag.split(":");
if (key && value) {
Tags.of(functionName).add(key, value);
}
});
}
setTags(lambdaFunction, props);
}
});
}
Expand Down
34 changes: 34 additions & 0 deletions src/tag.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Unless explicitly stated otherwise all files in this repository are licensed
* under the Apache License Version 2.0.
*
* This product includes software developed at Datadog (https://www.datadoghq.com/).
* Copyright 2021 Datadog, Inc.
*/

import { Tags } from "aws-cdk-lib";
import * as lambda from "aws-cdk-lib/aws-lambda";
import log from "loglevel";
import { TagKeys, DatadogLambdaProps } from "./index";

export function setTags(resource: lambda.Function, props: DatadogLambdaProps): void {
log.debug(`Adding datadog tags`);
if (props.env) {
Tags.of(resource).add(TagKeys.ENV, props.env);
}
if (props.service) {
Tags.of(resource).add(TagKeys.SERVICE, props.service);
}
if (props.version) {
Tags.of(resource).add(TagKeys.VERSION, props.version);
}
if (props.tags) {
const tagsArray = props.tags.split(",");
tagsArray.forEach((tag: string) => {
const [key, value] = tag.split(":");
if (key && value) {
Tags.of(resource).add(key, value);
}
});
}
}
151 changes: 0 additions & 151 deletions test/datadog-lambda.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const { ISecret } = require("aws-cdk-lib/aws-secretsmanager");
const versionJson = require("../version.json");
const EXTENSION_LAYER_VERSION = 5;
const NODE_LAYER_VERSION = 91;
const PYTHON_LAYER_VERSION = 73;

describe("validateProps", () => {
it("throws an error when the site is set to an invalid site URL", () => {
Expand Down Expand Up @@ -382,156 +381,6 @@ describe("checkForMultipleApiKeys", () => {
});
});

describe("setTags", () => {
it("adds tags when forwarder is set", () => {
const app = new App();
const stack = new Stack(app, "stack", {
env: {
region: "sa-east-1",
},
});
const hello1 = new lambda.Function(stack, "HelloHandler1", {
runtime: lambda.Runtime.NODEJS_16_X,
code: lambda.Code.fromInline("test"),
handler: "hello.handler",
});
const hello2 = new lambda.Function(stack, "HelloHandler2", {
runtime: lambda.Runtime.PYTHON_3_8,
code: lambda.Code.fromInline("test"),
handler: "hello.handler",
});

const datadogLambda = new DatadogLambda(stack, "Datadog", {
nodeLayerVersion: NODE_LAYER_VERSION,
pythonLayerVersion: PYTHON_LAYER_VERSION,
extensionLayerVersion: EXTENSION_LAYER_VERSION,
addLayers: true,
enableDatadogTracing: false,
flushMetricsToLogs: true,
site: "datadoghq.com",
forwarderArn: "arn:test:forwarder:sa-east-1:12345678:1",
apiKey: "1234",
env: "test-env",
service: "test-service",
version: "1",
tags: "team:avengers,project:marvel",
});
datadogLambda.addLambdaFunctions([hello1, hello2]);

Template.fromStack(stack).hasResourceProperties("AWS::Lambda::Function", {
Runtime: "nodejs16.x",
Tags: [
{
Key: "dd_cdk_construct",
Value: `v${versionJson.version}`,
},
{
Key: "env",
Value: "test-env",
},
{
Key: "project",
Value: "marvel",
},
{
Key: "service",
Value: "test-service",
},
{
Key: "team",
Value: "avengers",
},
{
Key: "version",
Value: "1",
},
],
});
Template.fromStack(stack).hasResourceProperties("AWS::Lambda::Function", {
Runtime: "python3.8",
Tags: [
{
Key: "dd_cdk_construct",
Value: `v${versionJson.version}`,
},
{
Key: "env",
Value: "test-env",
},
{
Key: "project",
Value: "marvel",
},
{
Key: "service",
Value: "test-service",
},
{
Key: "team",
Value: "avengers",
},
{
Key: "version",
Value: "1",
},
],
});
});
it("does not add tags when forwarder isn't set", () => {
const app = new App();
const stack = new Stack(app, "stack", {
env: {
region: "sa-east-1",
},
});
const hello1 = new lambda.Function(stack, "HelloHandler1", {
runtime: lambda.Runtime.NODEJS_16_X,
code: lambda.Code.fromInline("test"),
handler: "hello.handler",
});
const hello2 = new lambda.Function(stack, "HelloHandler2", {
runtime: lambda.Runtime.PYTHON_3_8,
code: lambda.Code.fromInline("test"),
handler: "hello.handler",
});

const datadogLambda = new DatadogLambda(stack, "Datadog", {
nodeLayerVersion: NODE_LAYER_VERSION,
pythonLayerVersion: PYTHON_LAYER_VERSION,
extensionLayerVersion: EXTENSION_LAYER_VERSION,
addLayers: true,
enableDatadogTracing: false,
flushMetricsToLogs: true,
site: "datadoghq.com",
apiKey: "1234",
env: "test-env",
service: "test-service",
version: "1",
tags: "team:avengers,project:marvel",
});
datadogLambda.addLambdaFunctions([hello1, hello2]);

Template.fromStack(stack).hasResourceProperties("AWS::Lambda::Function", {
Runtime: "nodejs16.x",
Tags: [
{
Key: "dd_cdk_construct",
Value: `v${versionJson.version}`,
},
],
});
Template.fromStack(stack).hasResourceProperties("AWS::Lambda::Function", {
Runtime: "python3.8",
Tags: [
{
Key: "dd_cdk_construct",
Value: `v${versionJson.version}`,
},
],
});
});
});

describe("apiKeySecret", () => {
it("sets apiKeySecretArn", () => {
const app = new App();
Expand Down
Loading
Loading