Ensure agents handle whitespace in /proc/self/stat correctly
- Dominant language
- Gherkin
- Stars
- 427
- Forks
- 125
- PR merge metrics
- No merged PRs in 30d
Description
## Description of the issue
The fields in `/proc/self/stat` are all separated by whitespace, but one field can itself contain whitespace - but only sometimes - so simply splitting by whitespace will not be enough to find the fields you're looking for.
The 2nd field in the `/proc/self/stat` output (called `comm`) contains the name of the executable. For Node.js this might be `node`. But depending on how you start the process, it might also contain command arguments, e.g. `node app.js`. In the latter case, it will contain a space. Luckily the field is contained in parentheses, so it's easy to write an algorithm to avoid this problem.
Here is an example line with this problem:
```
44 (node /app/node_) R 1 44 44 0 -1 4210688 7948 0 0 0 109 21 0 0 20 0 10 0 133652 954462208 12906 18446744073709551615 4194304 32940036 140735797366336 0 0 0 0 4096 16898 0 0 0 17 0 0 0 0 0 0 35037200 35143856 41115648 140735797369050 140735797369131 140735797369131 140735797370852 0
```
## Proposed solution
The CPU metrics we want to get are `utime` and `stime` contained in field 14 and 15. To optain those, we use the following algorithm:
1. Get the first `line` of the `/proc/self/stat` file
1. Get the `index` of the last `)` in `line`: `index = line.lastIndexOf(')')`
1. Remove everything in `line` before the `index`: `line = line.slice(index)`
1. Split the remaining `line` on whitespace: `fields = line.split(/\s+/)`
1. All fields will now be off-by-one, so the index of `utime` will be `12` and the index for `stime` will be `13`
## What we are voting on
@elastic/apm-agent-devs If this is not an issue for your agent, tick the N/A box, otherwise tick "Yes" and add a link to an issue or PR.
## Vote
| Agent | Yes | N/A | Link to agent issue
| --------|:----:|:----:|:-------------------:|
| .NET |
- [ ]
- [x]
| Go |
- [x]
- [ ]
| Java |
- [ ]
- [x]
| Node.js |
- [x]
- [ ]
| Python |
- [ ]
- [ ]
| Ruby |
- [ ]
- [ ]
| RUM |
- [ ]
- [x]
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.