Skip to content

Commit

Permalink
feat: Enhance Proxy Request Handling to Display Actual External URLs (#…
Browse files Browse the repository at this point in the history
…2698)

Thanks for the contribution @mstarzec386
  • Loading branch information
mstarzec386 authored Nov 8, 2024
1 parent a229bbf commit 3ef7bbe
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
26 changes: 25 additions & 1 deletion lib/instrumentation/core/http-outbound.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const copy = require('../../util/copy')
const symbols = require('../../symbols')
const http = require('http')
const synthetics = require('../../synthetics')
const { URL } = require('node:url')

const NAMES = require('../../metrics/names')
const DEFAULT_HOST = 'localhost'
Expand Down Expand Up @@ -93,6 +94,27 @@ function extractHostPort(opts) {
return { host, hostname, port }
}

/**
* Extracts the host, hostname, and port from HTTP request options when using a proxy
*
* @param {object} opts HTTP request options
* @returns {object|null} { host, hostname, port } if proxy request is detected, or null otherwise.
*/
function extractHostPortViaProxy(opts) {
const pathname = opts.pathname || opts.path

if (pathname && (pathname.startsWith('https://') || pathname.startsWith('http://'))) {
const url = new URL(pathname)
return {
host: url.host,
hostname: url.hostname,
port: url.port || url.protocol === 'https:' ? '443' : '80'
}
}

return null
}

/**
* Instruments an outbound HTTP request.
*
Expand All @@ -103,7 +125,9 @@ function extractHostPort(opts) {
*/
module.exports = function instrumentOutbound(agent, opts, makeRequest) {
opts = parseOpts(opts)
const { host, hostname, port } = extractHostPort(opts)

const viaProxy = extractHostPortViaProxy(opts)
const { host, hostname, port } = viaProxy ? viaProxy : extractHostPort(opts)

if (!hostname || port < 1) {
logger.warn('Invalid host name (%s) or port (%s) for outbound request.', hostname, port)
Expand Down
39 changes: 39 additions & 0 deletions test/integration/instrumentation/http-outbound.tap.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,45 @@ tap.test('external requests', function (t) {
}
})

t.test('should recognize requests via proxy correctly', function (t) {
const proxyUrl = 'https://www.google.com/proxy/path'
const proxyServer = http.createServer(function onRequest(req, res) {
t.equal(req.url, proxyUrl)
req.resume()
res.end('ok')
})
t.teardown(() => proxyServer.close())

proxyServer.listen(0)

helper.runInTransaction(agent, function inTransaction() {
const opts = {
host: 'localhost',
port: proxyServer.address().port,
path: proxyUrl,
protocol: 'http:'
}

const req = http.get(opts, function onResponse(res) {
res.resume()
res.once('end', function () {
const segment = agent.tracer.getTransaction().trace.root.children[0]
t.equal(
segment.name,
`External/www.google.com/proxy/path`,
'should name segment as an external service'
)
t.end()
})
})

req.on('error', function onError(err) {
t.fail('Request should not error: ' + err.message)
t.end()
})
})
})

t.test('should not duplicate the external segment', function (t) {
const https = require('https')

Expand Down

0 comments on commit 3ef7bbe

Please sign in to comment.