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

add local cache stats #114

Merged
merged 3 commits into from
Jan 17, 2020
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
43 changes: 43 additions & 0 deletions src/redis/local_cache_stats.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package redis
mattklein123 marked this conversation as resolved.
Show resolved Hide resolved

import (
"github.com/coocood/freecache"
stats "github.com/lyft/gostats"
)

type localCacheStats struct {
cache *freecache.Cache
evacuateCount stats.Gauge
expiredCount stats.Gauge
entryCount stats.Gauge
averageAccessTime stats.Gauge
hitCount stats.Gauge
missCount stats.Gauge
lookupCount stats.Gauge
overwriteCount stats.Gauge
}

func NewLocalCacheStats(localCache *freecache.Cache, scope stats.Scope) stats.StatGenerator {
return localCacheStats{
cache: localCache,
evacuateCount: scope.NewGauge("evacuateCount"),
expiredCount: scope.NewGauge("expiredCount"),
entryCount: scope.NewGauge("entryCount"),
averageAccessTime: scope.NewGauge("averageAccessTime"),
hitCount: scope.NewGauge("hitCount"),
missCount: scope.NewGauge("missCount"),
lookupCount: scope.NewGauge("lookupCount"),
overwriteCount: scope.NewGauge("overwriteCount"),
}
}

func (stats localCacheStats) GenerateStats() {
stats.evacuateCount.Set(uint64(stats.cache.EvacuateCount()))
stats.expiredCount.Set(uint64(stats.cache.ExpiredCount()))
stats.entryCount.Set(uint64(stats.cache.EntryCount()))
stats.averageAccessTime.Set(uint64(stats.cache.AverageAccessTime()))
stats.hitCount.Set(uint64(stats.cache.HitCount()))
stats.missCount.Set(uint64(stats.cache.MissCount()))
stats.lookupCount.Set(uint64(stats.cache.LookupCount()))
stats.overwriteCount.Set(uint64(stats.cache.OverwriteCount()))
}
12 changes: 9 additions & 3 deletions src/server/server_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ import (
"net/http/pprof"
"sort"

"github.com/lyft/ratelimit/src/redis"

"os"
"os/signal"
"syscall"

"net"

"github.com/coocood/freecache"
"github.com/gorilla/mux"
reuseport "github.com/kavu/go_reuseport"
"github.com/lyft/goruntime/loader"
Expand Down Expand Up @@ -99,11 +102,11 @@ func (server *server) Runtime() loader.IFace {
return server.runtime
}

func NewServer(name string, store stats.Store, opts ...settings.Option) Server {
return newServer(name, store, opts...)
func NewServer(name string, store stats.Store, localCache *freecache.Cache, opts ...settings.Option) Server {
return newServer(name, store, localCache, opts...)
}

func newServer(name string, store stats.Store, opts ...settings.Option) *server {
func newServer(name string, store stats.Store, localCache *freecache.Cache, opts ...settings.Option) *server {
s := settings.NewSettings()

for _, opt := range opts {
Expand All @@ -122,6 +125,9 @@ func newServer(name string, store stats.Store, opts ...settings.Option) *server
ret.store = store
ret.scope = ret.store.Scope(name)
ret.store.AddStatGenerator(stats.NewRuntimeStats(ret.scope.Scope("go")))
if localCache != nil {
ret.store.AddStatGenerator(redis.NewLocalCacheStats(localCache, ret.scope.Scope("localcache")))
}

// setup runtime
loaderOpts := make([]loader.Option, 0, 1)
Expand Down
10 changes: 5 additions & 5 deletions src/service_cmd/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,12 @@ func (runner *Runner) Run() {
} else {
logger.SetLevel(logLevel)
}
var localCache *freecache.Cache
if s.LocalCacheSizeInBytes != 0 {
localCache = freecache.NewCache(s.LocalCacheSizeInBytes)
}

srv := server.NewServer("ratelimit", runner.statsStore, settings.GrpcUnaryInterceptor(nil))
srv := server.NewServer("ratelimit", runner.statsStore, localCache, settings.GrpcUnaryInterceptor(nil))

var perSecondPool redis.Pool
if s.RedisPerSecond {
Expand All @@ -52,10 +56,6 @@ func (runner *Runner) Run() {
var otherPool redis.Pool
otherPool = redis.NewPoolImpl(srv.Scope().Scope("redis_pool"), s.RedisTls, s.RedisAuth, s.RedisUrl, s.RedisPoolSize)

var localCache *freecache.Cache
if s.LocalCacheSizeInBytes != 0 {
localCache = freecache.NewCache(s.LocalCacheSizeInBytes)
}
service := ratelimit.NewService(
srv.Runtime(),
redis.NewRateLimitCacheImpl(
Expand Down
31 changes: 31 additions & 0 deletions test/common/common.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,42 @@
package common

import (
"sync"

pb_struct "github.com/envoyproxy/go-control-plane/envoy/api/v2/ratelimit"
pb "github.com/envoyproxy/go-control-plane/envoy/service/ratelimit/v2"
pb_legacy "github.com/lyft/ratelimit/proto/ratelimit"
)

type TestStatSink struct {
sync.Mutex
Record map[string]interface{}
}

func (s *TestStatSink) Clear() {
s.Lock()
s.Record = map[string]interface{}{}
s.Unlock()
}

func (s *TestStatSink) FlushCounter(name string, value uint64) {
s.Lock()
s.Record[name] = value
s.Unlock()
}

func (s *TestStatSink) FlushGauge(name string, value uint64) {
s.Lock()
s.Record[name] = value
s.Unlock()
}

func (s *TestStatSink) FlushTimer(name string, value float64) {
s.Lock()
s.Record[name] = value
s.Unlock()
}

func NewRateLimitRequest(domain string, descriptors [][][2]string, hitsAddend uint32) *pb.RateLimitRequest {
request := &pb.RateLimitRequest{}
request.Domain = domain
Expand Down
Loading