sources.go144 lines, 5.7 KB
| 1 | package metrics |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "net" |
| 7 | "time" |
| 8 | |
| 9 | "github.com/jackc/pgx/v5/pgxpool" |
| 10 | "github.com/prometheus/client_golang/prometheus" |
| 11 | "github.com/redis/go-redis/v9" |
| 12 | ) |
| 13 | |
| 14 | // A number is anything a gauge or a counter can be read from. |
| 15 | type number interface { |
| 16 | ~int | ~int32 | ~int64 | ~uint32 | ~float64 |
| 17 | } |
| 18 | |
| 19 | // Gauge registers a number that goes up and down. Nobody keeps it up to date: |
| 20 | // read is called when Prometheus asks. labels are pairs: a name, then its value. |
| 21 | func Gauge[N number](reg prometheus.Registerer, name, help string, read func() N, labels ...string) { |
| 22 | opts := prometheus.GaugeOpts{Name: name, Help: help, ConstLabels: pairs(labels)} |
| 23 | reg.MustRegister(prometheus.NewGaugeFunc(opts, func() float64 { return float64(read()) })) |
| 24 | } |
| 25 | |
| 26 | // Counter registers a number that only ever goes up, read the same way. |
| 27 | func Counter[N number](reg prometheus.Registerer, name, help string, read func() N, labels ...string) { |
| 28 | opts := prometheus.CounterOpts{Name: name, Help: help, ConstLabels: pairs(labels)} |
| 29 | reg.MustRegister(prometheus.NewCounterFunc(opts, func() float64 { return float64(read()) })) |
| 30 | } |
| 31 | |
| 32 | func pairs(labels []string) prometheus.Labels { |
| 33 | set := prometheus.Labels{} |
| 34 | for i := 0; i+1 < len(labels); i += 2 { |
| 35 | set[labels[i]] = labels[i+1] |
| 36 | } |
| 37 | return set |
| 38 | } |
| 39 | |
| 40 | // Pool registers what pgx's connection pool knows about itself. |
| 41 | func Pool(reg prometheus.Registerer, db *pgxpool.Pool) { |
| 42 | Gauge(reg, "murmur_db_pool_in_use_connections", "Connections that a query holds right now.", |
| 43 | func() int32 { return db.Stat().AcquiredConns() }) |
| 44 | Gauge(reg, "murmur_db_pool_idle_connections", "Connections that are open, with nothing to do.", |
| 45 | func() int32 { return db.Stat().IdleConns() }) |
| 46 | Gauge(reg, "murmur_db_pool_max_connections", "The most connections this pool opens.", |
| 47 | func() int32 { return db.Stat().MaxConns() }) |
| 48 | Counter(reg, "murmur_db_pool_waits_total", "Queries that found no free connection, and waited.", |
| 49 | func() int64 { return db.Stat().EmptyAcquireCount() }) |
| 50 | Counter(reg, "murmur_db_pool_wait_seconds_total", "How long those queries waited, added up.", |
| 51 | func() float64 { return db.Stat().EmptyAcquireWaitTime().Seconds() }) |
| 52 | } |
| 53 | |
| 54 | // Redis registers how long every command to Redis takes and how many fail, |
| 55 | // and what go-redis's connection pool knows about itself. |
| 56 | func Redis(reg prometheus.Registerer, rdb *redis.Client) { |
| 57 | h := redisHook{ |
| 58 | duration: prometheus.NewHistogramVec(prometheus.HistogramOpts{ |
| 59 | Name: "murmur_redis_call_seconds", |
| 60 | Help: "How long a call to Redis took, by command. A pipeline is one call.", |
| 61 | Buckets: []float64{.0001, .00025, .0005, .001, .0025, .005, .01, .025, .05, .1, .25, 1, 5}, |
| 62 | }, []string{"command"}), |
| 63 | errors: prometheus.NewCounterVec(prometheus.CounterOpts{ |
| 64 | Name: "murmur_redis_errors_total", |
| 65 | Help: "Commands to Redis that failed, by command.", |
| 66 | }, []string{"command"}), |
| 67 | } |
| 68 | reg.MustRegister(h.duration, h.errors) |
| 69 | rdb.AddHook(h) |
| 70 | Gauge(reg, "murmur_redis_pool_connections", "Connections to Redis that are open.", |
| 71 | func() uint32 { return rdb.PoolStats().TotalConns }) |
| 72 | Gauge(reg, "murmur_redis_pool_idle_connections", "Connections to Redis with nothing to do.", |
| 73 | func() uint32 { return rdb.PoolStats().IdleConns }) |
| 74 | Counter(reg, "murmur_redis_pool_timeouts_total", "Commands that gave up waiting for a connection.", |
| 75 | func() uint32 { return rdb.PoolStats().Timeouts }) |
| 76 | } |
| 77 | |
| 78 | // A redisHook sits around every command that go-redis sends. |
| 79 | type redisHook struct { |
| 80 | duration *prometheus.HistogramVec |
| 81 | errors *prometheus.CounterVec |
| 82 | } |
| 83 | |
| 84 | func (h redisHook) observe(command string, start time.Time, err error) { |
| 85 | h.duration.WithLabelValues(command).Observe(time.Since(start).Seconds()) |
| 86 | // Two answers are errors to go-redis, and not to us: "no such key", and |
| 87 | // "I don't know that script yet", which go-redis follows up with the script itself. |
| 88 | if err != nil && !errors.Is(err, redis.Nil) && !redis.HasErrorPrefix(err, "NOSCRIPT") { |
| 89 | h.errors.WithLabelValues(command).Inc() |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | func (h redisHook) DialHook(next redis.DialHook) redis.DialHook { |
| 94 | return func(ctx context.Context, network, addr string) (net.Conn, error) { |
| 95 | return next(ctx, network, addr) |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | func (h redisHook) ProcessHook(next redis.ProcessHook) redis.ProcessHook { |
| 100 | return func(ctx context.Context, cmd redis.Cmder) error { |
| 101 | start := time.Now() |
| 102 | err := next(ctx, cmd) |
| 103 | h.observe(cmd.Name(), start, err) |
| 104 | return err |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | func (h redisHook) ProcessPipelineHook(next redis.ProcessPipelineHook) redis.ProcessPipelineHook { |
| 109 | return func(ctx context.Context, cmds []redis.Cmder) error { |
| 110 | start := time.Now() |
| 111 | err := next(ctx, cmds) |
| 112 | h.observe("pipeline", start, err) |
| 113 | return err |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | // Jobs returns what a worker calls every time a job ends: how long it waited |
| 118 | // in the queue, how long it ran, and the error if it failed. |
| 119 | func Jobs(reg prometheus.Registerer) func(kind string, waited, ran time.Duration, err error) { |
| 120 | ended := prometheus.NewCounterVec(prometheus.CounterOpts{ |
| 121 | Name: "murmur_jobs_total", |
| 122 | Help: "Jobs that ended, by kind and result.", |
| 123 | }, []string{"kind", "result"}) |
| 124 | wait := prometheus.NewHistogram(prometheus.HistogramOpts{ |
| 125 | Name: "murmur_job_wait_seconds", |
| 126 | Help: "How long a job sat in the queue before a worker started it.", |
| 127 | Buckets: []float64{.01, .05, .25, 1, 5, 15, 60, 300}, |
| 128 | }) |
| 129 | run := prometheus.NewHistogramVec(prometheus.HistogramOpts{ |
| 130 | Name: "murmur_job_run_seconds", |
| 131 | Help: "How long a job's handler took, by kind.", |
| 132 | Buckets: []float64{.001, .0025, .005, .01, .025, .05, .1, .25, 1, 5}, |
| 133 | }, []string{"kind"}) |
| 134 | reg.MustRegister(ended, wait, run) |
| 135 | return func(kind string, waited, ran time.Duration, err error) { |
| 136 | result := "done" |
| 137 | if err != nil { |
| 138 | result = "failed" |
| 139 | } |
| 140 | ended.WithLabelValues(kind, result).Inc() |
| 141 | wait.Observe(waited.Seconds()) |
| 142 | run.WithLabelValues(kind).Observe(ran.Seconds()) |
| 143 | } |
| 144 | } |