Skip to content

Commit

Permalink
fix fetch error when request has already been sent (#4258)
Browse files Browse the repository at this point in the history
  • Loading branch information
rochdev authored and tlhunter committed Apr 25, 2024
1 parent c2ce4bb commit 88c5d53
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
13 changes: 9 additions & 4 deletions packages/datadog-instrumentations/src/helpers/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
}
7 changes: 5 additions & 2 deletions packages/datadog-plugin-fetch/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 88c5d53

Please sign in to comment.