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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/logger/index.ts
Expand Up @@ -87,6 +87,11 @@ 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 +152,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