From fa509d2bb0f6f3e7deec150c849444a671ebd11b Mon Sep 17 00:00:00 2001 From: William Conti Date: Thu, 1 Aug 2024 12:47:52 -0400 Subject: [PATCH 1/3] initial commit --- src/runtime/module_importer.js | 10 ++++------ src/utils/dd_tags.spec.ts | 21 +++++++++++++++++++++ src/utils/dd_tags.ts | 5 +++++ src/utils/index.ts | 1 + 4 files changed, 31 insertions(+), 6 deletions(-) create mode 100644 src/utils/dd_tags.spec.ts create mode 100644 src/utils/dd_tags.ts diff --git a/src/runtime/module_importer.js b/src/runtime/module_importer.js index 0ee71f47..2a48eebe 100644 --- a/src/runtime/module_importer.js +++ b/src/runtime/module_importer.js @@ -1,5 +1,5 @@ -const { logDebug } = require("../utils"); +const { logDebug, updateDDTags } = require("../utils"); // Currently no way to prevent typescript from auto-transpiling import into require, // so we expose a wrapper in js @@ -12,11 +12,9 @@ exports.initTracer = function () { // the version provided by the layer const path = require.resolve("dd-trace", { paths: ["/var/task/node_modules", ...module.paths] }); // tslint:disable-next-line:no-var-requires - const tracer = require(path).init({ - tags: { - "_dd.origin": "lambda", - }, - }); + // add lambda tags to DD_TAGS environment variable + updateDDTags("_dd.origin:lambda") + const tracer = require(path).init(); logDebug("automatically initialized dd-trace"); // Configure the tracer to ignore HTTP calls made from the Lambda Library to the Extension diff --git a/src/utils/dd_tags.spec.ts b/src/utils/dd_tags.spec.ts new file mode 100644 index 00000000..a6911c69 --- /dev/null +++ b/src/utils/dd_tags.spec.ts @@ -0,0 +1,21 @@ +import { updateDDTags } from "./dd_tags"; + +describe("updateDDTags", () => { + it("should work when updating an unset DD_TAGS", async () => { + expect(process.env.DD_TAGS).toBeUndefined(); + updateDDTags("hello:world") + expect(process.env.DD_TAGS).toEqual("hello:world"); + }); + + it("should work when updating a valid DD_TAGS", async () => { + process.env.DD_TAGS = "datadog:bits" + updateDDTags("hello:world") + expect(process.env.DD_TAGS).toEqual("datadog:bits,hello:world"); + }); + + it("should work when updating a valid DD_TAGS and comma at the end", async () => { + process.env.DD_TAGS = "datadog:bits," + updateDDTags("hello:world") + expect(process.env.DD_TAGS).toEqual("datadog:bits,hello:world"); + }); +}); \ No newline at end of file diff --git a/src/utils/dd_tags.ts b/src/utils/dd_tags.ts new file mode 100644 index 00000000..f00ff70d --- /dev/null +++ b/src/utils/dd_tags.ts @@ -0,0 +1,5 @@ +export function updateDDTags(newPair: string): void { + const tags = process.env.DD_TAGS ?? ''; + const updatedEnv = tags ? `${tags.trim().replace(/,+$/, '')},${newPair}` : newPair; + process.env.DD_TAGS = updatedEnv; +} diff --git a/src/utils/index.ts b/src/utils/index.ts index 58d6629e..2fde12fb 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -3,4 +3,5 @@ export { wrap, promisifiedHandler } from "./handler"; export { Timer } from "./timer"; export { logWarning, logError, logDebug, Logger, setLogLevel, setLogger, LogLevel } from "./log"; export { tagObject } from "./tag-object"; +export { updateDDTags } from "./dd_tags"; export { batchItemFailureCount, isBatchItemFailure } from "./batch-item-failures"; From cb26f0b0c6b395a4f68c3c60fd9a5436d3ef1d7b Mon Sep 17 00:00:00 2001 From: William Conti Date: Thu, 1 Aug 2024 13:36:41 -0400 Subject: [PATCH 2/3] fix lint --- src/utils/dd_tags.spec.ts | 12 ++++++------ src/utils/dd_tags.ts | 6 +++--- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/utils/dd_tags.spec.ts b/src/utils/dd_tags.spec.ts index a6911c69..29fc1c66 100644 --- a/src/utils/dd_tags.spec.ts +++ b/src/utils/dd_tags.spec.ts @@ -3,19 +3,19 @@ import { updateDDTags } from "./dd_tags"; describe("updateDDTags", () => { it("should work when updating an unset DD_TAGS", async () => { expect(process.env.DD_TAGS).toBeUndefined(); - updateDDTags("hello:world") + updateDDTags("hello:world"); expect(process.env.DD_TAGS).toEqual("hello:world"); }); it("should work when updating a valid DD_TAGS", async () => { - process.env.DD_TAGS = "datadog:bits" - updateDDTags("hello:world") + process.env.DD_TAGS = "datadog:bits"; + updateDDTags("hello:world"); expect(process.env.DD_TAGS).toEqual("datadog:bits,hello:world"); }); it("should work when updating a valid DD_TAGS and comma at the end", async () => { - process.env.DD_TAGS = "datadog:bits," - updateDDTags("hello:world") + process.env.DD_TAGS = "datadog:bits,"; + updateDDTags("hello:world"); expect(process.env.DD_TAGS).toEqual("datadog:bits,hello:world"); }); -}); \ No newline at end of file +}); diff --git a/src/utils/dd_tags.ts b/src/utils/dd_tags.ts index f00ff70d..4d5d710d 100644 --- a/src/utils/dd_tags.ts +++ b/src/utils/dd_tags.ts @@ -1,5 +1,5 @@ export function updateDDTags(newPair: string): void { - const tags = process.env.DD_TAGS ?? ''; - const updatedEnv = tags ? `${tags.trim().replace(/,+$/, '')},${newPair}` : newPair; - process.env.DD_TAGS = updatedEnv; + const tags = process.env.DD_TAGS ?? ""; + const updatedEnv = tags ? `${tags.trim().replace(/,+$/, "")},${newPair}` : newPair; + process.env.DD_TAGS = updatedEnv; } From 40ed022a3beedad5046a6f73d0924c838666debb Mon Sep 17 00:00:00 2001 From: William Conti Date: Thu, 1 Aug 2024 14:01:48 -0400 Subject: [PATCH 3/3] update tags code object instead --- src/runtime/module_importer.js | 4 ++-- src/utils/dd_tags.spec.ts | 12 ++++++------ src/utils/dd_tags.ts | 15 +++++++++++---- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/src/runtime/module_importer.js b/src/runtime/module_importer.js index 2a48eebe..9732e022 100644 --- a/src/runtime/module_importer.js +++ b/src/runtime/module_importer.js @@ -13,8 +13,8 @@ exports.initTracer = function () { const path = require.resolve("dd-trace", { paths: ["/var/task/node_modules", ...module.paths] }); // tslint:disable-next-line:no-var-requires // add lambda tags to DD_TAGS environment variable - updateDDTags("_dd.origin:lambda") - const tracer = require(path).init(); + const ddtags = updateDDTags({"_dd.origin": "lambda"}) + const tracer = require(path).init({tags: ddtags}); logDebug("automatically initialized dd-trace"); // Configure the tracer to ignore HTTP calls made from the Lambda Library to the Extension diff --git a/src/utils/dd_tags.spec.ts b/src/utils/dd_tags.spec.ts index 29fc1c66..8b13ff73 100644 --- a/src/utils/dd_tags.spec.ts +++ b/src/utils/dd_tags.spec.ts @@ -3,19 +3,19 @@ import { updateDDTags } from "./dd_tags"; describe("updateDDTags", () => { it("should work when updating an unset DD_TAGS", async () => { expect(process.env.DD_TAGS).toBeUndefined(); - updateDDTags("hello:world"); - expect(process.env.DD_TAGS).toEqual("hello:world"); + const tags = updateDDTags({ hello: "world" }); + expect(tags).toEqual({ hello: "world" }); }); it("should work when updating a valid DD_TAGS", async () => { process.env.DD_TAGS = "datadog:bits"; - updateDDTags("hello:world"); - expect(process.env.DD_TAGS).toEqual("datadog:bits,hello:world"); + const tags = updateDDTags({ hello: "world" }); + expect(tags).toEqual({ datadog: "bits", hello: "world" }); }); it("should work when updating a valid DD_TAGS and comma at the end", async () => { process.env.DD_TAGS = "datadog:bits,"; - updateDDTags("hello:world"); - expect(process.env.DD_TAGS).toEqual("datadog:bits,hello:world"); + const tags = updateDDTags({ hello: "world" }); + expect(tags).toEqual({ datadog: "bits", hello: "world" }); }); }); diff --git a/src/utils/dd_tags.ts b/src/utils/dd_tags.ts index 4d5d710d..7a80e318 100644 --- a/src/utils/dd_tags.ts +++ b/src/utils/dd_tags.ts @@ -1,5 +1,12 @@ -export function updateDDTags(newPair: string): void { - const tags = process.env.DD_TAGS ?? ""; - const updatedEnv = tags ? `${tags.trim().replace(/,+$/, "")},${newPair}` : newPair; - process.env.DD_TAGS = updatedEnv; +export function updateDDTags(newTags: Record = {}): Record { + const envTags = (process.env.DD_TAGS ?? "") + .split(",") + .filter((pair) => pair.includes(":")) + .reduce((acc: Record, pair: string) => { + const [key, value] = pair.split(":"); + if (key && value) acc[key] = value; + return acc; + }, {}); + + return { ...envTags, ...newTags }; }