Skip to content

refactor(logutil): standardize KLogTextParser and reconstruct full klog lines for control plane logs - #1162

Merged
kyasbal merged 7 commits into
GoogleCloudPlatform:mainfrom
Ackberry:refactor/klog-standardize-header
Oct 26, 2026
Merged

kyasbal merged 7 commits into
GoogleCloudPlatform:mainfrom
Ackberry:refactor/klog-standardize-header

Conversation

@Ackberry

Copy link
Copy Markdown
Contributor

Background & Problem

KLogTextParser took a hasHeader bool flag so it could parse GKE control plane component logs, where Cloud Logging moves the klog header into structured fields (severity, timestamp, sourceLocation) and leaves only the message body in jsonPayload.message. Supporting headerless input made the parser responsible for two formats.

Fixes #846

Solution Approach

  • logutil.FormatKLogString: Builds a standard klog line ([IWEF]mmdd hh:mm:ss.uuuuuu threadid file:line] msg) from structured fields.
  • Control plane extractors: ExtractK8sSchedulerComponent and K8sControllerManagerComponentExtractor.Extract rebuild the full line before parsing.
  • KLogTextParser: hasHeader is removed, along with the headerless parser entries in k8scontrolplane/parser.go. All call sites are updated.

Differences from the signature proposed in the issue:

  • Takes *pb.Severity instead of a severity string. Callers map Cloud Logging severities with the existing gcpcommon.ExtractGCPSeverity, so logutil stays independent of Cloud Logging.
  • Takes time.Time instead of a timestamp string, matching NodeReader.ReadTimestamp. The timestamp is converted to UTC.

Fallbacks, so that logs with missing fields still parse instead of being dropped:

  • Unknown or missing severity is written as I, because klog has only four levels and the header regex accepts only IWEF.
  • An empty sourceLocation.file is written as unknown, because the header requires file:line.
  • sourceLocation.line is read as a string and converted with strconv.Atoi, because protojson serializes int64 as a string and ReadIntOrDefault would silently return 0.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the KLogTextParser by removing the hasHeader option to unify parsing behavior, and introduces a FormatKLogString utility to reconstruct klog-formatted lines from structured log fields. GKE control plane extractors have been updated to reconstruct and parse logs using this new utility, and all test cases and parser configurations have been adapted accordingly. The review feedback highlights a critical issue in toKLogSeverityLetter where pointer addresses are compared directly instead of dereferencing them, which is fragile and can cause incorrect severity fallbacks.

Comment thread pkg/core/inspection/logutil/klog.go Resolved
@kyasbal

kyasbal commented Oct 25, 2026

Copy link
Copy Markdown
Member

/gcbrun

@kyasbal kyasbal added area:backend-parser Log parsers and log timeline mapper tasks type:refactor Code refactoring without behavioral change labels Oct 26, 2026

@kyasbal kyasbal left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Ackberry Thank you for your contribution. Now your changes looks good to me.
I'm going to merge this soon.

@kyasbal
kyasbal enabled auto-merge October 26, 2026 01:11
@kyasbal

kyasbal commented Oct 26, 2026

Copy link
Copy Markdown
Member

/gcbrun

@Ackberry

Copy link
Copy Markdown
Contributor Author

sounds good. I saw you've started to work on MCP. If any issue arises, I'd love to work! I'm gonna keep an eye 👀

@kyasbal
kyasbal added this pull request to the merge queue Oct 26, 2026
Merged via the queue into GoogleCloudPlatform:main with commit 67c1218 Oct 26, 2026
17 checks passed
@Ackberry
Ackberry deleted the refactor/klog-standardize-header branch October 26, 2026 06:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:backend-parser Log parsers and log timeline mapper tasks type:refactor Code refactoring without behavioral change

2 participants