6
6
"net/http"
7
7
"os"
8
8
"path/filepath"
9
- "sync"
10
9
11
10
"github.com/btcsuite/btcutil"
12
11
"github.com/lightninglabs/lndclient"
@@ -16,8 +15,6 @@ import (
16
15
)
17
16
18
17
var (
19
- metricsMtx sync.Mutex
20
-
21
18
// log configuration defaults.
22
19
defaultLogFilename = "lndmon.log"
23
20
defaultLogFileSize = 10
@@ -33,6 +30,13 @@ type PrometheusExporter struct {
33
30
lnd * lndclient.LndServices
34
31
35
32
monitoringCfg * MonitoringConfig
33
+
34
+ // collectors is the exporter's active set of collectors.
35
+ collectors []prometheus.Collector
36
+
37
+ // errChan is an error channel that we receive errors from our
38
+ // collectors on.
39
+ errChan <- chan error
36
40
}
37
41
38
42
// PrometheusConfig is the set of configuration data that specifies the
@@ -74,10 +78,26 @@ func DefaultConfig() *PrometheusConfig {
74
78
func NewPrometheusExporter (cfg * PrometheusConfig , lnd * lndclient.LndServices ,
75
79
monitoringCfg * MonitoringConfig ) * PrometheusExporter {
76
80
81
+ // We have six collectors, so we buffer our error channel by 6 so that
82
+ // we do not need to consume all errors from this channel (on the first
83
+ // one, we'll start shutting down, but a few could arrive quickly).
84
+ errChan := make (chan error , 6 )
85
+
77
86
return & PrometheusExporter {
78
87
cfg : cfg ,
79
88
lnd : lnd ,
80
89
monitoringCfg : monitoringCfg ,
90
+ collectors : []prometheus.Collector {
91
+ NewChainCollector (lnd .Client , errChan ),
92
+ NewChannelsCollector (
93
+ lnd .Client , errChan , monitoringCfg ,
94
+ ),
95
+ NewWalletCollector (lnd , errChan ),
96
+ NewGraphCollector (lnd .Client , errChan ),
97
+ NewPeerCollector (lnd .Client , errChan ),
98
+ NewInfoCollector (lnd .Client , errChan ),
99
+ },
100
+ errChan : errChan ,
81
101
}
82
102
}
83
103
@@ -128,23 +148,17 @@ func (p *PrometheusExporter) Start() error {
128
148
return nil
129
149
}
130
150
151
+ // Errors returns an error channel that any failures experienced by its
152
+ // collectors experience.
153
+ func (p * PrometheusExporter ) Errors () <- chan error {
154
+ return p .errChan
155
+ }
156
+
131
157
// registerMetrics iterates through all the registered collectors and attempts
132
158
// to register each one. If any of the collectors fail to register, then an
133
159
// error will be returned.
134
160
func (p * PrometheusExporter ) registerMetrics () error {
135
- metricsMtx .Lock ()
136
- defer metricsMtx .Unlock ()
137
-
138
- collectors := []prometheus.Collector {
139
- NewChainCollector (p .lnd .Client ),
140
- NewChannelsCollector (p .lnd .Client , p .monitoringCfg ),
141
- NewWalletCollector (p .lnd ),
142
- NewGraphCollector (p .lnd .Client ),
143
- NewPeerCollector (p .lnd .Client ),
144
- NewInfoCollector (p .lnd .Client ),
145
- }
146
-
147
- for _ , collector := range collectors {
161
+ for _ , collector := range p .collectors {
148
162
err := prometheus .Register (collector )
149
163
if err != nil {
150
164
return err
0 commit comments