Merge pull request #25 from anyproto/fix-metric-panic

validate rpc method name
This commit is contained in:
Sergey Cherepanov 2023-06-14 15:27:46 +02:00 committed by GitHub
commit 53cbfca3ca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 2 deletions

View File

@ -25,7 +25,6 @@ deps:
go mod download go mod download
go build -o deps storj.io/drpc/cmd/protoc-gen-go-drpc go build -o deps storj.io/drpc/cmd/protoc-gen-go-drpc
go build -o deps github.com/gogo/protobuf/protoc-gen-gogofaster go build -o deps github.com/gogo/protobuf/protoc-gen-gogofaster
go build -o deps github.com/golang/mock/mockgen
test: test:
go test ./... --cover go test ./... --cover

View File

@ -2,8 +2,10 @@ package metric
import ( import (
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
"go.uber.org/zap"
"storj.io/drpc" "storj.io/drpc"
"time" "time"
"unicode/utf8"
) )
type prometheusDRPC struct { type prometheusDRPC struct {
@ -14,7 +16,11 @@ type prometheusDRPC struct {
func (ph *prometheusDRPC) HandleRPC(stream drpc.Stream, rpc string) (err error) { func (ph *prometheusDRPC) HandleRPC(stream drpc.Stream, rpc string) (err error) {
st := time.Now() st := time.Now()
defer func() { defer func() {
if utf8.ValidString(rpc) {
ph.SummaryVec.WithLabelValues(rpc).Observe(time.Since(st).Seconds()) ph.SummaryVec.WithLabelValues(rpc).Observe(time.Since(st).Seconds())
} else {
log.WarnCtx(stream.Context(), "invalid rpc string", zap.String("rpc", rpc))
}
}() }()
return ph.Handler.HandleRPC(stream, rpc) return ph.Handler.HandleRPC(stream, rpc)
} }