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

🐛 browser-logs: fields of custom Error objects passed in log context no longer included (since 4.41.0) #2195

Closed
niekert opened this issue May 1, 2023 · 2 comments
Labels
bug Something isn't working

Comments

@niekert
Copy link

niekert commented May 1, 2023

Describe the bug
Since version 4.41.0 of @datadog/browser-logs any custom errors included in the log context field are serialized as [Error] instead of the fields being included in the error. I'm not sure if this was intentional, but it altered behavior quite significantly for our logging setup.

To Reproduce
Example code:

class CustomError extends Error {
  constructor(message, opts) {
    super(message);
    this.opts = opts;
  }
}

// initialize logger

datadogLogs.logger.error("just a log", {
  anotherError: new CustomError("something went wrong", {
    please: "showthisInDatadog "
  })
});

We are using this pattern in some places where we aggregate errors, or provide the original error cause as a custom field on the log context (where we do not wrap errors with cause)

Expected behavior

Would expect the behavior to be similar to that of v4.40.0

V4.40.0
image

V4.41.0
image

@niekert niekert added the bug Something isn't working label May 1, 2023
@niekert niekert changed the title 🐛 browser-logs: fields of custom Error objects passed in log context no longer included 🐛 browser-logs: fields of custom Error objects passed in log context no longer included (since 4.41.0) May 1, 2023
@yannickadam
Copy link
Contributor

Hi @niekert !

Thanks for reporting this issue. We have reverted the behavior of serializing an instanceof Error in 4.42.0. Let us know if everything works as expected.

However, it is our intention to enhance the serialization of certain object types - notably errors - in our next major release.
Currently:

=> JSON.stringify( new Error('Error') )
=> '{}'

This hides the fact that the object was actually an error.

Could you give us more insights about your use-cases, and how you would expect errors to be presented?
Also - have you tried the new Error parameter of the logger APIs, introduced in 4.36.0?
logger.[level](message: string, messageContext?: object, error?: Error)

@niekert
Copy link
Author

niekert commented May 3, 2023

Hi! Thank you for reverting the change :)

Our main usecase for this is that we have some middleware that wraps errors from an API in a custom Error object (ApiError) which includes some fields like requestId (from headers), url, statusCode. We include the error object in the log context to use fields like requestId. We also use facets, on the @error.url for example for grouping API errors by URL.

Indeed we are using the new Error parameter and it gives us stack traces, which is very nice, but afaik it does not parse the custom fields on error object or does not display those

We also tried to experiment with wrapping errors with cause property in places where we wrap errors and add more contextual data. This works but also seemed to cause custom fields on the error (not the original error) to no longer be displayed. Perhaps we are not using this as intended but wanted to bring this up for context. Thank you!

@bcaudan bcaudan closed this as completed Jun 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants