diff --git a/src/runtime/module_importer.js b/src/runtime/module_importer.js index 0ee71f47..9732e022 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 + 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 new file mode 100644 index 00000000..8b13ff73 --- /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(); + 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"; + 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,"; + 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 new file mode 100644 index 00000000..7a80e318 --- /dev/null +++ b/src/utils/dd_tags.ts @@ -0,0 +1,12 @@ +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 }; +} 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";