github.com/sj-piano/go-ethereum@v1.9.7/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 "github.com/ethereum/go-ethereum/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\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 createa 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 for i := range pv { 66 c.writeSummaryPercentile(name, strconv.FormatFloat(pv[i], 'f', -1, 64), ps[i]) 67 } 68 } 69 70 func (c *collector) addMeter(name string, m metrics.Meter) { 71 c.writeGaugeCounter(name, m.Count()) 72 } 73 74 func (c *collector) addTimer(name string, m metrics.Timer) { 75 pv := []float64{0.5, 0.75, 0.95, 0.99, 0.999, 0.9999} 76 ps := m.Percentiles(pv) 77 c.writeSummaryCounter(name, m.Count()) 78 for i := range pv { 79 c.writeSummaryPercentile(name, strconv.FormatFloat(pv[i], 'f', -1, 64), ps[i]) 80 } 81 } 82 83 func (c *collector) addResettingTimer(name string, m metrics.ResettingTimer) { 84 if len(m.Values()) <= 0 { 85 return 86 } 87 ps := m.Percentiles([]float64{50, 95, 99}) 88 val := m.Values() 89 c.writeSummaryCounter(name, len(val)) 90 c.writeSummaryPercentile(name, "0.50", ps[0]) 91 c.writeSummaryPercentile(name, "0.95", ps[1]) 92 c.writeSummaryPercentile(name, "0.99", ps[2]) 93 } 94 95 func (c *collector) writeGaugeCounter(name string, value interface{}) { 96 name = mutateKey(name) 97 c.buff.WriteString(fmt.Sprintf(typeGaugeTpl, name)) 98 c.buff.WriteString(fmt.Sprintf(keyValueTpl, name, value)) 99 } 100 101 func (c *collector) writeSummaryCounter(name string, value interface{}) { 102 name = mutateKey(name + "_count") 103 c.buff.WriteString(fmt.Sprintf(typeCounterTpl, name)) 104 c.buff.WriteString(fmt.Sprintf(keyValueTpl, name, value)) 105 } 106 107 func (c *collector) writeSummaryPercentile(name, p string, value interface{}) { 108 name = mutateKey(name) 109 c.buff.WriteString(fmt.Sprintf(typeSummaryTpl, name)) 110 c.buff.WriteString(fmt.Sprintf(keyQuantileTagValueTpl, name, p, value)) 111 } 112 113 func mutateKey(key string) string { 114 return strings.Replace(key, "/", "_", -1) 115 }