From 88c69559afb4c2b043cc944c25700bb1f8e04838 Mon Sep 17 00:00:00 2001 From: Thomas Decaux Date: Thu, 24 Jun 2021 14:42:27 +0200 Subject: [PATCH 1/3] Fix #1770 support for proxies in destination context Copy/paste from https://github.com/Qard/http-request-to-url/commit/7406c37baad7625c1bdfa17c1b62b821f55fbe03 fix linter add tests --- CHANGELOG.asciidoc | 2 ++ lib/instrumentation/http-shared.js | 7 ++++ test/instrumentation/github-issue-1770.js | 44 +++++++++++++++++++++++ 3 files changed, 53 insertions(+) create mode 100644 test/instrumentation/github-issue-1770.js diff --git a/CHANGELOG.asciidoc b/CHANGELOG.asciidoc index 46d2ff5fdc..32e0fa3eb6 100644 --- a/CHANGELOG.asciidoc +++ b/CHANGELOG.asciidoc @@ -48,6 +48,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 f02acda10b..9ef998623b 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 0000000000..33b9244f64 --- /dev/null +++ b/test/instrumentation/github-issue-1770.js @@ -0,0 +1,44 @@ +const agent = require('../..').start({ + serviceName: 'test', + secretToken: 'test', + centralConfig: false +}) + +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() + }) +}) From fc399d4f9ab49db50fd4aedfb3c3c9fd0d88ac5a Mon Sep 17 00:00:00 2001 From: Alan Storm Date: Fri, 25 Jun 2021 13:51:01 -0700 Subject: [PATCH 2/3] fix: timing issue with tests --- test/instrumentation/github-issue-1770.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/instrumentation/github-issue-1770.js b/test/instrumentation/github-issue-1770.js index 33b9244f64..047eaddcb0 100644 --- a/test/instrumentation/github-issue-1770.js +++ b/test/instrumentation/github-issue-1770.js @@ -1,7 +1,8 @@ const agent = require('../..').start({ serviceName: 'test', secretToken: 'test', - centralConfig: false + centralConfig: false, + metricsInterval:0 }) const tape = require('tape') From e9b52c6c70795472cfb4a8239603899a725119f0 Mon Sep 17 00:00:00 2001 From: Alan Storm Date: Sat, 26 Jun 2021 13:37:15 -0700 Subject: [PATCH 3/3] fix: lint --- test/instrumentation/github-issue-1770.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/instrumentation/github-issue-1770.js b/test/instrumentation/github-issue-1770.js index 047eaddcb0..d5c1907524 100644 --- a/test/instrumentation/github-issue-1770.js +++ b/test/instrumentation/github-issue-1770.js @@ -2,7 +2,7 @@ const agent = require('../..').start({ serviceName: 'test', secretToken: 'test', centralConfig: false, - metricsInterval:0 + metricsInterval: 0 }) const tape = require('tape')