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' (elastic#3049)

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: elastic#3045
Refs: elastic/apm#727
  • Loading branch information
trentm authored and fpm-peter committed Aug 20, 2024
1 parent b2f1ad0 commit 9f34e0d
Show file tree
Hide file tree
Showing 14 changed files with 32 additions and 32 deletions.
4 changes: 2 additions & 2 deletions docs/configuration.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ WARNING: The API key is sent as plain-text in every request to the server, so yo
==== `serverUrl`

* *Type:* String
* *Default:* `http://localhost:8200`
* *Default:* `http://127.0.0.1:8200`
* *Env:* `ELASTIC_APM_SERVER_URL`

The URL to where the APM Server is deployed.
Expand Down Expand Up @@ -1146,7 +1146,7 @@ module.exports = {
// Use if APM Server requires a token
secretToken: '',
// Set custom APM Server URL (default: http://localhost:8200)
// Set custom APM Server URL (default: http://127.0.0.1:8200)
serverUrl: ''
}
----
Expand Down
2 changes: 1 addition & 1 deletion docs/custom-stack.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ var apm = require('elastic-apm-node').start({
// Use if APM Server uses API keys for authentication
apiKey: '',
// Set custom APM Server URL (default: http://localhost:8200)
// Set custom APM Server URL (default: http://127.0.0.1:8200)
serverUrl: '',
})
----
Expand Down
2 changes: 1 addition & 1 deletion docs/express.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const apm = require('elastic-apm-node').start({
// Use if APM Server uses API keys for authentication
apiKey: '',
// Set custom APM Server URL (default: http://localhost:8200)
// Set custom APM Server URL (default: http://127.0.0.1:8200)
serverUrl: '',
})
Expand Down
2 changes: 1 addition & 1 deletion docs/fastify.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ var apm = require('elastic-apm-node').start({
// Use if APM Server uses API keys for authentication
apiKey: '',
// Set custom APM Server URL (default: http://localhost:8200)
// Set custom APM Server URL (default: http://127.0.0.1:8200)
serverUrl: '',
})
Expand Down
2 changes: 1 addition & 1 deletion docs/hapi.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const apm = require('elastic-apm-node').start({
// Use if APM Server uses API keys for authentication
apiKey: '',
// Set custom APM Server URL (default: http://localhost:8200)
// Set custom APM Server URL (default: http://127.0.0.1:8200)
serverUrl: '',
})
Expand Down
2 changes: 1 addition & 1 deletion docs/koa.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const apm = require('elastic-apm-node').start({
// Use if APM Server uses API keys for authentication
apiKey: '',
// Set custom APM Server URL (default: http://localhost:8200)
// Set custom APM Server URL (default: http://127.0.0.1:8200)
serverUrl: '',
})
Expand Down
2 changes: 1 addition & 1 deletion docs/opentracing.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ const agent = require('elastic-apm-node').start({
// Use if APM Server uses API keys for authentication
apiKey: '',
// Set custom APM Server URL (default: http://localhost:8200)
// Set custom APM Server URL (default: http://127.0.0.1:8200)
serverUrl: '',
})
Expand Down
2 changes: 1 addition & 1 deletion docs/restify.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const apm = require('elastic-apm-node').start({
// Use if APM Server uses API keys for authentication
apiKey: '',
// Set custom APM Server URL (default: http://localhost:8200)
// Set custom APM Server URL (default: http://127.0.0.1:8200)
serverUrl: '',
})
Expand Down
2 changes: 1 addition & 1 deletion docs/setup.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ require('elastic-apm-node').start({
// Use if APM Server uses API keys for authentication
apiKey: '',
// Set custom APM Server URL (default: http://localhost:8200)
// Set custom APM Server URL (default: http://127.0.0.1:8200)
serverUrl: '',
// Only activate the agent if it's running in production
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
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
"basic-auth": "^2.0.1",
"cookie": "^0.5.0",
"core-util-is": "^1.0.2",
"elastic-apm-http-client": "11.0.3",
"elastic-apm-http-client": "11.0.4",
"end-of-stream": "^1.4.4",
"error-callsites": "^2.0.4",
"error-stack-parser": "^2.0.6",
Expand Down
2 changes: 1 addition & 1 deletion test/config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ var optionFixtures = [
['secretToken', 'SECRET_TOKEN'],
['serverCaCertFile', 'SERVER_CA_CERT_FILE'],
['serverTimeout', 'SERVER_TIMEOUT', 30],
['serverUrl', 'SERVER_URL'],
['serverUrl', 'SERVER_URL', 'http://127.0.0.1:8200'],
['serviceName', 'SERVICE_NAME', apmName],
['serviceNodeName', 'SERVICE_NODE_NAME'],
['serviceVersion', 'SERVICE_VERSION', apmVersion],
Expand Down

0 comments on commit 9f34e0d

Please sign in to comment.