#1 #5

Merged
vtolstov merged 4 commits from kgorbunov/servicechecker:#1 into master 2024-11-24 12:57:54 +03:00
2 changed files with 13 additions and 29 deletions
Showing only changes of commit f28a771975 - Show all commits

View File

@ -174,11 +174,10 @@ func main() {
var fn any
var args []any
p := protoset.NewProtoSet(l)
switch {
case task.GRPC != nil:
fn, args, err = newGRPCTask(ctx, l, mtr, p, check.Name, task)
fn, args, err = newGRPCTask(ctx, l, mtr, check.Name, task)
case task.HTTP != nil:
fn, args, err = newHTTPTask(ctx, l, mtr, check.Name, task)
case task.GraphQL != nil:
@ -291,7 +290,7 @@ func newHTTPTask(ctx context.Context, l logger.Logger, m meter.Meter, check stri
return fn, nil, nil
}
func newGRPCTask(ctx context.Context, l logger.Logger, m meter.Meter, p *protoset.ProtoSet, check string, task *config.TaskConfig) (any, []any, error) {
func newGRPCTask(ctx context.Context, l logger.Logger, m meter.Meter, check string, task *config.TaskConfig) (any, []any, error) {
var err error
c, ok := clients["grpc"]
@ -305,6 +304,7 @@ func newGRPCTask(ctx context.Context, l logger.Logger, m meter.Meter, p *protose
var rsp interface{}
var treq client.Request
var labels []string
p := protoset.NewProtoSet()
if task.GRPC.Protoset != "" {
pkg, svc, mth := grpcconn.ServiceMethod(task.GRPC.Endpoint)
@ -315,12 +315,12 @@ func newGRPCTask(ctx context.Context, l logger.Logger, m meter.Meter, p *protose
return nil, nil, err
}
if err = p.AddProtoset(ctx, task.GRPC.Addr, svc, protosetBuf); err != nil {
if err = p.AddProtoset(task.GRPC.Addr, svc, protosetBuf); err != nil {
l.Error(ctx, "failed add protoset", err)
return nil, nil, err
}
req, rsp, err = p.GetMessage(ctx, task.GRPC.Addr, pkg, svc, mth)
req, rsp, err = p.GetMessage(task.GRPC.Addr, pkg, svc, mth)
if err != nil {
l.Error(ctx, "failed get req, rsp from protoset", err)
return nil, nil, err

View File

@ -7,7 +7,6 @@ import (
"sync"
protocodec "go.unistack.org/micro-codec-proto/v3"
"go.unistack.org/micro/v3/logger"
"google.golang.org/protobuf/reflect/protodesc"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/reflect/protoregistry"
@ -20,49 +19,39 @@ var errNotFound = errors.New("file descriptor not found")
type ProtoSet struct {
mu sync.Mutex
files map[string]*protoregistry.Files
log logger.Logger
}
func NewProtoSet(log logger.Logger) *ProtoSet {
func NewProtoSet() *ProtoSet {
return &ProtoSet{
mu: sync.Mutex{},
files: make(map[string]*protoregistry.Files, 0),
log: log,
}
}
func (p *ProtoSet) GetMessage(ctx context.Context, addr, pkg, svc, mth string) (protoreflect.Message, protoreflect.Message, error) {
p.log.Debug(ctx, "start of GetMessage")
func (p *ProtoSet) GetMessage(addr, pkg, svc, mth string) (protoreflect.Message, protoreflect.Message, error) {
if addr == "" || svc == "" || mth == "" {

проверку pkg пропустил, по факту все 4 параметра тут обязательные.

проверку pkg пропустил, по факту все 4 параметра тут обязательные.
p.log.Error(ctx, "protoset: empty addr or service param")
return nil, nil, errors.New("addr or service name is empty")
}
p.mu.Lock()
pfile, ok := p.files[addr+"|"+svc]
p.mu.Unlock()
if !ok || pfile == nil {
p.log.Error(ctx, "protoset: file desc not found")
return nil, nil, errNotFound
}
pdesc, err := pfile.FindDescriptorByName(protoreflect.FullName(pkg + "." + svc))
if err != nil {
p.log.Error(ctx, "failed to find service "+pkg+"."+svc)
return nil, nil, err
return nil, nil, fmt.Errorf("failed to find service %s.%s, err: %s", pkg, svc, err)
}
sdesc, ok := pdesc.(protoreflect.ServiceDescriptor)
if !ok {
err = fmt.Errorf("failed to find service " + pkg + "." + svc)
p.log.Error(ctx, "unable to find service in protoset", err)
return nil, nil, err
return nil, nil, fmt.Errorf("failed to find service " + pkg + "." + svc)
}
mdesc := sdesc.Methods().ByName(protoreflect.Name(mth))
if mdesc == nil {
err = fmt.Errorf("unknown method " + mth)
p.log.Error(ctx, "failed to find method", err)
return nil, nil, err
return nil, nil, fmt.Errorf("unknown method " + mth)
}
req := dynamicpb.NewMessageType(mdesc.Input()).New()
@ -71,20 +60,16 @@ func (p *ProtoSet) GetMessage(ctx context.Context, addr, pkg, svc, mth string) (
return req, rsp, nil
}
func (p *ProtoSet) AddProtoset(ctx context.Context, addr, svc string, data []byte) error {
p.log.Debug(ctx, "start of AddProtoset")
func (p *ProtoSet) AddProtoset(addr, svc string, data []byte) error {
fdset := &descriptorpb.FileDescriptorSet{}
if err := protocodec.NewCodec().Unmarshal(data, fdset); err != nil {
p.log.Error(ctx, "failed to unmarshal protoset file", err)
return err
return fmt.Errorf("failed to unmarshal protoset file: %s", err)

такие штуки fmt.Errorf("failed to unmarshal protoset file: %s", err) лучше через
fmt.Errorf("failed to unmarshal protoset file: %w", err) делать, чтобы оно врапало исходную ошибку.

такие штуки fmt.Errorf("failed to unmarshal protoset file: %s", err) лучше через fmt.Errorf("failed to unmarshal protoset file: %w", err) делать, чтобы оно врапало исходную ошибку.
}
pfileoptions := protodesc.FileOptions{AllowUnresolvable: true}
pfiles, err := pfileoptions.NewFiles(fdset)
if err != nil {
p.log.Error(ctx, "failed to use protoset file", err)
return err
return fmt.Errorf("failed to use protoset file, err: %s", err)
}
p.mu.Lock()
@ -95,6 +80,5 @@ func (p *ProtoSet) AddProtoset(ctx context.Context, addr, svc string, data []byt
}
func (p *ProtoSet) AddReflection(ctx context.Context, service string, addr string) error {
p.log.Debug(ctx, "start of AddReflection")
return nil
}