Skip to content

Commit

Permalink
Merge pull request #197 from authzed/postgres-prom
Browse files Browse the repository at this point in the history
fix the postgres prom GC metrics to respect enable prom option
  • Loading branch information
jakedt committed Oct 19, 2021
2 parents 56f3feb + 5750c29 commit 2ea1f2e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
4 changes: 4 additions & 0 deletions internal/datastore/postgres/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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{}
Expand Down
33 changes: 25 additions & 8 deletions internal/datastore/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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())
Expand Down

0 comments on commit 2ea1f2e

Please sign in to comment.