Skip to content

Commit 5fb8fd2

Browse files
committed
fix fetch not working in serverless environments (#5366)
1 parent 007f67a commit 5fb8fd2

File tree

4 files changed

+61
-5
lines changed

4 files changed

+61
-5
lines changed

packages/datadog-instrumentations/src/fetch.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ if (globalThis.fetch) {
1717

1818
const ch = tracingChannel('apm:fetch:request')
1919
const wrapFetch = createWrapFetch(globalThis.Request, ch, () => {
20-
channel('dd-trace:instrumentation:load').publish({ name: 'fetch' })
20+
channel('dd-trace:instrumentation:load').publish({ name: 'global:fetch' })
2121
})
2222

2323
fetch = wrapFetch(globalFetch)

packages/datadog-plugin-fetch/test/index.spec.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,5 +609,64 @@ describe('Plugin', function () {
609609
})
610610
})
611611
})
612+
613+
describe('in serverless', () => {
614+
beforeEach(() => {
615+
process.env.DD_TRACE_EXPERIMENTAL_EXPORTER = 'agent'
616+
process.env.AWS_LAMBDA_FUNCTION_NAME = 'test'
617+
})
618+
619+
beforeEach(() => {
620+
return agent.load('fetch')
621+
.then(() => {
622+
express = require('express')
623+
fetch = globalThis.fetch
624+
})
625+
})
626+
627+
beforeEach(() => {
628+
delete process.env.DD_TRACE_EXPERIMENTAL_EXPORTER
629+
delete process.env.AWS_LAMBDA_FUNCTION_NAME
630+
})
631+
632+
withNamingSchema(
633+
() => {
634+
const app = express()
635+
app.get('/user', (req, res) => {
636+
res.status(200).send()
637+
})
638+
639+
appListener = server(app, port => {
640+
fetch(`http://localhost:${port}/user`)
641+
})
642+
},
643+
rawExpectedSchema.client
644+
)
645+
646+
it('should do automatic instrumentation', done => {
647+
const app = express()
648+
app.get('/user', (req, res) => {
649+
res.status(200).send()
650+
})
651+
appListener = server(app, port => {
652+
agent
653+
.use(traces => {
654+
expect(traces[0][0]).to.have.property('service', SERVICE_NAME)
655+
expect(traces[0][0]).to.have.property('type', 'http')
656+
expect(traces[0][0]).to.have.property('resource', 'GET')
657+
expect(traces[0][0].meta).to.have.property('span.kind', 'client')
658+
expect(traces[0][0].meta).to.have.property('http.url', `http://localhost:${port}/user`)
659+
expect(traces[0][0].meta).to.have.property('http.method', 'GET')
660+
expect(traces[0][0].meta).to.have.property('http.status_code', '200')
661+
expect(traces[0][0].meta).to.have.property('component', 'fetch')
662+
expect(traces[0][0].meta).to.have.property('out.host', 'localhost')
663+
})
664+
.then(done)
665+
.catch(done)
666+
667+
fetch(`http://localhost:${port}/user`)
668+
})
669+
})
670+
})
612671
})
613672
})

packages/dd-trace/src/plugin_manager.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,6 @@ module.exports = class PluginManager {
103103
this._tracerConfig = config
104104
this._tracer._nomenclature.configure(config)
105105

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

packages/dd-trace/src/plugins/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ module.exports = {
3939
get express () { return require('../../../datadog-plugin-express/src') },
4040
get fastify () { return require('../../../datadog-plugin-fastify/src') },
4141
get 'find-my-way' () { return require('../../../datadog-plugin-find-my-way/src') },
42+
get 'global:fetch' () { return require('../../../datadog-plugin-fetch/src') },
4243
get graphql () { return require('../../../datadog-plugin-graphql/src') },
4344
get grpc () { return require('../../../datadog-plugin-grpc/src') },
4445
get hapi () { return require('../../../datadog-plugin-hapi/src') },

0 commit comments

Comments
 (0)