Skip to content

feat: add Trainer control-plane metrics - #4190

Open
abhijeet-dhumal wants to merge 1 commit into
kubeflow:masterfrom
abhijeet-dhumal:trainer-metrics-implementation
Open

abhijeet-dhumal wants to merge 1 commit into
kubeflow:masterfrom
abhijeet-dhumal:trainer-metrics-implementation

Conversation

@abhijeet-dhumal

@abhijeet-dhumal abhijeet-dhumal commented Oct 7, 2026 •

Copy link
Copy Markdown
Member

What this PR does / why we need it

Adds six upstream Trainer Prometheus metrics for runtime inventory, TrainJob creation, completion, failure, lifetime, and requested training-node counts proposed in #3429:

Metrics are registered only by the elected controller leader and collected from informer-backed snapshots without Kubernetes API calls during scraping. The implementation also handles duplicate terminal events, runtime updates/deletions, and leader transitions.

TLS, authentication, and ServiceMonitor configuration are handled separately by PR #4102.

  • kubeflow_trainer_runtime_info: current TrainingRuntime and ClusterTrainingRuntime inventory.
  • kubeflow_trainer_trainjob_created_total: TrainJobs observed after the initial informer cache listing.
  • kubeflow_trainer_trainjob_finished_total: terminal outcomes by result, reason, and runtime.
  • kubeflow_trainer_trainjob_lifetime_seconds: time from TrainJob creation to terminal state.
  • kubeflow_trainer_trainjob_requested_training_nodes_per_job: requested training-node size.

The controller uses its existing informer caches instead of calling the Kubernetes API during metric collection. Runtime information is kept in an in-memory snapshot and reused when processing TrainJobs.
Only the elected controller leader processes metric events and registers the Trainer metrics. When leadership changes, the old leader removes its metrics and the new leader registers them. This prevents duplicate metrics from multiple controller replicas.

The implementation also:

  • avoids counting TrainJobs already present when the cache starts;
  • records terminal metrics only once per TrainJob;
  • avoids high-cardinality labels such as TrainJob names and UIDs;
  • includes unit and race-test coverage;
  • documents metric behavior, PromQL examples, and limitations.

Validation

  • Unit tests for runtime inventory, TrainJob events, runtime policy resolution, terminal-state handling, and metric lifecycle.
  • Race tests for the metrics package.
  • go vet ./pkg/metrics/...
  • Formatting and git diff --check.
  • Existing repository code-quality, generation, and language test checks.
  • Prometheus metrics architecture and monitoring documentation.

Follow-up

Final CI, documentation build, approver review, and cluster E2E validation remain required before merge.

Checklist

  • Docs included for the user-facing metrics and monitoring changes
  • Unit tests added
  • Leader ownership and registration lifecycle covered

@google-oss-prow

Copy link
Copy Markdown
Contributor

[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 tenzen-y 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

@abhijeet-dhumal
abhijeet-dhumal marked this pull request as ready for review October 7, 2026 12:44
@google-oss-prow
google-oss-prow Bot requested a review from kuizhiqing October 7, 2026 12:44

@robert-bell robert-bell 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.

Thanks @abhijeet-dhumal! I've left some inline feedback.

Comment thread pkg/metrics/setup.go Outdated
Comment thread pkg/metrics/instrumentation.go
Comment thread pkg/metrics/trainer_metrics.go Outdated
Comment thread pkg/metrics/trainer_metrics.go Outdated
Comment thread pkg/metrics/trainer_metrics.go Outdated
Comment thread pkg/metrics/instrumentation_test.go
@robert-bell robert-bell mentioned this pull request Oct 7, 2026
6 of 9 tasks
@abhijeet-dhumal
abhijeet-dhumal force-pushed the trainer-metrics-implementation branch from 71adb35 to 9b58b90 Compare October 8, 2026 08:53
Comment thread cmd/trainer-controller-manager/main.go Outdated
Comment thread pkg/metrics/setup.go Outdated
Comment thread pkg/metrics/instrumentation.go
Comment thread pkg/metrics/instrumentation.go Outdated
Comment thread pkg/metrics/trainer_metrics.go Outdated
Comment thread pkg/metrics/trainer_metrics.go Outdated
Comment thread pkg/metrics/trainer_metrics.go Outdated
Comment thread pkg/metrics/instrumentation.go Outdated
@abhijeet-dhumal

Copy link
Copy Markdown
Member Author

Thanks @robert-bell .. I’ve tried to address the metric scope and test coverage updates.

Comment thread pkg/metrics/trainer_metrics.go Outdated
Comment thread pkg/metrics/trainer_metrics.go Outdated
Comment thread pkg/metrics/instrumentation_test.go
Comment thread docs/operator-guides/metrics.md Outdated
Comment thread docs/operator-guides/metrics.md Outdated
Comment thread docs/operator-guides/metrics.md
Comment thread pkg/metrics/metrics.go
Comment thread pkg/metrics/instrumentation.go Outdated
Comment thread pkg/metrics/instrumentation.go Outdated
Comment thread pkg/metrics/instrumentation.go Outdated
Comment thread pkg/metrics/instrumentation.go Outdated
Comment thread pkg/metrics/metrics.go
Comment thread docs/operator-guides/metrics.md Outdated
Comment thread docs/operator-guides/metrics.md Outdated
Comment thread pkg/metrics/instrumentation.go Outdated

@robert-bell robert-bell 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.

Thanks @abhijeet-dhumal this is looking good! I've left a few minor suggestions but this is looking pretty close.

Comment thread pkg/metrics/metrics.go Outdated
Comment thread pkg/metrics/instrumentation.go
Comment thread pkg/metrics/instrumentation_test.go
Comment thread pkg/metrics/trainer_metrics_test.go Outdated

@robert-bell robert-bell 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.

@abhijeet-dhumal I ran the new PR review skill over this - can you check if these any of these comments are valid?

Comment thread pkg/metrics/instrumentation.go Outdated
Comment thread pkg/metrics/instrumentation.go Outdated
Comment thread pkg/metrics/instrumentation.go
Comment thread pkg/metrics/instrumentation_test.go
Comment thread pkg/metrics/trainer_metrics_test.go Outdated
Comment thread docs/operator-guides/prometheus-monitoring.md Outdated
Comment thread docs/operator-guides/metrics.md Outdated
@abhijeet-dhumal
abhijeet-dhumal force-pushed the trainer-metrics-implementation branch 2 times, most recently from 1ad4f92 to 0f47c2d Compare October 14, 2026 12:41
@abhijeet-dhumal
abhijeet-dhumal marked this pull request as ready for review October 14, 2026 13:48

@andreyvelich andreyvelich 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.

Thanks @abhijeet-dhumal for the work on this! Review done using AI tools; inline comments below.

Comment thread pkg/metrics/instrumentation.go
Comment thread pkg/metrics/instrumentation_test.go Outdated
Comment thread pkg/metrics/instrumentation.go Outdated
Comment thread pkg/metrics/instrumentation.go Outdated
Comment thread pkg/metrics/instrumentation.go Outdated
Comment thread pkg/metrics/metrics.go Outdated
Comment thread docs/operator-guides/metrics.md Outdated
Comment thread docs/operator-guides/metrics.md
Comment thread docs/operator-guides/metrics-architecture.md Outdated
Comment thread docs/operator-guides/metrics-architecture.md Outdated
Signed-off-by: abhijeet-dhumal <abhijeetdhumal652@gmail.com>
@andreyvelich

Copy link
Copy Markdown
Member

/milestone v2.4

@google-oss-prow google-oss-prow Bot added this to the v2.4 milestone Oct 21, 2026

@robert-bell robert-bell 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.

Thanks @abhijeet-dhumal this is looking pretty close!

return fmt.Errorf("wait for TrainJob metrics observer: %w", err)
}
m.ready.Store(true)
<-ctx.Done()

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.

Can we log that the instrumentation has been configured correctly?

Suggested change
<-ctx.Done()
m.log.Info("trainer-metrics instrumentation ready")
<-ctx.Done()

return fmt.Errorf("wait for runtime metrics observers: %w", err)
}
c.ready.Store(true)
<-ctx.Done()

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.

Same here - can we log that the instrumentation has been configured correctly?

Suggested change
<-ctx.Done()
c.log.Info("runtime-metrics instrumentation ready")
<-ctx.Done()

}
defer func() {
if removeErr := clusterInformer.RemoveEventHandler(clusterRegistration); removeErr != nil {
ctrl.Log.WithName("trainer-metrics").Error(removeErr, "failed to remove ClusterTrainingRuntime metrics observer")

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.

nit: can we move this logger into the runtimeInfoCollector and use a different name?

return &runtimeInfoCollector{
		cache:    c,
        log:      ctrl.Log.WithName("runtime-metrics-collector")
		snapshot: make(map[string]runtimeInfo),
	}

}
defer func() {
if removeErr := trainingInformer.RemoveEventHandler(trainingRegistration); removeErr != nil {
ctrl.Log.WithName("trainer-metrics").Error(removeErr, "failed to remove TrainingRuntime metrics observer")

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.

nit: same as https://github.com/kubeflow/trainer/pull/4190/changes#r4338364858 - use a logger on the runtimeInfoCollector

This branch has not been deployed

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

Labels

Projects

Status: Needs Triage

Development

Successfully merging this pull request may close these issues.

3 participants