diff options
author | Patrick Steinhardt <psteinhardt@gitlab.com> | 2021-06-09 15:40:27 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2021-06-14 08:52:28 +0300 |
commit | cdf1a7d59b84ee1097be4e88df3f885171c028e7 (patch) | |
tree | 393c6d97deb4851416b2a82c083a007011bfe806 | |
parent | 64cda09950f43fdb7072f22755a8bb05c9d5f589 (diff) |
blackbox: Move Prometheus metrics into Blackbox struct
Now that we have a Blackbox structure which can host all dependencies,
let's move Prometheus metrics into this structure to get rid of one more
source of globals.
-rw-r--r-- | cmd/gitaly-blackbox/main.go | 2 | ||||
-rw-r--r-- | internal/blackbox/blackbox.go | 64 | ||||
-rw-r--r-- | internal/blackbox/prometheus.go | 29 |
3 files changed, 57 insertions, 38 deletions
diff --git a/cmd/gitaly-blackbox/main.go b/cmd/gitaly-blackbox/main.go index 0c96fa144..bcf31a194 100644 --- a/cmd/gitaly-blackbox/main.go +++ b/cmd/gitaly-blackbox/main.go @@ -6,6 +6,7 @@ import ( "io/ioutil" "os" + "github.com/prometheus/client_golang/prometheus" "github.com/sirupsen/logrus" "gitlab.com/gitlab-org/gitaly/v14/internal/blackbox" "gitlab.com/gitlab-org/gitaly/v14/internal/log" @@ -54,6 +55,7 @@ func run(configPath string) error { } bb := blackbox.New(config) + prometheus.MustRegister(bb) log.Configure(log.Loggers, config.Logging.Format, config.Logging.Level) diff --git a/internal/blackbox/blackbox.go b/internal/blackbox/blackbox.go index 34d2d45b4..7fc3fc22d 100644 --- a/internal/blackbox/blackbox.go +++ b/internal/blackbox/blackbox.go @@ -15,15 +15,61 @@ import ( // Blackbox encapsulates all details required to run the blackbox prober. type Blackbox struct { cfg Config + + getFirstPacket *prometheus.GaugeVec + getTotalTime *prometheus.GaugeVec + getAdvertisedRefs *prometheus.GaugeVec + wantedRefs *prometheus.GaugeVec + postTotalTime *prometheus.GaugeVec + postFirstProgressPacket *prometheus.GaugeVec + postFirstPackPacket *prometheus.GaugeVec + postPackBytes *prometheus.GaugeVec } // New creates a new Blackbox structure. func New(cfg Config) Blackbox { return Blackbox{ - cfg: cfg, + cfg: cfg, + getFirstPacket: newGauge("get_first_packet_seconds", "Time to first Git packet in GET /info/refs response"), + getTotalTime: newGauge("get_total_time_seconds", "Time to receive entire GET /info/refs response"), + getAdvertisedRefs: newGauge("get_advertised_refs", "Number of Git refs advertised in GET /info/refs"), + wantedRefs: newGauge("wanted_refs", "Number of Git refs selected for (fake) Git clone (branches + tags)"), + postTotalTime: newGauge("post_total_time_seconds", "Time to receive entire POST /upload-pack response"), + postFirstProgressPacket: newGauge("post_first_progress_packet_seconds", "Time to first progress band Git packet in POST /upload-pack response"), + postFirstPackPacket: newGauge("post_first_pack_packet_seconds", "Time to first pack band Git packet in POST /upload-pack response"), + postPackBytes: newGauge("post_pack_bytes", "Number of pack band bytes in POST /upload-pack response"), } } +func newGauge(name string, help string) *prometheus.GaugeVec { + return prometheus.NewGaugeVec( + prometheus.GaugeOpts{ + Namespace: "gitaly_blackbox", + Subsystem: "git_http", + Name: name, + Help: help, + }, + []string{"probe"}, + ) +} + +// Describe is used to describe Prometheus metrics. +func (b Blackbox) Describe(descs chan<- *prometheus.Desc) { + prometheus.DescribeByCollect(b, descs) +} + +// Collect is used to collect Prometheus metrics. +func (b Blackbox) Collect(metrics chan<- prometheus.Metric) { + b.getFirstPacket.Collect(metrics) + b.getTotalTime.Collect(metrics) + b.getAdvertisedRefs.Collect(metrics) + b.wantedRefs.Collect(metrics) + b.postTotalTime.Collect(metrics) + b.postFirstProgressPacket.Collect(metrics) + b.postFirstPackPacket.Collect(metrics) + b.postPackBytes.Collect(metrics) +} + // Run starts the blackbox. It sets up and serves the Prometheus listener and starts a Goroutine // which runs the probes. func (b Blackbox) Run() error { @@ -76,12 +122,12 @@ func (b Blackbox) doProbe(probe Probe) { gv.WithLabelValues(probe.Name).Set(value) } - setGauge(getFirstPacket, clone.Get.FirstGitPacket().Seconds()) - setGauge(getTotalTime, clone.Get.ResponseBody().Seconds()) - setGauge(getAdvertisedRefs, float64(len(clone.Get.Refs))) - setGauge(wantedRefs, float64(clone.RefsWanted())) - setGauge(postTotalTime, clone.Post.ResponseBody().Seconds()) - setGauge(postFirstProgressPacket, clone.Post.BandFirstPacket("progress").Seconds()) - setGauge(postFirstPackPacket, clone.Post.BandFirstPacket("pack").Seconds()) - setGauge(postPackBytes, float64(clone.Post.BandPayloadSize("pack"))) + setGauge(b.getFirstPacket, clone.Get.FirstGitPacket().Seconds()) + setGauge(b.getTotalTime, clone.Get.ResponseBody().Seconds()) + setGauge(b.getAdvertisedRefs, float64(len(clone.Get.Refs))) + setGauge(b.wantedRefs, float64(clone.RefsWanted())) + setGauge(b.postTotalTime, clone.Post.ResponseBody().Seconds()) + setGauge(b.postFirstProgressPacket, clone.Post.BandFirstPacket("progress").Seconds()) + setGauge(b.postFirstPackPacket, clone.Post.BandFirstPacket("pack").Seconds()) + setGauge(b.postPackBytes, float64(clone.Post.BandPayloadSize("pack"))) } diff --git a/internal/blackbox/prometheus.go b/internal/blackbox/prometheus.go deleted file mode 100644 index 7929ad5e4..000000000 --- a/internal/blackbox/prometheus.go +++ /dev/null @@ -1,29 +0,0 @@ -package blackbox - -import ( - "github.com/prometheus/client_golang/prometheus" - "github.com/prometheus/client_golang/prometheus/promauto" -) - -var ( - getFirstPacket = newGauge("get_first_packet_seconds", "Time to first Git packet in GET /info/refs response") - getTotalTime = newGauge("get_total_time_seconds", "Time to receive entire GET /info/refs response") - getAdvertisedRefs = newGauge("get_advertised_refs", "Number of Git refs advertised in GET /info/refs") - wantedRefs = newGauge("wanted_refs", "Number of Git refs selected for (fake) Git clone (branches + tags)") - postTotalTime = newGauge("post_total_time_seconds", "Time to receive entire POST /upload-pack response") - postFirstProgressPacket = newGauge("post_first_progress_packet_seconds", "Time to first progress band Git packet in POST /upload-pack response") - postFirstPackPacket = newGauge("post_first_pack_packet_seconds", "Time to first pack band Git packet in POST /upload-pack response") - postPackBytes = newGauge("post_pack_bytes", "Number of pack band bytes in POST /upload-pack response") -) - -func newGauge(name string, help string) *prometheus.GaugeVec { - return promauto.NewGaugeVec( - prometheus.GaugeOpts{ - Namespace: "gitaly_blackbox", - Subsystem: "git_http", - Name: name, - Help: help, - }, - []string{"probe"}, - ) -} |