opentelemetry-js icon indicating copy to clipboard operation
opentelemetry-js copied to clipboard

Support recording of errors with child errors: cause, AggregateError

Open safareli opened this issue 7 months ago • 3 comments
trafficstars

in node as well as in browser one can declare errors like:

new AggregateError(
  [
    new Error("test error 1"),
    new Error("test error 2", { cause: new Error("test error 3") }),
    new Error("test error 4"),
  ],
  "test error"
);

when such error is thrown in Span.recordException, the code name type and stack of the top most error is recorded. this means important information of all inner errors.

this is what that error looks like if you console.log it in node

Welcome to Node.js v20.11.1.
Type ".help" for more information.
> console.log(new AggregateError([ new Error("test error 1"), new Error("test error 2", { cause: new Error("test error 3") }), new Error("test error 4") ], "test error"))
AggregateError: test error
    at REPL1:1:5
    at ContextifyScript.runInThisContext (node:vm:121:12)
    at REPLServer.defaultEval (node:repl:599:22)
    at bound (node:domain:432:15)
    at REPLServer.runBound [as eval] (node:domain:443:12)
    at REPLServer.onLine (node:repl:929:10)
    at REPLServer.emit (node:events:530:35)
    at REPLServer.emit (node:domain:488:12)
    at [_onLine] [as _onLine] (node:internal/readline/interface:416:12)
    at [_line] [as _line] (node:internal/readline/interface:887:18) {
  [errors]: [
    Error: test error 1
        at REPL1:1:31
        at ContextifyScript.runInThisContext (node:vm:121:12)
        at REPLServer.defaultEval (node:repl:599:22)
        at bound (node:domain:432:15)
        at REPLServer.runBound [as eval] (node:domain:443:12)
        at REPLServer.onLine (node:repl:929:10)
        at REPLServer.emit (node:events:530:35)
        at REPLServer.emit (node:domain:488:12)
        at [_onLine] [as _onLine] (node:internal/readline/interface:416:12)
        at [_line] [as _line] (node:internal/readline/interface:887:18),
    Error: test error 2
        at REPL1:1:61
        at ContextifyScript.runInThisContext (node:vm:121:12)
        ... 7 lines matching cause stack trace ...
        at [_line] [as _line] (node:internal/readline/interface:887:18) {
      [cause]: Error: test error 3
          at REPL1:1:96
          at ContextifyScript.runInThisContext (node:vm:121:12)
          at REPLServer.defaultEval (node:repl:599:22)
          at bound (node:domain:432:15)
          at REPLServer.runBound [as eval] (node:domain:443:12)
          at REPLServer.onLine (node:repl:929:10)
          at REPLServer.emit (node:events:530:35)
          at REPLServer.emit (node:domain:488:12)
          at [_onLine] [as _onLine] (node:internal/readline/interface:416:12)
          at [_line] [as _line] (node:internal/readline/interface:887:18)
    },
    Error: test error 4
        at REPL1:1:129
        at ContextifyScript.runInThisContext (node:vm:121:12)
        at REPLServer.defaultEval (node:repl:599:22)
        at bound (node:domain:432:15)
        at REPLServer.runBound [as eval] (node:domain:443:12)
        at REPLServer.onLine (node:repl:929:10)
        at REPLServer.emit (node:events:530:35)
        at REPLServer.emit (node:domain:488:12)
        at [_onLine] [as _onLine] (node:internal/readline/interface:416:12)
        at [_line] [as _line] (node:internal/readline/interface:887:18)
  ]
}

potential solution

const util = require("util");
...
if (exception.stack) {
   attributes[semantic_conventions_1.SEMATTRS_EXCEPTION_STACKTRACE] = util.inspect(exception, {
    showHidden: false, depth: null, colors: false
  });
}

of course util is node lib and it will not work in web. we could just reimplement something that works the same.

some other notes:

  • util.inspect is basically what console.log is using in node.
  • the exception.stack is prefix of the util.inspect(exception, ...) which makes me think that it should not brake existing tools
  • In terms of support of this in tools like datadog this renders just fine (tho could be better of course).

Image

also by fixing this issue people can start asking such services to provide nicer rendering of such errors.

safareli avatar Apr 08 '25 10:04 safareli