@@ -213,24 +213,24 @@ func allReservedNames(client *mongo.Client) ([]string, error) {
213213 reservedNames := []string {}
214214 dbs , err := client .ListDatabaseNames (ctx , struct {}{})
215215 if err != nil {
216- return reservedNames , err
216+ return reservedNames , errors . Wrap ( err , "ListDatabaseNames failed" )
217217 }
218218
219219 for _ , dbName := range dbs {
220220 fmt .Printf ("Database: %s\n " , dbName )
221221 db := client .Database (dbName )
222222 collCursor , err := db .ListCollections (ctx , struct {}{})
223223 if err != nil {
224- return reservedNames , err
224+ return reservedNames , errors . Wrapf ( err , "ListCollections failed for db %s" , dbName )
225225 }
226- defer collCursor .Close (ctx )
226+ defer collCursor .Close (ctx ) //nolint:errcheck
227227 for collCursor .Next (ctx ) {
228228 var collInfo struct {
229229 Name string `bson:"name"`
230230 Type string `bson:"type"`
231231 }
232232 if err := collCursor .Decode (& collInfo ); err != nil {
233- return reservedNames , err
233+ return reservedNames , errors . Wrapf ( err , "Decode failed for collection in db %s" , dbName )
234234 }
235235 reservedNames = append (reservedNames , collInfo .Name )
236236 if collInfo .Type == "view" {
@@ -241,7 +241,7 @@ func allReservedNames(client *mongo.Client) ([]string, error) {
241241 if err != nil {
242242 continue // skip if cannot list indexes
243243 }
244- defer cursor .Close (ctx )
244+ defer cursor .Close (ctx ) //nolint:errcheck
245245 for cursor .Next (ctx ) {
246246 var indexDoc map [string ]interface {}
247247 if err := cursor .Decode (& indexDoc ); err != nil {
@@ -380,58 +380,64 @@ func makeMetrics(client *mongo.Client, prefix string, m bson.M, labels map[strin
380380 } else {
381381 l = labels
382382 }
383- switch v := val .(type ) {
384- case bson.M :
385- res = append (res , makeMetrics (client , nextPrefix , v , l , compatibleMode )... )
386- case map [string ]interface {}:
387- res = append (res , makeMetrics (client , nextPrefix , v , l , compatibleMode )... )
388- case primitive.A :
389- res = append (res , processSlice (client , nextPrefix , v , l , compatibleMode )... )
390- case []interface {}:
391- continue
392- default :
393- reservedNames , err := allReservedNames (client )
394- if err != nil {
395- fmt .Printf ("\n \n \n cannot get reserved names: %v \n \n \n " , err )
396- continue
397- }
383+ res = append (res , handleMetric (client , prefix , nextPrefix , k , val , l , compatibleMode )... )
384+ }
385+
386+ return res
387+ }
388+
389+ func handleMetric (client * mongo.Client , prefix , nextPrefix , k string , val interface {}, l map [string ]string , compatibleMode bool ) []prometheus.Metric {
390+ var res []prometheus.Metric
391+ switch v := val .(type ) {
392+ case bson.M :
393+ res = append (res , makeMetrics (client , nextPrefix , v , l , compatibleMode )... )
394+ case map [string ]interface {}:
395+ res = append (res , makeMetrics (client , nextPrefix , v , l , compatibleMode )... )
396+ case primitive.A :
397+ res = append (res , processSlice (client , nextPrefix , v , l , compatibleMode )... )
398+ case []interface {}:
399+ // skip
400+ default :
401+ reservedNames , err := allReservedNames (client )
402+ if err != nil {
403+ fmt .Printf ("\n \n \n cannot get reserved names: %v \n \n \n " , err )
404+ return res
405+ }
406+
407+ rm , err := makeRawMetric (reservedNames , prefix , k , v , l )
408+ if err != nil {
409+ invalidMetric := prometheus .NewInvalidMetric (prometheus .NewInvalidDesc (err ), err )
410+ res = append (res , invalidMetric )
411+ return res
412+ }
413+
414+ // makeRawMetric returns a nil metric for some data types like strings
415+ // because we cannot extract data from all types
416+ if rm == nil {
417+ return res
418+ }
398419
399- rm , err := makeRawMetric (reservedNames , prefix , k , v , l )
420+ metrics := []* rawMetric {rm }
421+
422+ if renamedMetrics := metricRenameAndLabel (rm , specialConversions ); renamedMetrics != nil {
423+ metrics = renamedMetrics
424+ }
425+
426+ for _ , m := range metrics {
427+ metric , err := rawToPrometheusMetric (m )
400428 if err != nil {
401429 invalidMetric := prometheus .NewInvalidMetric (prometheus .NewInvalidDesc (err ), err )
402430 res = append (res , invalidMetric )
403431 continue
404432 }
405433
406- // makeRawMetric returns a nil metric for some data types like strings
407- // because we cannot extract data from all types
408- if rm == nil {
409- continue
410- }
411-
412- metrics := []* rawMetric {rm }
434+ res = append (res , metric )
413435
414- if renamedMetrics := metricRenameAndLabel (rm , specialConversions ); renamedMetrics != nil {
415- metrics = renamedMetrics
416- }
417-
418- for _ , m := range metrics {
419- metric , err := rawToPrometheusMetric (m )
420- if err != nil {
421- invalidMetric := prometheus .NewInvalidMetric (prometheus .NewInvalidDesc (err ), err )
422- res = append (res , invalidMetric )
423- continue
424- }
425-
426- res = append (res , metric )
427-
428- if compatibleMode {
429- res = appendCompatibleMetric (res , m )
430- }
436+ if compatibleMode {
437+ res = appendCompatibleMetric (res , m )
431438 }
432439 }
433440 }
434-
435441 return res
436442}
437443
0 commit comments