package prometheus import ( "context" "fmt" "strings" "sync" "time" "github.com/prometheus/common/model" ) type Gauge struct { wg *sync.WaitGroup name string labels map[string]string rangeSelector string baseTime time.Time minutes int64 } func NewGauge(wg *sync.WaitGroup, name string, labels map[string]string, ts time.Time, mins int64) Gauge { rangeSelector := fmt.Sprintf("%dm", mins) return Gauge{ wg: wg, name: name, labels: labels, rangeSelector: rangeSelector, baseTime: ts, minutes: mins, } } func (g Gauge) makeMetricNameLabels(name string) string { labelList := make([]string, 0, len(g.labels)) for key, val := range g.labels { labelList = append(labelList, fmt.Sprintf("%s='%s'", key, val)) } return fmt.Sprintf("%s{%s}", name, strings.Join(labelList, ",")) } func (g Gauge) Max(res *float64) error { defer g.wg.Done() nameLabels := g.makeMetricNameLabels(g.name) query := fmt.Sprintf("max(max_over_time(%s[%s]))", nameLabels, g.rangeSelector) result, _, err := PromeApi().Query(context.Background(), query, g.baseTime) if err != nil { return err } for _, sample := range result.(model.Vector) { *res = float64(sample.Value) } return nil }