elastic / elastic/apm-agent-nodejs
hapi instrumentation capturing of `server.log(['error'], ...)` is obscure
- Dominant language
- JavaScript
- Stars
- 594
- Forks
- 244
- Avg merge
- 1d 8h
- Merged PRs (30d)
- 16
Description
First, the APM agent's instrumentation of hapi will capture an error for a hapi `server.log(tags, data)` (
https://hapi.dev/api/?v=20.1.2#-serverlogtags-data-timestamp) when one of the tags is "error". That isn't obvious -- at least not to someone relatively new to hapi (that server.log with a special case tag name is handled specially is initially subtle).
Second, the message displayed in APM UI for the captured error can be surprising. Using this sample code:
```
server.route({
method: 'GET',
path: '/logerr',
handler: (request, h) => {
let err = new Error('boom Error')
let str = 'boom string'
let obj = {num: 42}
server.log(['error', 'foo', 'bar'], obj)
return 'logged error'
}
})
```
1. If the `data` is an Error instance, you get a captured error of the form: `{"error":{"exception":{"message":"boom","type":"Error","stacktrace":[ ... "context":{...,"custom":{"tags":["error","foo","bar"],"data":{}}`. That's fine.
2. If the `data` is a string, you get a captured error of the form: `{"error":{"log":{"message":"boom string","stacktrace":[ ... "context":{...,"custom":{"tags":["error","foo","bar"],"data":"boom string"}`. Also fine.
3. Otherwise, you get a captured error of the form: `{"error":{"log":{"message":"hapi server emitted a log event tagged error","stacktrace":[ ... "context":{...,"custom":{"tags":["error","foo","bar"],"data":{"num":42}}`. That message can surprising. (E.g. https://discuss.elastic.co/t/hapi-server-emitted-a-log-event-tagged-error/272081).
Third, the screenshot of APM UI from the discuss link shows that stack trace created for a non-Error `data` points to the `agent.captureError(err, payload)` in the APM agent code. It would be very nice, if possible, to have the stack point to the application code's `server.log` call.
#### suggestions
1. Mostly I think having some docs that cover this would go a long way. Perhaps in https://www.elastic.co/guide/en/apm/agent/nodejs/current/hapi.html or if that is too much of a "Getting started" page, perhaps a separate more reference-y section of the docs (h/t https://documentation.divio.com/).
2. If feasible, make the captured error stack point to the application code's `server.log()` call.
Contributor guide
Assessment
This issue has not been assessed yet.