-
Notifications
You must be signed in to change notification settings - Fork 306
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Log correlation in traces in google cloud (#11276)
Adds a google cloud logger config so that logs trace and span ids use the required naming for correlation. Replaces #11019 Fixes #10937
- Loading branch information
1 parent
8fc2011
commit fbcc8ef
Showing
7 changed files
with
103 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { type pino } from 'pino'; | ||
|
||
/* eslint-disable camelcase */ | ||
|
||
const GOOGLE_CLOUD_TRACE_ID = 'logging.googleapis.com/trace'; | ||
const GOOGLE_CLOUD_SPAN_ID = 'logging.googleapis.com/spanId'; | ||
const GOOGLE_CLOUD_TRACE_SAMPLED = 'logging.googleapis.com/trace_sampled'; | ||
|
||
/** | ||
* Pino configuration for google cloud observability. Tweaks message and timestamp, | ||
* adds trace context attributes, and injects severity level. | ||
* Adapted from https://cloud.google.com/trace/docs/setup/nodejs-ot#config-structured-logging. | ||
*/ | ||
export const GoogleCloudLoggerConfig = { | ||
messageKey: 'message', | ||
// Same as pino.stdTimeFunctions.isoTime but uses "timestamp" key instead of "time" | ||
timestamp(): string { | ||
return `,"timestamp":"${new Date(Date.now()).toISOString()}"`; | ||
}, | ||
formatters: { | ||
log(object: Record<string, unknown>): Record<string, unknown> { | ||
// Add trace context attributes following Cloud Logging structured log format described | ||
// in https://cloud.google.com/logging/docs/structured-logging#special-payload-fields | ||
const { trace_id, span_id, trace_flags, ...rest } = object; | ||
|
||
if (trace_id && span_id) { | ||
return { | ||
[GOOGLE_CLOUD_TRACE_ID]: trace_id, | ||
[GOOGLE_CLOUD_SPAN_ID]: span_id, | ||
[GOOGLE_CLOUD_TRACE_SAMPLED]: trace_flags ? trace_flags === '01' : undefined, | ||
trace_flags, // Keep the original trace_flags for otel-pino-stream | ||
...rest, | ||
}; | ||
} | ||
return object; | ||
}, | ||
level(label: string, level: number): object { | ||
// Inspired by https://github.com/pinojs/pino/issues/726#issuecomment-605814879 | ||
// Severity labels https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#LogSeverity | ||
let severity: string; | ||
|
||
switch (label as pino.Level | keyof typeof customLevels) { | ||
case 'trace': | ||
case 'debug': | ||
severity = 'DEBUG'; | ||
break; | ||
case 'verbose': | ||
case 'info': | ||
severity = 'INFO'; | ||
break; | ||
case 'warn': | ||
severity = 'WARNING'; | ||
break; | ||
case 'error': | ||
severity = 'ERROR'; | ||
break; | ||
case 'fatal': | ||
severity = 'CRITICAL'; | ||
break; | ||
default: | ||
severity = 'DEFAULT'; | ||
break; | ||
} | ||
|
||
return { severity, level }; | ||
}, | ||
}, | ||
} satisfies pino.LoggerOptions; | ||
|
||
// Define custom logging levels for pino. Duplicate from pino-logger.ts. | ||
const customLevels = { verbose: 25 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters