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

Add trace property to logs created via logger.write() #1446

Merged
merged 6 commits into from
Aug 26, 2024
Merged
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
11 changes: 7 additions & 4 deletions src/logger/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ function removeCircular(obj: any, refs: any[] = []): any {
* @public
*/
export function write(entry: LogEntry) {
const ctx = traceContext.getStore();
if (ctx?.traceId) {
entry[
"logging.googleapis.com/trace"
] = `projects/${process.env.GCLOUD_PROJECT}/traces/${ctx.traceId}`;
}

UNPATCHED_CONSOLE[CONSOLE_SEVERITY[entry.severity]](JSON.stringify(removeCircular(entry)));
}

Expand Down Expand Up @@ -147,17 +154,13 @@ function entryFromArgs(severity: LogSeverity, args: any[]): LogEntry {
if (lastArg && typeof lastArg === "object" && lastArg.constructor === Object) {
entry = args.pop();
}
const ctx = traceContext.getStore();

// mimic `console.*` behavior, see https://nodejs.org/api/console.html#console_console_log_data_args
let message = format(...args);
if (severity === "ERROR" && !args.find((arg) => arg instanceof Error)) {
message = new Error(message).stack || message;
}
const out: LogEntry = {
"logging.googleapis.com/trace": ctx?.traceId
? `projects/${process.env.GCLOUD_PROJECT}/traces/${ctx.traceId}`
: undefined,
...entry,
severity,
};
Expand Down
Loading