Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for global fetch #3258

Merged
merged 4 commits into from
Jun 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion .github/workflows/plugins.yml
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,23 @@ jobs:
uses: ./.github/actions/testagent/logs
- uses: codecov/codecov-action@v2

fetch:
runs-on: ubuntu-latest
env:
PLUGINS: fetch
steps:
- uses: actions/checkout@v2
- uses: ./.github/actions/testagent/start
- uses: ./.github/actions/node/setup
- run: yarn install
- uses: ./.github/actions/node/oldest
- run: yarn test:plugins:ci
- uses: ./.github/actions/node/latest
- run: yarn test:plugins:ci
- if: always()
uses: ./.github/actions/testagent/logs
- uses: codecov/codecov-action@v2

generic-pool:
runs-on: ubuntu-latest
env:
Expand Down Expand Up @@ -802,7 +819,7 @@ jobs:
- 5500:5500
testagent:
image: ghcr.io/datadog/dd-apm-test-agent/ddapm-test-agent:latest
env:
env:
LOG_LEVEL: DEBUG
TRACE_LANGUAGE: javascript
DISABLED_CHECKS: trace_content_length
Expand Down
2 changes: 2 additions & 0 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ tracer.use('pg', {
<h5 id="generic-pool"></h5>
<h5 id="google-cloud-pubsub"></h5>
<h5 id="fastify"></h5>
<h5 id="fetch"></h5>
<h5 id="graphql"></h5>
<h5 id="graphql-tags"></h5>
<h5 id="graphql-config"></h5>
Expand Down Expand Up @@ -110,6 +111,7 @@ tracer.use('pg', {
* [elasticsearch](./interfaces/plugins.elasticsearch.html)
* [express](./interfaces/plugins.express.html)
* [fastify](./interfaces/plugins.fastify.html)
* [fetch](./interfaces/plugins.fetch.html)
* [generic-pool](./interfaces/plugins.generic_pool.html)
* [google-cloud-pubsub](./interfaces/plugins.google_cloud_pubsub.html)
* [graphql](./interfaces/plugins.graphql.html)
Expand Down
2 changes: 2 additions & 0 deletions docs/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,8 @@ tracer.use('express');
tracer.use('express', httpServerOptions);
tracer.use('fastify');
tracer.use('fastify', httpServerOptions);
tracer.use('fetch');
tracer.use('fetch', httpClientOptions);
tracer.use('generic-pool');
tracer.use('google-cloud-pubsub');
tracer.use('graphql');
Expand Down
7 changes: 7 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,7 @@ interface Plugins {
"elasticsearch": plugins.elasticsearch;
"express": plugins.express;
"fastify": plugins.fastify;
"fetch": plugins.fetch;
"generic-pool": plugins.generic_pool;
"google-cloud-pubsub": plugins.google_cloud_pubsub;
"graphql": plugins.graphql;
Expand Down Expand Up @@ -1092,6 +1093,12 @@ declare namespace plugins {
*/
interface fastify extends HttpServer {}

/**
* This plugin automatically instruments the
* [fetch](https://nodejs.org/api/globals.html#fetch) global.
*/
interface fetch extends HttpClient {}

/**
* This plugin patches the [generic-pool](https://github.com/coopernurse/node-pool)
* module to bind the callbacks the the caller context.
Expand Down
48 changes: 48 additions & 0 deletions packages/datadog-instrumentations/src/fetch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
'use strict'

const shimmer = require('../../datadog-shimmer')
const { channel } = require('./helpers/instrument')

const startChannel = channel('apm:fetch:request:start')
const finishChannel = channel('apm:fetch:request:finish')
const errorChannel = channel('apm:fetch:request:error')

function wrapFetch (fetch, Request) {
if (typeof fetch !== 'function') return fetch

return function (input, init) {
if (!startChannel.hasSubscribers) return fetch.apply(this, arguments)

const req = new Request(input, init)
const headers = req.headers
const message = { req, headers }

startChannel.publish(message)

// Request object is read-only so we need new objects to change headers.
arguments[0] = message.req
arguments[1] = { headers: message.headers }

return fetch.apply(this, arguments)
.then(
res => {
finishChannel.publish({ req, res })

return res
},
err => {
if (err.name !== 'AbortError') {
errorChannel.publish(err)
}

finishChannel.publish({ req })

throw err
}
)
}
}

if (globalThis.fetch) {
globalThis.fetch = shimmer.wrap(fetch, wrapFetch(fetch, globalThis.Request))
}
3 changes: 3 additions & 0 deletions packages/datadog-instrumentations/src/helpers/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ const pathSepExpr = new RegExp(`\\${path.sep}`, 'g')

const loadChannel = channel('dd-trace:instrumentation:load')

// Globals
require('../fetch')

// TODO: make this more efficient

for (const packageName of names) {
Expand Down
36 changes: 36 additions & 0 deletions packages/datadog-plugin-fetch/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict'

const HttpClientPlugin = require('../../datadog-plugin-http/src/client')
const { HTTP_HEADERS } = require('../../../ext/formats')

class FetchPlugin extends HttpClientPlugin {
static get id () { return 'fetch' }

addTraceSub (eventName, handler) {
this.addSub(`apm:${this.constructor.id}:${this.operation}:${eventName}`, handler)
}

start (message) {
const req = message.req
const options = new URL(req.url)
const headers = options.headers = Object.fromEntries(req.headers.entries())

const args = { options }

super.start({ args })

message.req = new globalThis.Request(req, { headers })
}

_inject (span, headers) {
const carrier = {}

this.tracer.inject(span, HTTP_HEADERS, carrier)

for (const name in carrier) {
headers.append(name, carrier[name])
}
}
}

module.exports = FetchPlugin
Loading