Skip to content

armgit / cua

Public Imported from GitHub
0

Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.

Make a degraded database startup visible on the cyclops-cs backend (#6886)

r33drichardsc9f754c6c1c9
diff --git a/libs/fleet/backend/go.mod b/libs/fleet/backend/go.mod
index bb12006001..386253b04b 100644
--- a/libs/fleet/backend/go.mod
+++ b/libs/fleet/backend/go.mod
@@ -13,6 +13,7 @@ require (
github.com/open-feature/go-sdk v1.17.0
github.com/open-policy-agent/opa v0.69.0
github.com/prometheus/client_golang v1.20.4
+ github.com/prometheus/client_model v0.6.1
github.com/spf13/cobra v1.10.2
github.com/spf13/pflag v1.0.10
github.com/spf13/viper v1.21.0
@@ -91,7 +92,6 @@ require (
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
- github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.55.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/rcrowley/go-metrics v0.0.0-20200313005456-10cdbea86bc0 // indirect
diff --git a/libs/fleet/backend/main.go b/libs/fleet/backend/main.go
index 8518a19579..17f1834100 100644
--- a/libs/fleet/backend/main.go
+++ b/libs/fleet/backend/main.go
@@ -386,9 +386,15 @@ func initializeDatabaseFeatures(parent context.Context, cfg config.DatabaseConfi
if cfg.URL == "" {
slog.Info("postgres-backed features disabled (DATABASE_URL unset)")
+ metrics.DatabaseFeaturesReady.WithLabelValues("false").Set(1)
return
}
+ // Every path below reports through this gauge. Readiness stays
+ // database-independent by design, so this metric is the only way an
+ // operator learns that a serving pod came up degraded.
+ metrics.DatabaseFeaturesReady.WithLabelValues("true").Set(0)
+
ctx, cancel := context.WithTimeout(parent, databaseStartupTimeout)
defer cancel()
@@ -408,4 +414,6 @@ func initializeDatabaseFeatures(parent context.Context, cfg config.DatabaseConfi
auth.SetGitHubTrustResolver(handlers.NewGitHubTrustResolver(store))
slog.Info("github trust policies: enabled")
}
+
+ metrics.DatabaseFeaturesReady.WithLabelValues("true").Set(1)
}
diff --git a/libs/fleet/backend/main_test.go b/libs/fleet/backend/main_test.go
index 822566055f..aee640ea2c 100644
--- a/libs/fleet/backend/main_test.go
+++ b/libs/fleet/backend/main_test.go
@@ -26,8 +26,12 @@ import (
"cyclops-cs-backend/config"
"cyclops-cs-backend/githubtrust"
"cyclops-cs-backend/handlers"
+ "cyclops-cs-backend/metrics"
"cyclops-cs-backend/statequery"
+ "github.com/prometheus/client_golang/prometheus"
+ dto "github.com/prometheus/client_model/go"
+
jwt "github.com/golang-jwt/jwt/v5"
"github.com/trycua/cloud/pkg/featureflags"
)
@@ -727,3 +731,84 @@ func (githubTrustStoreStub) Delete(context.Context, string, string) (bool, error
func (githubTrustStoreStub) ResolveByRepository(context.Context, string) ([]*githubtrust.Policy, error) {
return nil, nil
}
+
+// The serving tier keeps readiness database-independent on purpose, so a pod
+// that comes up without its database stays Ready and answers 503 from the
+// database-backed routes only. cyclops_cs_database_features_ready is what makes
+// that state visible; these cases pin the three values it can report.
+func TestInitializeDatabaseFeaturesReportsReadinessMetric(t *testing.T) {
+ tests := []struct {
+ name string
+ config config.DatabaseConfiguration
+ requireVersionError error
+ newStoreError error
+ wantConfigured string
+ wantValue float64
+ }{
+ {
+ name: "unset database url reports the disabled configuration as ready",
+ config: config.DatabaseConfiguration{},
+ wantConfigured: "false",
+ wantValue: 1,
+ },
+ {
+ name: "healthy database reports ready",
+ config: config.DatabaseConfiguration{URL: "postgres://application/application"},
+ wantConfigured: "true",
+ wantValue: 1,
+ },
+ {
+ name: "unavailable schema reports degraded",
+ config: config.DatabaseConfiguration{URL: "postgres://application/application"},
+ requireVersionError: fmt.Errorf("schema unavailable"),
+ wantConfigured: "true",
+ wantValue: 0,
+ },
+ {
+ name: "trust store failure reports degraded",
+ config: config.DatabaseConfiguration{URL: "postgres://application/application"},
+ newStoreError: fmt.Errorf("store unavailable"),
+ wantConfigured: "true",
+ wantValue: 0,
+ },
+ }
+
+ for _, test := range tests {
+ t.Run(test.name, func(t *testing.T) {
+ metrics.DatabaseFeaturesReady.Reset()
+ dependencies := databaseFeatureDependencies{
+ requireVersion: func(context.Context, string, int64) error {
+ return test.requireVersionError
+ },
+ newStateQueryExecutor: func(string, string) (handlers.StateQueryExecutor, error) {
+ return stateQueryExecutorStub{}, nil
+ },
+ newGitHubTrustStore: func(context.Context, string) (githubtrust.Store, error) {
+ if test.newStoreError != nil {
+ return nil, test.newStoreError
+ }
+ return githubTrustStoreStub{}, nil
+ },
+ }
+ h := handlers.Handlers{}
+ t.Cleanup(func() { auth.SetGitHubTrustResolver(nil) })
+
+ initializeDatabaseFeatures(context.Background(), test.config, &h, dependencies)
+
+ got := gaugeValue(t, metrics.DatabaseFeaturesReady.WithLabelValues(test.wantConfigured))
+ if got != test.wantValue {
+ t.Fatalf("cyclops_cs_database_features_ready{configured=%q} = %v, want %v",
+ test.wantConfigured, got, test.wantValue)
+ }
+ })
+ }
+}
+
+func gaugeValue(t *testing.T, gauge prometheus.Gauge) float64 {
+ t.Helper()
+ var measurement dto.Metric
+ if err := gauge.Write(&measurement); err != nil {
+ t.Fatalf("read gauge: %v", err)
+ }
+ return measurement.GetGauge().GetValue()
+}
diff --git a/libs/fleet/backend/metrics/metrics.go b/libs/fleet/backend/metrics/metrics.go
index 0a16e66141..3b77fdc138 100644
--- a/libs/fleet/backend/metrics/metrics.go
+++ b/libs/fleet/backend/metrics/metrics.go
@@ -69,6 +69,27 @@ var (
Help: "Total Stripe billing webhook requests by processing result and event type.",
}, []string{"result", "event_type"})
+ // DatabaseFeaturesReady reports whether the PostgreSQL-backed features
+ // came up at startup. The serving tier deliberately does NOT gate
+ // readiness on the database (see
+ // docs/superpowers/plans/2026-08-12-cyclops-serving-availability.md):
+ // initializeDatabaseFeatures logs, continues, and lets the affected
+ // routes answer 503, so the pod stays Ready and keeps serving everything
+ // else. That is the intended behaviour, but it is otherwise invisible —
+ // a pod whose database never initialized looks identical to a healthy one
+ // until somebody calls a database-backed route. This gauge is the signal.
+ //
+ // configured="false" means DATABASE_URL was unset, which is a supported
+ // configuration and always reports 1. Only configured="true" with value 0
+ // is a fault.
+ //
+ // Initialization runs once at startup, so the value is fixed for the
+ // lifetime of the process and recovery means restarting the pod.
+ DatabaseFeaturesReady = promauto.NewGaugeVec(prometheus.GaugeOpts{
+ Name: "cyclops_cs_database_features_ready",
+ Help: "1 when the PostgreSQL-backed features initialized at startup (or are intentionally disabled), 0 when a configured database failed to come up.",
+ }, []string{"configured"})
+
UpstreamProxyDuration = promauto.NewHistogramVec(prometheus.HistogramOpts{
Name: "cyclops_cs_upstream_proxy_duration_seconds",
Help: "Latency of requests proxied to upstream services.",