|
1 | 1 | import { TracerWrapper } from "../../tracer-wrapper"; |
2 | 2 | import { HTTPEventSubType, HTTPEventTraceExtractor } from "./http"; |
| 3 | +const albMultivalueHeadersEvent = require("../../../../event_samples/application-load-balancer-multivalue-headers.json"); |
3 | 4 |
|
4 | 5 | let mockSpanContext: any = null; |
5 | 6 |
|
@@ -97,6 +98,66 @@ describe("HTTPEventTraceExtractor", () => { |
97 | 98 | expect(traceContext?.source).toBe("event"); |
98 | 99 | }); |
99 | 100 |
|
| 101 | + it("extracts trace context from payload with multiValueHeaders", () => { |
| 102 | + mockSpanContext = { |
| 103 | + toTraceId: () => "123", |
| 104 | + toSpanId: () => "456", |
| 105 | + _sampling: { priority: "1" }, |
| 106 | + }; |
| 107 | + const tracerWrapper = new TracerWrapper(); |
| 108 | + const payload = { |
| 109 | + multiValueHeaders: { |
| 110 | + "X-Datadog-Trace-Id": ["123", "789"], |
| 111 | + "X-Datadog-Parent-Id": ["456"], |
| 112 | + "X-Datadog-Sampling-Priority": ["1"], |
| 113 | + }, |
| 114 | + }; |
| 115 | + const extractor = new HTTPEventTraceExtractor(tracerWrapper); |
| 116 | + const traceContext = extractor.extract(payload); |
| 117 | + |
| 118 | + expect(traceContext).not.toBeNull(); |
| 119 | + expect(spyTracerWrapper).toHaveBeenCalledWith({ |
| 120 | + "x-datadog-trace-id": "123", |
| 121 | + "x-datadog-parent-id": "456", |
| 122 | + "x-datadog-sampling-priority": "1", |
| 123 | + }); |
| 124 | + |
| 125 | + expect(traceContext?.toTraceId()).toBe("123"); |
| 126 | + expect(traceContext?.toSpanId()).toBe("456"); |
| 127 | + expect(traceContext?.sampleMode()).toBe("1"); |
| 128 | + }); |
| 129 | + |
| 130 | + it("flattens a real ALB multiValueHeaders payload into a lowercase, single-value map", () => { |
| 131 | + const tracerWrapper = new TracerWrapper(); |
| 132 | + const extractor = new HTTPEventTraceExtractor(tracerWrapper); |
| 133 | + |
| 134 | + spyTracerWrapper.mockClear(); |
| 135 | + extractor.extract(albMultivalueHeadersEvent); |
| 136 | + expect(spyTracerWrapper).toHaveBeenCalled(); |
| 137 | + |
| 138 | + const captured = spyTracerWrapper.mock.calls[0][0] as Record<string, string>; |
| 139 | + |
| 140 | + expect(captured).toEqual({ |
| 141 | + accept: "*/*", |
| 142 | + "accept-encoding": "gzip, deflate", |
| 143 | + "accept-language": "*", |
| 144 | + connection: "keep-alive", |
| 145 | + host: "nhulston-test-0987654321.us-east-1.elb.amazonaws.com", |
| 146 | + "sec-fetch-mode": "cors", |
| 147 | + "user-agent": "node", |
| 148 | + traceparent: "00-68126c4300000000125a7f065cf9a530-1c6dcc8ab8a6e99d-01", |
| 149 | + tracestate: "dd=t.dm:-0;t.tid:68126c4300000000;s:1;p:1c6dcc8ab8a6e99d", |
| 150 | + "x-amzn-trace-id": "Root=1-68126c45-01b175997ab51c4c47a2d643", |
| 151 | + "x-datadog-tags": "_dd.p.tid=68126c4300000000,_dd.p.dm=-0", |
| 152 | + "x-datadog-sampling-priority": "1", |
| 153 | + "x-datadog-trace-id": "0987654321", |
| 154 | + "x-datadog-parent-id": "1234567890", |
| 155 | + "x-forwarded-for": "18.204.55.6", |
| 156 | + "x-forwarded-port": "80", |
| 157 | + "x-forwarded-proto": "http", |
| 158 | + }); |
| 159 | + }); |
| 160 | + |
100 | 161 | it("extracts trace context from payload with authorizer", () => { |
101 | 162 | mockSpanContext = { |
102 | 163 | toTraceId: () => "2389589954026090296", |
|
0 commit comments