Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clearer profiling error #4647

Merged
merged 1 commit into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/dd-trace/src/profiling/exporters/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ class AgentExporter {
this._logger.error(`Error from the agent: ${err.message}`)
return
} else if (err) {
reject(new Error('Profiler agent export back-off period expired'))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't there like a convention to combine/chain errors? Something like this:

            const error = new Error('Profiler agent export back-off period expired')
            error.reason = err
            reject(error)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or even this:

reject(new Error('Profiler agent export back-off period expired: ' + err.message))
// Profiler agent export back-off period expired: HTTP Error 500

Though that might ruin the cardinality or error messages and undermine the problem you're trying to solve...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's error.cause, but I'm not sure it's supported across our full version range.

Copy link
Collaborator

@watson watson Sep 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we don't use the custom syntax:

new Error('parent', { cause: new Error('child') })

But instead build it manually:

const parent = new Error('parent')
const child = new Error('child')
parent.cause = child

Then the Node.js version being used only matters if we care how console.log outputs the error - which I guess we don't. I assume we just need to make sure that the log.error function knows to also take into account any linked cause error.

This is something we should make sure regardless of this PR as other libraries might make use of the cause property and we need to properly capture that information.

Gotcha: There is a slight difference between how console.log outputs an error created with the two approaches listed above. If using the options object with the cause property that's only supported in Node.js 16.9+, then the stack trace outputted looks like this:

Error: parent
    at REPL53:1:5
    at ContextifyScript.runInThisContext (node:vm:136:12)
    ... 7 lines matching cause stack trace ...
    at [_line] [as _line] (node:internal/readline/interface:887:18) {
  [cause]: Error: child
      at REPL53:1:34
      at ContextifyScript.runInThisContext (node:vm:136:12)
      at REPLServer.defaultEval (node:repl:598:22)
      at bound (node:domain:432:15)
      at REPLServer.runBound [as eval] (node:domain:443:12)
      at REPLServer.onLine (node:repl:927:10)
      at REPLServer.emit (node:events:532: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)
}

If we just naively add a cause property on an error object after creation-time, the console.log output looks like this:

Error: parent
    at REPL31:1:5
    at ContextifyScript.runInThisContext (node:vm:136:12)
    ... 7 lines matching cause stack trace ...
    at [_line] [as _line] (node:internal/readline/interface:887:18) {
  cause: Error: child
      at REPL32:1:5
      at ContextifyScript.runInThisContext (node:vm:136:12)
      at REPLServer.defaultEval (node:repl:598:22)
      at bound (node:domain:432:15)
      at REPLServer.runBound [as eval] (node:domain:443:12)
      at REPLServer.onLine (node:repl:927:10)
      at REPLServer.emit (node:events:532: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)
}

Notice the tiny difference between [cause] and cause. I'm not sure if that's important, but it means that there's a slight difference in the implementation.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we sub-class our errors we could have it version detect and do super(message, options) where supported and do super(message); this.cause = options.cause where not supported.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's moot in this case because I don't think the wrapper/parent error adds any value here.

reject(err)
return
}

Expand Down
2 changes: 1 addition & 1 deletion packages/dd-trace/test/profiling/exporters/agent.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ describe('exporters/agent', function () {
try {
await exporter.export({ profiles, start, end, tags })
} catch (err) {
expect(err.message).to.match(/^Profiler agent export back-off period expired$/)
expect(err.message).to.match(/^HTTP Error 500$/)
failed = true
}
expect(failed).to.be.true
Expand Down
Loading