github.com/GuanceCloud/cliutils@v1.1.21/metrics/metrics.go (about)

     1  // Unless explicitly stated otherwise all files in this repository are licensed
     2  // under the MIT License.
     3  // This product includes software developed at Guance Cloud (https://www.guance.com/).
     4  // Copyright 2021-present Guance, Inc.
     5  
     6  // Package metrics implements datakit's Prometheus metrics
     7  package metrics
     8  
     9  import (
    10  	"bytes"
    11  
    12  	"github.com/prometheus/client_golang/prometheus"
    13  	"github.com/prometheus/client_golang/prometheus/collectors"
    14  	dto "github.com/prometheus/client_model/go"
    15  	"github.com/prometheus/common/expfmt"
    16  )
    17  
    18  var reg = prometheus.NewRegistry()
    19  
    20  // MustRegister add c to global registry and panic on any error.
    21  func MustRegister(c ...prometheus.Collector) {
    22  	reg.MustRegister(c...)
    23  }
    24  
    25  // Register add c to global registry.
    26  func Register(c prometheus.Collector) error {
    27  	return reg.Register(c)
    28  }
    29  
    30  // MustAddGolangMetrics enable Golang runtime metrics.
    31  func MustAddGolangMetrics() {
    32  	goexporter := collectors.NewGoCollector(collectors.WithGoCollectorRuntimeMetrics(collectors.MetricsAll))
    33  	MustRegister(goexporter)
    34  }
    35  
    36  // Unregister remove c from global registry.
    37  func Unregister(c prometheus.Collector) bool {
    38  	return reg.Unregister(c)
    39  }
    40  
    41  // Gather collect all metrics within global registry.
    42  func Gather() ([]*dto.MetricFamily, error) {
    43  	return reg.Gather()
    44  }
    45  
    46  // MustGather collect all metrics within global registry and panic on any error.
    47  func MustGather() []*dto.MetricFamily {
    48  	x, err := reg.Gather()
    49  	if err != nil {
    50  		panic(err.Error())
    51  	}
    52  	return x
    53  }
    54  
    55  func sameLabels(got []*dto.LabelPair, wanted ...string) bool {
    56  	if len(got) != len(wanted) {
    57  		return false
    58  	}
    59  
    60  	for i, w := range wanted {
    61  		if got[i].GetValue() != w {
    62  			return false
    63  		}
    64  	}
    65  
    66  	return true
    67  }
    68  
    69  // GetMetricOnLabels search mfs with wanted labels. wanted values order must be same as label names.
    70  func GetMetricOnLabels(mfs []*dto.MetricFamily, name string, wanted ...string) *dto.Metric {
    71  	for _, mf := range mfs {
    72  		if *mf.Name != name {
    73  			continue
    74  		}
    75  
    76  		for _, m := range mf.Metric {
    77  			if sameLabels(m.GetLabel(), wanted...) {
    78  				return m
    79  			}
    80  		}
    81  	}
    82  
    83  	return nil
    84  }
    85  
    86  // GetMetric with specific idx.
    87  func GetMetric(mfs []*dto.MetricFamily, name string, idx int) *dto.Metric {
    88  	for _, mf := range mfs {
    89  		if *mf.Name == name {
    90  			if len(mf.Metric) < idx {
    91  				return nil
    92  			}
    93  			return mf.Metric[idx]
    94  		}
    95  	}
    96  	return nil
    97  }
    98  
    99  // MetricFamily2Text convert metrics to text format.
   100  func MetricFamily2Text(mfs []*dto.MetricFamily) string {
   101  	buf := bytes.NewBuffer(nil)
   102  	for _, mf := range mfs {
   103  		if _, err := expfmt.MetricFamilyToText(buf, mf); err != nil {
   104  			return ""
   105  		}
   106  	}
   107  	return buf.String()
   108  }