@@ -83,7 +83,7 @@ func main() {
8383}
8484
8585type collector interface {
86- collect (* as.Connection , chan <- prometheus.Metric ) error
86+ collect (* as.Connection ) ([] prometheus.Metric , error )
8787 describe (ch chan <- * prometheus.Desc )
8888}
8989
@@ -131,34 +131,44 @@ func (asc *asCollector) Collect(ch chan<- prometheus.Metric) {
131131 asc .totalScrapes .Inc ()
132132 ch <- asc .totalScrapes
133133
134- conn , err := as . NewConnection ( asc .nodeAddr , 3 * time . Second )
134+ ms , err := asc .collect ( )
135135 if err != nil {
136+ log .Print (err )
136137 ch <- prometheus .MustNewConstMetric (upDesc , prometheus .GaugeValue , 0.0 )
137138 return
138139 }
140+ ch <- prometheus .MustNewConstMetric (upDesc , prometheus .GaugeValue , 1.0 )
141+ for _ , m := range ms {
142+ ch <- m
143+ }
144+ }
145+
146+ func (asc * asCollector ) collect () ([]prometheus.Metric , error ) {
147+ conn , err := as .NewConnection (asc .nodeAddr , 3 * time .Second )
148+ if err != nil {
149+ return nil , err
150+ }
151+ defer conn .Close ()
152+
139153 if asc .username != "" {
140154 hp , err := hashPassword (asc .password )
141155 if err != nil {
142- log .Printf ("hashPassword: %s" , err )
143- ch <- prometheus .MustNewConstMetric (upDesc , prometheus .GaugeValue , 0.0 )
144- return
156+ return nil , fmt .Errorf ("hashPassword: %s" , err )
145157 }
146158 if err := conn .Authenticate (asc .username , hp ); err != nil {
147- log .Printf ("auth error: %s" , err )
148- ch <- prometheus .MustNewConstMetric (upDesc , prometheus .GaugeValue , 0.0 )
149- return
159+ return nil , fmt .Errorf ("auth error: %s" , err )
150160 }
151161 }
152- ch <- prometheus .MustNewConstMetric (upDesc , prometheus .GaugeValue , 1.0 )
153-
154- defer conn .Close ()
155162
163+ var metrics []prometheus.Metric
156164 for _ , c := range asc .collectors {
157- if err := c .collect (conn , ch ); err != nil {
158- log . Print ( err )
159- return
165+ ms , err := c .collect (conn )
166+ if err != nil {
167+ return nil , err
160168 }
169+ metrics = append (metrics , ms ... )
161170 }
171+ return metrics , nil
162172}
163173
164174// take from github.com/aerospike/aerospike-client-go/admin_command.go
0 commit comments