-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
backup: Export tenant backup schedule metric
Export Cloud tenant backup schedule completion metric with tenant id tag. Issue: #141167 Epic: None Release note: None
- Loading branch information
1 parent
c693357
commit e30ec71
Showing
4 changed files
with
122 additions
and
1 deletion.
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
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,105 @@ | ||
// Copyright 2025 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the CockroachDB Software License | ||
// included in the /LICENSE file. | ||
|
||
package backup | ||
|
||
import ( | ||
"context" | ||
"github.com/cockroachdb/cockroach/pkg/util/leaktest" | ||
"testing" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/backup/backuppb" | ||
"github.com/cockroachdb/cockroach/pkg/jobs" | ||
"github.com/cockroachdb/cockroach/pkg/jobs/jobspb" | ||
"github.com/cockroachdb/cockroach/pkg/roachpb" | ||
"github.com/cockroachdb/cockroach/pkg/util/hlc" | ||
"github.com/cockroachdb/cockroach/pkg/util/metric" | ||
pbtypes "github.com/gogo/protobuf/types" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestBackupSucceededUpdatesMetrics(t *testing.T) { | ||
defer leaktest.AfterTest(t)() | ||
ctx := context.Background() | ||
executor := &scheduledBackupExecutor{ | ||
metrics: backupMetrics{ | ||
RpoMetric: metric.NewGauge(metric.Metadata{}), | ||
RpoTenantMetric: metric.NewExportedGaugeVec(metric.Metadata{}, []string{"tenant_id"}), | ||
}, | ||
} | ||
|
||
t.Run("updates RPO metric", func(t *testing.T) { | ||
schedule := createSchedule(t, true) | ||
endTime := hlc.Timestamp{WallTime: hlc.UnixNano()} | ||
details := jobspb.BackupDetails{EndTime: endTime} | ||
|
||
err := executor.backupSucceeded(ctx, nil, schedule, details, nil) | ||
require.NoError(t, err) | ||
require.Equal(t, endTime.GoTime().Unix(), executor.metrics.RpoMetric.Value()) | ||
}) | ||
|
||
t.Run("updates RPO tenant metric", func(t *testing.T) { | ||
schedule := createSchedule(t, true) | ||
tenantIDs := mustMakeTenantIDs(t, 1, 2) | ||
endTime := hlc.Timestamp{WallTime: hlc.UnixNano()} | ||
details := jobspb.BackupDetails{ | ||
EndTime: endTime, | ||
SpecificTenantIds: tenantIDs, | ||
} | ||
|
||
err := executor.backupSucceeded(ctx, nil, schedule, details, nil) | ||
require.NoError(t, err) | ||
|
||
expectedTenantIDs := []string{"system", "2"} | ||
verifyRPOTenantMetricLabels(t, executor.metrics.RpoTenantMetric, expectedTenantIDs) | ||
verifyRPOTenantMetricGaugeValue(t, executor.metrics.RpoTenantMetric, details.EndTime) | ||
}) | ||
} | ||
|
||
func createSchedule(t *testing.T, updatesLastBackupMetric bool) *jobs.ScheduledJob { | ||
schedule := jobs.NewScheduledJob(nil) | ||
|
||
args := &backuppb.ScheduledBackupExecutionArgs{ | ||
UpdatesLastBackupMetric: updatesLastBackupMetric, | ||
} | ||
any, err := pbtypes.MarshalAny(args) | ||
require.NoError(t, err) | ||
schedule.SetExecutionDetails(schedule.ExecutorType(), jobspb.ExecutionArguments{Args: any}) | ||
return schedule | ||
} | ||
|
||
func mustMakeTenantIDs(t *testing.T, ids ...int) []roachpb.TenantID { | ||
var tenantIDs []roachpb.TenantID | ||
for _, id := range ids { | ||
tid, err := roachpb.MakeTenantID(uint64(id)) | ||
require.NoError(t, err) | ||
tenantIDs = append(tenantIDs, tid) | ||
} | ||
return tenantIDs | ||
} | ||
|
||
func verifyRPOTenantMetricLabels( | ||
t *testing.T, metric *metric.GaugeVec, expectedTenantIDs []string, | ||
) { | ||
prometheusMetrics := metric.ToPrometheusMetrics() | ||
var actualTenantIDs []string | ||
for _, promMetric := range prometheusMetrics { | ||
labels := promMetric.GetLabel() | ||
for _, label := range labels { | ||
if label.GetName() == "tenant_id" { | ||
actualTenantIDs = append(actualTenantIDs, label.GetValue()) | ||
} | ||
} | ||
} | ||
require.ElementsMatch(t, expectedTenantIDs, actualTenantIDs) | ||
} | ||
|
||
func verifyRPOTenantMetricGaugeValue(t *testing.T, metric *metric.GaugeVec, endTime hlc.Timestamp) { | ||
prometheusMetrics := metric.ToPrometheusMetrics() | ||
for _, promMetric := range prometheusMetrics { | ||
value := promMetric.Gauge.GetValue() | ||
require.Equal(t, float64(endTime.GoTime().Unix()), value) | ||
} | ||
} |