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

[Beta] Serialize Logged Errors #1248

Merged
merged 1 commit into from
Nov 27, 2023
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
14 changes: 13 additions & 1 deletion src/logs/api.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { Logger as OtelLogger, LogRecord, logs } from "@opentelemetry/api-logs";

Check warning on line 4 in src/logs/api.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 14.x)

'logs' is defined but never used

Check warning on line 4 in src/logs/api.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 16.x)

'logs' is defined but never used

Check warning on line 4 in src/logs/api.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 18.x)

'logs' is defined but never used
import { LogRecord as SDKLogRecord } from "@opentelemetry/sdk-logs";
import { Attributes } from "@opentelemetry/api";
import { IdGenerator, RandomIdGenerator } from "@opentelemetry/sdk-trace-base";
Expand Down Expand Up @@ -119,9 +119,21 @@
const attributes: Attributes = {};
if (telemetry.properties) {
for (const [key, value] of Object.entries(telemetry.properties)) {
attributes[key] = typeof value === 'object'
// Serialize Error objects as strings to avoid serialization errors
if (value?.constructor.name === "Error") {
attributes[key] = Util.getInstance().stringify(
{
name: value.name,
message: value.message,
stack: value.stack,
cause: value.cause,
}
);
} else {
attributes[key] = typeof value === 'object'
? Util.getInstance().stringify(value)
: value;
}
}
}

Expand Down
20 changes: 20 additions & 0 deletions test/unitTests/logs/api.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,26 @@ describe("logs/API", () => {
assert.equal(logRecord.attributes["_MS.baseType"], "TestData");
});

it("should serialize errors", () => {
let testLogger = new TestLogger();
let logApi = new LogApi(testLogger);
let error = new Error("test error");
const telemetry: Telemetry = {
properties: { "testAttribute": "testValue", "error": error }
};
const data: MonitorDomain = {};
const logRecord = logApi["_telemetryToLogRecord"](
telemetry,
"TestData",
data,
) as LogRecord;
assert.equal(logRecord.body, "{}");
assert.equal(logRecord.attributes["testAttribute"], "testValue");
const errorStr: string = logRecord.attributes["error"] as string;
assert.ok(errorStr.includes("test error"));
assert.equal(logRecord.attributes["_MS.baseType"], "TestData");
});

it("trackAvailability", () => {
let testLogger = new TestLogger();
let logApi = new LogApi(testLogger);
Expand Down
Loading