|
| 1 | +import { TraceListener } from "./listener"; |
| 2 | +import { Source } from "./constants"; |
| 3 | + |
| 4 | +let mockWrap: jest.Mock<any, any>; |
| 5 | +let mockExtract: jest.Mock<any, any>; |
| 6 | +let mockTraceHeaders: Record<string, string> | undefined = undefined; |
| 7 | +let mockTraceSource: Source | undefined = undefined; |
| 8 | + |
| 9 | +jest.mock("dd-trace", () => { |
| 10 | + mockWrap = jest.fn().mockImplementation((name, options, func) => func); |
| 11 | + mockExtract = jest.fn().mockImplementation((_, val) => val); |
| 12 | + return { |
| 13 | + wrap: mockWrap, |
| 14 | + extract: mockExtract, |
| 15 | + }; |
| 16 | +}); |
| 17 | + |
| 18 | +jest.mock("./trace-context-service", () => { |
| 19 | + class MockTraceContextService { |
| 20 | + get traceSource() { |
| 21 | + return mockTraceSource; |
| 22 | + } |
| 23 | + get currentTraceHeaders() { |
| 24 | + return mockTraceHeaders; |
| 25 | + } |
| 26 | + } |
| 27 | + return { |
| 28 | + TraceContextService: MockTraceContextService, |
| 29 | + }; |
| 30 | +}); |
| 31 | + |
| 32 | +describe("TraceListener", () => { |
| 33 | + const defaultConfig = { autoPatchHTTP: true, mergeDatadogXrayTraces: false }; |
| 34 | + const context = { |
| 35 | + invokedFunctionArn: "arn:aws:lambda:us-east-1:123456789101:function:my-lambda", |
| 36 | + awsRequestId: "1234", |
| 37 | + functionName: "my-lambda", |
| 38 | + }; |
| 39 | + beforeEach(() => { |
| 40 | + mockWrap.mockClear(); |
| 41 | + mockExtract.mockClear(); |
| 42 | + mockTraceHeaders = undefined; |
| 43 | + mockTraceSource = undefined; |
| 44 | + }); |
| 45 | + |
| 46 | + it("wraps dd-trace span around invocation", async () => { |
| 47 | + const listener = new TraceListener(defaultConfig, "handler.my-handler"); |
| 48 | + listener.onStartInvocation({}, context as any); |
| 49 | + const unwrappedFunc = () => {}; |
| 50 | + const wrappedFunc = listener.onWrap(unwrappedFunc); |
| 51 | + wrappedFunc(); |
| 52 | + await listener.onCompleteInvocation(); |
| 53 | + |
| 54 | + expect(mockWrap).toHaveBeenCalledWith( |
| 55 | + "aws.lambda", |
| 56 | + { |
| 57 | + resource: "handler.my-handler", |
| 58 | + tags: { |
| 59 | + cold_start: true, |
| 60 | + function_arn: "arn:aws:lambda:us-east-1:123456789101:function:my-lambda", |
| 61 | + request_id: "1234", |
| 62 | + resource_names: "my-lambda", |
| 63 | + }, |
| 64 | + }, |
| 65 | + unwrappedFunc, |
| 66 | + ); |
| 67 | + }); |
| 68 | + |
| 69 | + it("wraps dd-trace span around invocation, with trace context from event", async () => { |
| 70 | + const listener = new TraceListener(defaultConfig, "handler.my-handler"); |
| 71 | + mockTraceHeaders = { |
| 72 | + "x-datadog-parent-id": "797643193680388251", |
| 73 | + "x-datadog-sampling-priority": "2", |
| 74 | + "x-datadog-trace-id": "4110911582297405551", |
| 75 | + }; |
| 76 | + mockTraceSource = Source.Event; |
| 77 | + listener.onStartInvocation({}, context as any); |
| 78 | + const unwrappedFunc = () => {}; |
| 79 | + const wrappedFunc = listener.onWrap(unwrappedFunc); |
| 80 | + wrappedFunc(); |
| 81 | + await listener.onCompleteInvocation(); |
| 82 | + |
| 83 | + expect(mockWrap).toHaveBeenCalledWith( |
| 84 | + "aws.lambda", |
| 85 | + { |
| 86 | + resource: "handler.my-handler", |
| 87 | + tags: { |
| 88 | + cold_start: true, |
| 89 | + function_arn: "arn:aws:lambda:us-east-1:123456789101:function:my-lambda", |
| 90 | + request_id: "1234", |
| 91 | + resource_names: "my-lambda", |
| 92 | + }, |
| 93 | + childOf: mockTraceHeaders, |
| 94 | + }, |
| 95 | + unwrappedFunc, |
| 96 | + ); |
| 97 | + }); |
| 98 | + |
| 99 | + it("wraps dd-trace span around invocation, without trace context from xray", async () => { |
| 100 | + const listener = new TraceListener(defaultConfig, "handler.my-handler"); |
| 101 | + mockTraceHeaders = { |
| 102 | + "x-datadog-parent-id": "797643193680388251", |
| 103 | + "x-datadog-sampling-priority": "2", |
| 104 | + "x-datadog-trace-id": "4110911582297405551", |
| 105 | + }; |
| 106 | + mockTraceSource = Source.Xray; |
| 107 | + |
| 108 | + listener.onStartInvocation({}, context as any); |
| 109 | + const unwrappedFunc = () => {}; |
| 110 | + const wrappedFunc = listener.onWrap(unwrappedFunc); |
| 111 | + wrappedFunc(); |
| 112 | + await listener.onCompleteInvocation(); |
| 113 | + |
| 114 | + expect(mockWrap).toHaveBeenCalledWith( |
| 115 | + "aws.lambda", |
| 116 | + { |
| 117 | + resource: "handler.my-handler", |
| 118 | + tags: { |
| 119 | + cold_start: true, |
| 120 | + function_arn: "arn:aws:lambda:us-east-1:123456789101:function:my-lambda", |
| 121 | + request_id: "1234", |
| 122 | + resource_names: "my-lambda", |
| 123 | + }, |
| 124 | + }, |
| 125 | + unwrappedFunc, |
| 126 | + ); |
| 127 | + }); |
| 128 | + |
| 129 | + it("wraps dd-trace span around invocation, with trace context from xray when mergeDatadogXrayTraces is enabled", async () => { |
| 130 | + const listener = new TraceListener({ ...defaultConfig, mergeDatadogXrayTraces: true }, "handler.my-handler"); |
| 131 | + mockTraceHeaders = { |
| 132 | + "x-datadog-parent-id": "797643193680388251", |
| 133 | + "x-datadog-sampling-priority": "2", |
| 134 | + "x-datadog-trace-id": "4110911582297405551", |
| 135 | + }; |
| 136 | + mockTraceSource = Source.Xray; |
| 137 | + |
| 138 | + listener.onStartInvocation({}, context as any); |
| 139 | + const unwrappedFunc = () => {}; |
| 140 | + const wrappedFunc = listener.onWrap(unwrappedFunc); |
| 141 | + wrappedFunc(); |
| 142 | + await listener.onCompleteInvocation(); |
| 143 | + |
| 144 | + expect(mockWrap).toHaveBeenCalledWith( |
| 145 | + "aws.lambda", |
| 146 | + { |
| 147 | + resource: "handler.my-handler", |
| 148 | + tags: { |
| 149 | + cold_start: true, |
| 150 | + function_arn: "arn:aws:lambda:us-east-1:123456789101:function:my-lambda", |
| 151 | + request_id: "1234", |
| 152 | + resource_names: "my-lambda", |
| 153 | + }, |
| 154 | + childOf: mockTraceHeaders, |
| 155 | + }, |
| 156 | + unwrappedFunc, |
| 157 | + ); |
| 158 | + }); |
| 159 | +}); |
0 commit comments