Skip to content

Commit

Permalink
monitoring: add garden collector
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgeTsagk committed Dec 11, 2023
1 parent ccff2c9 commit fe7033a
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 1 deletion.
7 changes: 7 additions & 0 deletions monitoring/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package monitoring

import (
"github.com/lightninglabs/taproot-assets/tapdb"
"github.com/lightninglabs/taproot-assets/tapgarden"
"github.com/lightninglabs/taproot-assets/universe"
"google.golang.org/grpc"
)
Expand All @@ -25,8 +26,14 @@ type PrometheusConfig struct {
// universe.
UniverseStats universe.Telemetry

// AssetStore is used to collect any stats that are relevant to the
// asset store.
AssetStore *tapdb.AssetStore

// AssetMinter is used to collect any stats that are relevant to the
// asset minter.
AssetMinter tapgarden.Planter

// PerfHistograms indicates if the additional histogram information for
// latency, and handling time of gRPC calls should be enabled. This
// generates additional data, and consume more memory for the
Expand Down
120 changes: 120 additions & 0 deletions monitoring/garden_collector.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package monitoring

import (
"sync"

"github.com/lightninglabs/taproot-assets/tapgarden"
"github.com/prometheus/client_golang/prometheus"
)

// assetBalancesCollector is a Prometheus collector that exports the balances
// of all taproot assets.
type gardenCollector struct {
collectMx sync.Mutex

cfg *PrometheusConfig
registry *prometheus.Registry

pendingBatches *prometheus.GaugeVec
completedBatches prometheus.Gauge
}

func newGardenCollector(cfg *PrometheusConfig,
registry *prometheus.Registry) *gardenCollector {

return &gardenCollector{
cfg: cfg,
registry: registry,
pendingBatches: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "mint_batches",
Help: "Batched mint transactions",
},
[]string{"batch_pubkey"},
),
completedBatches: prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "completed_batches",
Help: "Total number of completed mint batches",
}),
}
}

// 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 *gardenCollector) Describe(ch chan<- *prometheus.Desc) {
a.collectMx.Lock()
defer a.collectMx.Unlock()

a.pendingBatches.Describe(ch)
a.completedBatches.Describe(ch)
}

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

if a.cfg == nil {
log.Error("cfg is nil")
return
}

if a.cfg.AssetStore == nil {
log.Error("asset store is nil")
return
}

a.completedBatches.Set(0)

// Get the number of pending batches.
batches, err := a.cfg.AssetMinter.ListBatches(nil)
if err != nil {
log.Errorf("unable to list batches: %v", err)
return
}

completed := 0

for _, batch := range batches {
state := batch.State()

switch {
case state == tapgarden.BatchStatePending ||
state == tapgarden.BatchStateFrozen ||
state == tapgarden.BatchStateCommitted ||
state == tapgarden.BatchStateBroadcast ||
state == tapgarden.BatchStateConfirmed:

if state == tapgarden.BatchStatePending {
a.pendingBatches.WithLabelValues(
batch.BatchKey.PubKey.X().String(),
).Set(
float64(len(batch.Seedlings)),
)
}

case state == tapgarden.BatchStateFinalized ||
state == tapgarden.BatchStateSeedlingCancelled ||
state == tapgarden.BatchStateSproutCancelled:

a.pendingBatches.DeleteLabelValues(
batch.BatchKey.PubKey.X().String(),
)

if state == tapgarden.BatchStateFinalized {
completed += 1
}
}
}

a.completedBatches.Set(float64(completed))

a.pendingBatches.Collect(ch)
a.completedBatches.Collect(ch)
}
3 changes: 3 additions & 0 deletions monitoring/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ func (p *PrometheusExporter) Start() error {
assetBalancesCollecor := newAssetBalancesCollector(p.config, p.registry)
p.registry.MustRegister(assetBalancesCollecor)

gardenCollector := newGardenCollector(p.config, p.registry)
p.registry.MustRegister(gardenCollector)

// Make ensure that all metrics exist when collecting and querying.
serverMetrics.InitializeMetrics(p.config.RPCServer)

Expand Down
5 changes: 4 additions & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,10 @@ func (s *Server) RunUntilShutdown(mainErrChan <-chan error) error {
// Provide Prometheus collectors with access to the asset store.
s.cfg.Prometheus.AssetStore = s.cfg.AssetStore

// Provide Prometheus collectors with access to the asset
// minter.
s.cfg.Prometheus.AssetMinter = s.cfg.AssetMinter

promExporter, err := monitoring.NewPrometheusExporter(
&s.cfg.Prometheus,
)
Expand All @@ -334,7 +338,6 @@ func (s *Server) RunUntilShutdown(mainErrChan <-chan error) error {

srvrLog.Infof("Prometheus exporter server listening on %v",
s.cfg.Prometheus.ListenAddr)

}

srvrLog.Infof("Taproot Asset Daemon fully active!")
Expand Down

0 comments on commit fe7033a

Please sign in to comment.