Skip to content

Commit

Permalink
feat: Stats Prometheus
Browse files Browse the repository at this point in the history
  • Loading branch information
daoshenzzg committed Sep 12, 2024
1 parent 16d93ed commit a13a914
Show file tree
Hide file tree
Showing 7 changed files with 422 additions and 10 deletions.
23 changes: 20 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@

# Getting started

## Remote
## Remote Adapter

### goRedisV8Adapter - [redis/go-redis v8](https://github.com/go-redis/redis/v8)
### [redis/go-redis v8](https://github.com/go-redis/redis/v8)
```go
import (
"github.com/mgtv-tech/jetcache-go"
"github.com/mgtv-tech/jetcache-go-plugin/remote"
)

Expand All @@ -31,7 +32,23 @@ TODO

## Stats

TODO
### [prometheus](https://prometheus.io/)
```go
import (
"github.com/mgtv-tech/jetcache-go"
"github.com/mgtv-tech/jetcache-go-plugin/remote"
pstats "github.com/mgtv-tech/jetcache-go-plugin/stats"
"github.com/mgtv-tech/jetcache-go/stats"
)

cacheName := "demo"
jetcache := cache.New(cache.WithRemote(remote.NewGoRedisV8Adapter(ring)),
cache.WithStatsHandler(
stats.NewHandles(false,
stats.NewStatsLogger(cacheName),
pstats.NewPrometheus(cacheName))))
```
> 同时集成日志统计和Prometheus统计。
## Encoding

Expand Down
12 changes: 11 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,25 @@ go 1.18
require (
github.com/alicebob/miniredis/v2 v2.33.0
github.com/go-redis/redis/v8 v8.11.5
github.com/prometheus/client_golang v1.20.3
github.com/stretchr/testify v1.9.0
)

require (
github.com/alicebob/gopher-json v0.0.0-20200520072559-a9ecdc9d1d3a // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/kr/text v0.2.0 // indirect
github.com/kylelemons/godebug v1.1.0 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.55.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/yuin/gopher-lua v1.1.1 // indirect
golang.org/x/sys v0.22.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
36 changes: 30 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,53 @@ github.com/alicebob/gopher-json v0.0.0-20200520072559-a9ecdc9d1d3a h1:HbKu58rmZp
github.com/alicebob/gopher-json v0.0.0-20200520072559-a9ecdc9d1d3a/go.mod h1:SGnFV6hVsYE877CKEZ6tDNTjaSXYUk6QqoIK6PrAtcc=
github.com/alicebob/miniredis/v2 v2.33.0 h1:uvTF0EDeu9RLnUEG27Db5I68ESoIxTiXbNUiji6lZrA=
github.com/alicebob/miniredis/v2 v2.33.0/go.mod h1:MhP4a3EU7aENRi9aO+tHfTBZicLqQevyi/DJpoj6mi0=
github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE=
github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
github.com/go-redis/redis/v8 v8.11.5 h1:AcZZR7igkdvfVmQTPnu9WE37LRrO/YrBH5zWyjDC0oI=
github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq8Jd4h5lpwo=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
github.com/onsi/gomega v1.18.1 h1:M1GfJqGRrBrrGGsbxzV5dqM2U2ApXefZCQpkukxYRLE=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_golang v1.20.3 h1:oPksm4K8B+Vt35tUhw6GbSNSgVlVSBH0qELP/7u83l4=
github.com/prometheus/client_golang v1.20.3/go.mod h1:PIEt8X02hGcP8JWbeHyeZ53Y/jReSnHgO035n//V5WE=
github.com/prometheus/client_model v0.6.1 h1:ZKSh/rekM+n3CeS952MLRAdFwIKqeY8b62p8ais2e9E=
github.com/prometheus/client_model v0.6.1/go.mod h1:OrxVMOVHjw3lKMa8+x6HeMGkHMQyHDk9E3jmP2AmGiY=
github.com/prometheus/common v0.55.0 h1:KEi6DK7lXW/m7Ig5i47x0vRzuBsHuvJdi5ee6Y3G1dc=
github.com/prometheus/common v0.55.0/go.mod h1:2SECS4xJG1kd8XF9IcM1gMX6510RAEL65zxzNImwdc8=
github.com/prometheus/procfs v0.15.1 h1:YagwOFzUgYfKKHX6Dr+sHT7km/hxC76UB0learggepc=
github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoGhij/e3PBqk=
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/yuin/gopher-lua v1.1.1 h1:kYKnWBjvbNP4XLT3+bPEwAXJx262OhaHDWDVOPjL46M=
github.com/yuin/gopher-lua v1.1.1/go.mod h1:GBR0iDaNXjAgGg9zfCvksxSRnQx76gclCIb7kdAd1Pw=
golang.org/x/net v0.0.0-20210428140749-89ef3d95e781 h1:DzZ89McO9/gWPsQXS/FVKAlG02ZjaQ6AlZRBimEYOd0=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e h1:fLOSk5Q00efkSvAm+4xcoXD+RRmLmmulPn5I3Y9F2EM=
golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
golang.org/x/net v0.26.0 h1:soB7SVo0PWrY4vPW/+ay0jKDNScG2X9wFeYlXIvJsOQ=
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
110 changes: 110 additions & 0 deletions stats/stats.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package stats

type (
// Handler defines the interface that the Transport uses to collect cache metrics.
// Note that implementations of this interface must be thread-safe; the methods of a Handler
// can be called from concurrent goroutines.
Handler interface {
IncrHit()
IncrMiss()
IncrLocalHit()
IncrLocalMiss()
IncrRemoteHit()
IncrRemoteMiss()
IncrQuery()
IncrQueryFail(err error)
}

Handlers struct {
disable bool
handlers []Handler
}
)

// NewHandles creates a new instance of Handlers.
func NewHandles(disable bool, handlers ...Handler) Handler {
return &Handlers{
disable: disable,
handlers: handlers,
}
}

func (hs *Handlers) IncrHit() {
if hs.disable {
return
}

for _, h := range hs.handlers {
h.IncrHit()
}
}

func (hs *Handlers) IncrMiss() {
if hs.disable {
return
}

for _, h := range hs.handlers {
h.IncrMiss()
}
}

func (hs *Handlers) IncrLocalHit() {
if hs.disable {
return
}

for _, h := range hs.handlers {
h.IncrLocalHit()
}
}

func (hs *Handlers) IncrLocalMiss() {
if hs.disable {
return
}

for _, h := range hs.handlers {
h.IncrLocalMiss()
}
}

func (hs *Handlers) IncrRemoteHit() {
if hs.disable {
return
}

for _, h := range hs.handlers {
h.IncrRemoteHit()
}
}

func (hs *Handlers) IncrRemoteMiss() {
if hs.disable {
return
}

for _, h := range hs.handlers {
h.IncrRemoteMiss()
}
}

func (hs *Handlers) IncrQuery() {
if hs.disable {
return
}

for _, h := range hs.handlers {
h.IncrQuery()
}
}

func (hs *Handlers) IncrQueryFail(err error) {
if hs.disable {
return
}

for _, h := range hs.handlers {
h.IncrQueryFail(err)
}
}
89 changes: 89 additions & 0 deletions stats/stats_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package stats

import (
"errors"
"sync/atomic"
"testing"

"github.com/stretchr/testify/assert"
)

type testHandler struct {
Hit uint64
Miss uint64
LocalHit uint64
LocalMiss uint64
RemoteHit uint64
RemoteMiss uint64
Query uint64
QueryFail uint64
}

func TestNewHandles(t *testing.T) {
tests := []struct {
input bool
expect uint64
}{
{
input: false,
expect: 1,
},
{
input: true,
expect: 0,
},
}
for _, v := range tests {
var handler testHandler
h := NewHandles(v.input, &handler)
h.IncrHit()
h.IncrMiss()
h.IncrLocalHit()
h.IncrLocalMiss()
h.IncrRemoteHit()
h.IncrRemoteMiss()
h.IncrQuery()
h.IncrQueryFail(errors.New("any"))

assert.Equal(t, v.expect, handler.Hit)
assert.Equal(t, v.expect, handler.Miss)
assert.Equal(t, v.expect, handler.LocalHit)
assert.Equal(t, v.expect, handler.LocalMiss)
assert.Equal(t, v.expect, handler.RemoteHit)
assert.Equal(t, v.expect, handler.RemoteMiss)
assert.Equal(t, v.expect, handler.Query)
assert.Equal(t, v.expect, handler.QueryFail)
}
}

func (h *testHandler) IncrHit() {
atomic.AddUint64(&h.Hit, 1)
}

func (h *testHandler) IncrMiss() {
atomic.AddUint64(&h.Miss, 1)
}

func (h *testHandler) IncrLocalHit() {
atomic.AddUint64(&h.LocalHit, 1)
}

func (h *testHandler) IncrLocalMiss() {
atomic.AddUint64(&h.LocalMiss, 1)
}

func (h *testHandler) IncrRemoteHit() {
atomic.AddUint64(&h.RemoteHit, 1)
}

func (h *testHandler) IncrRemoteMiss() {
atomic.AddUint64(&h.RemoteMiss, 1)
}

func (h *testHandler) IncrQuery() {
atomic.AddUint64(&h.Query, 1)
}

func (h *testHandler) IncrQueryFail(err error) {
atomic.AddUint64(&h.QueryFail, 1)
}
Loading

0 comments on commit a13a914

Please sign in to comment.