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

Adds support for undici tracing #1642

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
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
10 changes: 10 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,11 @@ jobs:
environment:
- PLUGINS=sharedb

node-undici:
<<: *node-plugin-base
docker:
- image: node:<< parameters.node-version >>

node-when: *node-plugin-base

node-winston: *node-plugin-base
Expand Down Expand Up @@ -818,6 +823,10 @@ workflows:
- node-router: *matrix-supported-node-versions
- node-sharedb: *matrix-supported-node-versions
- node-tedious: *matrix-supported-node-versions
- node-undici:
matrix:
parameters:
node-version: ["14.9", "16"]
- node-when: *matrix-supported-node-versions
- node-winston: *matrix-supported-node-versions
- codecov:
Expand Down Expand Up @@ -873,6 +882,7 @@ workflows:
- node-router
- node-sharedb
- node-tedious
- node-undici
- node-when
- node-winston
bench: &bench-jobs
Expand Down
1 change: 1 addition & 0 deletions LICENSE-3rdparty.csv
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ require,retry,MIT,Copyright 2011 Tim Koschützki Felix Geisendörfer
require,semver,ISC,Copyright Isaac Z. Schlueter and Contributors
require,source-map,BSD-3-Clause,Copyright 2009-2011 Mozilla Foundation and contributors
require,source-map-resolve,MIT,Copyright 2014-2020 Simon Lydell 2019 Jinxiang
dev,abort-controller,MIT,Copyright (c) 2017 Toru Nagashima
dev,autocannon,MIT,Copyright 2016 Matteo Collina
dev,axios,MIT,Copyright 2014-present Matt Zabriskie
dev,benchmark,MIT,Copyright 2010-2016 Mathias Bynens Robert Kieffer John-David Dalton
Expand Down
2 changes: 2 additions & 0 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,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 @@ -138,6 +139,7 @@ tracer.use('pg', {
* [restify](./interfaces/plugins.restify.html)
* [router](./interfaces/plugins.router.html)
* [tedious](./interfaces/plugins.tedious.html)
* [undici](./interfaces/plugins.undici.html)
* [when](./interfaces/plugins.when.html)
* [winston](./interfaces/plugins.winston.html)

Expand Down
1 change: 1 addition & 0 deletions docs/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ tracer.use('rhea');
tracer.use('router');
tracer.use('sharedb', sharedbOptions);
tracer.use('tedious');
tracer.use('undici', httpClientOptions);
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 @@ -513,6 +513,7 @@ interface Plugins {
"router": plugins.router;
"sharedb": plugins.sharedb;
"tedious": plugins.tedious;
"undici": plugins.undici;
"winston": plugins.winston;
}

Expand Down Expand Up @@ -1282,6 +1283,12 @@ declare namespace plugins {
*/
interface tedious extends Instrumentation {}

/**
* This plugin automatically instruments
* [undici](https://github.com/nodejs/undici/)
*/
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 package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
"source-map-resolve": "^0.6.0"
},
"devDependencies": {
"abort-controller": "^3.0.0",
"autocannon": "^4.5.2",
"axios": "^0.21.2",
"benchmark": "^2.1.4",
Expand Down
110 changes: 5 additions & 105 deletions packages/datadog-plugin-http/src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ const log = require('../../dd-trace/src/log')
const tags = require('../../../ext/tags')
const kinds = require('../../../ext/kinds')
const formats = require('../../../ext/formats')
const urlFilter = require('../../dd-trace/src/plugins/util/urlfilter')
const analyticsSampler = require('../../dd-trace/src/analytics_sampler')
const { storage } = require('../../datadog-core')
const { addErrorToSpan, getServiceName, hasAmazonSignature, client } = require('../../dd-trace/src/plugins/util/web')

const HTTP_HEADERS = formats.HTTP_HEADERS
const HTTP_STATUS_CODE = tags.HTTP_STATUS_CODE
Expand All @@ -17,7 +17,7 @@ const SPAN_KIND = tags.SPAN_KIND
const CLIENT = kinds.CLIENT

function patch (http, methodName, tracer, config) {
config = normalizeConfig(tracer, config)
config = normalizeConfig(config)
this.wrap(http, methodName, fn => makeRequestTrace(fn))

function makeRequestTrace (request) {
Expand Down Expand Up @@ -84,7 +84,7 @@ function patch (http, methodName, tracer, config) {
break
}
case 'error':
addError(span, arg)
addErrorToSpan(span, arg)
case 'abort': // eslint-disable-line no-fallthrough
case 'timeout': // eslint-disable-line no-fallthrough
finish(req, null, span, config)
Expand Down Expand Up @@ -119,16 +119,6 @@ function patch (http, methodName, tracer, config) {
span.finish()
}

function addError (span, error) {
span.addTags({
'error.type': error.name,
'error.msg': error.message,
'error.stack': error.stack
})

return error
}

function addRequestHeaders (req, span, config) {
config.headers.forEach(key => {
const value = req.getHeader(key)
Expand Down Expand Up @@ -220,105 +210,15 @@ function patch (http, methodName, tracer, config) {
}
}

function getHost (options) {
if (typeof options === 'string') {
return url.parse(options).host
}

const hostname = options.hostname || options.host || 'localhost'
const port = options.port

return [hostname, port].filter(val => val).join(':')
}

function getServiceName (tracer, config, options) {
if (config.splitByDomain) {
return getHost(options)
} else if (config.service) {
return config.service
}

return `${tracer._service}-http-client`
}

function hasAmazonSignature (options) {
if (!options) {
return false
}

if (options.headers) {
const headers = Object.keys(options.headers)
.reduce((prev, next) => Object.assign(prev, {
[next.toLowerCase()]: options.headers[next]
}), {})

if (headers['x-amz-signature']) {
return true
}

if ([].concat(headers['authorization']).some(startsWith('AWS4-HMAC-SHA256'))) {
return true
}
}

return options.path && options.path.toLowerCase().indexOf('x-amz-signature=') !== -1
}

function startsWith (searchString) {
return value => String(value).startsWith(searchString)
}

function unpatch (http) {
this.unwrap(http, 'request')
this.unwrap(http, 'get')
}

function getStatusValidator (config) {
if (typeof config.validateStatus === 'function') {
return config.validateStatus
} else if (config.hasOwnProperty('validateStatus')) {
log.error('Expected `validateStatus` to be a function.')
}
return code => code < 400 || code >= 500
}

function getFilter (config) {
config = Object.assign({}, config, {
blocklist: config.blocklist || []
})

return urlFilter.getFilter(config)
}

function normalizeConfig (tracer, config) {
function normalizeConfig (config) {
config = config.client || config

const validateStatus = getStatusValidator(config)
const propagationFilter = getFilter({ blocklist: config.propagationBlocklist })
const headers = getHeaders(config)
const hooks = getHooks(config)

return Object.assign({}, config, {
validateStatus,
propagationFilter,
headers,
hooks
})
}

function getHeaders (config) {
if (!Array.isArray(config.headers)) return []

return config.headers
.filter(key => typeof key === 'string')
.map(key => key.toLowerCase())
}

function getHooks (config) {
const noop = () => {}
const request = (config.hooks && config.hooks.request) || noop

return { request }
return client.normalizeConfig(config)
}

module.exports = [
Expand Down
Loading