From 49f48e6956dde412af172a427a89c7c3ab28c59d Mon Sep 17 00:00:00 2001 From: Thomas Decaux Date: Tue, 29 Jun 2021 08:19:01 +0200 Subject: [PATCH] Fix #1770 support for proxies in destination context (#2126) * Fix #1770 support for proxies in destination context Fix for #1770, ensures only a single url is set --- CHANGELOG.asciidoc | 2 + lib/instrumentation/http-shared.js | 7 ++++ test/instrumentation/github-issue-1770.js | 45 +++++++++++++++++++++++ 3 files changed, 54 insertions(+) create mode 100644 test/instrumentation/github-issue-1770.js diff --git a/CHANGELOG.asciidoc b/CHANGELOG.asciidoc index 4ffbbc6aa96..3511a9e2004 100644 --- a/CHANGELOG.asciidoc +++ b/CHANGELOG.asciidoc @@ -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 diff --git a/lib/instrumentation/http-shared.js b/lib/instrumentation/http-shared.js index f02acda10b6..9ef998623b1 100644 --- a/lib/instrumentation/http-shared.js +++ b/lib/instrumentation/http-shared.js @@ -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 @@ -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 diff --git a/test/instrumentation/github-issue-1770.js b/test/instrumentation/github-issue-1770.js new file mode 100644 index 00000000000..d5c19075249 --- /dev/null +++ b/test/instrumentation/github-issue-1770.js @@ -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() + }) +})