-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 46afc6a
Showing
7 changed files
with
237 additions
and
0 deletions.
There are no files selected for viewing
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,26 @@ | ||
name: ci | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
pull_request: | ||
branches: | ||
- main | ||
|
||
jobs: | ||
ci: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v3 | ||
- name: Setup go | ||
uses: actions/setup-go@v4 | ||
with: | ||
go-version-file: 'go.mod' | ||
- name: Build | ||
run: go build -v ./... | ||
- name: Lint | ||
uses: golangci/golangci-lint-action@v3 | ||
- name: Test | ||
run: go test -v ./... |
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,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2024 Iñigo Horcajo Unanue | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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,55 @@ | ||
# ChainStats | ||
|
||
ChainStats is a simple Go library that allows you to chain multiple `grpc.StatsHandler` instances. | ||
|
||
## Installation | ||
|
||
```sh | ||
go get github.com/inigohu/chainstats | ||
``` | ||
|
||
### Usage | ||
|
||
```go | ||
|
||
package main | ||
|
||
import ( | ||
"context" | ||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/stats" | ||
"github.com/inigohu/chainstats" | ||
) | ||
|
||
// Example custom stats handler | ||
type customStatsHandler struct{} | ||
|
||
func (h *customStatsHandler) TagRPC(ctx context.Context, info *stats.RPCTagInfo) context.Context { | ||
// Add custom logic here | ||
return ctx | ||
} | ||
|
||
func (h *customStatsHandler) HandleRPC(ctx context.Context, stat stats.RPCStats) { | ||
// Add custom logic here | ||
} | ||
|
||
func (h *customStatsHandler) TagConn(ctx context.Context, info *stats.ConnTagInfo) context.Context { | ||
// Add custom logic here | ||
return ctx | ||
} | ||
|
||
func (h *customStatsHandler) HandleConn(ctx context.Context, stat stats.ConnStats) { | ||
// Add custom logic here | ||
} | ||
|
||
func main() { | ||
customHandler1 := &customStatsHandler{} | ||
customHandler2 := &customStatsHandler{} | ||
|
||
statsHandler := chainstats.NewChainStatsHandler(customHandler1, customHandler2) | ||
|
||
server := grpc.NewServer(grpc.StatsHandler(statsHandler)) | ||
|
||
// Register your gRPC services and start the server | ||
} | ||
``` |
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,47 @@ | ||
package chainstats | ||
|
||
import ( | ||
"context" | ||
|
||
"google.golang.org/grpc/stats" | ||
) | ||
|
||
// ChainStatsHandler is a composite stats.Handler that chains multiple stats.Handler instances. | ||
type ChainStatsHandler struct { | ||
handlers []stats.Handler | ||
} | ||
|
||
// NewChainStatsHandler creates a new ChainStatsHandler. | ||
func NewChainStatsHandler(handlers ...stats.Handler) *ChainStatsHandler { | ||
return &ChainStatsHandler{handlers: handlers} | ||
} | ||
|
||
// TagRPC calls TagRPC on all the chained stats.Handler instances. | ||
func (c *ChainStatsHandler) TagRPC(ctx context.Context, info *stats.RPCTagInfo) context.Context { | ||
for _, handler := range c.handlers { | ||
ctx = handler.TagRPC(ctx, info) | ||
} | ||
return ctx | ||
} | ||
|
||
// HandleRPC calls HandleRPC on all the chained stats.Handler instances. | ||
func (c *ChainStatsHandler) HandleRPC(ctx context.Context, stat stats.RPCStats) { | ||
for _, handler := range c.handlers { | ||
handler.HandleRPC(ctx, stat) | ||
} | ||
} | ||
|
||
// TagConn calls TagConn on all the chained stats.Handler instances. | ||
func (c *ChainStatsHandler) TagConn(ctx context.Context, info *stats.ConnTagInfo) context.Context { | ||
for _, handler := range c.handlers { | ||
ctx = handler.TagConn(ctx, info) | ||
} | ||
return ctx | ||
} | ||
|
||
// HandleConn calls HandleConn on all the chained stats.Handler instances. | ||
func (c *ChainStatsHandler) HandleConn(ctx context.Context, stat stats.ConnStats) { | ||
for _, handler := range c.handlers { | ||
handler.HandleConn(ctx, stat) | ||
} | ||
} |
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,69 @@ | ||
package chainstats | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"google.golang.org/grpc/stats" | ||
) | ||
|
||
type mockStatsHandler struct { | ||
tagRPCCalled bool | ||
handleRPCCalled bool | ||
tagConnCalled bool | ||
handleConnCalled bool | ||
} | ||
|
||
func (m *mockStatsHandler) TagRPC(ctx context.Context, info *stats.RPCTagInfo) context.Context { | ||
m.tagRPCCalled = true | ||
return ctx | ||
} | ||
|
||
func (m *mockStatsHandler) HandleRPC(ctx context.Context, stat stats.RPCStats) { | ||
m.handleRPCCalled = true | ||
} | ||
|
||
func (m *mockStatsHandler) TagConn(ctx context.Context, info *stats.ConnTagInfo) context.Context { | ||
m.tagConnCalled = true | ||
return ctx | ||
} | ||
|
||
func (m *mockStatsHandler) HandleConn(ctx context.Context, stat stats.ConnStats) { | ||
m.handleConnCalled = true | ||
} | ||
|
||
func TestChainStatsHandler(t *testing.T) { | ||
handler1 := &mockStatsHandler{} | ||
handler2 := &mockStatsHandler{} | ||
chainHandler := NewChainStatsHandler(handler1, handler2) | ||
|
||
ctx := context.Background() | ||
info := &stats.RPCTagInfo{} | ||
stat := &stats.InPayload{} | ||
|
||
// Test TagRPC | ||
ctx = chainHandler.TagRPC(ctx, info) | ||
if !handler1.tagRPCCalled || !handler2.tagRPCCalled { | ||
t.Errorf("TagRPC was not called on all handlers") | ||
} | ||
|
||
// Test HandleRPC | ||
chainHandler.HandleRPC(ctx, stat) | ||
if !handler1.handleRPCCalled || !handler2.handleRPCCalled { | ||
t.Errorf("HandleRPC was not called on all handlers") | ||
} | ||
|
||
// Test TagConn | ||
connInfo := &stats.ConnTagInfo{} | ||
ctx = chainHandler.TagConn(ctx, connInfo) | ||
if !handler1.tagConnCalled || !handler2.tagConnCalled { | ||
t.Errorf("TagConn was not called on all handlers") | ||
} | ||
|
||
// Test HandleConn | ||
connStat := &stats.ConnBegin{} | ||
chainHandler.HandleConn(ctx, connStat) | ||
if !handler1.handleConnCalled || !handler2.handleConnCalled { | ||
t.Errorf("HandleConn was not called on all handlers") | ||
} | ||
} |
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,7 @@ | ||
module github.com/inigohu/chainstats | ||
|
||
go 1.22.0 | ||
|
||
require google.golang.org/grpc v1.64.0 | ||
|
||
require golang.org/x/sys v0.18.0 // indirect |
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,12 @@ | ||
golang.org/x/net v0.22.0 h1:9sGLhx7iRIHEiX0oAJ3MRZMUCElJgy7Br1nO+AMN3Tc= | ||
golang.org/x/net v0.22.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg= | ||
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4= | ||
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= | ||
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= | ||
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= | ||
google.golang.org/genproto/googleapis/rpc v0.0.0-20240318140521-94a12d6c2237 h1:NnYq6UN9ReLM9/Y01KWNOWyI5xQ9kbIms5GGJVwS/Yc= | ||
google.golang.org/genproto/googleapis/rpc v0.0.0-20240318140521-94a12d6c2237/go.mod h1:WtryC6hu0hhx87FDGxWCDptyssuo68sk10vYjF+T9fY= | ||
google.golang.org/grpc v1.64.0 h1:KH3VH9y/MgNQg1dE7b3XfVK0GsPSIzJwdF617gUSbvY= | ||
google.golang.org/grpc v1.64.0/go.mod h1:oxjF8E3FBnjp+/gVFYdWacaLDx9na1aqy9oovLpxQYg= | ||
google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI= | ||
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= |