diff --git a/packages/dd-trace/src/appsec/remote_config/manager.js b/packages/dd-trace/src/appsec/remote_config/manager.js index 8f2aa44cea2..5d4be7d7617 100644 --- a/packages/dd-trace/src/appsec/remote_config/manager.js +++ b/packages/dd-trace/src/appsec/remote_config/manager.js @@ -1,6 +1,6 @@ 'use strict' -const { URL, format } = require('url') +const { format } = require('url') const uuid = require('crypto-randomuuid') const { EventEmitter } = require('events') const tracerVersion = require('../../../../../package.json').version @@ -27,11 +27,11 @@ class RemoteConfigManager extends EventEmitter { const pollInterval = Math.floor(config.remoteConfig.pollInterval * 1000) - this.url = config.url || new URL(format({ + this.url = config.url || format({ protocol: 'http:', hostname: config.hostname || 'localhost', port: config.port - })) + }) this._handlers = new Map() const appliedConfigs = this.appliedConfigs = new Map() diff --git a/packages/dd-trace/src/config.js b/packages/dd-trace/src/config.js index db473b42525..000773ea481 100644 --- a/packages/dd-trace/src/config.js +++ b/packages/dd-trace/src/config.js @@ -1226,7 +1226,10 @@ function maybeFloat (number) { } function getAgentUrl (url, options) { - if (url) return new URL(url) + if (url) { + if (typeof url === 'string') return url + if (url instanceof URL) return url.toString() + } if (os.type() === 'Windows_NT') return @@ -1238,7 +1241,7 @@ function getAgentUrl (url, options) { !process.env.DD_TRACE_AGENT_PORT && fs.existsSync('/var/run/datadog/apm.socket') ) { - return new URL('unix:///var/run/datadog/apm.socket') + return 'unix:///var/run/datadog/apm.socket' } } diff --git a/packages/dd-trace/src/dogstatsd.js b/packages/dd-trace/src/dogstatsd.js index ba84de71341..b1ca00b44c0 100644 --- a/packages/dd-trace/src/dogstatsd.js +++ b/packages/dd-trace/src/dogstatsd.js @@ -5,7 +5,7 @@ const request = require('./exporters/common/request') const dgram = require('dgram') const isIP = require('net').isIP const log = require('./log') -const { URL, format } = require('url') +const { format } = require('url') const MAX_BUFFER_SIZE = 1024 // limit from the agent @@ -174,11 +174,11 @@ class DogStatsDClient { if (config.url) { clientConfig.metricsProxyUrl = config.url } else if (config.port) { - clientConfig.metricsProxyUrl = new URL(format({ + clientConfig.metricsProxyUrl = format({ protocol: 'http:', hostname: config.hostname || 'localhost', port: config.port - })) + }) } return clientConfig diff --git a/packages/dd-trace/src/startup-log.js b/packages/dd-trace/src/startup-log.js index 12086ae1168..d8dbcb181bb 100644 --- a/packages/dd-trace/src/startup-log.js +++ b/packages/dd-trace/src/startup-log.js @@ -1,5 +1,6 @@ 'use strict' +const { format } = require('url') const { info, warn } = require('./log/writer') const os = require('os') @@ -54,7 +55,11 @@ function startupLog ({ agentError } = {}) { } function tracerInfo () { - const url = config.url || `http://${config.hostname || 'localhost'}:${config.port}` + const url = config.url || format({ + protocol: 'http:', + hostname: config.hostname || 'localhost', + port: config.port + }) const out = { [inspect.custom] () { diff --git a/packages/dd-trace/test/config.spec.js b/packages/dd-trace/test/config.spec.js index 0c18150850e..2b925d6d606 100644 --- a/packages/dd-trace/test/config.spec.js +++ b/packages/dd-trace/test/config.spec.js @@ -681,9 +681,7 @@ describe('Config', () => { expect(config).to.have.property('tracing', false) expect(config).to.have.nested.property('dogstatsd.hostname', 'agent') - expect(config).to.have.nested.property('url.protocol', 'https:') - expect(config).to.have.nested.property('url.hostname', 'agent2') - expect(config).to.have.nested.property('url.port', '7777') + expect(config).to.have.property('url', 'https://agent2:7777') expect(config).to.have.property('site', 'datadoghq.eu') expect(config).to.have.property('service', 'service') expect(config).to.have.property('env', 'test') @@ -925,9 +923,7 @@ describe('Config', () => { plugins: false }) - expect(config).to.have.nested.property('url.protocol', 'https:') - expect(config).to.have.nested.property('url.hostname', 'agent2') - expect(config).to.have.nested.property('url.port', '7777') + expect(config).to.have.property('url', 'https://agent2:7777') expect(config).to.have.property('site', 'datadoghq.eu') expect(config).to.have.property('service', 'service') expect(config).to.have.property('env', 'test') @@ -1156,9 +1152,7 @@ describe('Config', () => { }) expect(config).to.have.property('protocolVersion', '0.5') - expect(config).to.have.nested.property('url.protocol', 'https:') - expect(config).to.have.nested.property('url.hostname', 'agent2') - expect(config).to.have.nested.property('url.port', '6218') + expect(config).to.have.property('url', 'https://agent2:6218') expect(config).to.have.nested.property('dogstatsd.hostname', 'server') expect(config).to.have.nested.property('dogstatsd.port', '8888') expect(config).to.have.property('site', 'datadoghq.com') @@ -1345,9 +1339,7 @@ describe('Config', () => { env: 'development' }) - expect(config).to.have.nested.property('url.protocol', 'https:') - expect(config).to.have.nested.property('url.hostname', 'agent3') - expect(config).to.have.nested.property('url.port', '7778') + expect(config).to.have.property('url', 'https://agent3:7778') expect(config).to.have.property('service', 'test') expect(config).to.have.property('env', 'development') }) @@ -1715,7 +1707,7 @@ describe('Config', () => { const config = new Config() expect(existsSyncParam).to.equal('/var/run/datadog/apm.socket') - expect(config.url.toString()).to.equal('unix:///var/run/datadog/apm.socket') + expect(config.url).to.equal('unix:///var/run/datadog/apm.socket') }) it('should not be used when DD_TRACE_AGENT_URL provided', () => { @@ -1723,7 +1715,7 @@ describe('Config', () => { const config = new Config() - expect(config.url.toString()).to.equal('https://example.com/') + expect(config.url).to.equal('https://example.com/') }) it('should not be used when DD_TRACE_URL provided', () => { @@ -1731,13 +1723,13 @@ describe('Config', () => { const config = new Config() - expect(config.url.toString()).to.equal('https://example.com/') + expect(config.url).to.equal('https://example.com/') }) it('should not be used when options.url provided', () => { const config = new Config({ url: 'https://example.com/' }) - expect(config.url.toString()).to.equal('https://example.com/') + expect(config.url).to.equal('https://example.com/') }) it('should not be used when DD_TRACE_AGENT_PORT provided', () => { diff --git a/packages/dd-trace/test/runtime_metrics.spec.js b/packages/dd-trace/test/runtime_metrics.spec.js index f3f20464630..531b348b26d 100644 --- a/packages/dd-trace/test/runtime_metrics.spec.js +++ b/packages/dd-trace/test/runtime_metrics.spec.js @@ -66,7 +66,7 @@ suiteDescribe('runtimeMetrics', () => { runtimeMetrics.start(config) expect(Client).to.have.been.calledWithMatch({ - metricsProxyUrl: new URL('http://localhost:8126'), + metricsProxyUrl: 'http://localhost:8126', host: 'localhost', tags: [ 'str:bar', @@ -82,7 +82,7 @@ suiteDescribe('runtimeMetrics', () => { runtimeMetrics.start(config) expect(Client).to.have.been.calledWithMatch({ - metricsProxyUrl: new URL('http://[::1]:8126'), + metricsProxyUrl: 'http://[::1]:8126', host: 'localhost', tags: [ 'str:bar',