Skip to content

Commit c9b9d08

Browse files
authored
lazy load fetch instrumentation only when needed (#5317)
1 parent 2070869 commit c9b9d08

3 files changed

Lines changed: 39 additions & 14 deletions

File tree

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,33 @@
11
'use strict'
22

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')
64

75
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
127

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+
}
1433
}

packages/dd-trace/src/config.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const { GIT_REPOSITORY_URL, GIT_COMMIT_SHA } = require('./plugins/util/tags')
1616
const { getGitMetadataFromGitProperties, removeUserSensitiveInfo } = require('./git_properties')
1717
const { updateConfig } = require('./telemetry')
1818
const telemetryMetrics = require('./telemetry/metrics')
19-
const { getIsGCPFunction, getIsAzureFunction } = require('./serverless')
19+
const { isInServerlessEnvironment, getIsGCPFunction, getIsAzureFunction } = require('./serverless')
2020
const { ORIGIN_KEY, GRPC_CLIENT_ERROR_STATUSES, GRPC_SERVER_ERROR_STATUSES } = require('./constants')
2121
const { appendRules } = require('./payload-tagging/config')
2222

@@ -419,10 +419,7 @@ class Config {
419419
}
420420

421421
_isInServerlessEnvironment () {
422-
const inAWSLambda = process.env.AWS_LAMBDA_FUNCTION_NAME !== undefined
423-
const isGCPFunction = getIsGCPFunction()
424-
const isAzureFunction = getIsAzureFunction()
425-
return inAWSLambda || isGCPFunction || isAzureFunction
422+
return isInServerlessEnvironment()
426423
}
427424

428425
// for _merge to work, every config value must have a default value

packages/dd-trace/src/serverless.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,18 @@ function getIsAzureFunction () {
6060
return isAzureFunction
6161
}
6262

63+
function isInServerlessEnvironment () {
64+
const inAWSLambda = process.env.AWS_LAMBDA_FUNCTION_NAME !== undefined
65+
const isGCPFunction = getIsGCPFunction()
66+
const isAzureFunction = getIsAzureFunction()
67+
68+
return inAWSLambda || isGCPFunction || isAzureFunction
69+
}
70+
6371
module.exports = {
6472
maybeStartServerlessMiniAgent,
6573
getIsGCPFunction,
6674
getIsAzureFunction,
67-
getRustBinaryPath
75+
getRustBinaryPath,
76+
isInServerlessEnvironment
6877
}

0 commit comments

Comments
 (0)