github.com/GuanceCloud/cliutils@v1.1.21/pipeline/stats/metric.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 stats 7 8 import ( 9 "time" 10 11 "github.com/prometheus/client_golang/prometheus" 12 ) 13 14 var defaultLabelNames = []string{"category", "name", "namespace"} 15 16 type RecMetric struct { 17 plPtsVec, 18 plDropVec, 19 plErrPtsVec *prometheus.CounterVec 20 plCostVec *prometheus.SummaryVec 21 plUpdateVec *prometheus.GaugeVec 22 labeslNames []string 23 } 24 25 func (rec *RecMetric) WriteMetric(tags map[string]string, pt, ptDrop, ptError float64, cost time.Duration) { 26 lbVals := selectLabels(tags, rec.labeslNames) 27 28 if pt > 0 { 29 rec.plPtsVec.WithLabelValues(lbVals...).Add(pt) 30 } 31 32 if ptDrop > 0 { 33 rec.plDropVec.WithLabelValues(lbVals...).Add(ptDrop) 34 } 35 36 if ptError > 0 { 37 rec.plErrPtsVec.WithLabelValues(lbVals...).Add(ptError) 38 } 39 40 if cost > 0 { 41 rec.plCostVec.WithLabelValues(lbVals...).Observe(float64(cost) / float64(time.Second)) 42 } 43 } 44 45 func (rec *RecMetric) WriteUpdateTime(tags map[string]string) { 46 rec.plUpdateVec.WithLabelValues(selectLabels( 47 tags, rec.labeslNames)...).Set(float64(time.Now().Unix())) 48 } 49 50 func (rec *RecMetric) Metrics() []prometheus.Collector { 51 return []prometheus.Collector{ 52 rec.plPtsVec, 53 rec.plDropVec, 54 rec.plErrPtsVec, 55 rec.plCostVec, 56 rec.plUpdateVec, 57 } 58 } 59 60 func newRecMetric(namespace, subsystem string, labelNames []string) *RecMetric { 61 if len(labelNames) == 0 { 62 labelNames = defaultLabelNames 63 } 64 65 plPtsVec := prometheus.NewCounterVec( 66 prometheus.CounterOpts{ 67 Namespace: namespace, 68 Subsystem: subsystem, 69 Name: "point_total", 70 Help: "Pipeline processed total points", 71 }, labelNames, 72 ) 73 74 plDropVec := prometheus.NewCounterVec( 75 prometheus.CounterOpts{ 76 Namespace: namespace, 77 Subsystem: subsystem, 78 Name: "drop_point_total", 79 Help: "Pipeline total dropped points", 80 }, labelNames, 81 ) 82 83 plErrPtsVec := prometheus.NewCounterVec( 84 prometheus.CounterOpts{ 85 Namespace: namespace, 86 Subsystem: subsystem, 87 Name: "error_point_total", 88 Help: "Pipeline processed total error points", 89 }, labelNames, 90 ) 91 92 plCostVec := prometheus.NewSummaryVec( 93 prometheus.SummaryOpts{ 94 Namespace: namespace, 95 Subsystem: subsystem, 96 Name: "cost_seconds", 97 Help: "Pipeline total running time", 98 }, labelNames, 99 ) 100 101 plUpdateVec := prometheus.NewGaugeVec( 102 prometheus.GaugeOpts{ 103 Namespace: namespace, 104 Subsystem: subsystem, 105 Name: "last_update_timestamp_seconds", 106 Help: "Pipeline last update time", 107 }, labelNames, 108 ) 109 110 return &RecMetric{ 111 plPtsVec: plPtsVec, 112 plDropVec: plDropVec, 113 plErrPtsVec: plErrPtsVec, 114 plCostVec: plCostVec, 115 plUpdateVec: plUpdateVec, 116 labeslNames: labelNames, 117 } 118 } 119 120 func selectLabels(tags map[string]string, lb []string) []string { 121 if len(lb) == 0 { 122 lb = defaultLabelNames 123 } 124 125 lbVals := make([]string, len(lb)) 126 127 for i := range lb { 128 if val, ok := tags[lb[i]]; ok { 129 lbVals[i] = val 130 } 131 } 132 133 return lbVals 134 }