Skip to content

Commit 8429c32

Browse files
authored
ensure tracer initialization does not override DD_TAGS (#563)
* initial commit * fix lint * update tags code object instead
1 parent 65a44b2 commit 8429c32

File tree

4 files changed

+38
-6
lines changed

4 files changed

+38
-6
lines changed

src/runtime/module_importer.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
const { logDebug } = require("../utils");
2+
const { logDebug, updateDDTags } = require("../utils");
33

44
// Currently no way to prevent typescript from auto-transpiling import into require,
55
// so we expose a wrapper in js
@@ -12,11 +12,9 @@ exports.initTracer = function () {
1212
// the version provided by the layer
1313
const path = require.resolve("dd-trace", { paths: ["/var/task/node_modules", ...module.paths] });
1414
// tslint:disable-next-line:no-var-requires
15-
const tracer = require(path).init({
16-
tags: {
17-
"_dd.origin": "lambda",
18-
},
19-
});
15+
// add lambda tags to DD_TAGS environment variable
16+
const ddtags = updateDDTags({"_dd.origin": "lambda"})
17+
const tracer = require(path).init({tags: ddtags});
2018
logDebug("automatically initialized dd-trace");
2119

2220
// Configure the tracer to ignore HTTP calls made from the Lambda Library to the Extension

src/utils/dd_tags.spec.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { updateDDTags } from "./dd_tags";
2+
3+
describe("updateDDTags", () => {
4+
it("should work when updating an unset DD_TAGS", async () => {
5+
expect(process.env.DD_TAGS).toBeUndefined();
6+
const tags = updateDDTags({ hello: "world" });
7+
expect(tags).toEqual({ hello: "world" });
8+
});
9+
10+
it("should work when updating a valid DD_TAGS", async () => {
11+
process.env.DD_TAGS = "datadog:bits";
12+
const tags = updateDDTags({ hello: "world" });
13+
expect(tags).toEqual({ datadog: "bits", hello: "world" });
14+
});
15+
16+
it("should work when updating a valid DD_TAGS and comma at the end", async () => {
17+
process.env.DD_TAGS = "datadog:bits,";
18+
const tags = updateDDTags({ hello: "world" });
19+
expect(tags).toEqual({ datadog: "bits", hello: "world" });
20+
});
21+
});

src/utils/dd_tags.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export function updateDDTags(newTags: Record<string, any> = {}): Record<string, any> {
2+
const envTags = (process.env.DD_TAGS ?? "")
3+
.split(",")
4+
.filter((pair) => pair.includes(":"))
5+
.reduce((acc: Record<string, any>, pair: string) => {
6+
const [key, value] = pair.split(":");
7+
if (key && value) acc[key] = value;
8+
return acc;
9+
}, {});
10+
11+
return { ...envTags, ...newTags };
12+
}

src/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ export { wrap, promisifiedHandler } from "./handler";
33
export { Timer } from "./timer";
44
export { logWarning, logError, logDebug, Logger, setLogLevel, setLogger, LogLevel } from "./log";
55
export { tagObject } from "./tag-object";
6+
export { updateDDTags } from "./dd_tags";
67
export { batchItemFailureCount, isBatchItemFailure } from "./batch-item-failures";

0 commit comments

Comments
 (0)