Skip to content

Commit

Permalink
fix: avoid IPv4 vs IPv6 ambiguity in default 'serverUrl' by using '12…
Browse files Browse the repository at this point in the history
…7.0.0.1' rather than 'localhost'

Starting in node v17 the defaults for DNS resolution order was
changed (nodejs/node#39987) such that
`dns.lookup()` no longer sorted IPv4 addresses first. This impacts
usage of the *default* APM Server URL (the `serverUrl` config var),
'http://localhost:8200', when using node >=17 because the APM server
only binds to the IPv4 address by default
(elastic/apm-server#1405).

Fixes: #3045
  • Loading branch information
trentm committed Nov 28, 2022
1 parent 3071cdf commit 5a33615
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
2 changes: 2 additions & 0 deletions examples/trace-http-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ const apm = require('../').start({ // elastic-apm-node
useElasticTraceparentHeader: false
})

console.log('XXX ', apm._conf.serverUrl)

const http = require('http')

function makeARequest (url, opts, cb) {
Expand Down
22 changes: 11 additions & 11 deletions lib/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,15 +198,6 @@ Agent.prototype.setSpanOutcome = function (outcome) {
Agent.prototype._config = function (opts) {
this._conf = config.createConfig(opts, this.logger)
this.logger = this._conf.logger

const { host, port, protocol } = this._conf.serverUrl
? parsers.parseUrl(this._conf.serverUrl)
: { host: 'localhost:8200', port: '8200' }

this._conf.serverHost = host
this._conf.serverPort = port === ''
? (protocol === 'https:' ? 443 : 80)
: parseInt(port, 10)
}

Agent.prototype.isStarted = function () {
Expand All @@ -225,18 +216,27 @@ Agent.prototype.start = function (opts) {
this.addFilter(require('./filters/http-headers'))
}

// Check cases where we do *not* start.
if (!this._conf.active) {
this.logger.debug('Elastic APM agent disabled (`active` is false)')
return this
} else if (!this._conf.serviceName) {
this.logger.error('Elastic APM is incorrectly configured: Missing serviceName (APM will be disabled)')
this._conf.active = false
return this
} else if (!(this._conf.serverPort >= 1 && this._conf.serverPort <= 65535)) {
}
// Sanity check the port from `serverUrl`.
const parsedUrl = parsers.parseUrl(this._conf.serverUrl)
const serverPort = (parsedUrl.port
? Number(parsedUrl.port)
: (parsedUrl.protocol === 'https:' ? 443 : 80))
if (!(serverPort >= 1 && serverPort <= 65535)) {
this.logger.error('Elastic APM is incorrectly configured: serverUrl "%s" contains an invalid port! (allowed: 1-65535)', this._conf.serverUrl)
this._conf.active = false
return this
} else if (this._conf.logLevel === 'trace') {
}

if (this._conf.logLevel === 'trace') {
var stackObj = {}
Error.captureStackTrace(stackObj)

Expand Down
4 changes: 2 additions & 2 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ var DEFAULTS = {
],
serviceNodeName: undefined,
serverTimeout: '30s',
serverUrl: 'http://127.0.0.1:8200',
sourceLinesErrorAppFrames: 5,
sourceLinesErrorLibraryFrames: 5,
sourceLinesSpanAppFrames: 0,
Expand Down Expand Up @@ -541,8 +542,7 @@ class Config {
const REDACT_FIELDS = {
apiKey: true,
secretToken: true,
serverUrl: true,
serverHost: true
serverUrl: true
}
const NICE_REGEXPS_FIELDS = {
ignoreUrlRegExp: true,
Expand Down

0 comments on commit 5a33615

Please sign in to comment.