Skip to content

Commit 9ff81ee

Browse files
authored
Add maintenance metric (#18703)
1 parent 7bd00c6 commit 9ff81ee

File tree

3 files changed

+45
-4
lines changed

3 files changed

+45
-4
lines changed

components/ws-manager-mk2/controllers/maintenance_controller.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"time"
1414

1515
"github.com/gitpod-io/gitpod/ws-manager/api/config"
16+
"github.com/prometheus/client_golang/prometheus"
1617
corev1 "k8s.io/api/core/v1"
1718
"k8s.io/apimachinery/pkg/api/errors"
1819
"k8s.io/apimachinery/pkg/types"
@@ -33,11 +34,16 @@ var (
3334
lookupOnce sync.Once
3435
)
3536

36-
func NewMaintenanceReconciler(c client.Client) (*MaintenanceReconciler, error) {
37-
return &MaintenanceReconciler{
37+
func NewMaintenanceReconciler(c client.Client, reg prometheus.Registerer) (*MaintenanceReconciler, error) {
38+
r := &MaintenanceReconciler{
3839
Client: c,
3940
enabledUntil: nil,
40-
}, nil
41+
}
42+
43+
gauge := newMaintenanceEnabledGauge(r)
44+
reg.MustRegister(gauge)
45+
46+
return r, nil
4147
}
4248

4349
type MaintenanceReconciler struct {

components/ws-manager-mk2/controllers/metrics.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"time"
1111

1212
wsk8s "github.com/gitpod-io/gitpod/common-go/kubernetes"
13+
"github.com/gitpod-io/gitpod/ws-manager-mk2/pkg/maintenance"
1314
workspacev1 "github.com/gitpod-io/gitpod/ws-manager/api/crd/v1"
1415
"github.com/go-logr/logr"
1516
lru "github.com/hashicorp/golang-lru"
@@ -18,6 +19,7 @@ import (
1819
)
1920

2021
const (
22+
maintenanceEnabled string = "maintenance_enabled"
2123
workspaceStartupSeconds string = "workspace_startup_seconds"
2224
workspaceStartFailuresTotal string = "workspace_starts_failure_total"
2325
workspaceFailuresTotal string = "workspace_failure_total"
@@ -399,3 +401,36 @@ func (tsv *timeoutSettingsVec) Collect(ch chan<- prometheus.Metric) {
399401
ch <- metric
400402
}
401403
}
404+
405+
type maintenanceEnabledGauge struct {
406+
name string
407+
desc *prometheus.Desc
408+
maintenance maintenance.Maintenance
409+
}
410+
411+
func newMaintenanceEnabledGauge(m maintenance.Maintenance) *maintenanceEnabledGauge {
412+
name := prometheus.BuildFQName(metricsNamespace, metricsWorkspaceSubsystem, maintenanceEnabled)
413+
return &maintenanceEnabledGauge{
414+
name: name,
415+
desc: prometheus.NewDesc(name, "Whether the cluster is in maintenance mode", nil, prometheus.Labels(map[string]string{})),
416+
maintenance: m,
417+
}
418+
}
419+
420+
func (m *maintenanceEnabledGauge) Describe(ch chan<- *prometheus.Desc) {
421+
ch <- m.desc
422+
}
423+
424+
func (m *maintenanceEnabledGauge) Collect(ch chan<- prometheus.Metric) {
425+
var value float64
426+
if m.maintenance.IsEnabled(context.Background()) {
427+
value = 1
428+
}
429+
430+
metric, err := prometheus.NewConstMetric(m.desc, prometheus.GaugeValue, value)
431+
if err != nil {
432+
return
433+
}
434+
435+
ch <- metric
436+
}

components/ws-manager-mk2/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ func main() {
137137

138138
mgrCtx := ctrl.SetupSignalHandler()
139139

140-
maintenanceReconciler, err := controllers.NewMaintenanceReconciler(mgr.GetClient())
140+
maintenanceReconciler, err := controllers.NewMaintenanceReconciler(mgr.GetClient(), metrics.Registry)
141141
if err != nil {
142142
setupLog.Error(err, "unable to create maintenance controller", "controller", "Maintenance")
143143
os.Exit(1)

0 commit comments

Comments
 (0)