44 "errors"
55 "fmt"
66 "net/http"
7- "sync"
87 "time"
98
109 //nolint:lll
@@ -15,35 +14,29 @@ import (
1514)
1615
1716var (
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.
4030type 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.
4637func (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- }
0 commit comments