diff --git a/pkg/monitoring/metrics/forklift-controller/migration_metrics.go b/pkg/monitoring/metrics/forklift-controller/migration_metrics.go index 829dcbcd2..b30530d9e 100644 --- a/pkg/monitoring/metrics/forklift-controller/migration_metrics.go +++ b/pkg/monitoring/metrics/forklift-controller/migration_metrics.go @@ -10,9 +10,11 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" ) -var processedSucceededMigrations = make(map[string]struct{}) -var processedFailedMigrations = make(map[string]struct{}) -var processedCanceledMigrations = make(map[string]struct{}) +var ( + processedSucceededMigrations = make(map[string]struct{}) + processedFailedMigrations = make(map[string]struct{}) + processedCanceledMigrations = make(map[string]struct{}) +) // Calculate Migrations metrics every 10 seconds func RecordMigrationMetrics(c client.Client) { @@ -65,55 +67,64 @@ func RecordMigrationMetrics(c client.Client) { } provider := sourceProvider.Type().String() - - statusList := []string{Succeeded, Failed, Executing, Canceled} - for _, status := range statusList { - if m.Status.HasCondition(status) { - switch status { - case Succeeded: - if _, exists := processedSucceededMigrations[string(m.UID)]; !exists { - updateMetricsCount(status, provider, mode, target, string(plan.UID)) - - startTime := m.Status.Started.Time - endTime := m.Status.Completed.Time - duration := endTime.Sub(startTime).Seconds() - - var totalDataTransferred float64 - for _, vm := range m.Status.VMs { - for _, step := range vm.Pipeline { - if step.Name == "DiskTransferV2v" || step.Name == "DiskTransfer" { - for _, task := range step.Tasks { - totalDataTransferred += float64(task.Progress.Completed) * 1024 * 1024 // convert to Bytes - } - } - } - } - - migrationDurationGauge.With(prometheus.Labels{"provider": provider, "mode": mode, "target": target, "plan": string(plan.UID)}).Set(duration) - migrationDurationHistogram.With(prometheus.Labels{"provider": provider, "mode": mode, "target": target}).Observe(duration) - dataTransferredGauge.With(prometheus.Labels{"provider": provider, "mode": mode, "target": target, "plan": string(plan.UID)}).Set(totalDataTransferred) - - processedSucceededMigrations[string(m.UID)] = struct{}{} - } - case Failed: - if _, exists := processedFailedMigrations[string(m.UID)]; !exists { - updateMetricsCount(status, provider, mode, target, string(plan.UID)) - processedFailedMigrations[string(m.UID)] = struct{}{} - } - case Canceled: - if _, exists := processedCanceledMigrations[string(m.UID)]; !exists { - updateMetricsCount(status, provider, mode, target, string(plan.UID)) - processedCanceledMigrations[string(m.UID)] = struct{}{} - } - } - } - } + processMigration(m, provider, mode, target, string(plan.UID)) } } }() } +func processMigration(migration api.Migration, provider, mode, target, planUID string) { + var ( + processedMigrations map[string]struct{} + status string + ) + + switch { + case migration.Status.HasCondition(Succeeded): + processedMigrations = processedSucceededMigrations + status = Succeeded + case migration.Status.HasCondition(Failed): + processedMigrations = processedFailedMigrations + status = Failed + case migration.Status.HasCondition(Canceled): + processedMigrations = processedCanceledMigrations + status = Canceled + default: + // otherwise, there's nothing to do with the current state of the migration + return + } + + if _, exists := processedMigrations[string(migration.UID)]; !exists { + updateMetricsCount(status, provider, mode, target, planUID) + if status == Succeeded { + recordSuccessfulMigrationMetrics(migration, provider, mode, target, planUID) + } + processedMigrations[string(migration.UID)] = struct{}{} + } +} + func updateMetricsCount(status, provider, mode, target, plan string) { migrationStatusCounter.With(prometheus.Labels{"status": status, "provider": provider, "mode": mode, "target": target}).Inc() migrationPlanCorrelationStatusCounter.With(prometheus.Labels{"status": status, "provider": provider, "mode": mode, "target": target, "plan": plan}).Inc() } + +func recordSuccessfulMigrationMetrics(migration api.Migration, provider, mode, target, planUID string) { + startTime := migration.Status.Started.Time + endTime := migration.Status.Completed.Time + duration := endTime.Sub(startTime).Seconds() + + var totalDataTransferred float64 + for _, vm := range migration.Status.VMs { + for _, step := range vm.Pipeline { + if step.Name == "DiskTransferV2v" || step.Name == "DiskTransfer" { + for _, task := range step.Tasks { + totalDataTransferred += float64(task.Progress.Completed) * 1024 * 1024 // convert to Bytes + } + } + } + } + + migrationDurationGauge.With(prometheus.Labels{"provider": provider, "mode": mode, "target": target, "plan": planUID}).Set(duration) + migrationDurationHistogram.With(prometheus.Labels{"provider": provider, "mode": mode, "target": target}).Observe(duration) + dataTransferredGauge.With(prometheus.Labels{"provider": provider, "mode": mode, "target": target, "plan": planUID}).Set(totalDataTransferred) +}