123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311 |
- // Copyright (C) 2024 The Syncthing Authors.
- //
- // This Source Code Form is subject to the terms of the Mozilla Public
- // License, v. 2.0. If a copy of the MPL was not distributed with this file,
- // You can obtain one at https://mozilla.org/MPL/2.0/.
- package servemetrics
- import (
- "reflect"
- "slices"
- "strconv"
- "strings"
- "sync"
- "time"
- "github.com/prometheus/client_golang/prometheus"
- "github.com/syncthing/syncthing/lib/ur/contract"
- )
- const namePrefix = "syncthing_usage_"
- type metricsSet struct {
- srv *server
- gauges map[string]prometheus.Gauge
- gaugeVecs map[string]*prometheus.GaugeVec
- gaugeVecLabels map[string][]string
- summaries map[string]*metricSummary
- collectMut sync.Mutex
- }
- func newMetricsSet(srv *server) *metricsSet {
- s := &metricsSet{
- srv: srv,
- gauges: make(map[string]prometheus.Gauge),
- gaugeVecs: make(map[string]*prometheus.GaugeVec),
- gaugeVecLabels: make(map[string][]string),
- summaries: make(map[string]*metricSummary),
- }
- var initForType func(reflect.Type)
- initForType = func(t reflect.Type) {
- for i := 0; i < t.NumField(); i++ {
- field := t.Field(i)
- if field.Type.Kind() == reflect.Struct {
- initForType(field.Type)
- continue
- }
- name, typ, label := fieldNameTypeLabel(field)
- sname, labels := nameConstLabels(name)
- switch typ {
- case "gauge":
- s.gauges[name] = prometheus.NewGauge(prometheus.GaugeOpts{
- Name: namePrefix + sname,
- ConstLabels: labels,
- })
- case "summary":
- s.summaries[name] = newMetricSummary(namePrefix+sname, nil, labels)
- case "gaugeVec":
- s.gaugeVecLabels[name] = append(s.gaugeVecLabels[name], label)
- case "summaryVec":
- s.summaries[name] = newMetricSummary(namePrefix+sname, []string{label}, labels)
- }
- }
- }
- initForType(reflect.ValueOf(contract.Report{}).Type())
- for name, labels := range s.gaugeVecLabels {
- s.gaugeVecs[name] = prometheus.NewGaugeVec(prometheus.GaugeOpts{
- Name: namePrefix + name,
- }, labels)
- }
- return s
- }
- func fieldNameTypeLabel(rf reflect.StructField) (string, string, string) {
- metric := rf.Tag.Get("metric")
- name, typ, ok := strings.Cut(metric, ",")
- if !ok {
- return "", "", ""
- }
- gv, label, ok := strings.Cut(typ, ":")
- if ok {
- typ = gv
- }
- return name, typ, label
- }
- func nameConstLabels(name string) (string, prometheus.Labels) {
- if name == "-" {
- return "", nil
- }
- name, labels, ok := strings.Cut(name, "{")
- if !ok {
- return name, nil
- }
- lls := strings.Split(labels[:len(labels)-1], ",")
- m := make(map[string]string)
- for _, l := range lls {
- k, v, _ := strings.Cut(l, "=")
- m[k] = v
- }
- return name, m
- }
- func (s *metricsSet) addReport(r *contract.Report) {
- gaugeVecs := make(map[string][]string)
- s.addReportStruct(reflect.ValueOf(r).Elem(), gaugeVecs)
- for name, lv := range gaugeVecs {
- s.gaugeVecs[name].WithLabelValues(lv...).Add(1)
- }
- }
- func (s *metricsSet) addReportStruct(v reflect.Value, gaugeVecs map[string][]string) {
- t := v.Type()
- for i := 0; i < v.NumField(); i++ {
- field := v.Field(i)
- if field.Kind() == reflect.Struct {
- s.addReportStruct(field, gaugeVecs)
- continue
- }
- name, typ, label := fieldNameTypeLabel(t.Field(i))
- switch typ {
- case "gauge":
- switch v := field.Interface().(type) {
- case int:
- s.gauges[name].Add(float64(v))
- case string:
- s.gaugeVecs[name].WithLabelValues(v).Add(1)
- case bool:
- if v {
- s.gauges[name].Add(1)
- }
- }
- case "gaugeVec":
- var labelValue string
- switch v := field.Interface().(type) {
- case string:
- labelValue = v
- case int:
- labelValue = strconv.Itoa(v)
- case map[string]int:
- for k, v := range v {
- labelValue = k
- field.SetInt(int64(v))
- break
- }
- }
- if _, ok := gaugeVecs[name]; !ok {
- gaugeVecs[name] = make([]string, len(s.gaugeVecLabels[name]))
- }
- for i, l := range s.gaugeVecLabels[name] {
- if l == label {
- gaugeVecs[name][i] = labelValue
- break
- }
- }
- case "summary", "summaryVec":
- switch v := field.Interface().(type) {
- case int:
- s.summaries[name].Observe("", float64(v))
- case float64:
- s.summaries[name].Observe("", v)
- case []int:
- for _, v := range v {
- s.summaries[name].Observe("", float64(v))
- }
- case map[string]int:
- for k, v := range v {
- if k == "" {
- // avoid empty string labels as those are the sign
- // of a non-vec summary
- k = "unknown"
- }
- s.summaries[name].Observe(k, float64(v))
- }
- }
- }
- }
- }
- func (s *metricsSet) Describe(c chan<- *prometheus.Desc) {
- for _, g := range s.gauges {
- g.Describe(c)
- }
- for _, g := range s.gaugeVecs {
- g.Describe(c)
- }
- for _, g := range s.summaries {
- g.Describe(c)
- }
- }
- func (s *metricsSet) Collect(c chan<- prometheus.Metric) {
- s.collectMut.Lock()
- defer s.collectMut.Unlock()
- t0 := time.Now()
- defer func() {
- metricsCollectSecondsTotal.Add(time.Since(t0).Seconds())
- metricsCollectsTotal.Inc()
- }()
- for _, g := range s.gauges {
- g.Set(0)
- }
- for _, g := range s.gaugeVecs {
- g.Reset()
- }
- for _, g := range s.summaries {
- g.Reset()
- }
- cutoff := time.Now().Add(-24 * time.Hour)
- s.srv.reports.Range(func(key string, r *contract.Report) bool {
- if r.Received.Before(cutoff) {
- s.srv.reports.Delete(key)
- return true
- }
- s.addReport(r)
- return true
- })
- for _, g := range s.gauges {
- c <- g
- }
- for _, g := range s.gaugeVecs {
- g.Collect(c)
- }
- for _, g := range s.summaries {
- g.Collect(c)
- }
- }
- type metricSummary struct {
- name string
- values map[string][]float64
- zeroes map[string]int
- qDesc *prometheus.Desc
- countDesc *prometheus.Desc
- sumDesc *prometheus.Desc
- zDesc *prometheus.Desc
- }
- func newMetricSummary(name string, labels []string, constLabels prometheus.Labels) *metricSummary {
- return &metricSummary{
- name: name,
- values: make(map[string][]float64),
- zeroes: make(map[string]int),
- qDesc: prometheus.NewDesc(name, "", append(labels, "quantile"), constLabels),
- countDesc: prometheus.NewDesc(name+"_nonzero_count", "", labels, constLabels),
- sumDesc: prometheus.NewDesc(name+"_sum", "", labels, constLabels),
- zDesc: prometheus.NewDesc(name+"_zero_count", "", labels, constLabels),
- }
- }
- func (q *metricSummary) Observe(labelValue string, v float64) {
- if v == 0 {
- q.zeroes[labelValue]++
- return
- }
- q.values[labelValue] = append(q.values[labelValue], v)
- }
- func (q *metricSummary) Describe(c chan<- *prometheus.Desc) {
- c <- q.qDesc
- c <- q.countDesc
- c <- q.sumDesc
- c <- q.zDesc
- }
- func (q *metricSummary) Collect(c chan<- prometheus.Metric) {
- for lv, vs := range q.values {
- var labelVals []string
- if lv != "" {
- labelVals = []string{lv}
- }
- c <- prometheus.MustNewConstMetric(q.countDesc, prometheus.GaugeValue, float64(len(vs)), labelVals...)
- c <- prometheus.MustNewConstMetric(q.zDesc, prometheus.GaugeValue, float64(q.zeroes[lv]), labelVals...)
- var sum float64
- for _, v := range vs {
- sum += v
- }
- c <- prometheus.MustNewConstMetric(q.sumDesc, prometheus.GaugeValue, sum, labelVals...)
- if len(vs) == 0 {
- return
- }
- slices.Sort(vs)
- c <- prometheus.MustNewConstMetric(q.qDesc, prometheus.GaugeValue, vs[0], append(labelVals, "0")...)
- c <- prometheus.MustNewConstMetric(q.qDesc, prometheus.GaugeValue, vs[len(vs)*5/100], append(labelVals, "0.05")...)
- c <- prometheus.MustNewConstMetric(q.qDesc, prometheus.GaugeValue, vs[len(vs)/2], append(labelVals, "0.5")...)
- c <- prometheus.MustNewConstMetric(q.qDesc, prometheus.GaugeValue, vs[len(vs)*9/10], append(labelVals, "0.9")...)
- c <- prometheus.MustNewConstMetric(q.qDesc, prometheus.GaugeValue, vs[len(vs)*95/100], append(labelVals, "0.95")...)
- c <- prometheus.MustNewConstMetric(q.qDesc, prometheus.GaugeValue, vs[len(vs)-1], append(labelVals, "1")...)
- }
- }
- func (q *metricSummary) Reset() {
- clear(q.values)
- clear(q.zeroes)
- }
|