Skip to content

Commit

Permalink
Add prometheus metrics to gobeansproxy (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
dispensable authored Jul 7, 2023
1 parent a3474bd commit 13d4199
Show file tree
Hide file tree
Showing 5 changed files with 591 additions and 10 deletions.
39 changes: 39 additions & 0 deletions dstore/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package dstore

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

var (
totalReqs *prometheus.CounterVec
errorReqs *prometheus.CounterVec
cmdReqDurationSeconds *prometheus.HistogramVec
cmdE2EDurationSeconds *prometheus.HistogramVec
BdbProxyPromRegistry *prometheus.Registry
)

func init() {
BdbProxyPromRegistry = prometheus.NewRegistry()
totalReqs = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: "gobeansproxy",
Name: "total_reqs",
Help: "total requests counter",
},

[]string{"cmd", "store"},
)
BdbProxyPromRegistry.MustRegister(totalReqs)

cmdE2EDurationSeconds = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: "gobeansproxy",
Name: "cmd_e2e_duration_seconds",
Help: "cmd e2e duration",
Buckets: []float64{0.03, 0.05, 0.07, 0.1, 0.25, 0.5, 1, 2.5, 5},
},

[]string{"cmd", "br", "bw", "cr", "cw"},
)
BdbProxyPromRegistry.MustRegister(cmdE2EDurationSeconds)
}
25 changes: 24 additions & 1 deletion dstore/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/douban/gobeansdb/cmem"
"github.com/douban/gobeansdb/loghub"
mc "github.com/douban/gobeansdb/memcache"

"github.com/prometheus/client_golang/prometheus"
"github.com/douban/gobeansproxy/config"
)

Expand Down Expand Up @@ -60,6 +60,12 @@ func (c *StorageClient) Clean() {
}

func (c *StorageClient) Get(key string) (item *mc.Item, err error) {
timer := prometheus.NewTimer(
cmdE2EDurationSeconds.WithLabelValues("get", "true", "true", "false", "false"),
)
defer timer.ObserveDuration()
totalReqs.WithLabelValues("get", "beansdb").Inc()

c.sched = GetScheduler()

hosts := c.sched.GetHostsByKey(key)
Expand Down Expand Up @@ -146,6 +152,12 @@ func (c *StorageClient) getMulti(keys []string) (rs map[string]*mc.Item, targets
}

func (c *StorageClient) GetMulti(keys []string) (rs map[string]*mc.Item, err error) {
timer := prometheus.NewTimer(
cmdE2EDurationSeconds.WithLabelValues("getm", "true", "true", "false", "false"),
)
defer timer.ObserveDuration()
totalReqs.WithLabelValues("getm", "beansdb").Inc()

c.sched = GetScheduler()
var lock sync.Mutex
rs = make(map[string]*mc.Item, len(keys))
Expand Down Expand Up @@ -181,6 +193,11 @@ func (c *StorageClient) GetMulti(keys []string) (rs map[string]*mc.Item, err err
}

func (c *StorageClient) Set(key string, item *mc.Item, noreply bool) (ok bool, err error) {
timer := prometheus.NewTimer(
cmdE2EDurationSeconds.WithLabelValues("set", "true", "true", "false", "false"),
)
defer timer.ObserveDuration()
totalReqs.WithLabelValues("set", "beansdb").Inc()
c.sched = GetScheduler()
hosts := c.sched.GetHostsByKey(key)
ok = false
Expand Down Expand Up @@ -308,6 +325,12 @@ func (c *StorageClient) Incr(key string, value int) (result int, err error) {

// TODO: 弄清楚为什么 delete 不遵循 NWR 规则
func (c *StorageClient) Delete(key string) (flag bool, err error) {
timer := prometheus.NewTimer(
cmdE2EDurationSeconds.WithLabelValues("del", "true", "true", "false", "false"),
)
defer timer.ObserveDuration()
totalReqs.WithLabelValues("del", "beansdb").Inc()

c.sched = GetScheduler()
suc := 0
errCnt := 0
Expand Down
7 changes: 3 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
module github.com/douban/gobeansproxy

require (
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/douban/gobeansdb v1.1.2
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.2.2
gopkg.in/yaml.v2 v2.2.1
github.com/prometheus/client_golang v1.16.0
github.com/stretchr/testify v1.8.0
gopkg.in/yaml.v2 v2.4.0
)

go 1.13
Loading

0 comments on commit 13d4199

Please sign in to comment.