Skip to content

Commit

Permalink
Fix elastic#1770 support for proxies in destination context (elastic#…
Browse files Browse the repository at this point in the history
…2126)

* Fix elastic#1770 support for proxies in destination context

Fix for elastic#1770, ensures only a single url is set
  • Loading branch information
ebuildy authored and dgieselaar committed Sep 10, 2021
1 parent 5ba5bb0 commit 49f48e6
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ Notes:
* Fixed error where SQS messages sent without an active transactions could
crash the agent. ({issues}2113[#2113])
* Fixed support for proxies in destination context ({issues}1770[#1770])
[[release-notes-x.x.x]]
==== 3.16.0 - 2021/06/14
Expand Down
7 changes: 7 additions & 0 deletions lib/instrumentation/http-shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,9 @@ function getUrlFromRequestAndOptions (req, options, fallbackProtocol) {
options = options || {}
req = req || {}
req.agent = req.agent || {}
if (isProxiedRequest(req)) {
return req.path
}

const port = options.port ? `:${options.port}` : ''
// req.host and req.protocol are node versions v14.5.0/v12.19.0 and later
Expand All @@ -294,4 +297,8 @@ function getUrlFromRequestAndOptions (req, options, fallbackProtocol) {
return `${protocol}//${host}${port}${req.path}`
}

function isProxiedRequest (req) {
return req.path.indexOf('https:') === 0 || req.path.indexOf('http:') === 0
}

exports.getUrlFromRequestAndOptions = getUrlFromRequestAndOptions
45 changes: 45 additions & 0 deletions test/instrumentation/github-issue-1770.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const agent = require('../..').start({
serviceName: 'test',
secretToken: 'test',
centralConfig: false,
metricsInterval: 0
})

const tape = require('tape')
const http = require('http')
const mockClient = require('../_mock_http_client')

function resetAgent (cb) {
agent._transport = mockClient(3, cb)
}

tape.test('span url contains single url', function (t) {
resetAgent(function (results) {
t.equals(results.spans.length, 1, 'one span')

const span = results.spans.pop()
const url = span.context.http.url

// ensure url is not in the form
// http://127.0.0.1:59281http://127.0.0.1/foo
t.equals(url, 'http://127.0.0.1/foo', 'records single url only')
t.end()
})
const server = http.createServer(function (req, res) {
res.end('hello')
})
server.unref()
server.listen(0, '0.0.0.0', () => {
const port = server.address().port
agent.startTransaction()
const opts = {
host: '127.0.0.1',
port,
method: 'GET',
path: 'http://127.0.0.1/foo'
}
const client = http.request(opts)
client.end()
agent.endTransaction()
})
})

0 comments on commit 49f48e6

Please sign in to comment.