|
1 | 1 | 'use strict' |
2 | 2 |
|
3 | | -const shimmer = require('../../datadog-shimmer') |
4 | | -const { channel, tracingChannel } = require('dc-polyfill') |
5 | | -const { createWrapFetch } = require('./helpers/fetch') |
| 3 | +const { isInServerlessEnvironment } = require('../../dd-trace/src/serverless') |
6 | 4 |
|
7 | 5 | if (globalThis.fetch) { |
8 | | - const ch = tracingChannel('apm:fetch:request') |
9 | | - const wrapFetch = createWrapFetch(globalThis.Request, ch, () => { |
10 | | - channel('dd-trace:instrumentation:load').publish({ name: 'fetch' }) |
11 | | - }) |
| 6 | + const globalFetch = globalThis.fetch |
12 | 7 |
|
13 | | - globalThis.fetch = shimmer.wrapFunction(fetch, fetch => wrapFetch(fetch)) |
| 8 | + let fetch = (input, init) => { |
| 9 | + wrapRealFetch() |
| 10 | + |
| 11 | + return fetch(input, init) |
| 12 | + } |
| 13 | + |
| 14 | + function wrapRealFetch () { |
| 15 | + const { channel, tracingChannel } = require('dc-polyfill') |
| 16 | + const { createWrapFetch } = require('./helpers/fetch') |
| 17 | + |
| 18 | + const ch = tracingChannel('apm:fetch:request') |
| 19 | + const wrapFetch = createWrapFetch(globalThis.Request, ch, () => { |
| 20 | + channel('dd-trace:instrumentation:load').publish({ name: 'fetch' }) |
| 21 | + }) |
| 22 | + |
| 23 | + fetch = wrapFetch(globalFetch) |
| 24 | + } |
| 25 | + |
| 26 | + if (!isInServerlessEnvironment()) { |
| 27 | + wrapRealFetch() |
| 28 | + } |
| 29 | + |
| 30 | + globalThis.fetch = function value (input, init) { |
| 31 | + return fetch(input, init) |
| 32 | + } |
14 | 33 | } |
0 commit comments