Skip to content
Closed
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
10 changes: 10 additions & 0 deletions .projen/deps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .projen/tasks.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .projenrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ const project = new awscdk.AwsCdkConstructLibrary({
mavenArtifactId: "datadog-cdk-constructs",
},
peerDeps: [],
peerDependencyOptions: {
pinnedDevDependency: false,
},
cdkVersion: "2.245.0",
cdkCliVersion: "^2.245.0",
deps: ["loglevel"],
Expand Down Expand Up @@ -110,6 +113,8 @@ const project = new awscdk.AwsCdkConstructLibrary({
},
});

project.addDevDeps("aws-cdk-lib@2.253.0", "constructs@10.5.1");

// Pin GitHub Actions to commit SHAs instead of tags
project.github.actions.set(
"actions/checkout",
Expand Down
2 changes: 1 addition & 1 deletion package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions src/datadog-lambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,11 @@ export class DatadogLambda extends Construct {
// If any lambdas have already been added, override the commit sha and url
if (this.lambdas) {
this.lambdas.forEach((lambdaFunction: any) => {
if (lambdaFunction.environment[DD_TAGS] === undefined) {
const existingTags = lambdaFunction.environment[DD_TAGS] ?? lambdaFunction.environment.map?.get?.(DD_TAGS);
if (existingTags === undefined) {
return;
}
const tags = lambdaFunction.environment[DD_TAGS].value.split(",");
const tags = existingTags.value.split(",");
if (gitCommitSha) {
const index = tags.findIndex((val: string) => val.split(":")[0] === "git.commit.sha");
tags[index] = `git.commit.sha:${gitCommitSha}`;
Expand All @@ -206,7 +207,7 @@ export class DatadogLambda extends Construct {
tags[index] = `git.repository_url:${gitRepoUrl}`;
}

lambdaFunction.environment[DD_TAGS].value = tags.join(",");
lambdaFunction.addEnvironment(DD_TAGS, tags.join(","));
});
}
}
Expand Down
13 changes: 6 additions & 7 deletions src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,10 @@ export function setGitEnvironmentVariables(

// We're using an any type here because AWS does not expose the `environment` field in their type
lambdas.forEach((lam) => {
if (lam.environment[DD_TAGS] !== undefined) {
lam.environment[DD_TAGS].value += `,git.commit.sha:${hash}`;
} else {
lam.addEnvironment(DD_TAGS, `git.commit.sha:${hash}`);
}
lam.environment[DD_TAGS].value += `,git.repository_url:${gitRepoUrl}`;
const tagsValue = `git.commit.sha:${hash},git.repository_url:${gitRepoUrl}`;
const existingTagValue = lam.environment[DD_TAGS]?.value ?? lam.environment.map?.get?.(DD_TAGS)?.value;
const finalTagValue = existingTagValue ? `${existingTagValue},${tagsValue}` : tagsValue;
lam.addEnvironment(DD_TAGS, finalTagValue);
});
}

Expand Down Expand Up @@ -123,7 +121,8 @@ export function applyEnvVariables(lam: lambda.Function, baseProps: DatadogLambda
log.debug(`Setting environment variables...`);
const lam_with_env_vars: any = lam; //cast to any to access the private environment fields like in setGitEnvironmentVariables
const setEnvIfUndefined = (envVar: string, value: string | boolean) => {
if (lam_with_env_vars.environment[envVar] === undefined) {
const existingEnvVar = lam_with_env_vars.environment[envVar] ?? lam_with_env_vars.environment.map?.get?.(envVar);
if (existingEnvVar === undefined) {
lam.addEnvironment(envVar, value.toString().toLowerCase());
}
};
Expand Down
27 changes: 15 additions & 12 deletions test/datadog-lambda.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -854,10 +854,10 @@ describe("overrideGitMetadata", () => {
});
datadogLambda.overrideGitMetadata("fake-sha", "fake-url");
datadogLambda.addLambdaFunctions([hello], stack);
expect((<any>hello).environment[DD_TAGS].value.split(",")).toEqual(
expect((<any>hello).environment.map.get(DD_TAGS).value.split(",")).toEqual(
expect.arrayContaining(["git.commit.sha:fake-sha"]),
);
expect((<any>hello).environment[DD_TAGS].value.split(",")).toEqual(
expect((<any>hello).environment.map.get(DD_TAGS).value.split(",")).toEqual(
expect.arrayContaining(["git.repository_url:fake-url"]),
);
});
Expand All @@ -880,10 +880,10 @@ describe("overrideGitMetadata", () => {
});
datadogLambda.addLambdaFunctions([hello], stack);
datadogLambda.overrideGitMetadata("fake-sha", "fake-url");
expect((<any>hello).environment[DD_TAGS].value.split(",")).toEqual(
expect((<any>hello).environment.map.get(DD_TAGS).value.split(",")).toEqual(
expect.arrayContaining(["git.commit.sha:fake-sha"]),
);
expect((<any>hello).environment[DD_TAGS].value.split(",")).toEqual(
expect((<any>hello).environment.map.get(DD_TAGS).value.split(",")).toEqual(
expect.arrayContaining(["git.repository_url:fake-url"]),
);
});
Expand Down Expand Up @@ -913,10 +913,10 @@ describe("overrideGitMetadata", () => {
datadogLambda.addLambdaFunctions([goodbye], stack);

[hello, goodbye].forEach((f) => {
expect((<any>f).environment[DD_TAGS].value.split(",")).toEqual(
expect((<any>f).environment.map.get(DD_TAGS).value.split(",")).toEqual(
expect.arrayContaining(["git.commit.sha:fake-sha"]),
);
expect((<any>f).environment[DD_TAGS].value.split(",")).toEqual(
expect((<any>f).environment.map.get(DD_TAGS).value.split(",")).toEqual(
expect.arrayContaining(["git.repository_url:fake-url"]),
);
});
Expand Down Expand Up @@ -949,10 +949,10 @@ describe("overrideGitMetadata", () => {
datadogLambda.addLambdaFunctions([goodbye], stack);

[hello, goodbye].forEach((f) => {
expect((<any>f).environment[DD_TAGS].value.split(",")).toEqual(
expect((<any>f).environment.map.get(DD_TAGS).value.split(",")).toEqual(
expect.arrayContaining(["git.commit.sha:fake-sha"]),
);
expect((<any>f).environment[DD_TAGS].value.split(",")).toEqual(expect.arrayContaining(["testVar:xyz"]));
expect((<any>f).environment.map.get(DD_TAGS).value.split(",")).toEqual(expect.arrayContaining(["testVar:xyz"]));
});
});

Expand Down Expand Up @@ -982,7 +982,7 @@ describe("overrideGitMetadata", () => {
datadogLambda.addLambdaFunctions([goodbye], stack);

[hello, goodbye].forEach((f) => {
expect((<any>f).environment[DD_TAGS].value.split(",")).toEqual(
expect((<any>f).environment.map.get(DD_TAGS).value.split(",")).toEqual(
expect.arrayContaining([expect.stringContaining("git.commit.sha:fake-sha"), expect.stringMatching(REPO_REGEX)]),
);
});
Expand All @@ -1007,9 +1007,12 @@ describe("overrideGitMetadata", () => {
datadogLambda.addLambdaFunctions([hello], stack);

expect(
(<any>hello).environment[DD_TAGS].value.split(",").some((item: string) => item.includes("git.commit.sha")),
(<any>hello).environment.map
.get(DD_TAGS)
.value.split(",")
.some((item: string) => item.includes("git.commit.sha")),
).toEqual(true);
expect((<any>hello).environment[DD_TAGS].value.split(",")).toEqual(
expect((<any>hello).environment.map.get(DD_TAGS).value.split(",")).toEqual(
expect.arrayContaining([expect.stringMatching(REPO_REGEX)]),
);
});
Expand All @@ -1036,7 +1039,7 @@ describe("overrideGitMetadata", () => {
});

datadogLambda.addLambdaFunctions([hello], stack);
expect((<any>hello).environment[DD_TAGS].value.split(",")).toEqual(
expect((<any>hello).environment.map.get(DD_TAGS).value.split(",")).toEqual(
expect.arrayContaining(["git.commit.sha:fake-sha"]),
);
});
Expand Down
60 changes: 30 additions & 30 deletions yarn.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.