github.com/XiaoMi/Gaea@v1.2.5/stats/prometheus/collectors.go (about) 1 package prometheus 2 3 import ( 4 "strings" 5 6 "github.com/prometheus/client_golang/prometheus" 7 "github.com/XiaoMi/Gaea/stats" 8 ) 9 10 type metricFuncCollector struct { 11 // f returns the floating point value of the metric. 12 f func() float64 13 desc *prometheus.Desc 14 vt prometheus.ValueType 15 } 16 17 func newMetricFuncCollector(v stats.Variable, name string, vt prometheus.ValueType, f func() float64) { 18 collector := &metricFuncCollector{ 19 f: f, 20 desc: prometheus.NewDesc( 21 name, 22 v.Help(), 23 nil, 24 nil), 25 vt: vt} 26 27 prometheus.MustRegister(collector) 28 } 29 30 // Describe implements Collector. 31 func (mc *metricFuncCollector) Describe(ch chan<- *prometheus.Desc) { 32 ch <- mc.desc 33 } 34 35 // Collect implements Collector. 36 func (mc *metricFuncCollector) Collect(ch chan<- prometheus.Metric) { 37 ch <- prometheus.MustNewConstMetric(mc.desc, mc.vt, float64(mc.f())) 38 } 39 40 // countersWithSingleLabelCollector collects stats.CountersWithSingleLabel. 41 type countersWithSingleLabelCollector struct { 42 counters *stats.CountersWithSingleLabel 43 desc *prometheus.Desc 44 vt prometheus.ValueType 45 } 46 47 func newCountersWithSingleLabelCollector(c *stats.CountersWithSingleLabel, name string, labelName string, vt prometheus.ValueType) { 48 collector := &countersWithSingleLabelCollector{ 49 counters: c, 50 desc: prometheus.NewDesc( 51 name, 52 c.Help(), 53 []string{labelName}, 54 nil), 55 vt: vt} 56 57 prometheus.MustRegister(collector) 58 } 59 60 // Describe implements Collector. 61 func (c *countersWithSingleLabelCollector) Describe(ch chan<- *prometheus.Desc) { 62 ch <- c.desc 63 } 64 65 // Collect implements Collector. 66 func (c *countersWithSingleLabelCollector) Collect(ch chan<- prometheus.Metric) { 67 for tag, val := range c.counters.Counts() { 68 ch <- prometheus.MustNewConstMetric( 69 c.desc, 70 c.vt, 71 float64(val), 72 tag) 73 } 74 } 75 76 // gaugesWithSingleLabelCollector collects stats.GaugesWithSingleLabel. 77 type gaugesWithSingleLabelCollector struct { 78 gauges *stats.GaugesWithSingleLabel 79 desc *prometheus.Desc 80 vt prometheus.ValueType 81 } 82 83 func newGaugesWithSingleLabelCollector(g *stats.GaugesWithSingleLabel, name string, labelName string, vt prometheus.ValueType) { 84 collector := &gaugesWithSingleLabelCollector{ 85 gauges: g, 86 desc: prometheus.NewDesc( 87 name, 88 g.Help(), 89 []string{labelName}, 90 nil), 91 vt: vt} 92 93 prometheus.MustRegister(collector) 94 } 95 96 // Describe implements Collector. 97 func (g *gaugesWithSingleLabelCollector) Describe(ch chan<- *prometheus.Desc) { 98 ch <- g.desc 99 } 100 101 // Collect implements Collector. 102 func (g *gaugesWithSingleLabelCollector) Collect(ch chan<- prometheus.Metric) { 103 for tag, val := range g.gauges.Counts() { 104 ch <- prometheus.MustNewConstMetric( 105 g.desc, 106 g.vt, 107 float64(val), 108 tag) 109 } 110 } 111 112 type metricWithMultiLabelsCollector struct { 113 cml *stats.CountersWithMultiLabels 114 desc *prometheus.Desc 115 } 116 117 func newMetricWithMultiLabelsCollector(cml *stats.CountersWithMultiLabels, name string) { 118 c := &metricWithMultiLabelsCollector{ 119 cml: cml, 120 desc: prometheus.NewDesc( 121 name, 122 cml.Help(), 123 labelsToSnake(cml.Labels()), 124 nil), 125 } 126 127 prometheus.MustRegister(c) 128 } 129 130 // Describe implements Collector. 131 func (c *metricWithMultiLabelsCollector) Describe(ch chan<- *prometheus.Desc) { 132 ch <- c.desc 133 } 134 135 // Collect implements Collector. 136 func (c *metricWithMultiLabelsCollector) Collect(ch chan<- prometheus.Metric) { 137 for lvs, val := range c.cml.Counts() { 138 labelValues := strings.Split(lvs, ".") 139 value := float64(val) 140 ch <- prometheus.MustNewConstMetric(c.desc, prometheus.CounterValue, value, labelValues...) 141 } 142 } 143 144 type gaugesWithMultiLabelsCollector struct { 145 gml *stats.GaugesWithMultiLabels 146 desc *prometheus.Desc 147 } 148 149 func newGaugesWithMultiLabelsCollector(gml *stats.GaugesWithMultiLabels, name string) { 150 c := &gaugesWithMultiLabelsCollector{ 151 gml: gml, 152 desc: prometheus.NewDesc( 153 name, 154 gml.Help(), 155 labelsToSnake(gml.Labels()), 156 nil), 157 } 158 159 prometheus.MustRegister(c) 160 } 161 162 // Describe implements Collector. 163 func (c *gaugesWithMultiLabelsCollector) Describe(ch chan<- *prometheus.Desc) { 164 ch <- c.desc 165 } 166 167 // Collect implements Collector. 168 func (c *gaugesWithMultiLabelsCollector) Collect(ch chan<- prometheus.Metric) { 169 for lvs, val := range c.gml.Counts() { 170 labelValues := strings.Split(lvs, ".") 171 value := float64(val) 172 ch <- prometheus.MustNewConstMetric(c.desc, prometheus.GaugeValue, value, labelValues...) 173 } 174 } 175 176 type metricsFuncWithMultiLabelsCollector struct { 177 cfml *stats.CountersFuncWithMultiLabels 178 desc *prometheus.Desc 179 vt prometheus.ValueType 180 } 181 182 func newMetricsFuncWithMultiLabelsCollector(cfml *stats.CountersFuncWithMultiLabels, name string, vt prometheus.ValueType) { 183 collector := &metricsFuncWithMultiLabelsCollector{ 184 cfml: cfml, 185 desc: prometheus.NewDesc( 186 name, 187 cfml.Help(), 188 labelsToSnake(cfml.Labels()), 189 nil), 190 vt: vt, 191 } 192 193 prometheus.MustRegister(collector) 194 } 195 196 // Describe implements Collector. 197 func (c *metricsFuncWithMultiLabelsCollector) Describe(ch chan<- *prometheus.Desc) { 198 ch <- c.desc 199 } 200 201 // Collect implements Collector. 202 func (c *metricsFuncWithMultiLabelsCollector) Collect(ch chan<- prometheus.Metric) { 203 for lvs, val := range c.cfml.Counts() { 204 labelValues := strings.Split(lvs, ".") 205 value := float64(val) 206 ch <- prometheus.MustNewConstMetric(c.desc, c.vt, value, labelValues...) 207 } 208 } 209 210 type timingsCollector struct { 211 t *stats.Timings 212 cutoffs []float64 213 desc *prometheus.Desc 214 } 215 216 func newTimingsCollector(t *stats.Timings, name string) { 217 cutoffs := make([]float64, len(t.Cutoffs())) 218 for i, val := range t.Cutoffs() { 219 cutoffs[i] = float64(val) / 1000000000 220 } 221 222 collector := &timingsCollector{ 223 t: t, 224 cutoffs: cutoffs, 225 desc: prometheus.NewDesc( 226 name, 227 t.Help(), 228 []string{t.Label()}, 229 nil), 230 } 231 232 prometheus.MustRegister(collector) 233 } 234 235 // Describe implements Collector. 236 func (c *timingsCollector) Describe(ch chan<- *prometheus.Desc) { 237 ch <- c.desc 238 } 239 240 // Collect implements Collector. 241 func (c *timingsCollector) Collect(ch chan<- prometheus.Metric) { 242 for cat, his := range c.t.Histograms() { 243 ch <- prometheus.MustNewConstHistogram( 244 c.desc, 245 uint64(his.Count()), 246 float64(his.Total())/1000000000, 247 makeCumulativeBuckets(c.cutoffs, his.Buckets()), 248 cat) 249 } 250 } 251 252 func makeCumulativeBuckets(cutoffs []float64, buckets []int64) map[float64]uint64 { 253 output := make(map[float64]uint64) 254 last := uint64(0) 255 for i, key := range cutoffs { 256 //TODO(zmagg): int64 => uint64 conversion. error if it overflows? 257 output[key] = uint64(buckets[i]) + last 258 last = output[key] 259 } 260 return output 261 } 262 263 type multiTimingsCollector struct { 264 mt *stats.MultiTimings 265 cutoffs []float64 266 desc *prometheus.Desc 267 } 268 269 func newMultiTimingsCollector(mt *stats.MultiTimings, name string) { 270 cutoffs := make([]float64, len(mt.Cutoffs())) 271 for i, val := range mt.Cutoffs() { 272 cutoffs[i] = float64(val) / 1000000000 273 } 274 275 collector := &multiTimingsCollector{ 276 mt: mt, 277 cutoffs: cutoffs, 278 desc: prometheus.NewDesc( 279 name, 280 mt.Help(), 281 labelsToSnake(mt.Labels()), 282 nil), 283 } 284 285 prometheus.MustRegister(collector) 286 } 287 288 // Describe implements Collector. 289 func (c *multiTimingsCollector) Describe(ch chan<- *prometheus.Desc) { 290 ch <- c.desc 291 } 292 293 // Collect implements Collector. 294 func (c *multiTimingsCollector) Collect(ch chan<- prometheus.Metric) { 295 for cat, his := range c.mt.Timings.Histograms() { 296 labelValues := strings.Split(cat, ".") 297 ch <- prometheus.MustNewConstHistogram( 298 c.desc, 299 uint64(his.Count()), 300 float64(his.Total())/1000000000, 301 makeCumulativeBuckets(c.cutoffs, his.Buckets()), 302 labelValues...) 303 } 304 } 305 306 type histogramCollector struct { 307 h *stats.Histogram 308 cutoffs []float64 309 desc *prometheus.Desc 310 } 311 312 func newHistogramCollector(h *stats.Histogram, name string) { 313 cutoffs := make([]float64, len(h.Cutoffs())) 314 for i, val := range h.Cutoffs() { 315 cutoffs[i] = float64(val) 316 } 317 318 collector := &histogramCollector{ 319 h: h, 320 cutoffs: cutoffs, 321 desc: prometheus.NewDesc( 322 name, 323 h.Help(), 324 []string{}, 325 nil), 326 } 327 328 prometheus.MustRegister(collector) 329 } 330 331 // Describe implements Collector. 332 func (c *histogramCollector) Describe(ch chan<- *prometheus.Desc) { 333 ch <- c.desc 334 } 335 336 // Collect implements Collector. 337 func (c *histogramCollector) Collect(ch chan<- prometheus.Metric) { 338 ch <- prometheus.MustNewConstHistogram( 339 c.desc, 340 uint64(c.h.Count()), 341 float64(c.h.Total()), 342 makeCumulativeBuckets(c.cutoffs, c.h.Buckets()), 343 ) 344 }