From 30535cd1f4eaab7b2aa5e00b3e576f439594f1b6 Mon Sep 17 00:00:00 2001 From: Zephyr He <2825841950@qq.com> Date: Wed, 27 Nov 2024 13:23:11 +0000 Subject: [PATCH] feat(collector): add Bamboo API metrics fetching and Prometheus integration --- collector/collector.go | 185 +++++++++++++++++++++++++++++++++++++++++ go.mod | 2 + go.sum | 23 +++++ 3 files changed, 210 insertions(+) create mode 100644 collector/collector.go diff --git a/collector/collector.go b/collector/collector.go new file mode 100644 index 0000000..554b8f7 --- /dev/null +++ b/collector/collector.go @@ -0,0 +1,185 @@ +package collector + +import ( + "crypto/tls" + "encoding/json" + "errors" + "fmt" + "io" + "net/http" + "sync" + + "github.com/go-kit/log" + "github.com/go-kit/log/level" + "github.com/prometheus/client_golang/prometheus" +) + +const namespace = "bamboo" + +// Exporter collects metrics from Bamboo and exposes them to Prometheus. +type Exporter struct { + URI string + client *http.Client + mutex sync.Mutex + up *prometheus.Desc + failures prometheus.Counter + agents *prometheus.GaugeVec + queue prometheus.Gauge + logger log.Logger +} + +// Config holds the configuration for the exporter. +type Config struct { + ScrapeURI string + Insecure bool +} + +// BambooAgent represents an agent's data fetched from the Bamboo API. +type BambooAgent struct { + ID int64 `json:"id"` + Name string `json:"name"` + IsActive bool `json:"active"` + IsBusy bool `json:"busy"` +} + +// BambooQueue represents the build queue data fetched from the Bamboo API. +type BambooQueue struct { + QueuedBuilds struct { + Size int64 `json:"size"` + } `json:"queuedBuilds"` +} + +// NewExporter creates a new instance of Exporter. +func NewExporter(config *Config, logger log.Logger) *Exporter { + return &Exporter{ + URI: config.ScrapeURI, + client: &http.Client{ + Transport: &http.Transport{ + TLSClientConfig: &tls.Config{InsecureSkipVerify: config.Insecure}, + }, + }, + up: prometheus.NewDesc( + prometheus.BuildFQName(namespace, "", "up"), + "Whether the Bamboo API is reachable.", + nil, + nil, + ), + failures: prometheus.NewCounter(prometheus.CounterOpts{ + Namespace: namespace, + Name: "scrape_failures_total", + Help: "Total number of scrape failures.", + }), + agents: prometheus.NewGaugeVec(prometheus.GaugeOpts{ + Namespace: namespace, + Name: "agents_status", + Help: "Status of Bamboo agents (active/busy).", + }, []string{"name", "status"}), + queue: prometheus.NewGauge(prometheus.GaugeOpts{ + Namespace: namespace, + Name: "queue_size", + Help: "Number of builds in the Bamboo queue.", + }), + logger: logger, + } +} + +// Describe describes the Prometheus metrics for the exporter. +func (e *Exporter) Describe(ch chan<- *prometheus.Desc) { + ch <- e.up + e.failures.Describe(ch) + e.agents.Describe(ch) + e.queue.Describe(ch) +} + +// Collect collects metrics from Bamboo and sends them to Prometheus. +func (e *Exporter) Collect(ch chan<- prometheus.Metric) { + e.mutex.Lock() + defer e.mutex.Unlock() + + success := e.scrapeMetrics(ch) + ch <- prometheus.MustNewConstMetric(e.up, prometheus.GaugeValue, success) + e.failures.Collect(ch) + e.agents.Collect(ch) + e.queue.Collect(ch) +} + +// scrapeMetrics fetches metrics from Bamboo and processes them. +func (e *Exporter) scrapeMetrics(ch chan<- prometheus.Metric) float64 { + if err := e.scrapeAgents(); err != nil { + level.Error(e.logger).Log("msg", "Failed to scrape agents", "err", err) + e.failures.Inc() + return 0 + } + + if err := e.scrapeQueue(); err != nil { + level.Error(e.logger).Log("msg", "Failed to scrape queue", "err", err) + e.failures.Inc() + return 0 + } + + return 1 +} + +// scrapeAgents fetches and processes agent metrics from Bamboo. +func (e *Exporter) scrapeAgents() error { + data, err := e.doRequest("/rest/api/latest/agent") + if err != nil { + return fmt.Errorf("error fetching agents: %w", err) + } + + var agents []BambooAgent + if err := json.Unmarshal(data, &agents); err != nil { + return fmt.Errorf("error unmarshaling agents: %w", err) + } + + e.agents.Reset() + for _, agent := range agents { + status := "inactive" + if agent.IsActive { + status = "active" + } + e.agents.WithLabelValues(agent.Name, status).Set(1) + + if agent.IsBusy { + e.agents.WithLabelValues(agent.Name, "busy").Set(1) + } + } + return nil +} + +// scrapeQueue fetches and processes queue metrics from Bamboo. +func (e *Exporter) scrapeQueue() error { + data, err := e.doRequest("/rest/api/latest/queue") + if err != nil { + return fmt.Errorf("error fetching queue: %w", err) + } + + var queue BambooQueue + if err := json.Unmarshal(data, &queue); err != nil { + return fmt.Errorf("error unmarshaling queue: %w", err) + } + + e.queue.Set(float64(queue.QueuedBuilds.Size)) + return nil +} + +// doRequest sends a GET request to the Bamboo API and returns the response body. +func (e *Exporter) doRequest(endpoint string) ([]byte, error) { + req, err := http.NewRequest("GET", e.URI+endpoint, nil) + if err != nil { + return nil, fmt.Errorf("error creating request: %w", err) + } + + req.Header.Set("Accept", "application/json") + resp, err := e.client.Do(req) + if err != nil { + return nil, fmt.Errorf("error performing request: %w", err) + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + return nil, errors.New("unexpected status code: " + resp.Status) + } + + return io.ReadAll(resp.Body) +} diff --git a/go.mod b/go.mod index fe5534a..ccfc184 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.23.1 require ( github.com/alecthomas/kingpin/v2 v2.4.0 + github.com/go-kit/log v0.2.1 github.com/prometheus/client_golang v1.20.4 github.com/prometheus/common v0.60.0 github.com/prometheus/exporter-toolkit v0.13.0 @@ -14,6 +15,7 @@ require ( github.com/beorn7/perks v1.0.1 // indirect github.com/cespare/xxhash/v2 v2.3.0 // indirect github.com/coreos/go-systemd/v22 v22.5.0 // indirect + github.com/go-logfmt/logfmt v0.5.1 // indirect github.com/jpillora/backoff v1.0.0 // indirect github.com/klauspost/compress v1.17.9 // indirect github.com/mdlayher/socket v0.4.1 // indirect diff --git a/go.sum b/go.sum index 95e6397..03b3316 100644 --- a/go.sum +++ b/go.sum @@ -9,11 +9,25 @@ github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XL github.com/coreos/go-systemd/v22 v22.5.0 h1:RrqgGjYQKalulkV8NGVIfkXQf6YYmOyiJKk8iXXhfZs= github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/go-kit/log v0.2.1 h1:MRVx0/zhvdseW+Gza6N9rVzU/IVzaeE1SFI4raAhmBU= +github.com/go-kit/log v0.2.1/go.mod h1:NwTd00d/i8cPZ3xOwwiv2PO5MOcx78fFErGNcVmBjv0= +github.com/go-logfmt/logfmt v0.5.1 h1:otpy5pqBCBZ1ng9RQ0dPu4PN7ba75Y/aA+UpowDyNVA= +github.com/go-logfmt/logfmt v0.5.1/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs= github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= +github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/jpillora/backoff v1.0.0 h1:uvFg412JmmHBHw7iwprIxkPMI+sGQ4kzOWsMeHnm2EA= github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4= github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA= github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw= +github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= +github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc= +github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= github.com/mdlayher/socket v0.4.1 h1:eM9y2/jlbs1M615oshPQOHZzj6R6wMT7bX5NPiQvn2U= github.com/mdlayher/socket v0.4.1/go.mod h1:cAqeGjoufqdxWkD7DkpyS+wcefOtmu5OQ8KuoJGIReA= github.com/mdlayher/vsock v1.2.1 h1:pC1mTJTvjo1r9n9fbm7S1j04rCgCzhCOS5DY0zqHlnQ= @@ -22,6 +36,7 @@ github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ= github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f h1:KUppIJq7/+SVif2QVs3tOP0zanoHgBEVAwHxUSIzRqU= github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prometheus/client_golang v1.20.4 h1:Tgh3Yr67PaOv/uTqloMsCEdeuFTatm5zIq5+qNN23vI= github.com/prometheus/client_golang v1.20.4/go.mod h1:PIEt8X02hGcP8JWbeHyeZ53Y/jReSnHgO035n//V5WE= @@ -33,8 +48,12 @@ github.com/prometheus/exporter-toolkit v0.13.0 h1:lmA0Q+8IaXgmFRKw09RldZmZdnvu9w github.com/prometheus/exporter-toolkit v0.13.0/go.mod h1:2uop99EZl80KdXhv/MxVI2181fMcwlsumFOqBecGkG0= github.com/prometheus/procfs v0.15.1 h1:YagwOFzUgYfKKHX6Dr+sHT7km/hxC76UB0learggepc= github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoGhij/e3PBqk= +github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= +github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/xhit/go-str2duration/v2 v2.1.0 h1:lxklc02Drh6ynqX+DdPyp5pCKLUQpRT8bp8Ydu2Bstc= github.com/xhit/go-str2duration/v2 v2.1.0/go.mod h1:ohY8p+0f07DiV6Em5LKB0s2YpLtXVyJfNt1+BlmyAsU= golang.org/x/crypto v0.27.0 h1:GXm2NjJrPaiv/h1tb2UH8QfgC/hOf/+z0p6PT8o1w7A= @@ -52,6 +71,10 @@ golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg= google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=