Skip to content

Commit

Permalink
#316: Adds support for http/https APIs for node >= 10
Browse files Browse the repository at this point in the history
  • Loading branch information
kennethaasan authored and nijotz committed Feb 21, 2020
1 parent 94c2f0f commit 2790183
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 120 deletions.
20 changes: 11 additions & 9 deletions lib/instrumentation/core/http-outbound.js
Expand Up @@ -29,19 +29,21 @@ const NEWRELIC_SYNTHETICS_HEADER = 'x-newrelic-synthetics'
*
* @return {http.ClientRequest} The instrumented outbound request.
*/
module.exports = function instrumentOutbound(agent, opts, makeRequest) {
if (typeof opts === 'string') {
opts = url.parse(opts)
} else {
opts = copy.shallow(opts)
module.exports = function instrumentOutbound(agent, _url, opts, makeRequest) {
opts = copy.shallow(opts)

if (!_url) {
_url = opts
} else if (typeof _url === 'string') {
_url = url.parse(_url)
}

let hostname = opts.hostname || opts.host || DEFAULT_HOST
let port = opts.port || opts.defaultPort
let port = _url.port || opts.defaultPort
if (!port) {
port = (!opts.protocol || opts.protocol === 'http:') ? DEFAULT_PORT : DEFAULT_SSL_PORT
port = (!_url.protocol || _url.protocol === 'http:') ? DEFAULT_PORT : DEFAULT_SSL_PORT
}

let hostname = _url.hostname || _url.host || DEFAULT_HOST
if (!hostname || port < 1) {
logger.warn(
'Invalid host name (%s) or port (%s) for outbound request.',
Expand Down Expand Up @@ -124,7 +126,7 @@ module.exports = function instrumentOutbound(agent, opts, makeRequest) {
segment.start()
const request = makeRequest(opts)
const parsed = urltils.scrubAndParseParameters(request.path)
const proto = parsed.protocol || opts.protocol || 'http:'
const proto = parsed.protocol || _url.protocol || 'http:'
segment.name += parsed.path
Object.defineProperty(request, '__NR_segment', {
value: segment
Expand Down
41 changes: 34 additions & 7 deletions lib/instrumentation/core/http.js
Expand Up @@ -371,13 +371,36 @@ function wrapWriteHead(agent, writeHead) {
}

function wrapRequest(agent, request) {
return function wrappedRequest(options) {
return function wrappedRequest() {
let _url
let options
let cb

if (
typeof arguments[0] === "string" ||
(global.URL && arguments[0] instanceof global.URL)
) {
_url = arguments[0]

if (arguments[2]) {
options = arguments[1]
cb = arguments[2]
} else {
cb = arguments[1]
}
} else {
options = arguments[0]
cb = arguments[1]
}

// Don't pollute metrics and calls with NR connections
const internalOnly = options && options[NR_CONNECTION_PROP]
if (internalOnly) {
delete options[NR_CONNECTION_PROP]
}

const reqArgs = _url ? [_url, options, cb] : [options, cb]

// If this is not a request we're recording, exit early.
const transaction = agent.tracer.getTransaction()
if (!transaction || internalOnly) {
Expand All @@ -389,15 +412,19 @@ function wrapRequest(agent, request) {
logOpts.port
)
}
return request.apply(this, arguments)
return request.apply(this, reqArgs)
}

const args = agent.tracer.slice(arguments)
const args = agent.tracer.slice(reqArgs)
const context = this

// hostname & port logic pulled directly from node's 0.10 lib/http.js
return instrumentOutbound(agent, options, function makeRequest(opts) {
args[0] = opts
return instrumentOutbound(agent, _url, options, function makeRequest(opts) {
if (_url) {
args[1] = opts
} else {
args[0] = opts
}
return request.apply(context, args)
})
}
Expand All @@ -408,7 +435,7 @@ function wrapLegacyRequest(agent, request) {
var makeRequest = request.bind(this, method, path, headers)

if (agent.tracer.getTransaction()) {
return instrumentOutbound(agent, this, makeRequest)
return instrumentOutbound(agent, undefined, this, makeRequest)
}

logger.trace('No transaction, not recording external to %s:%s', this.host, this.port)
Expand Down Expand Up @@ -481,7 +508,7 @@ module.exports = function initialize(agent, http, moduleName) {
// change originally also appeared in 8.9.0 but was reverted in 8.9.1.
//
// TODO: Remove `SHOULD_WRAP_HTTPS` after deprecating Node <9.
if (SHOULD_WRAP_HTTPS || !IS_HTTPS) {
if (SHOULD_WRAP_HTTPS) {
shimmer.wrapMethod(
http,
'http',
Expand Down
106 changes: 14 additions & 92 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -165,7 +165,7 @@
"mocha": "^6.2.1",
"mongodb": "^3.3.3",
"mysql": "*",
"nock": "9.1.9",
"nock": "11.7.0",
"proxyquire": "^1.8.0",
"q": "*",
"redis": "^1.0.0",
Expand Down

0 comments on commit 2790183

Please sign in to comment.