Skip to content

Commit 20e01e2

Browse files
Saad Karimmastersingh24
authored andcommitted
FAB-12592 Create a stats handler for the server
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>
1 parent f23ebd5 commit 20e01e2

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

Gopkg.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/comm/serverstatshandler.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
package comm
8+
9+
import (
10+
"context"
11+
12+
"github.com/go-kit/kit/metrics"
13+
"google.golang.org/grpc/stats"
14+
)
15+
16+
type ServerStatsHandler struct {
17+
OpenConnCounter metrics.Counter
18+
ClosedConnCounter metrics.Counter
19+
}
20+
21+
func (h *ServerStatsHandler) TagRPC(ctx context.Context, info *stats.RPCTagInfo) context.Context {
22+
return ctx
23+
}
24+
25+
func (h *ServerStatsHandler) HandleRPC(ctx context.Context, s stats.RPCStats) {
26+
switch s.(type) {
27+
case *stats.Begin:
28+
h.OpenConnCounter.Add(1)
29+
case *stats.End:
30+
h.ClosedConnCounter.Add(1)
31+
}
32+
}
33+
34+
func (h *ServerStatsHandler) TagConn(ctx context.Context, info *stats.ConnTagInfo) context.Context {
35+
return ctx
36+
}
37+
38+
func (h *ServerStatsHandler) HandleConn(ctx context.Context, s stats.ConnStats) {}

core/comm/serverstatshandler_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
package comm_test
8+
9+
import (
10+
"context"
11+
"testing"
12+
13+
"github.com/hyperledger/fabric/common/metrics/metricsfakes"
14+
"github.com/hyperledger/fabric/core/comm"
15+
"github.com/stretchr/testify/assert"
16+
"google.golang.org/grpc/stats"
17+
)
18+
19+
func TestConnectionCounters(t *testing.T) {
20+
openConn := &metricsfakes.Counter{}
21+
closedConn := &metricsfakes.Counter{}
22+
sh := &comm.ServerStatsHandler{
23+
OpenConnCounter: openConn,
24+
ClosedConnCounter: closedConn,
25+
}
26+
27+
for i := 1; i <= 10; i++ {
28+
sh.HandleRPC(context.Background(), &stats.Begin{})
29+
}
30+
assert.Equal(t, 10, openConn.AddCallCount())
31+
32+
for i := 1; i <= 5; i++ {
33+
sh.HandleRPC(context.Background(), &stats.End{})
34+
}
35+
assert.Equal(t, 5, closedConn.AddCallCount())
36+
37+
}

0 commit comments

Comments
 (0)