Skip to content

Commit

Permalink
try with if
Browse files Browse the repository at this point in the history
  • Loading branch information
krzysztofzuraw committed Sep 27, 2024
1 parent 876da33 commit b6a1f0d
Showing 1 changed file with 23 additions and 21 deletions.
44 changes: 23 additions & 21 deletions packages/logger/src/logger-otel-transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,28 +42,30 @@ export const attachLoggerOtelTransport = (
{} as Record<string, LogAttributeValue>,
);

/**
* Try to serialize Error. Modern-errors has plugin to serialize
* https://github.com/ehmicky/modern-errors-serialize
*
* It add "serialize" method that converts class to plain object, working for OTEL.
*
* This is not perfect, doesn't work for nested object. We probably need to introduce some abstraction
* on logger error?
*/
try {
const errorAttribute = serializedAttributes.error;
const ErrorConstructor = errorAttribute["constructor"];
if (serializedAttributes.error) {
/**
* Try to serialize Error. Modern-errors has plugin to serialize
* https://github.com/ehmicky/modern-errors-serialize
*
* It add "serialize" method that converts class to plain object, working for OTEL.
*
* This is not perfect, doesn't work for nested object. We probably need to introduce some abstraction
* on logger error?
*/
try {
const errorAttribute = serializedAttributes.error;
const ErrorConstructor = errorAttribute["constructor"];

console.log("ErrorConstructor", JSON.stringify(serializedAttributes.error));
// @ts-expect-error - ErrorConstructor is a class that could have serialize method. If not, safely throw and ignore
serializedAttributes.error = ErrorConstructor.serialize(serializedAttributes.error, {
exclude: ["errors"],
});
// @ts-expect-error - Additional mapping for Datadog
serializedAttributes.error.type = serializedAttributes.error.name;
} catch (e) {
console.error("Error serialization failed", JSON.stringify(e));
console.log("ErrorConstructor", JSON.stringify(serializedAttributes.error));
// @ts-expect-error - ErrorConstructor is a class that could have serialize method. If not, safely throw and ignore
serializedAttributes.error = ErrorConstructor.serialize(serializedAttributes.error, {
exclude: ["errors"],
});
// @ts-expect-error - Additional mapping for Datadog
serializedAttributes.error.type = serializedAttributes.error.name;
} catch (e) {
console.error("Error serialization failed", JSON.stringify(e));
}
}

logs.getLogger("app-logger-otel").emit({
Expand Down

0 comments on commit b6a1f0d

Please sign in to comment.