-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b11fc42
commit c0561ec
Showing
2 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters