Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ensure tracer initialization does not override DD_TAGS #563

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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: 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
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
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();
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");
});
});
5 changes: 5 additions & 0 deletions src/utils/dd_tags.ts
Original file line number Diff line number Diff line change
@@ -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;
}
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";
Loading