mirror of
https://github.com/nais/wonderwall.git
synced 2026-08-23 21:16:14 +00:00
@@ -1,11 +1,44 @@
|
||||
package metrics
|
||||
|
||||
import (
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
Namespace = "wonderwall"
|
||||
|
||||
RedisOperationLabel = "operation"
|
||||
)
|
||||
|
||||
var (
|
||||
RedisLatency = prometheus.NewHistogramVec(prometheus.HistogramOpts{
|
||||
Name: "redis_latency",
|
||||
Namespace: Namespace,
|
||||
Help: "latency in redis operations",
|
||||
Buckets: prometheus.ExponentialBuckets(0.02, 2, 14),
|
||||
}, []string{RedisOperationLabel})
|
||||
)
|
||||
|
||||
func Handle(address string) error {
|
||||
handler := promhttp.Handler()
|
||||
return http.ListenAndServe(address, handler)
|
||||
}
|
||||
|
||||
func Register(registry prometheus.Registerer) {
|
||||
registry.MustRegister(
|
||||
RedisLatency,
|
||||
)
|
||||
}
|
||||
|
||||
func ObserveRedisLatency(operation string, fun func() error) error {
|
||||
timer := time.Now()
|
||||
err := fun()
|
||||
used := time.Now().Sub(timer)
|
||||
RedisLatency.With(prometheus.Labels{
|
||||
RedisOperationLabel: operation,
|
||||
}).Observe(used.Seconds())
|
||||
return err
|
||||
}
|
||||
|
||||
+15
-6
@@ -3,6 +3,7 @@ package session
|
||||
import (
|
||||
"context"
|
||||
"github.com/go-redis/redis/v8"
|
||||
"github.com/nais/wonderwall/pkg/metrics"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -20,8 +21,12 @@ func NewRedis(client redis.Cmdable) Store {
|
||||
|
||||
func (s *redisSessionStore) Read(ctx context.Context, key string) (*Data, error) {
|
||||
data := &Data{}
|
||||
status := s.client.Get(ctx, key)
|
||||
err := status.Scan(data)
|
||||
err := metrics.ObserveRedisLatency("Read", func() error {
|
||||
var err error
|
||||
status := s.client.Get(ctx, key)
|
||||
err = status.Scan(data)
|
||||
return err
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -29,11 +34,15 @@ func (s *redisSessionStore) Read(ctx context.Context, key string) (*Data, error)
|
||||
}
|
||||
|
||||
func (s *redisSessionStore) Write(ctx context.Context, key string, value *Data, expiration time.Duration) error {
|
||||
status := s.client.Set(ctx, key, value, expiration)
|
||||
return status.Err()
|
||||
return metrics.ObserveRedisLatency("Write", func() error {
|
||||
status := s.client.Set(ctx, key, value, expiration)
|
||||
return status.Err()
|
||||
})
|
||||
}
|
||||
|
||||
func (s *redisSessionStore) Delete(ctx context.Context, keys ...string) error {
|
||||
status := s.client.Del(ctx, keys...)
|
||||
return status.Err()
|
||||
return metrics.ObserveRedisLatency("Delete", func() error {
|
||||
status := s.client.Del(ctx, keys...)
|
||||
return status.Err()
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user