From 88c5d53b996907f2b818927e0769efe14b62cae4 Mon Sep 17 00:00:00 2001 From: Roch Devost Date: Tue, 23 Apr 2024 17:48:39 -0400 Subject: [PATCH] fix fetch error when request has already been sent (#4258) --- .../datadog-instrumentations/src/helpers/fetch.js | 13 +++++++++---- packages/datadog-plugin-fetch/src/index.js | 7 +++++-- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/packages/datadog-instrumentations/src/helpers/fetch.js b/packages/datadog-instrumentations/src/helpers/fetch.js index 3d383d9c322..0ae1f821e9e 100644 --- a/packages/datadog-instrumentations/src/helpers/fetch.js +++ b/packages/datadog-instrumentations/src/helpers/fetch.js @@ -7,11 +7,16 @@ exports.createWrapFetch = function createWrapFetch (Request, ch) { return function (input, init) { if (!ch.start.hasSubscribers) return fetch.apply(this, arguments) - const req = new Request(input, init) - const headers = req.headers - const ctx = { req, headers } + if (input instanceof Request) { + const ctx = { req: input } - return ch.tracePromise(() => fetch.call(this, req, { headers: ctx.headers }), ctx) + return ch.tracePromise(() => fetch.call(this, input, init), ctx) + } else { + const req = new Request(input, init) + const ctx = { req } + + return ch.tracePromise(() => fetch.call(this, req), ctx) + } } } } diff --git a/packages/datadog-plugin-fetch/src/index.js b/packages/datadog-plugin-fetch/src/index.js index 69f7b170fdb..44173a561ca 100644 --- a/packages/datadog-plugin-fetch/src/index.js +++ b/packages/datadog-plugin-fetch/src/index.js @@ -17,8 +17,11 @@ class FetchPlugin extends HttpClientPlugin { const store = super.bindStart(ctx) - ctx.headers = headers - ctx.req = new globalThis.Request(req, { headers }) + for (const name in headers) { + if (!req.headers.has(name)) { + req.headers.set(name, headers[name]) + } + } return store }