-
Notifications
You must be signed in to change notification settings - Fork 8.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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>
- Loading branch information
1 parent
f23ebd5
commit 20e01e2
Showing
3 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
|
||
} |