Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional metrics and bug fixes #7

Merged
merged 1 commit into from
Mar 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion examples/prometheus/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
Expand Down Expand Up @@ -31,7 +32,7 @@ func main() {
panic(err)
}

rdb := cache.NewCache(redisClient)
rdb := cache.New(redisClient)

if err := prometheus.InstrumentMetrics(rdb); err != nil {
panic(err)
Expand Down Expand Up @@ -63,6 +64,27 @@ func main() {
return
}
}))
http.Handle("/mget", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

type request struct {
Keys []string `json:"keys"`
}

var req request
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}

res, err := cache.MGet[string](r.Context(), rdb, req.Keys...)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}

w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(res)
}))

http.ListenAndServe(":8080", nil)
}
31 changes: 22 additions & 9 deletions prometheus/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,26 @@ import (
"github.com/prometheus/client_golang/prometheus"
)

var defaultBuckets = prometheus.ExponentialBuckets(0.005, 2, 8)
var (
defaultBuckets = prometheus.ExponentialBuckets(0.005, 2, 8)
defaultMgetKeysBuckets = []float64{50, 100, 200, 500, 1000, 2000, 4000, 8000, 16000}
)

type config struct {
namespace string
subSystem string
globalLabels map[string]string
buckets []float64
namespace string
subSystem string
globalLabels map[string]string
buckets []float64
mgetKeysBuckets []float64
}

func newConfig() *config {
return &config{
namespace: "redis",
subSystem: "cache",
globalLabels: make(map[string]string),
buckets: defaultBuckets,
namespace: "redis",
subSystem: "cache",
globalLabels: make(map[string]string),
buckets: defaultBuckets,
mgetKeysBuckets: defaultMgetKeysBuckets,
}
}

Expand Down Expand Up @@ -51,3 +56,11 @@ func WithBuckets(buckets []float64) Option {
c.buckets = buckets
}
}

// WithMGETKeysBuckets overrides the default buckets used for the Prometheus
// histogram that tracks the number of keys requested in MGET commands
func WithMGETKeysBuckets(buckets []float64) Option {
return func(c *config) {
c.mgetKeysBuckets = buckets
}
}
22 changes: 20 additions & 2 deletions prometheus/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,14 +250,23 @@ func InstrumentClientMetrics(rdb redis.UniversalClient, opts ...Option) error {
Help: "Number of operations cancelled in flight",
ConstLabels: conf.globalLabels,
}, []string{"operation"})
mgetKeys := prometheus.NewHistogram(prometheus.HistogramOpts{
Namespace: conf.namespace,
Subsystem: conf.subSystem,
Name: "mget_keys_requested",
Help: "Count of keys requested in MGET operations",
ConstLabels: conf.globalLabels,
Buckets: conf.mgetKeysBuckets,
})

err := multierr.Combine(
prometheus.Register(processTime),
prometheus.Register(dialTime),
prometheus.Register(hits),
prometheus.Register(misses),
prometheus.Register(errs),
prometheus.Register(cancels))
prometheus.Register(cancels),
prometheus.Register(mgetKeys))
if err != nil {
return fmt.Errorf("failed to register prometheus collectors: %w", err)
}
Expand All @@ -269,6 +278,7 @@ func InstrumentClientMetrics(rdb redis.UniversalClient, opts ...Option) error {
misses: misses,
errors: errs,
cancels: cancels,
mgetKeys: mgetKeys,
}
rdb.AddHook(hook)

Expand Down Expand Up @@ -301,6 +311,7 @@ type clientMetricsHook struct {
misses prometheus.Counter
errors *prometheus.CounterVec
cancels *prometheus.CounterVec
mgetKeys prometheus.Histogram
}

func (c *clientMetricsHook) DialHook(next redis.DialHook) redis.DialHook {
Expand Down Expand Up @@ -335,15 +346,22 @@ func (c *clientMetricsHook) ProcessHook(next redis.ProcessHook) redis.ProcessHoo

c.processTime.WithLabelValues(cmd.Name()).Observe(dur)

if strings.ToLower(cmd.Name()) == "get" {
switch strings.ToLower(cmd.Name()) {
case "get":
if strCmd, ok := cmd.(*redis.StringCmd); ok {
if errors.Is(strCmd.Err(), redis.Nil) || (strCmd.Val() == "" && strCmd.Err() == nil) {
c.misses.Inc()
break
}
if strCmd.Err() == nil {
c.hits.Inc()
}
}
case "mget":
if mgetCmd, ok := cmd.(*redis.SliceCmd); ok {
keysRequested := len(mgetCmd.Args()) - 1
c.mgetKeys.Observe(float64(keysRequested))
}
}

if errors.Is(err, context.Canceled) {
Expand Down
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ package cache

// Version returns the current version of redis-cache.
func Version() string {
return "1.5.0"
return "1.5.1"
}
Loading