[Beta] Some errors get serialized as empty objects in logs
mderriey opened this issue · comments
Hi there 👋
What
We're experiencing a behavior where some errors are not logged properly when using the OTel-based beta package.
We tracked this down to my previous PR that attempted to fix the case where objects were serialized as [object Object]
.
Why
The issue with the naïve approach of serializing as JSON is that it works for enumerable properties only.
Most native Error
types have non-enumerable properties only:
Welcome to Node.js v18.18.2.
Type ".help" for more information.
> const basicError = new Error('Boom!')
undefined
> JSON.stringify(basicError)
'{}'
> const typeError = new TypeError('Boom!')
undefined
> JSON.stringify(typeError)
'{}'
Some libraries, like axios, define custom error types with either enumerable properties or a toJSON
function that is invoked by JSON.stringify
, which explains why the issue depends on which type of error is logged:
Welcome to Node.js v18.18.2.
Type ".help" for more information.
> const { AxiosError } = require('axios')
undefined
> const axiosError = new AxiosError('Boom!')
undefined
> JSON.stringify(axiosError)
'{"message":"Boom!","name":"AxiosError","stack":"AxiosError: Boom!\\n at REPL2:1:20\\n at Script.runInThisContext (node:vm:123:12)\\n at REPLServer.defaultEval (node:repl:569:29)\\n at bound (node:domain:433:15)\\n at REPLServer.runBound [as eval] (node:domain:444:12)\\n at REPLServer.onLine (node:repl:899:10)\\n at REPLServer.emit (node:events:529:35)\\n at REPLServer.emit (node:domain:489:12)\\n at [_onLine] [as _onLine] (node:internal/readline/interface:423:12)\\n at [_line] [as _line] (node:internal/readline/interface:894:18)","status":null}'
How
It seems like a common issue in the JavaScript world, and it sounds like Error
s would need to be special-cased when transforming a telemetry item into a LogRecord
.
Existing npm packages like serialize-error go to great length to detect errors and destructure them; I'm not sure what the project policy is when it comes to third-party libraries, and this one in particular is an ES module which I had trouble using in my current TypeScript project configured to output CommonJS code.
Thanks let me know if you need more information.
@mderriey I'll take a look into this one further. Thank you for the investigation!
@mderriey PR above should resolve this issue.
@mderriey Closing this issue as we're waiting for an OpenTelemetry release. Please let me know if there are any follow ups to this issue and we can reopen. Thank you!