diff --git a/internal/datastore/postgres/options.go b/internal/datastore/postgres/options.go index cf899c44b9..4e3e7cb7b7 100644 --- a/internal/datastore/postgres/options.go +++ b/internal/datastore/postgres/options.go @@ -175,6 +175,8 @@ func GCMaxOperationTime(time time.Duration) Option { // EnablePrometheusStats enables Prometheus metrics provided by the Postgres // clients being used by the datastore. +// +// Prometheus metrics are disable by default. func EnablePrometheusStats() Option { return func(po *postgresOptions) { po.enablePrometheusStats = true @@ -183,6 +185,8 @@ func EnablePrometheusStats() Option { // EnableTracing enables trace-level logging for the Postgres clients being // used by the datastore. +// +// Tracing is disabled by default. func EnableTracing() Option { return func(po *postgresOptions) { po.logger = &tracingLogger{} diff --git a/internal/datastore/postgres/postgres.go b/internal/datastore/postgres/postgres.go index 2d207c44da..6f3b87a02a 100644 --- a/internal/datastore/postgres/postgres.go +++ b/internal/datastore/postgres/postgres.go @@ -59,25 +59,30 @@ const ( var ( gcDurationHistogram = prometheus.NewHistogram(prometheus.HistogramOpts{ - Name: "postgres_gc_duration", - Help: "postgres garbage collection duration distribution in seconds.", - Buckets: []float64{0.01, 0.1, 0.5, 1, 5, 10, 25, 60, 120}, + Namespace: "spicedb", + Subsystem: "datastore", + Name: "postgres_gc_duration", + Help: "postgres garbage collection duration distribution in seconds.", + Buckets: []float64{0.01, 0.1, 0.5, 1, 5, 10, 25, 60, 120}, }) gcRelationshipsClearedGauge = prometheus.NewGauge(prometheus.GaugeOpts{ - Name: "postgres_relationships_cleared", - Help: "number of relationships cleared by postgres garbage collection.", + Namespace: "spicedb", + Subsystem: "datastore", + Name: "postgres_relationships_cleared", + Help: "number of relationships cleared by postgres garbage collection.", }) gcTransactionsClearedGauge = prometheus.NewGauge(prometheus.GaugeOpts{ - Name: "postgres_transactions_cleared", - Help: "number of transactions cleared by postgres garbage collection.", + Namespace: "spicedb", + Subsystem: "datastore", + Name: "postgres_transactions_cleared", + Help: "number of transactions cleared by postgres garbage collection.", }) ) func init() { dbsql.Register(tracingDriverName, sqlmw.Driver(stdlib.GetDefaultDriver(), new(traceInterceptor))) - prometheus.MustRegister(gcDurationHistogram, gcRelationshipsClearedGauge, gcTransactionsClearedGauge) } var ( @@ -144,6 +149,18 @@ func NewPostgresDatastore( if err != nil { return nil, fmt.Errorf(errUnableToInstantiate, err) } + err = prometheus.Register(gcDurationHistogram) + if err != nil { + return nil, fmt.Errorf(errUnableToInstantiate, err) + } + err = prometheus.Register(gcRelationshipsClearedGauge) + if err != nil { + return nil, fmt.Errorf(errUnableToInstantiate, err) + } + err = prometheus.Register(gcTransactionsClearedGauge) + if err != nil { + return nil, fmt.Errorf(errUnableToInstantiate, err) + } } gcCtx, cancelGc := context.WithCancel(context.Background())