elastic / elastic/apm-agent-nodejs

Error when elasticsearch config hosts property is an array

Open
#1,762 4 comments 1 reaction 0 assignees View on GitHub
agent-nodejs bug
Dominant language
JavaScript
Stars
594
Forks
244
Avg merge
1d 8h
Merged PRs (30d)
16

Description

**Describe the bug**

When you have an elasticsearch config similar to this:
```
elasticsearch: {
host: [
{
host: process.env.ELASTIC_HOST,
auth: process.env.ELASTIC_AUTHENTICATION,
protocol: process.env.ELASTIC_PROTOCOL,
port: process.env.ELASTIC_PORT
}
],
log: 'error',
requestTimeout: 60000
}
```

The updated APM code crashes because it's not anticipating the array for the host property.

Issue was introduced in this commit: https://github.com/elastic/apm-agent-nodejs/commit/aa8eb56a8486b53802e378638920d44248ce64dc#diff-daccb535f6ab4446fed3816cc2a6dc92

Specifically this bit of code in the elasticsearch module:
```
// get the remote host information from elasticsearch Transport options
const transportConfig = this._config
let host, port
if (typeof transportConfig === 'object' && transportConfig.host) {
[host, port] = transportConfig.host.split(':')
}
span.setDestinationContext(getDBDestination(span, host, port))
```

The host.split is where the error occurs: "TypeError: transportConfig.host.split is not a function or its return value is not iterable"

**To Reproduce**

Steps to reproduce the behavior:
1. Use an array to specify one or more hosts for the elasticsearch client
2. Use the latest release of the apm client.

**Expected behavior**

APM should be able to handle host specified as an array.

**Environment (please complete the following information)**

- OS: OSX and Ubuntu, should apply to any
- Node.js version: testing on 12, should be an issue for all
- APM Server version: latest in elastic cloud
- Agent version: 3.6.1

**How are you starting the agent? (please tick one of the boxes)**

- [ X] Calling `agent.start()` directly (e.g. `require('elastic-apm-node').start(...)`)
- [ ] Requiring `elastic-apm-node/start` from within the source code
- [ ] Starting node with `-r elastic-apm-node/start`

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.