Skip to content

Fix metrics-collector panic on invalid regular expression filters - #2708

Open
AdeshDeshmukh wants to merge 1 commit into
kubeflow:masterfrom
AdeshDeshmukh:fix/metricscollector-nil-regexp
Open

Fix metrics-collector panic on invalid regular expression filters#2708
AdeshDeshmukh wants to merge 1 commit into
kubeflow:masterfrom
AdeshDeshmukh:fix/metricscollector-nil-regexp

Conversation

@AdeshDeshmukh

Copy link
Copy Markdown

What this PR does / why we need it:

Fixes a crash in the file-metricscollector sidecar when a user-provided metrics filter is not a valid regular expression.

GetFilterRegexpList discarded the error from regexp.Compile, so an invalid filter produced a nil *regexp.Regexp entry in the returned list. The first pattern match against a log line (FindAllStringSubmatch) then dereferenced the nil regexp and panicked the metrics-collector container — either in parseLogsInTextFormat or in the watch loop in main.go — so no metrics were ever reported for the Trial, with no hint that the user's filter was the problem.

With this change:

  • The compile error is handled and the collector fails fast at startup with a descriptive message identifying the invalid filter (invalid metric filter "...": ...), consistent with the existing error handling in watchMetricsFile.
  • GetFilterRegexpList now returns an error, propagated through parseLogsInTextFormat / CollectObservationLog.
  • Filters are compiled once at startup instead of on every log line in the tail loop.

Behavior change note: previously an invalid filter silently matched nothing; now the sidecar exits immediately with a clear error. This is intentional — silent failure is what left Trials stuck without metrics.

Tests: added TestGetFilterRegexpList covering invalid filter (returns descriptive error, no panic), empty filters (default filter fallback), and valid multi-filter compilation. Existing TestCollectObservationLog cases pass unchanged.

Which issue(s) this PR fixes:
Fixes #2707

Checklist:

  • Docs included if any changes are user facing

GetFilterRegexpList discarded the error from regexp.Compile, so an
invalid user-provided filter produced a nil *regexp.Regexp that
panicked the metrics-collector sidecar when matched against log lines,
leaving the Trial without reported metrics.

Handle the compile error and fail fast with a descriptive message
identifying the invalid filter. Filters are also now compiled once at
startup instead of on every log line in the watch loop.

Fixes kubeflow#2707

Signed-off-by: Adesh Deshmukh <adeshkd123@gmail.com>
Copilot AI balanced review requested due to automatic review settings August 26, 2026 00:09

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@google-oss-prow

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign electronic-waste for approval. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@vjkumar2756 vjkumar2756 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

@AdeshDeshmukh
This is a great fix that addresses both a stability issue (preventing potential runtime panics) and a performance bottleneck in the file metrics collector.

some Suggestions:

  1. Duplicate Error Message (Bug): In main.go, klog.Fatalf("Invalid metric filter: %v", err) duplicates the error prefix because GetFilterRegexpList already returns invalid metric filter %q. Change to klog.Fatalf("Failed to compile metric filters: %v", err).

  2. Sub-optimal Memory Allocation: In GetFilterRegexpList(), make([]*regexp.Regexp, 0, len(filters)) is called before handling len(filters) == 0, allocating 0 capacity. Check/set default filters before allocating regexpList.

  3. Typo: In main.go comment, fix "regural expressions" -> "regular expressions".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

metrics-collector: file-metricscollector panics (nil regexp) when a user-provided filter is an invalid regular expression

3 participants