Skip to content

Commit

Permalink
monitoring: add dbsize collector
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgeTsagk committed Aug 5, 2024
1 parent b11fc42 commit c0561ec
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
81 changes: 81 additions & 0 deletions monitoring/db_collector.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package monitoring

import (
"context"
"errors"
"sync"

"github.com/prometheus/client_golang/prometheus"
)

const (
dbSizeMetric = "total_db_size"
)

// dbCollector is a Prometheus collector that exports metrics related to the
// daemon's database.
type dbCollector struct {
collectMx sync.Mutex

cfg *PrometheusConfig
registry *prometheus.Registry

dbSize prometheus.Gauge
}

func newDbCollector(cfg *PrometheusConfig,
registry *prometheus.Registry) (*dbCollector, error) {

if cfg == nil {
return nil, errors.New("db collector prometheus cfg is nil")
}

if cfg.AssetStore == nil {
return nil, errors.New("db collector asset store is nil")
}

return &dbCollector{
cfg: cfg,
registry: registry,
dbSize: prometheus.NewGauge(
prometheus.GaugeOpts{
Name: dbSizeMetric,
Help: "Total size of db",
},
),
}, nil
}

// Describe sends the super-set of all possible descriptors of metrics
// collected by this Collector to the provided channel and returns once the
// last descriptor has been sent.
//
// NOTE: Part of the prometheus.Collector interface.
func (a *dbCollector) Describe(ch chan<- *prometheus.Desc) {
a.collectMx.Lock()
defer a.collectMx.Unlock()

a.dbSize.Describe(ch)
}

// Collect is called by the Prometheus registry when collecting metrics.
//
// NOTE: Part of the prometheus.Collector interface.
func (a *dbCollector) Collect(ch chan<- prometheus.Metric) {
a.collectMx.Lock()
defer a.collectMx.Unlock()

ctxdb, cancel := context.WithTimeout(context.Background(), dbTimeout)
defer cancel()

// Fetch the db size.
dbSize, err := a.cfg.AssetStore.AssetsDBSize(ctxdb)
if err != nil {
log.Errorf("unable to fetch db size: %v", err)
return
}

a.dbSize.Set(float64(dbSize))

a.dbSize.Collect(ch)
}
6 changes: 6 additions & 0 deletions monitoring/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ func (p *PrometheusExporter) Start() error {
}
p.registry.MustRegister(gardenCollector)

dbCollector, err := newDbCollector(p.config, p.registry)
if err != nil {
return err
}
p.registry.MustRegister(dbCollector)

assetProofCollector, err := newAssetProofCollector(p.config, p.registry)
if err != nil {
return err
Expand Down

0 comments on commit c0561ec

Please sign in to comment.