Skip to content

Commit a5fe157

Browse files
committed
Register Prometheus counter
1 parent 80b68c9 commit a5fe157

File tree

6 files changed

+58
-1
lines changed

6 files changed

+58
-1
lines changed

adapter/grpc.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ func NewGRPCServer(store store.ScanStore, coordinate *kv.Coordinate) *GRPCServer
3838
}
3939

4040
func (r GRPCServer) RawGet(ctx context.Context, req *pb.RawGetRequest) (*pb.RawGetResponse, error) {
41+
opCounter.WithLabelValues("raw_get", "grpc").Inc()
4142
v, err := r.store.Get(ctx, req.Key)
4243
if err != nil {
4344
switch {
@@ -60,6 +61,7 @@ func (r GRPCServer) RawGet(ctx context.Context, req *pb.RawGetRequest) (*pb.RawG
6061
}
6162

6263
func (r GRPCServer) RawPut(_ context.Context, req *pb.RawPutRequest) (*pb.RawPutResponse, error) {
64+
opCounter.WithLabelValues("raw_put", "grpc").Inc()
6365
m, err := r.grpcTranscoder.RawPutToRequest(req)
6466
if err != nil {
6567
return nil, errors.WithStack(err)
@@ -80,6 +82,7 @@ func (r GRPCServer) RawPut(_ context.Context, req *pb.RawPutRequest) (*pb.RawPut
8082
}
8183

8284
func (r GRPCServer) RawDelete(ctx context.Context, req *pb.RawDeleteRequest) (*pb.RawDeleteResponse, error) {
85+
opCounter.WithLabelValues("raw_delete", "grpc").Inc()
8386
m, err := r.grpcTranscoder.RawDeleteToRequest(req)
8487
if err != nil {
8588
return nil, errors.WithStack(err)
@@ -112,6 +115,7 @@ func (r GRPCServer) Rollback(ctx context.Context, req *pb.RollbackRequest) (*pb.
112115
}
113116

114117
func (r GRPCServer) Put(ctx context.Context, req *pb.PutRequest) (*pb.PutResponse, error) {
118+
opCounter.WithLabelValues("put", "grpc").Inc()
115119
reqs, err := r.grpcTranscoder.TransactionalPutToRequests(req)
116120
if err != nil {
117121
return nil, errors.WithStack(err)
@@ -133,6 +137,7 @@ func (r GRPCServer) Put(ctx context.Context, req *pb.PutRequest) (*pb.PutRespons
133137
}
134138

135139
func (r GRPCServer) Get(ctx context.Context, req *pb.GetRequest) (*pb.GetResponse, error) {
140+
opCounter.WithLabelValues("get", "grpc").Inc()
136141
h := murmur3.New64()
137142
if _, err := h.Write(req.Key); err != nil {
138143
return nil, errors.WithStack(err)
@@ -153,6 +158,7 @@ func (r GRPCServer) Get(ctx context.Context, req *pb.GetRequest) (*pb.GetRespons
153158
}
154159

155160
func (r GRPCServer) Delete(ctx context.Context, req *pb.DeleteRequest) (*pb.DeleteResponse, error) {
161+
opCounter.WithLabelValues("delete", "grpc").Inc()
156162
reqs, err := r.grpcTranscoder.TransactionalDeleteToRequests(req)
157163
if err != nil {
158164
return nil, errors.WithStack(err)
@@ -174,6 +180,7 @@ func (r GRPCServer) Delete(ctx context.Context, req *pb.DeleteRequest) (*pb.Dele
174180
}
175181

176182
func (r GRPCServer) Scan(ctx context.Context, req *pb.ScanRequest) (*pb.ScanResponse, error) {
183+
opCounter.WithLabelValues("scan", "grpc").Inc()
177184
limit, err := internal.Uint64ToInt(req.Limit)
178185
if err != nil {
179186
return &pb.ScanResponse{

adapter/metrics.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package adapter
2+
3+
import "github.com/prometheus/client_golang/prometheus"
4+
5+
var (
6+
opCounter = prometheus.NewCounterVec(prometheus.CounterOpts{
7+
Name: "elastickv_operations_total",
8+
Help: "Total number of KV operations",
9+
}, []string{"op", "client"})
10+
)
11+
12+
func init() {
13+
prometheus.MustRegister(opCounter)
14+
}

adapter/redis.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ func (r *RedisServer) ping(conn redcon.Conn, _ redcon.Command) {
9797
}
9898

9999
func (r *RedisServer) set(conn redcon.Conn, cmd redcon.Command) {
100+
opCounter.WithLabelValues("set", "redis").Inc()
100101
res, err := r.redisTranscoder.SetToRequest(cmd.Args[1], cmd.Args[2])
101102
if err != nil {
102103
conn.WriteError(err.Error())
@@ -113,6 +114,7 @@ func (r *RedisServer) set(conn redcon.Conn, cmd redcon.Command) {
113114
}
114115

115116
func (r *RedisServer) get(conn redcon.Conn, cmd redcon.Command) {
117+
opCounter.WithLabelValues("get", "redis").Inc()
116118
v, err := r.store.Get(context.Background(), cmd.Args[1])
117119
if err != nil {
118120
conn.WriteNull()
@@ -123,6 +125,7 @@ func (r *RedisServer) get(conn redcon.Conn, cmd redcon.Command) {
123125
}
124126

125127
func (r *RedisServer) del(conn redcon.Conn, cmd redcon.Command) {
128+
opCounter.WithLabelValues("del", "redis").Inc()
126129
res, err := r.redisTranscoder.DeleteToRequest(cmd.Args[1])
127130
if err != nil {
128131
conn.WriteError(err.Error())
@@ -139,6 +142,7 @@ func (r *RedisServer) del(conn redcon.Conn, cmd redcon.Command) {
139142
}
140143

141144
func (r *RedisServer) exists(conn redcon.Conn, cmd redcon.Command) {
145+
opCounter.WithLabelValues("exists", "redis").Inc()
142146
ok, err := r.store.Exists(context.Background(), cmd.Args[1])
143147
if err != nil {
144148
conn.WriteError(err.Error())
@@ -153,6 +157,7 @@ func (r *RedisServer) exists(conn redcon.Conn, cmd redcon.Command) {
153157
}
154158

155159
func (r *RedisServer) keys(conn redcon.Conn, cmd redcon.Command) {
160+
opCounter.WithLabelValues("keys", "redis").Inc()
156161

157162
// If an asterisk (*) is not included, the match will be exact,
158163
// so check if the key exists.

go.mod

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@ require (
1515
github.com/aws/aws-sdk-go-v2/service/dynamodb v1.50.0
1616
github.com/cockroachdb/errors v1.12.0
1717
github.com/emirpasic/gods v1.18.1
18+
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0
1819
github.com/hashicorp/go-hclog v1.6.3
1920
github.com/hashicorp/raft v1.7.3
2021
github.com/hashicorp/raft-boltdb/v2 v2.3.1
2122
github.com/pkg/errors v0.9.1
23+
github.com/prometheus/client_golang v1.19.0
2224
github.com/redis/go-redis/v9 v9.12.1
2325
github.com/spaolacci/murmur3 v1.1.0
2426
github.com/stretchr/testify v1.11.1
@@ -42,6 +44,7 @@ require (
4244
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.34.1 // indirect
4345
github.com/aws/aws-sdk-go-v2/service/sts v1.38.1 // indirect
4446
github.com/aws/smithy-go v1.23.0 // indirect
47+
github.com/beorn7/perks v1.0.1 // indirect
4548
github.com/boltdb/bolt v1.3.1 // indirect
4649
github.com/cespare/xxhash/v2 v2.3.0 // indirect
4750
github.com/cockroachdb/logtags v0.0.0-20230118201751-21c54148d20b // indirect
@@ -63,6 +66,9 @@ require (
6366
github.com/mattn/go-colorable v0.1.13 // indirect
6467
github.com/mattn/go-isatty v0.0.19 // indirect
6568
github.com/pmezard/go-difflib v1.0.0 // indirect
69+
github.com/prometheus/client_model v0.5.0 // indirect
70+
github.com/prometheus/common v0.48.0 // indirect
71+
github.com/prometheus/procfs v0.12.0 // indirect
6672
github.com/rogpeppe/go-internal v1.13.1 // indirect
6773
github.com/tidwall/btree v1.1.0 // indirect
6874
github.com/tidwall/match v1.1.1 // indirect

go.sum

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ github.com/aws/smithy-go v1.23.0 h1:8n6I3gXzWJB2DxBDnfxgBaSX6oe0d/t10qGz7OKqMCE=
5555
github.com/aws/smithy-go v1.23.0/go.mod h1:t1ufH5HMublsJYulve2RKmHDC15xu1f26kHCp/HgceI=
5656
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
5757
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
58+
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
5859
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
5960
github.com/boltdb/bolt v1.3.1 h1:JQmyP4ZBrce+ZQu0dY660FMfatumYDLun9hBCUVIkF4=
6061
github.com/boltdb/bolt v1.3.1/go.mod h1:clJnj/oiGkjum5o1McbSZDSLxVThjynRyGBgiAx27Ps=
@@ -153,6 +154,8 @@ github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/
153154
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
154155
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
155156
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
157+
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 h1:Ovs26xHkKqVztRpIrF/92BcuyuQ/YW4NSIpoGtfXNho=
158+
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
156159
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
157160
github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
158161
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
@@ -248,21 +251,29 @@ github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5Fsn
248251
github.com/prometheus/client_golang v1.4.0/go.mod h1:e9GMxYsXl05ICDXkRhurwBS4Q3OK1iX/F2sw+iXX5zU=
249252
github.com/prometheus/client_golang v1.7.1/go.mod h1:PY5Wy2awLA44sXw4AOSfFBetzPP4j5+D6mVACh+pe2M=
250253
github.com/prometheus/client_golang v1.11.1/go.mod h1:Z6t4BnS23TR94PD6BsDNk8yVqroYurpAkEiz0P2BEV0=
254+
github.com/prometheus/client_golang v1.19.0 h1:ygXvpU1AoN1MhdzckN+PyD9QJOSD4x7kmXYlnfbA6JU=
255+
github.com/prometheus/client_golang v1.19.0/go.mod h1:ZRM9uEAypZakd+q/x7+gmsvXdURP+DABIEIjnmDdp+k=
251256
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
252257
github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
253258
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
254259
github.com/prometheus/client_model v0.2.0/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
260+
github.com/prometheus/client_model v0.5.0 h1:VQw1hfvPvk3Uv6Qf29VrPF32JB6rtbgI6cYPYQjL0Qw=
261+
github.com/prometheus/client_model v0.5.0/go.mod h1:dTiFglRmd66nLR9Pv9f0mZi7B7fk5Pm3gvsjB5tr+kI=
255262
github.com/prometheus/common v0.0.0-20181126121408-4724e9255275/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro=
256263
github.com/prometheus/common v0.4.1/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4=
257264
github.com/prometheus/common v0.9.1/go.mod h1:yhUN8i9wzaXS3w1O07YhxHEBxD+W35wd8bs7vj7HSQ4=
258265
github.com/prometheus/common v0.10.0/go.mod h1:Tlit/dnDKsSWFlCLTWaA1cyBgKHSMdTB80sz/V91rCo=
259266
github.com/prometheus/common v0.26.0/go.mod h1:M7rCNAaPfAosfx8veZJCuw84e35h3Cfd9VFqTh1DIvc=
267+
github.com/prometheus/common v0.48.0 h1:QO8U2CdOzSn1BBsmXJXduaaW+dY/5QLjfB8svtSzKKE=
268+
github.com/prometheus/common v0.48.0/go.mod h1:0/KsvlIEfPQCQ5I2iNSAWKPZziNCvRs5EC6ILDTlAPc=
260269
github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
261270
github.com/prometheus/procfs v0.0.0-20181204211112-1dc9a6cbc91a/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
262271
github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA=
263272
github.com/prometheus/procfs v0.0.8/go.mod h1:7Qr8sr6344vo1JqZ6HhLceV9o3AJ1Ff+GxbHq6oeK9A=
264273
github.com/prometheus/procfs v0.1.3/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU=
265274
github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA=
275+
github.com/prometheus/procfs v0.12.0 h1:jluTpSng7V9hY0O2R9DzzJHYb2xULk9VTR1V1R/k6Bo=
276+
github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3cnaOZAZEfOo=
266277
github.com/redis/go-redis/v9 v9.12.1 h1:k5iquqv27aBtnTm2tIkROUDp8JBXhXZIVu1InSgvovg=
267278
github.com/redis/go-redis/v9 v9.12.1/go.mod h1:huWgSWd8mW6+m0VPhJjSSQ+d6Nh1VICQ6Q5lHuCH/Iw=
268279
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=

main.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"log"
88
"net"
9+
"net/http"
910
"os"
1011
"path/filepath"
1112

@@ -17,8 +18,10 @@ import (
1718
pb "github.com/bootjp/elastickv/proto"
1819
"github.com/bootjp/elastickv/store"
1920
"github.com/cockroachdb/errors"
21+
"github.com/grpc-ecosystem/go-grpc-prometheus"
2022
"github.com/hashicorp/raft"
2123
boltdb "github.com/hashicorp/raft-boltdb/v2"
24+
"github.com/prometheus/client_golang/prometheus/promhttp"
2225
"golang.org/x/sync/errgroup"
2326
"google.golang.org/grpc"
2427
"google.golang.org/grpc/credentials/insecure"
@@ -31,6 +34,7 @@ var (
3134
raftId = flag.String("raft_id", "", "Node id used by Raft")
3235
raftDir = flag.String("raft_data_dir", "data/", "Raft data dir")
3336
raftBootstrap = flag.Bool("raft_bootstrap", false, "Whether to bootstrap the Raft cluster")
37+
metricsAddr = flag.String("metrics_address", ":2112", "TCP host+port for Prometheus metrics")
3438
)
3539

3640
func main() {
@@ -62,7 +66,10 @@ func main() {
6266
log.Fatalf("failed to start raft: %v", err)
6367
}
6468

65-
gs := grpc.NewServer()
69+
gs := grpc.NewServer(
70+
grpc.UnaryInterceptor(grpc_prometheus.UnaryServerInterceptor),
71+
grpc.StreamInterceptor(grpc_prometheus.StreamServerInterceptor),
72+
)
6673
trx := kv.NewTransaction(r)
6774
coordinate := kv.NewCoordinator(trx, r)
6875
pb.RegisterRawKVServer(gs, adapter.NewGRPCServer(s, coordinate))
@@ -73,19 +80,26 @@ func main() {
7380
leaderhealth.Setup(r, gs, []string{"RawKV", "Example"})
7481
raftadmin.Register(gs, r)
7582
reflection.Register(gs)
83+
grpc_prometheus.Register(gs)
7684

7785
redisL, err := lc.Listen(ctx, "tcp", *redisAddr)
7886
if err != nil {
7987
log.Fatalf("failed to listen: %v", err)
8088
}
8189

90+
mux := http.NewServeMux()
91+
mux.Handle("/metrics", promhttp.Handler())
92+
8293
eg := errgroup.Group{}
8394
eg.Go(func() error {
8495
return errors.WithStack(gs.Serve(grpcSock))
8596
})
8697
eg.Go(func() error {
8798
return errors.WithStack(adapter.NewRedisServer(redisL, s, coordinate).Run())
8899
})
100+
eg.Go(func() error {
101+
return errors.WithStack(http.ListenAndServe(*metricsAddr, mux))
102+
})
89103

90104
err = eg.Wait()
91105
if err != nil {

0 commit comments

Comments
 (0)