1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
|
package blackbox
import (
"context"
"fmt"
"net"
"os"
"runtime/debug"
"time"
"github.com/prometheus/client_golang/prometheus"
"gitlab.com/gitlab-org/gitaly/v16/internal/git"
"gitlab.com/gitlab-org/gitaly/v16/internal/git/stats"
"gitlab.com/gitlab-org/gitaly/v16/internal/log"
"gitlab.com/gitlab-org/labkit/monitoring"
)
type httpReferenceDiscoveryMetrics struct {
firstPacket *prometheus.GaugeVec
totalTime *prometheus.GaugeVec
advertisedRefs *prometheus.GaugeVec
}
func (m httpReferenceDiscoveryMetrics) measure(probeName string, rd stats.HTTPReferenceDiscovery) {
m.firstPacket.WithLabelValues(probeName).Set(rd.FirstGitPacket().Seconds())
m.totalTime.WithLabelValues(probeName).Set(rd.ResponseBody().Seconds())
m.advertisedRefs.WithLabelValues(probeName).Set(float64(len(rd.Refs())))
}
// Describe is used to describe Prometheus metrics.
func (m httpReferenceDiscoveryMetrics) Describe(descs chan<- *prometheus.Desc) {
prometheus.DescribeByCollect(m, descs)
}
// Collect is used to collect Prometheus metrics.
func (m httpReferenceDiscoveryMetrics) Collect(metrics chan<- prometheus.Metric) {
m.firstPacket.Collect(metrics)
m.totalTime.Collect(metrics)
m.advertisedRefs.Collect(metrics)
}
type httpPostStats interface {
ResponseBody() time.Duration
BandFirstPacket(b string) time.Duration
BandPayloadSize(b string) int64
}
type httpPostMetrics struct {
totalTime *prometheus.GaugeVec
firstProgressPacket *prometheus.GaugeVec
firstPackPacket *prometheus.GaugeVec
packBytes *prometheus.GaugeVec
}
func (m httpPostMetrics) measure(probeName string, stats httpPostStats) {
m.totalTime.WithLabelValues(probeName).Set(stats.ResponseBody().Seconds())
m.firstProgressPacket.WithLabelValues(probeName).Set(stats.BandFirstPacket("progress").Seconds())
m.firstPackPacket.WithLabelValues(probeName).Set(stats.BandFirstPacket("pack").Seconds())
m.packBytes.WithLabelValues(probeName).Set(float64(stats.BandPayloadSize("pack")))
}
// Describe is used to describe Prometheus metrics.
func (m httpPostMetrics) Describe(descs chan<- *prometheus.Desc) {
prometheus.DescribeByCollect(m, descs)
}
// Collect is used to collect Prometheus metrics.
func (m httpPostMetrics) Collect(metrics chan<- prometheus.Metric) {
m.totalTime.Collect(metrics)
m.firstProgressPacket.Collect(metrics)
m.firstPackPacket.Collect(metrics)
m.packBytes.Collect(metrics)
}
// Blackbox encapsulates all details required to run the blackbox prober.
type Blackbox struct {
cfg Config
fetchReferenceDiscoveryMetrics httpReferenceDiscoveryMetrics
httpPostMetrics httpPostMetrics
wantedRefs *prometheus.GaugeVec
}
// New creates a new Blackbox structure.
func New(cfg Config) Blackbox {
return Blackbox{
cfg: cfg,
fetchReferenceDiscoveryMetrics: httpReferenceDiscoveryMetrics{
firstPacket: newGauge("get_first_packet_seconds", "Time to first Git packet in GET /info/refs response"),
totalTime: newGauge("get_total_time_seconds", "Time to receive entire GET /info/refs response"),
advertisedRefs: newGauge("get_advertised_refs", "Number of Git refs advertised in GET /info/refs"),
},
httpPostMetrics: httpPostMetrics{
totalTime: newGauge("post_total_time_seconds", "Time to receive entire POST /upload-pack response"),
firstProgressPacket: newGauge("post_first_progress_packet_seconds", "Time to first progress band Git packet in POST /upload-pack response"),
firstPackPacket: newGauge("post_first_pack_packet_seconds", "Time to first pack band Git packet in POST /upload-pack response"),
packBytes: newGauge("post_pack_bytes", "Number of pack band bytes in POST /upload-pack response"),
},
wantedRefs: newGauge("wanted_refs", "Number of Git refs selected for (fake) Git clone (branches + tags)"),
}
}
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.fetchReferenceDiscoveryMetrics.Collect(metrics)
b.httpPostMetrics.Collect(metrics)
b.wantedRefs.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 {
listener, err := net.Listen("tcp", b.cfg.PrometheusListenAddr)
if err != nil {
return err
}
go b.runProbes()
return servePrometheus(listener)
}
func (b Blackbox) runProbes() {
for ; ; time.Sleep(b.cfg.sleepDuration.Duration()) {
for _, probe := range b.cfg.Probes {
entry := log.Default().WithFields(map[string]interface{}{
"probe": probe.Name,
"type": probe.Type,
})
entry.Info("starting probe")
var err error
switch probe.Type {
case Fetch:
err = b.fetch(probe)
case Push:
err = b.push(probe)
default:
err = fmt.Errorf("unsupported probe type: %q", probe.Type)
}
if err != nil {
entry.WithError(err).Error("probe failed")
}
entry.Info("finished probe")
}
}
}
func servePrometheus(l net.Listener) error {
opts := []monitoring.Option{
monitoring.WithListener(l),
}
if buildInfo, ok := debug.ReadBuildInfo(); ok {
opts = append(opts, monitoring.WithGoBuildInformation(buildInfo))
}
return monitoring.Start(opts...)
}
func (b Blackbox) fetch(probe Probe) error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
clone, err := stats.PerformHTTPClone(ctx, probe.URL, probe.User, probe.Password, false)
if err != nil {
return err
}
setGauge := func(gv *prometheus.GaugeVec, value float64) {
gv.WithLabelValues(probe.Name).Set(value)
}
b.fetchReferenceDiscoveryMetrics.measure(probe.Name, clone.ReferenceDiscovery)
b.httpPostMetrics.measure(probe.Name, &clone.FetchPack)
setGauge(b.wantedRefs, float64(clone.FetchPack.RefsWanted()))
return nil
}
func (b Blackbox) push(probe Probe) error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
objectFormat := probe.Push.ObjectFormat
if objectFormat == "" {
objectFormat = git.ObjectHashSHA1.Format
}
objectHash, err := git.ObjectHashByFormat(objectFormat)
if err != nil {
return fmt.Errorf("looking up object format: %w", err)
}
commands := make([]stats.PushCommand, len(probe.Push.Commands))
for i, command := range probe.Push.Commands {
oldOID, err := git.ObjectHashSHA1.FromHex(command.OldOID)
if err != nil {
return fmt.Errorf("invalid old object ID for probe %q: %w", probe.Name, err)
}
newOID, err := git.ObjectHashSHA1.FromHex(command.NewOID)
if err != nil {
return fmt.Errorf("invalid new object ID for probe %q: %w", probe.Name, err)
}
commands[i] = stats.PushCommand{
Reference: git.ReferenceName(command.Reference),
OldOID: oldOID,
NewOID: newOID,
}
}
packfile, err := os.Open(probe.Push.Packfile)
if err != nil {
return fmt.Errorf("opening packfile for probe %q: %w", probe.Name, err)
}
defer packfile.Close()
clone, err := stats.PerformHTTPPush(
ctx, probe.URL, probe.User, probe.Password, objectHash, commands, packfile, false)
if err != nil {
return err
}
b.httpPostMetrics.measure(probe.Name, &clone.SendPack)
return nil
}
|