Merge pull request #3 from nais/metrics

Metrics and dashboard
This commit is contained in:
Morten Lied Johansen
2021-09-29 15:06:54 +02:00
committed by GitHub
3 changed files with 142 additions and 6 deletions
+33
View File
@@ -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
View File
@@ -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()
})
}