Skip to content

Commit

Permalink
FAB-12592 Create a stats handler for the server
Browse files Browse the repository at this point in the history
Create counters for grpc connections. There will be
one counter that will track number of connections opened,
and the second counter will track the number of connections
closed.

Change-Id: I218886e4b7f3710bd342129526d5555c9fe9701c
Signed-off-by: Saad Karim <skarim@us.ibm.com>
  • Loading branch information
Saad Karim authored and mastersingh24 committed Oct 29, 2018
1 parent f23ebd5 commit 20e01e2
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
1 change: 1 addition & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions core/comm/serverstatshandler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package comm

import (
"context"

"github.com/go-kit/kit/metrics"
"google.golang.org/grpc/stats"
)

type ServerStatsHandler struct {
OpenConnCounter metrics.Counter
ClosedConnCounter metrics.Counter
}

func (h *ServerStatsHandler) TagRPC(ctx context.Context, info *stats.RPCTagInfo) context.Context {
return ctx
}

func (h *ServerStatsHandler) HandleRPC(ctx context.Context, s stats.RPCStats) {
switch s.(type) {
case *stats.Begin:
h.OpenConnCounter.Add(1)
case *stats.End:
h.ClosedConnCounter.Add(1)
}
}

func (h *ServerStatsHandler) TagConn(ctx context.Context, info *stats.ConnTagInfo) context.Context {
return ctx
}

func (h *ServerStatsHandler) HandleConn(ctx context.Context, s stats.ConnStats) {}
37 changes: 37 additions & 0 deletions core/comm/serverstatshandler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package comm_test

import (
"context"
"testing"

"github.com/hyperledger/fabric/common/metrics/metricsfakes"
"github.com/hyperledger/fabric/core/comm"
"github.com/stretchr/testify/assert"
"google.golang.org/grpc/stats"
)

func TestConnectionCounters(t *testing.T) {
openConn := &metricsfakes.Counter{}
closedConn := &metricsfakes.Counter{}
sh := &comm.ServerStatsHandler{
OpenConnCounter: openConn,
ClosedConnCounter: closedConn,
}

for i := 1; i <= 10; i++ {
sh.HandleRPC(context.Background(), &stats.Begin{})
}
assert.Equal(t, 10, openConn.AddCallCount())

for i := 1; i <= 5; i++ {
sh.HandleRPC(context.Background(), &stats.End{})
}
assert.Equal(t, 5, closedConn.AddCallCount())

}

0 comments on commit 20e01e2

Please sign in to comment.