Skip to content

Commit

Permalink
Change config.url to be a string instead of a URL object
Browse files Browse the repository at this point in the history
  • Loading branch information
watson committed Sep 11, 2024
1 parent 14ebf97 commit 99b3555
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 27 deletions.
6 changes: 3 additions & 3 deletions packages/dd-trace/src/appsec/remote_config/manager.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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()
Expand Down
7 changes: 5 additions & 2 deletions packages/dd-trace/src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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'
}
}

Expand Down
6 changes: 3 additions & 3 deletions packages/dd-trace/src/dogstatsd.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion packages/dd-trace/src/startup-log.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

const { format } = require('url')
const { info, warn } = require('./log/writer')

const os = require('os')
Expand Down Expand Up @@ -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] () {
Expand Down
24 changes: 8 additions & 16 deletions packages/dd-trace/test/config.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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')
})
Expand Down Expand Up @@ -1715,29 +1707,29 @@ 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', () => {
process.env.DD_TRACE_AGENT_URL = 'https://example.com/'

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', () => {
process.env.DD_TRACE_URL = 'https://example.com/'

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', () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/dd-trace/test/runtime_metrics.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand Down

0 comments on commit 99b3555

Please sign in to comment.