gitee.com/liu-zhao234568/cntest@v1.0.0/metrics/prometheus/collector.go (about) 1 // Copyright 2019 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package prometheus 18 19 import ( 20 "bytes" 21 "fmt" 22 "strconv" 23 "strings" 24 25 "gitee.com/liu-zhao234568/cntest/metrics" 26 ) 27 28 var ( 29 typeGaugeTpl = "# TYPE %s gauge\n" 30 typeCounterTpl = "# TYPE %s counter\n" 31 typeSummaryTpl = "# TYPE %s summary\n" 32 keyValueTpl = "%s %v\n\n" 33 keyQuantileTagValueTpl = "%s {quantile=\"%s\"} %v\n" 34 ) 35 36 // collector is a collection of byte buffers that aggregate Prometheus reports 37 // for different metric types. 38 type collector struct { 39 buff *bytes.Buffer 40 } 41 42 // newCollector creates a new Prometheus metric aggregator. 43 func newCollector() *collector { 44 return &collector{ 45 buff: &bytes.Buffer{}, 46 } 47 } 48 49 func (c *collector) addCounter(name string, m metrics.Counter) { 50 c.writeGaugeCounter(name, m.Count()) 51 } 52 53 func (c *collector) addGauge(name string, m metrics.Gauge) { 54 c.writeGaugeCounter(name, m.Value()) 55 } 56 57 func (c *collector) addGaugeFloat64(name string, m metrics.GaugeFloat64) { 58 c.writeGaugeCounter(name, m.Value()) 59 } 60 61 func (c *collector) addHistogram(name string, m metrics.Histogram) { 62 pv := []float64{0.5, 0.75, 0.95, 0.99, 0.999, 0.9999} 63 ps := m.Percentiles(pv) 64 c.writeSummaryCounter(name, m.Count()) 65 c.buff.WriteString(fmt.Sprintf(typeSummaryTpl, mutateKey(name))) 66 for i := range pv { 67 c.writeSummaryPercentile(name, strconv.FormatFloat(pv[i], 'f', -1, 64), ps[i]) 68 } 69 c.buff.WriteRune('\n') 70 } 71 72 func (c *collector) addMeter(name string, m metrics.Meter) { 73 c.writeGaugeCounter(name, m.Count()) 74 } 75 76 func (c *collector) addTimer(name string, m metrics.Timer) { 77 pv := []float64{0.5, 0.75, 0.95, 0.99, 0.999, 0.9999} 78 ps := m.Percentiles(pv) 79 c.writeSummaryCounter(name, m.Count()) 80 c.buff.WriteString(fmt.Sprintf(typeSummaryTpl, mutateKey(name))) 81 for i := range pv { 82 c.writeSummaryPercentile(name, strconv.FormatFloat(pv[i], 'f', -1, 64), ps[i]) 83 } 84 c.buff.WriteRune('\n') 85 } 86 87 func (c *collector) addResettingTimer(name string, m metrics.ResettingTimer) { 88 if len(m.Values()) <= 0 { 89 return 90 } 91 ps := m.Percentiles([]float64{50, 95, 99}) 92 val := m.Values() 93 c.writeSummaryCounter(name, len(val)) 94 c.buff.WriteString(fmt.Sprintf(typeSummaryTpl, mutateKey(name))) 95 c.writeSummaryPercentile(name, "0.50", ps[0]) 96 c.writeSummaryPercentile(name, "0.95", ps[1]) 97 c.writeSummaryPercentile(name, "0.99", ps[2]) 98 c.buff.WriteRune('\n') 99 } 100 101 func (c *collector) writeGaugeCounter(name string, value interface{}) { 102 name = mutateKey(name) 103 c.buff.WriteString(fmt.Sprintf(typeGaugeTpl, name)) 104 c.buff.WriteString(fmt.Sprintf(keyValueTpl, name, value)) 105 } 106 107 func (c *collector) writeSummaryCounter(name string, value interface{}) { 108 name = mutateKey(name + "_count") 109 c.buff.WriteString(fmt.Sprintf(typeCounterTpl, name)) 110 c.buff.WriteString(fmt.Sprintf(keyValueTpl, name, value)) 111 } 112 113 func (c *collector) writeSummaryPercentile(name, p string, value interface{}) { 114 name = mutateKey(name) 115 c.buff.WriteString(fmt.Sprintf(keyQuantileTagValueTpl, name, p, value)) 116 } 117 118 func mutateKey(key string) string { 119 return strings.Replace(key, "/", "_", -1) 120 }