github.com/ethersphere/bee/v2@v2.2.0/pkg/metrics/metrics.go (about)

     1  // Copyright 2020 The Swarm Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package metrics
     6  
     7  import (
     8  	"reflect"
     9  
    10  	"github.com/prometheus/client_golang/prometheus"
    11  )
    12  
    13  // Namespace is prefixed before every metric. If it is changed, it must be done
    14  // before any metrics collector is registered.
    15  var Namespace = "bee"
    16  
    17  type Collector interface {
    18  	Metrics() []prometheus.Collector
    19  }
    20  
    21  func PrometheusCollectorsFromFields(i interface{}) (cs []prometheus.Collector) {
    22  	v := reflect.Indirect(reflect.ValueOf(i))
    23  	for i := 0; i < v.NumField(); i++ {
    24  		if !v.Field(i).CanInterface() {
    25  			continue
    26  		}
    27  		if u, ok := v.Field(i).Interface().(prometheus.Collector); ok {
    28  			cs = append(cs, u)
    29  		}
    30  	}
    31  	return cs
    32  }