Skip to content

Commit

Permalink
ensure tracer initialization does not override DD_TAGS (#563)
Browse files Browse the repository at this point in the history
* initial commit

* fix lint

* update tags code object instead
  • Loading branch information
wconti27 authored Aug 1, 2024
1 parent 65a44b2 commit 8429c32
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 6 deletions.
10 changes: 4 additions & 6 deletions src/runtime/module_importer.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
21 changes: 21 additions & 0 deletions src/utils/dd_tags.spec.ts
Original file line number Diff line number Diff line change
@@ -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" });
});
});
12 changes: 12 additions & 0 deletions src/utils/dd_tags.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export function updateDDTags(newTags: Record<string, any> = {}): Record<string, any> {
const envTags = (process.env.DD_TAGS ?? "")
.split(",")
.filter((pair) => pair.includes(":"))
.reduce((acc: Record<string, any>, pair: string) => {
const [key, value] = pair.split(":");
if (key && value) acc[key] = value;
return acc;
}, {});

return { ...envTags, ...newTags };
}
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

0 comments on commit 8429c32

Please sign in to comment.