Skip to content

Commit

Permalink
Merge pull request #234 from buildkite/pdp-1943-ignore-cluster-if-it-…
Browse files Browse the repository at this point in the history
…is-empty
  • Loading branch information
triarius authored Nov 27, 2023
2 parents c867087 + a2fd1bc commit 3303fca
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
8 changes: 6 additions & 2 deletions backend/cloudwatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,14 @@ func (cb *CloudWatchBackend) Collect(r *collector.Result) error {
Name: aws.String("Org"),
Value: aws.String(r.Org),
},
{
}

// Add cluster dimension if a cluster token was used
if r.Cluster != "" {
dimensions = append(dimensions, &cloudwatch.Dimension{
Name: aws.String("Cluster"),
Value: aws.String(r.Cluster),
},
})
}

// Add custom dimension if provided
Expand Down
7 changes: 5 additions & 2 deletions backend/newrelic.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,11 @@ func (nr *NewRelicBackend) Collect(r *collector.Result) error {
// toCustomEvent converts a map of metrics to a valid New Relic event body
func toCustomEvent(clusterName, queueName string, queueMetrics map[string]int) map[string]any {
eventData := map[string]any{
"Cluster": clusterName,
"Queue": queueName,
"Queue": queueName,
}

if clusterName != "" {
eventData["Cluster"] = clusterName
}

for k, v := range queueMetrics {
Expand Down
26 changes: 21 additions & 5 deletions backend/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,37 +39,53 @@ func (p *Prometheus) Serve(path, addr string) {
}

func (p *Prometheus) Collect(r *collector.Result) error {

// Clear the gauges to prevent stale values from persisting forever.
for _, gauge := range p.queues {
gauge.Reset()
}

for name, value := range r.Totals {
labelNames := []string{}
if r.Cluster != "" {
labelNames = append(labelNames, "cluster")
}
gauge, ok := p.totals[name]
if !ok {
gauge = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: fmt.Sprintf("buildkite_total_%s", camelToUnderscore(name)),
Help: fmt.Sprintf("Buildkite Total: %s", name),
}, []string{"cluster"})
}, labelNames)
prometheus.MustRegister(gauge)
p.totals[name] = gauge
}
gauge.WithLabelValues(r.Cluster).Set(float64(value))

if r.Cluster != "" {
gauge.WithLabelValues(r.Cluster).Set(float64(value))
} else {
gauge.WithLabelValues().Set(float64(value))
}
}

for queue, counts := range r.Queues {
for name, value := range counts {
gauge, ok := p.queues[name]
if !ok {
labelNames := []string{"queue"}
if r.Cluster != "" {
labelNames = append(labelNames, "cluster")
}
gauge = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: fmt.Sprintf("buildkite_queues_%s", camelToUnderscore(name)),
Help: fmt.Sprintf("Buildkite Queues: %s", name),
}, []string{"cluster", "queue"})
}, labelNames)
prometheus.MustRegister(gauge)
p.queues[name] = gauge
}
gauge.WithLabelValues(r.Cluster, queue).Set(float64(value))
if r.Cluster != "" {
gauge.WithLabelValues(queue, r.Cluster).Set(float64(value))
} else {
gauge.WithLabelValues(queue).Set(float64(value))
}
}
}

Expand Down

0 comments on commit 3303fca

Please sign in to comment.