Skip to content

Commit

Permalink
Merge branch 'master' into ugaitz/rasp-metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
uurien authored Jun 11, 2024
2 parents 6f3f6df + 32a7005 commit ae0c3df
Show file tree
Hide file tree
Showing 36 changed files with 1,606 additions and 50 deletions.
17 changes: 17 additions & 0 deletions .github/workflows/plugins.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1245,6 +1245,23 @@ jobs:
uses: ./.github/actions/testagent/logs
- uses: codecov/codecov-action@v3

undici:
runs-on: ubuntu-latest
env:
PLUGINS: undici
steps:
- uses: actions/checkout@v4
- 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@v3

when:
runs-on: ubuntu-latest
env:
Expand Down
9 changes: 4 additions & 5 deletions CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

/packages/dd-trace/src/appsec/ @DataDog/asm-js
/packages/dd-trace/test/appsec/ @DataDog/asm-js
/packages/dd-trace/src/lambda/ @DataDog/serverless
/packages/dd-trace/test/lambda/ @DataDog/serverless
/packages/dd-trace/src/lambda/ @DataDog/serverless-aws
/packages/dd-trace/test/lambda/ @DataDog/serverless-aws

/packages/datadog-plugin-*/ @Datadog/dd-trace-js @Datadog/apm-idm-js
/packages/datadog-instrumentations/ @Datadog/dd-trace-js @Datadog/apm-idm-js
Expand Down Expand Up @@ -42,7 +42,7 @@
/.github/workflows/ci-visibility-performance.yml @DataDog/ci-app-libraries
/.github/workflows/codeql-analysis.yml @DataDog/dd-trace-js
/.github/workflows/core.yml @DataDog/dd-trace-js
/.github/workflows/lambda.yml @DataDog/serverless-apm
/.github/workflows/lambda.yml @DataDog/serverless-aws
/.github/workflows/package-size.yml @DataDog/dd-trace-js
/.github/workflows/plugins.yml @DataDog/dd-trace-js
/.github/workflows/pr-labels.yml @DataDog/dd-trace-js
Expand All @@ -52,8 +52,7 @@
/.github/workflows/release-dev.yml @DataDog/dd-trace-js
/.github/workflows/release-latest.yml @DataDog/dd-trace-js
/.github/workflows/release-proposal.yml @DataDog/dd-trace-js
/.github/workflows/serverless-integration-test.yml @DataDog/serverless
/.github/workflows/serverless-performance.yml @DataDog/serverless-apm @DataDog/serverless
/.github/workflows/serverless-integration-test.yml @DataDog/serverless-aws @DataDog/serverless
/.github/workflows/system-tests.yml @DataDog/asm-js
/.github/workflows/test-k8s-lib-injection.yaml @DataDog/dd-trace-js
/.github/workflows/tracing.yml @DataDog/dd-trace-js
Expand Down
2 changes: 2 additions & 0 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ tracer.use('pg', {
<h5 id="restify-tags"></h5>
<h5 id="restify-config"></h5>
<h5 id="tedious"></h5>
<h5 id="undici"></h5>
<h5 id="when"></h5>
<h5 id="winston"></h5>
<h3 id="integrations-list">Available Plugins</h3>
Expand Down Expand Up @@ -146,6 +147,7 @@ tracer.use('pg', {
* [restify](./interfaces/export_.plugins.restify.html)
* [router](./interfaces/export_.plugins.router.html)
* [tedious](./interfaces/export_.plugins.tedious.html)
* [undici](./interfaces/export_.plugins.undici.html)
* [when](./interfaces/export_.plugins.when.html)
* [winston](./interfaces/export_.plugins.winston.html)

Expand Down
1 change: 1 addition & 0 deletions docs/add-redirects.sh
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ declare -a plugins=(
"restify"
"router"
"tedious"
"undici"
"when"
"winston"
)
Expand Down
1 change: 1 addition & 0 deletions docs/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ tracer.use('selenium');
tracer.use('sharedb');
tracer.use('sharedb', sharedbOptions);
tracer.use('tedious');
tracer.use('undici');
tracer.use('winston');

tracer.use('express', false)
Expand Down
7 changes: 7 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ interface Plugins {
"selenium": tracer.plugins.selenium;
"sharedb": tracer.plugins.sharedb;
"tedious": tracer.plugins.tedious;
"undici": tracer.plugins.undici;
"winston": tracer.plugins.winston;
}

Expand Down Expand Up @@ -1800,6 +1801,12 @@ declare namespace tracer {
*/
interface tedious extends Instrumentation {}

/**
* This plugin automatically instruments the
* [undici](https://github.com/nodejs/undici) module.
*/
interface undici extends HttpClient {}

/**
* This plugin patches the [winston](https://github.com/winstonjs/winston)
* to automatically inject trace identifiers in log records when the
Expand Down
1 change: 1 addition & 0 deletions packages/datadog-instrumentations/src/helpers/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ module.exports = {
sequelize: () => require('../sequelize'),
sharedb: () => require('../sharedb'),
tedious: () => require('../tedious'),
undici: () => require('../undici'),
when: () => require('../when'),
winston: () => require('../winston')
}
98 changes: 98 additions & 0 deletions packages/datadog-instrumentations/src/http/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const startServerCh = channel('apm:http:server:request:start')
const exitServerCh = channel('apm:http:server:request:exit')
const errorServerCh = channel('apm:http:server:request:error')
const finishServerCh = channel('apm:http:server:request:finish')
const startWriteHeadCh = channel('apm:http:server:response:writeHead:start')
const finishSetHeaderCh = channel('datadog:http:server:response:set-header:finish')

const requestFinishedSet = new WeakSet()
Expand All @@ -20,6 +21,9 @@ const httpsNames = ['https', 'node:https']
addHook({ name: httpNames }, http => {
shimmer.wrap(http.ServerResponse.prototype, 'emit', wrapResponseEmit)
shimmer.wrap(http.Server.prototype, 'emit', wrapEmit)
shimmer.wrap(http.ServerResponse.prototype, 'writeHead', wrapWriteHead)
shimmer.wrap(http.ServerResponse.prototype, 'write', wrapWrite)
shimmer.wrap(http.ServerResponse.prototype, 'end', wrapEnd)
return http
})

Expand Down Expand Up @@ -86,3 +90,97 @@ function wrapSetHeader (res) {
}
})
}

function wrapWriteHead (writeHead) {
return function wrappedWriteHead (statusCode, reason, obj) {
if (!startWriteHeadCh.hasSubscribers) {
return writeHead.apply(this, arguments)
}

const abortController = new AbortController()

if (typeof reason !== 'string') {
obj ??= reason
}

// support writeHead(200, ['key1', 'val1', 'key2', 'val2'])
if (Array.isArray(obj)) {
const headers = {}

for (let i = 0; i < obj.length; i += 2) {
headers[obj[i]] = obj[i + 1]
}

obj = headers
}

// this doesn't support explicit duplicate headers, but it's an edge case
const responseHeaders = Object.assign(this.getHeaders(), obj)

startWriteHeadCh.publish({
req: this.req,
res: this,
abortController,
statusCode,
responseHeaders
})

if (abortController.signal.aborted) {
return this
}

return writeHead.apply(this, arguments)
}
}

function wrapWrite (write) {
return function wrappedWrite () {
if (!startWriteHeadCh.hasSubscribers) {
return write.apply(this, arguments)
}

const abortController = new AbortController()

const responseHeaders = this.getHeaders()

startWriteHeadCh.publish({
req: this.req,
res: this,
abortController,
statusCode: this.statusCode,
responseHeaders
})

if (abortController.signal.aborted) {
return true
}

return write.apply(this, arguments)
}
}

function wrapEnd (end) {
return function wrappedEnd () {
if (!startWriteHeadCh.hasSubscribers) {
return end.apply(this, arguments)
}

const abortController = new AbortController()

const responseHeaders = this.getHeaders()

startWriteHeadCh.publish({
req: this.req,
res: this,
abortController,
statusCode: this.statusCode,
responseHeaders
})

if (abortController.signal.aborted) {
return this
}

return end.apply(this, arguments)
}
}
18 changes: 18 additions & 0 deletions packages/datadog-instrumentations/src/undici.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict'

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

const tracingChannel = require('dc-polyfill').tracingChannel
const ch = tracingChannel('apm:undici:fetch')

const { createWrapFetch } = require('./helpers/fetch')

addHook({
name: 'undici',
versions: ['^4.4.1', '5', '>=6.0.0']
}, undici => {
return shimmer.wrap(undici, 'fetch', createWrapFetch(undici.Request, ch))
})
12 changes: 12 additions & 0 deletions packages/datadog-plugin-undici/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict'

const FetchPlugin = require('../../datadog-plugin-fetch/src/index.js')

class UndiciPlugin extends FetchPlugin {
static get id () { return 'undici' }
static get prefix () {
return 'tracing:apm:undici:fetch'
}
}

module.exports = UndiciPlugin
Loading

0 comments on commit ae0c3df

Please sign in to comment.