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

Added metrics for lock acquiring attempts #3

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
36 changes: 25 additions & 11 deletions pkg/server/prometheus_exporter.go
Original file line number Diff line number Diff line change
@@ -1,41 +1,55 @@
package server

import (
"github.com/prometheus/client_golang/prometheus"
"strings"

"github.com/prometheus/client_golang/prometheus"
)

var (
buildInfoGaugeVec = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "sidecache_admission_build_info",
Help: "Build info for sidecache admission webhook",
}, []string{"version"})

cacheHitCounter = prometheus.NewCounter(
prometheus.CounterOpts{
Namespace: "sidecache_" + ProjectName,
Name: "cache_hit_counter",
Help: "Cache hit count",
})

lockAcquiringAttemptsHistogram = prometheus.NewHistogram(
prometheus.HistogramOpts{
Namespace: "sidecache_" + ProjectName,
Name: "lock_acquiring_attempts_histogram",
Help: "Lock acquiring attempts histogram",
Buckets: []float64{0.999, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 100, 200, 300, 500, 1000},
})

totalRequestCounter = prometheus.NewCounter(
prometheus.CounterOpts{
Namespace: "sidecache_" + ProjectName,
Name: "all_request_hit_counter",
Help: "All request hit counter",
})

buildInfoGaugeVec = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "sidecache_admission_build_info",
Help: "Build info for sidecache admission webhook",
}, []string{"version"})
)

type Prometheus struct {
CacheHitCounter prometheus.Counter
TotalRequestCounter prometheus.Counter
CacheHitCounter prometheus.Counter
LockAcquiringAttemptsHistogram prometheus.Histogram
TotalRequestCounter prometheus.Counter
}

func NewPrometheusClient() *Prometheus {
prometheus.MustRegister(cacheHitCounter, totalRequestCounter, buildInfoGaugeVec)
prometheus.MustRegister(buildInfoGaugeVec, cacheHitCounter, lockAcquiringAttemptsHistogram, totalRequestCounter)

return &Prometheus{TotalRequestCounter: totalRequestCounter, CacheHitCounter: cacheHitCounter}
return &Prometheus{
CacheHitCounter: cacheHitCounter,
LockAcquiringAttemptsHistogram: lockAcquiringAttemptsHistogram,
TotalRequestCounter: totalRequestCounter,
}
}

func BuildInfo(admission string) {
Expand Down
16 changes: 8 additions & 8 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"encoding/hex"
"encoding/json"
"errors"
"github.com/zeriontech/sidecache/pkg/lock"
"io"
"io/ioutil"
"net/http"
Expand All @@ -19,6 +18,7 @@ import (

"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/zeriontech/sidecache/pkg/cache"
"github.com/zeriontech/sidecache/pkg/lock"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -144,14 +144,14 @@ func (server CacheServer) CacheHandler(w http.ResponseWriter, r *http.Request) {
}()

path := strings.Split(r.URL.Path, "/")
key := "lock:" + path[1]
key := "lock:" + path[1] + "/" + path[2]
resultKey := server.HashURL(server.ReorderQueryString(r.URL))

if UseLock {
attempt := 0
attempt := 1
for {
// check the cache
server.Logger.Info("checking the cache", zap.String("resultKey", resultKey), zap.Int("attempt", attempt+1))
server.Logger.Info("checking the cache", zap.String("resultKey", resultKey), zap.Int("attempt", attempt))
if cachedDataBytes := server.CheckCache(resultKey); cachedDataBytes != nil {
serveFromCache(cachedDataBytes, server, w, r)
return
Expand All @@ -160,7 +160,8 @@ func (server CacheServer) CacheHandler(w http.ResponseWriter, r *http.Request) {
// try to acquire the lock
server.Logger.Info("acquiring the lock", zap.String("key", key))
if err := server.LockMgr.Acquire(key, LockTtl); err == nil {
server.Logger.Info("lock acquired", zap.String("key", key))
server.Prometheus.LockAcquiringAttemptsHistogram.Observe(float64(attempt))
server.Logger.Info("lock acquired", zap.String("key", key), zap.Int("attempt", attempt))
defer func() {
// release the lock
if err := server.LockMgr.Release(key); err != nil {
Expand Down Expand Up @@ -251,9 +252,8 @@ func (server CacheServer) ReorderQueryString(url *url.URL) string {
}

func (server CacheServer) GetBackoff(attempt int) time.Duration {
if attempt < 10 {
if attempt <= 10 {
return 100 * time.Millisecond
} else {
return 500 * time.Millisecond
}
return 500 * time.Millisecond
}