Skip to content

Commit bbcd35d

Browse files
committed
monitoring+tapd: register collectors to prometheus
1 parent 0e2a065 commit bbcd35d

File tree

4 files changed

+66
-118
lines changed

4 files changed

+66
-118
lines changed

monitoring/config.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package monitoring
22

3-
import "google.golang.org/grpc"
3+
import (
4+
"github.com/lightninglabs/taproot-assets/tapdb"
5+
"github.com/lightninglabs/taproot-assets/tapgarden"
6+
"github.com/lightninglabs/taproot-assets/universe"
7+
"google.golang.org/grpc"
8+
)
49

510
// PrometheusConfig is the set of configuration data that specifies if
611
// Prometheus metric exporting is activated, and if so the listening address of
@@ -17,6 +22,18 @@ type PrometheusConfig struct {
1722
// generic RPC metrics to monitor the health of the service.
1823
RPCServer *grpc.Server
1924

25+
// UniverseStats is used to collect any stats that are relevant to the
26+
// universe.
27+
UniverseStats universe.Telemetry
28+
29+
// AssetStore is used to collect any stats that are relevant to the
30+
// asset store.
31+
AssetStore *tapdb.AssetStore
32+
33+
// AssetMinter is used to collect any stats that are relevant to the
34+
// asset minter.
35+
AssetMinter tapgarden.Planter
36+
2037
// PerfHistograms indicates if the additional histogram information for
2138
// latency, and handling time of gRPC calls should be enabled. This
2239
// generates additional data, and consume more memory for the

monitoring/interface.go

Lines changed: 0 additions & 29 deletions
This file was deleted.

monitoring/prometheus.go

Lines changed: 35 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"errors"
55
"fmt"
66
"net/http"
7-
"sync"
87
"time"
98

109
//nolint:lll
@@ -15,35 +14,29 @@ import (
1514
)
1615

1716
var (
18-
// metricGroups is a global variable of all registered metrics
19-
// projected by the mutex below. All new MetricGroups should add
20-
// themselves to this map within the init() method of their file.
21-
metricGroups = make(map[string]metricGroupFactory)
22-
23-
// activeGroups is a global map of all active metric groups. This can
24-
// be used by some of the "static' package level methods to look up the
25-
// target metric group to export observations.
26-
activeGroups = make(map[string]MetricGroup)
27-
28-
// metricsMtx is a global mutex that should be held when accessing the
29-
// global maps.
30-
metricsMtx sync.Mutex
31-
3217
// serverMetrics is a global variable that holds the Prometheus metrics
3318
// for the gRPC server.
3419
serverMetrics *grpc_prometheus.ServerMetrics
3520
)
3621

22+
const (
23+
// dbTimeout is the default database timeout.
24+
dbTimeout = 20 * time.Second
25+
)
26+
3727
// PrometheusExporter is a metric exporter that uses Prometheus directly. The
3828
// internal server will interact with this struct in order to export relevant
3929
// metrics.
4030
type PrometheusExporter struct {
41-
config *PrometheusConfig
31+
config *PrometheusConfig
32+
registry *prometheus.Registry
4233
}
4334

4435
// Start registers all relevant metrics with the Prometheus library, then
4536
// launches the HTTP server that Prometheus will hit to scrape our metrics.
4637
func (p *PrometheusExporter) Start() error {
38+
log.Infof("Starting Prometheus Exporter")
39+
4740
// If we're not active, then there's nothing more to do.
4841
if !p.config.Active {
4942
return nil
@@ -54,28 +47,43 @@ func (p *PrometheusExporter) Start() error {
5447
return fmt.Errorf("server metrics not set")
5548
}
5649

57-
reg := prometheus.NewRegistry()
58-
reg.MustRegister(collectors.NewProcessCollector(
50+
// Create a custom Prometheus registry.
51+
p.registry = prometheus.NewRegistry()
52+
p.registry.MustRegister(collectors.NewProcessCollector(
5953
collectors.ProcessCollectorOpts{},
6054
))
61-
reg.MustRegister(collectors.NewGoCollector())
62-
reg.MustRegister(serverMetrics)
55+
p.registry.MustRegister(collectors.NewGoCollector())
56+
p.registry.MustRegister(serverMetrics)
6357

64-
// Make ensure that all metrics exist when collecting and querying.
65-
serverMetrics.InitializeMetrics(p.config.RPCServer)
58+
uniStatsCollector, err := newUniverseStatsCollector(p.config, p.registry)
59+
if err != nil {
60+
return err
61+
}
62+
p.registry.MustRegister(uniStatsCollector)
6663

67-
// Next, we'll attempt to register all our metrics. If we fail to
68-
// register ANY metric, then we'll fail all together.
69-
if err := p.registerMetrics(); err != nil {
64+
assetBalancesCollecor, err :=
65+
newAssetBalancesCollector(p.config, p.registry)
66+
if err != nil {
7067
return err
7168
}
69+
p.registry.MustRegister(assetBalancesCollecor)
70+
71+
gardenCollector, err := newGardenCollector(p.config, p.registry)
72+
if err != nil {
73+
return err
74+
}
75+
p.registry.MustRegister(gardenCollector)
76+
77+
// Make ensure that all metrics exist when collecting and querying.
78+
serverMetrics.InitializeMetrics(p.config.RPCServer)
7279

7380
// Finally, we'll launch the HTTP server that Prometheus will use to
74-
// scape our metrics.
81+
// scrape our metrics.
7582
go func() {
83+
// Use our custom prometheus registry.
7684
promMux := http.NewServeMux()
7785
promMux.Handle("/metrics", promhttp.HandlerFor(
78-
reg, promhttp.HandlerOpts{
86+
p.registry, promhttp.HandlerOpts{
7987
EnableOpenMetrics: true,
8088
MaxRequestsInFlight: 1,
8189
}),
@@ -98,61 +106,3 @@ func (p *PrometheusExporter) Start() error {
98106

99107
return nil
100108
}
101-
102-
// registerMetrics iterates through all the registered metric groups and
103-
// attempts to register each one. If any of the MetricGroups fail to register,
104-
// then an error will be returned.
105-
func (p *PrometheusExporter) registerMetrics() error {
106-
metricsMtx.Lock()
107-
defer metricsMtx.Unlock()
108-
109-
for _, metricGroupFunc := range metricGroups {
110-
metricGroup, err := metricGroupFunc(p.config)
111-
if err != nil {
112-
return err
113-
}
114-
115-
if err := metricGroup.RegisterMetricFuncs(); err != nil {
116-
return err
117-
}
118-
119-
activeGroups[metricGroup.Name()] = metricGroup
120-
}
121-
122-
return nil
123-
}
124-
125-
// gauges is a map type that maps a gauge to its unique name.
126-
type gauges map[string]*prometheus.GaugeVec // nolint:unused
127-
128-
// addGauge adds a new gauge vector to the map.
129-
func (g gauges) addGauge(name, help string, labels []string) { // nolint:unused
130-
g[name] = prometheus.NewGaugeVec(
131-
prometheus.GaugeOpts{
132-
Name: name,
133-
Help: help,
134-
},
135-
labels,
136-
)
137-
}
138-
139-
// describe describes all gauges contained in the map to the given channel.
140-
func (g gauges) describe(ch chan<- *prometheus.Desc) { // nolint:unused
141-
for _, gauge := range g {
142-
gauge.Describe(ch)
143-
}
144-
}
145-
146-
// collect collects all metrics of the map's gauges to the given channel.
147-
func (g gauges) collect(ch chan<- prometheus.Metric) { // nolint:unused
148-
for _, gauge := range g {
149-
gauge.Collect(ch)
150-
}
151-
}
152-
153-
// reset resets all gauges in the map.
154-
func (g gauges) reset() { // nolint:unused
155-
for _, gauge := range g {
156-
gauge.Reset()
157-
}
158-
}

server.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,16 @@ func (s *Server) RunUntilShutdown(mainErrChan <-chan error) error {
313313
// configuration.
314314
s.cfg.Prometheus.RPCServer = grpcServer
315315

316+
// Provide Prometheus collectors with access to Universe stats.
317+
s.cfg.Prometheus.UniverseStats = s.cfg.UniverseStats
318+
319+
// Provide Prometheus collectors with access to the asset store.
320+
s.cfg.Prometheus.AssetStore = s.cfg.AssetStore
321+
322+
// Provide Prometheus collectors with access to the asset
323+
// minter.
324+
s.cfg.Prometheus.AssetMinter = s.cfg.AssetMinter
325+
316326
promExporter, err := monitoring.NewPrometheusExporter(
317327
&s.cfg.Prometheus,
318328
)
@@ -321,13 +331,13 @@ func (s *Server) RunUntilShutdown(mainErrChan <-chan error) error {
321331
err)
322332
}
323333

324-
srvrLog.Infof("Prometheus exporter server listening on %v",
325-
s.cfg.Prometheus.ListenAddr)
326-
327334
if err := promExporter.Start(); err != nil {
328335
return mkErr("Unable to start prometheus exporter: %v",
329336
err)
330337
}
338+
339+
srvrLog.Infof("Prometheus exporter server listening on %v",
340+
s.cfg.Prometheus.ListenAddr)
331341
}
332342

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

0 commit comments

Comments
 (0)