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

Only wrap the handler once #57

Merged
merged 5 commits into from
Mar 9, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
21 changes: 12 additions & 9 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe("datadog", () => {
nock("http://www.example.com")
.get("/")
.reply(200, {});
const wrapped = datadog(handler);
const wrapped = datadog(handler, true);
await wrapped(
{
headers: {
Expand All @@ -78,7 +78,7 @@ describe("datadog", () => {
nock("http://www.example.com")
.get("/")
.reply(200, {});
const wrapped = datadog(handler, { autoPatchHTTP: false });
const wrapped = datadog(handler, true, { autoPatchHTTP: false });
await wrapped(
{
headers: {
Expand Down Expand Up @@ -110,7 +110,7 @@ describe("datadog", () => {
const wrapped = datadog(async () => {
sendDistributionMetric("my-dist", 100, "first-tag", "second-tag");
return "";
});
}, true);
await wrapped({}, {} as any, () => {});

expect(nock.isDone()).toBeTruthy();
Expand All @@ -131,6 +131,7 @@ describe("datadog", () => {
sendDistributionMetric("my-dist", 100, "first-tag", "second-tag");
return "";
},
true,
{ apiKey },
);
await wrapped({}, {} as any, () => {});
Expand All @@ -153,6 +154,7 @@ describe("datadog", () => {
sendDistributionMetric("my-dist", 100, "first-tag", "second-tag");
return "";
},
true,
{ apiKey },
);
await wrapped({}, {} as any, () => {});
Expand All @@ -173,7 +175,7 @@ describe("datadog", () => {
const wrapped = datadog(async () => {
traceHeaders = getTraceHeaders();
return "";
});
}, true);
await wrapped(event, {} as any, () => {});
expect(traceHeaders).toEqual({
"x-datadog-parent-id": "9101112",
Expand All @@ -197,6 +199,7 @@ describe("datadog", () => {
console.log("Hello");
return "";
},
true,
{ injectLogContext: true },
);

Expand All @@ -219,14 +222,14 @@ describe("datadog", () => {
const wrapped = datadog(async () => {
console.log("Hello");
return "";
});
}, true);

await wrapped(event, {} as any, () => {});
expect(spy).toHaveBeenCalledWith("[dd.trace_id=123456 dd.span_id=9101112] Hello");
});

it("increments invocations for each function call", async () => {
const wrapped = datadog(handler);
const wrapped = datadog(handler, true);

await wrapped({}, mockContext, () => {});

Expand All @@ -245,7 +248,7 @@ describe("datadog", () => {
throw Error("Some error");
};

const wrappedHandler = datadog(handlerError);
const wrappedHandler = datadog(handlerError, true);

const result = wrappedHandler({}, mockContext, () => {});
await expect(result).rejects.toEqual(Error("Some error"));
Expand All @@ -262,7 +265,7 @@ describe("datadog", () => {
throw Error("Some error");
};

const wrappedHandler = datadog(handlerError, { enhancedMetrics: false });
const wrappedHandler = datadog(handlerError, true, { enhancedMetrics: false });

const result = wrappedHandler({}, mockContext, () => {});
await expect(result).rejects.toEqual(Error("Some error"));
Expand All @@ -278,7 +281,7 @@ describe("datadog", () => {
throw Error("Some error");
};

const wrappedHandler = datadog(handlerError);
const wrappedHandler = datadog(handlerError, true);

const result = wrappedHandler({}, mockContext, () => {});
await expect(result).rejects.toEqual(Error("Some error"));
Expand Down
12 changes: 11 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,13 @@ export const defaultConfig: Config = {
let currentMetricsListener: MetricsListener | undefined;
let currentTraceListener: TraceListener | undefined;

let wrapped = false;

/**
* Wraps your AWS lambda handler functions to add tracing/metrics support
* @param handler A lambda handler function.
* @param config Configuration options for datadog.
* @param forceWrap Force to wrap.
* @param config Configuration options for datadog.
* @returns A wrapped handler function.
*
* ```javascript
Expand All @@ -65,6 +68,7 @@ let currentTraceListener: TraceListener | undefined;
*/
export function datadog<TEvent, TResult>(
handler: Handler<TEvent, TResult>,
forceWrap = false,
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we add forceWrap to the config object instead? Changing the signature of this handler means any customer using the config parameter will have to update their code. It also means we have to go in and update the serverless-plugin to use the new property.

config?: Partial<Config>,
): Handler<TEvent, TResult> {
const finalConfig = getConfig(config);
Expand All @@ -73,6 +77,12 @@ export function datadog<TEvent, TResult>(
const traceListener = new TraceListener(finalConfig, handlerName);
const listeners = [metricsListener, traceListener];

// Only wrap the handler once
if (wrapped && !forceWrap) {
return handler;
}
wrapped = true;

return wrap(
handler,
(event, context) => {
Expand Down