add server reflection to Probes list

This commit is contained in:
Tim Möhlmann 2023-05-07 16:47:43 +02:00
parent f011882b2d
commit 62b4c31834
2 changed files with 54 additions and 0 deletions

View File

@ -1,5 +1,12 @@
package grpc
import (
"path"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection/grpc_reflection_v1alpha"
)
const (
Healthz = "/Healthz"
Readiness = "/Ready"
@ -9,3 +16,18 @@ const (
var (
Probes = []string{Healthz, Readiness, Validation}
)
func init() {
Probes = append(Probes, AllPaths(grpc_reflection_v1alpha.ServerReflection_ServiceDesc)...)
}
func AllPaths(sd grpc.ServiceDesc) []string {
paths := make([]string, 0, len(sd.Methods)+len(sd.Streams))
for _, method := range sd.Methods {
paths = append(paths, path.Join("/", sd.ServiceName, method.MethodName))
}
for _, stream := range sd.Streams {
paths = append(paths, path.Join("/", sd.ServiceName, stream.StreamName))
}
return paths
}

View File

@ -0,0 +1,32 @@
package grpc
import (
"testing"
"github.com/stretchr/testify/assert"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection/grpc_reflection_v1alpha"
)
func TestAllPaths(t *testing.T) {
type args struct {
sd grpc.ServiceDesc
}
tests := []struct {
name string
args args
want []string
}{
{
name: "server reflection",
args: args{grpc_reflection_v1alpha.ServerReflection_ServiceDesc},
want: []string{"/grpc.reflection.v1alpha.ServerReflection/ServerReflectionInfo"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := AllPaths(tt.args.sd)
assert.Equal(t, tt.want, got)
})
}
}