Skip to content

Commit

Permalink
monitoring+tapd: add asset and assetBalances collectors to registry
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgeTsagk committed Dec 4, 2023
1 parent 7d0f6e9 commit ccff2c9
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 80 deletions.
6 changes: 5 additions & 1 deletion monitoring/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package monitoring

import (
"github.com/lightninglabs/taproot-assets/tapdb"
"github.com/lightninglabs/taproot-assets/universe"
"google.golang.org/grpc"
)
Expand All @@ -20,9 +21,12 @@ type PrometheusConfig struct {
// generic RPC metrics to monitor the health of the service.
RPCServer *grpc.Server

// UniverseStats ...
// UniverseStats is used to collect any stats that are relevant to the
// universe.
UniverseStats universe.Telemetry

AssetStore *tapdb.AssetStore

// 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
86 changes: 7 additions & 79 deletions monitoring/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"errors"
"fmt"
"net/http"
"sync"
"time"

//nolint:lll
Expand All @@ -15,20 +14,6 @@ import (
)

var (
// metricGroups is a global variable of all registered metrics
// projected by the mutex below. All new MetricGroups should add
// themselves to this map within the init() method of their file.
metricGroups = make(map[string]metricGroupFactory)

// activeGroups is a global map of all active metric groups. This can
// be used by some of the "static' package level methods to look up the
// target metric group to export observations.
activeGroups = make(map[string]MetricGroup)

// metricsMtx is a global mutex that should be held when accessing the
// global maps.
metricsMtx sync.Mutex

// serverMetrics is a global variable that holds the Prometheus metrics
// for the gRPC server.
serverMetrics *grpc_prometheus.ServerMetrics
Expand All @@ -51,6 +36,7 @@ type PrometheusExporter struct {
// launches the HTTP server that Prometheus will hit to scrape our metrics.
func (p *PrometheusExporter) Start() error {
log.Infof("Starting Prometheus Exporter")

// If we're not active, then there's nothing more to do.
if !p.config.Active {
return nil
Expand All @@ -69,15 +55,15 @@ func (p *PrometheusExporter) Start() error {
p.registry.MustRegister(collectors.NewGoCollector())
p.registry.MustRegister(serverMetrics)

uniStatsCollector := newUniverseStatsCollector(p.config, p.registry)
p.registry.MustRegister(uniStatsCollector)

assetBalancesCollecor := newAssetBalancesCollector(p.config, p.registry)
p.registry.MustRegister(assetBalancesCollecor)

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

// Next, we'll attempt to register all our metrics. If we fail to
// register ANY metric, then we'll fail all together.
if err := p.registerMetrics(); err != nil {
return err
}

// Finally, we'll launch the HTTP server that Prometheus will use to
// scrape our metrics.
go func() {
Expand Down Expand Up @@ -107,61 +93,3 @@ func (p *PrometheusExporter) Start() error {

return nil
}

// registerMetrics iterates through all the registered metric groups and
// attempts to register each one. If any of the MetricGroups fail to register,
// then an error will be returned.
func (p *PrometheusExporter) registerMetrics() error {
metricsMtx.Lock()
defer metricsMtx.Unlock()

for name, metricGroupFunc := range metricGroups {
metricGroup, err := metricGroupFunc(p.config, p.registry)
if err != nil {
return fmt.Errorf("failed to create metric group %s: %v", name, err)
}

if err := metricGroup.RegisterMetricFuncs(); err != nil {
return err
}

activeGroups[metricGroup.Name()] = metricGroup
}

return nil
}

// gauges is a map type that maps a gauge to its unique name.
type gauges map[string]*prometheus.GaugeVec // nolint:unused

// addGauge adds a new gauge vector to the map.
func (g gauges) addGauge(name, help string, labels []string) { // nolint:unused
g[name] = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: name,
Help: help,
},
labels,
)
}

// describe describes all gauges contained in the map to the given channel.
func (g gauges) describe(ch chan<- *prometheus.Desc) { // nolint:unused
for _, gauge := range g {
gauge.Describe(ch)
}
}

// collect collects all metrics of the map's gauges to the given channel.
func (g gauges) collect(ch chan<- prometheus.Metric) { // nolint:unused
for _, gauge := range g {
gauge.Collect(ch)
}
}

// reset resets all gauges in the map.
func (g gauges) reset() { // nolint:unused
for _, gauge := range g {
gauge.Reset()
}
}
3 changes: 3 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,9 @@ func (s *Server) RunUntilShutdown(mainErrChan <-chan error) error {
// Provide Prometheus collectors with access to Universe stats.
s.cfg.Prometheus.UniverseStats = s.cfg.UniverseStats

// Provide Prometheus collectors with access to the asset store.
s.cfg.Prometheus.AssetStore = s.cfg.AssetStore

promExporter, err := monitoring.NewPrometheusExporter(
&s.cfg.Prometheus,
)
Expand Down

0 comments on commit ccff2c9

Please sign in to comment.