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