Skip to content

Commit f97f991

Browse files
authored
only load fetch plugin when fetch is used (#5272)
1 parent f87acc2 commit f97f991

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
'use strict'
22

33
const shimmer = require('../../datadog-shimmer')
4-
const { tracingChannel } = require('dc-polyfill')
4+
const { channel, tracingChannel } = require('dc-polyfill')
55
const { createWrapFetch } = require('./helpers/fetch')
66

77
if (globalThis.fetch) {
88
const ch = tracingChannel('apm:fetch:request')
9-
const wrapFetch = createWrapFetch(globalThis.Request, ch)
9+
const wrapFetch = createWrapFetch(globalThis.Request, ch, () => {
10+
channel('dd-trace:instrumentation:load').publish({ name: 'fetch' })
11+
})
1012

1113
globalThis.fetch = shimmer.wrapFunction(fetch, fetch => wrapFetch(fetch))
1214
}

packages/datadog-instrumentations/src/helpers/fetch.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
'use strict'
22

3-
exports.createWrapFetch = function createWrapFetch (Request, ch) {
3+
exports.createWrapFetch = function createWrapFetch (Request, ch, onLoad) {
44
return function wrapFetch (fetch) {
55
if (typeof fetch !== 'function') return fetch
66

77
return function (input, init) {
8+
if (onLoad) {
9+
onLoad()
10+
onLoad = undefined
11+
}
12+
813
if (!ch.start.hasSubscribers) return fetch.apply(this, arguments)
914

1015
if (input instanceof Request) {

packages/dd-trace/src/plugin_manager.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@ loadChannel.subscribe(({ name }) => {
2828
maybeEnable(plugins[name])
2929
})
3030

31-
// Globals
32-
maybeEnable(require('../../datadog-plugin-fetch/src'))
33-
3431
// Always enabled
3532
maybeEnable(require('../../datadog-plugin-dd-trace-api/src'))
3633

@@ -106,6 +103,10 @@ module.exports = class PluginManager {
106103
this._tracerConfig = config
107104
this._tracer._nomenclature.configure(config)
108105

106+
if (!config._isInServerlessEnvironment?.()) {
107+
maybeEnable(require('../../datadog-plugin-fetch/src'))
108+
}
109+
109110
for (const name in pluginClasses) {
110111
this.loadPlugin(name)
111112
}

0 commit comments

Comments
 (0)