Skip to content

Commit

Permalink
*: add "etcd_server_client_requests_total", tests
Browse files Browse the repository at this point in the history
Signed-off-by: Gyuho Lee <leegyuho@amazon.com>
  • Loading branch information
gyuho committed Mar 17, 2020
1 parent 58ba322 commit 3390747
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 4 deletions.
1 change: 1 addition & 0 deletions .words
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ etcd
gRPC
goroutine
goroutines
hasleader
healthcheck
hostname
iff
Expand Down
19 changes: 19 additions & 0 deletions clientv3/integration/kv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ package integration
import (
"bytes"
"context"
"fmt"
"os"
"reflect"
"strconv"
"strings"
"testing"
"time"
Expand All @@ -28,6 +30,7 @@ import (
"go.etcd.io/etcd/integration"
"go.etcd.io/etcd/mvcc/mvccpb"
"go.etcd.io/etcd/pkg/testutil"
"go.etcd.io/etcd/version"

"google.golang.org/grpc"
"google.golang.org/grpc/codes"
Expand Down Expand Up @@ -208,6 +211,22 @@ func TestKVPutWithRequireLeader(t *testing.T) {
t.Fatal(err)
}

cnt, err := clus.Members[0].Metric(
"etcd_server_client_requests_total",
`type="unary"`,
fmt.Sprintf(`client_api_version="%v"`, version.APIVersion),
)
if err != nil {
t.Fatal(err)
}
cv, err := strconv.ParseInt(cnt, 10, 32)
if err != nil {
t.Fatal(err)
}
if cv < 1 { // >1 when retried
t.Fatalf("expected at least 1, got %q", cnt)
}

// clients may give timeout errors since the members are stopped; take
// the clients so that terminating the cluster won't complain
clus.Client(1).Close()
Expand Down
18 changes: 18 additions & 0 deletions clientv3/integration/watch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"math/rand"
"reflect"
"sort"
"strconv"
"testing"
"time"

Expand All @@ -29,6 +30,7 @@ import (
"go.etcd.io/etcd/integration"
mvccpb "go.etcd.io/etcd/mvcc/mvccpb"
"go.etcd.io/etcd/pkg/testutil"
"go.etcd.io/etcd/version"

"google.golang.org/grpc/metadata"
)
Expand Down Expand Up @@ -839,6 +841,22 @@ func TestWatchWithRequireLeader(t *testing.T) {
if _, ok := <-chNoLeader; !ok {
t.Fatalf("expected response, got closed channel")
}

cnt, err := clus.Members[0].Metric(
"etcd_server_client_requests_total",
`type="stream"`,
fmt.Sprintf(`client_api_version="%v"`, version.APIVersion),
)
if err != nil {
t.Fatal(err)
}
cv, err := strconv.ParseInt(cnt, 10, 32)
if err != nil {
t.Fatal(err)
}
if cv < 2 { // >2 when retried
t.Fatalf("expected at least 2, got %q", cnt)
}
}

// TestWatchWithFilter checks that watch filtering works.
Expand Down
13 changes: 12 additions & 1 deletion etcdserver/api/v3rpc/interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ func newUnaryInterceptor(s *etcdserver.EtcdServer) grpc.UnaryServerInterceptor {

md, ok := metadata.FromIncomingContext(ctx)
if ok {
ver, vs := "unknown", md.Get(rpctypes.MetadataClientAPIVersionKey)
if len(vs) > 0 {
ver = vs[0]
}
clientRequests.WithLabelValues("unary", ver).Inc()

if ks := md[rpctypes.MetadataRequireLeaderKey]; len(ks) > 0 && ks[0] == rpctypes.MetadataHasLeader {
if s.Leader() == types.ID(raft.None) {
return nil, rpctypes.ErrGRPCNoLeader
Expand Down Expand Up @@ -184,6 +190,12 @@ func newStreamInterceptor(s *etcdserver.EtcdServer) grpc.StreamServerInterceptor

md, ok := metadata.FromIncomingContext(ss.Context())
if ok {
ver, vs := "unknown", md.Get(rpctypes.MetadataClientAPIVersionKey)
if len(vs) > 0 {
ver = vs[0]
}
clientRequests.WithLabelValues("stream", ver).Inc()

if ks := md[rpctypes.MetadataRequireLeaderKey]; len(ks) > 0 && ks[0] == rpctypes.MetadataHasLeader {
if s.Leader() == types.ID(raft.None) {
return rpctypes.ErrGRPCNoLeader
Expand All @@ -202,7 +214,6 @@ func newStreamInterceptor(s *etcdserver.EtcdServer) grpc.StreamServerInterceptor
smap.mu.Unlock()
cancel()
}()

}
}

Expand Down
10 changes: 10 additions & 0 deletions etcdserver/api/v3rpc/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,20 @@ var (
},
[]string{"Type", "API"},
)

clientRequests = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: "etcd",
Subsystem: "server",
Name: "client_requests_total",
Help: "The total number of client requests per client version.",
},
[]string{"type", "client_api_version"},
)
)

func init() {
prometheus.MustRegister(sentBytes)
prometheus.MustRegister(receivedBytes)
prometheus.MustRegister(streamFailures)
prometheus.MustRegister(clientRequests)
}
17 changes: 14 additions & 3 deletions integration/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -1143,7 +1143,7 @@ func (m *member) Terminate(t testing.TB) {
}

// Metric gets the metric value for a member
func (m *member) Metric(metricName string) (string, error) {
func (m *member) Metric(metricName string, expectLabels ...string) (string, error) {
cfgtls := transport.TLSInfo{}
tr, err := transport.NewTimeoutTransport(cfgtls, time.Second, time.Second, time.Second)
if err != nil {
Expand All @@ -1161,9 +1161,20 @@ func (m *member) Metric(metricName string) (string, error) {
}
lines := strings.Split(string(b), "\n")
for _, l := range lines {
if strings.HasPrefix(l, metricName) {
return strings.Split(l, " ")[1], nil
if !strings.HasPrefix(l, metricName) {
continue
}
ok := true
for _, lv := range expectLabels {
if !strings.Contains(l, lv) {
ok = false
break
}
}
if !ok {
continue
}
return strings.Split(l, " ")[1], nil
}
return "", nil
}
Expand Down

0 comments on commit 3390747

Please sign in to comment.