Skip to content

Commit

Permalink
Prefer label maps, remove some slice vars
Browse files Browse the repository at this point in the history
  • Loading branch information
DrJosh9000 committed Sep 10, 2024
1 parent 2f27ad1 commit 152e57a
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions backend/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,18 @@ func (p *Prometheus) Serve(path, addr string) {
// Note: This is called once per agent token per interval
func (p *Prometheus) Collect(r *collector.Result) error {
for name, value := range r.Totals {
labelNames := []string{"cluster"}
gauge, ok := p.totals[name]
if !ok { // first time this metric has been seen so create a new gauge
gauge = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: fmt.Sprintf("buildkite_total_%s", camelToUnderscore(name)),
Help: fmt.Sprintf("Buildkite Total: %s", name),
}, labelNames)
}, []string{"cluster"})
prometheus.MustRegister(gauge)
p.totals[name] = gauge
}

// note that r.Cluster will be empty for unclustered agents, this label will be dropped by prometheus
gauge.WithLabelValues(r.Cluster).Set(float64(value))
gauge.With(prometheus.Labels{"cluster": r.Cluster}).Set(float64(value))
}

currentQueues := make(map[string]struct{})
Expand All @@ -72,17 +71,19 @@ func (p *Prometheus) Collect(r *collector.Result) error {
for name, value := range counts {
gauge, ok := p.queues[name]
if !ok { // first time this metric has been seen so create a new gauge
labelNames := []string{"queue", "cluster"}
gauge = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: fmt.Sprintf("buildkite_queues_%s", camelToUnderscore(name)),
Help: fmt.Sprintf("Buildkite Queues: %s", name),
}, labelNames)
}, []string{"queue", "cluster"})
prometheus.MustRegister(gauge)
p.queues[name] = gauge
}

// note that r.Cluster will be empty for unclustered agents, this label will be dropped by prometheus
gauge.WithLabelValues(queue, r.Cluster).Set(float64(value))
gauge.With(prometheus.Labels{
"cluster": r.Cluster,
"queue": queue,
}).Set(float64(value))
}
}

Expand Down

0 comments on commit 152e57a

Please sign in to comment.