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 all 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
const ddtags = updateDDTags({"_dd.origin": "lambda"})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Name might differ with the current existing function used in metrics, but might be a good think to refactor in the future

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";
Loading