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

Fix #1770 support for proxies in destination context #2126

Merged
merged 7 commits into from
Jun 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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()
})
})