add protovalidate support
This commit is contained in:
@ -4,7 +4,9 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"buf.build/go/protovalidate"
|
||||
grpclogging "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
|
||||
protovalidate_middleware "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/protovalidate"
|
||||
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
|
||||
"go.opentelemetry.io/otel"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
@ -29,6 +31,7 @@ func (a *appGRPCServer) prepGRPCServer(spanCtx context.Context) error {
|
||||
// Prepare GRPC Server Opts
|
||||
a.prepareOTEL(ctx)
|
||||
a.prepareLogging(ctx)
|
||||
a.prepareProtovalidate(ctx)
|
||||
|
||||
// Chain interceptors for unary RPCs
|
||||
a.serverOpts = append(a.serverOpts,
|
||||
@ -69,6 +72,28 @@ func (a *appGRPCServer) prepGRPCServer(spanCtx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// prepareProtovalidate() adds interceptors for buf.build protovalidate
|
||||
func (a *appGRPCServer) prepareProtovalidate(spanCtx context.Context) {
|
||||
_, span := a.tracer.Start(spanCtx, "appgrpc.init.prepare.protovalidate", trace.WithAttributes(
|
||||
attribute.Bool("grpc.server.protovalidate", a.opts.EnableProtovalidate)))
|
||||
defer span.End()
|
||||
|
||||
if !a.opts.EnableProtovalidate {
|
||||
return
|
||||
}
|
||||
|
||||
validator, err := protovalidate.New()
|
||||
if err != nil {
|
||||
span.RecordError(err)
|
||||
}
|
||||
|
||||
a.opts.UnaryInterceptors = append(a.opts.UnaryInterceptors,
|
||||
protovalidate_middleware.UnaryServerInterceptor(validator))
|
||||
|
||||
a.opts.StreamInterceptors = append(a.opts.StreamInterceptors,
|
||||
protovalidate_middleware.StreamServerInterceptor(validator))
|
||||
}
|
||||
|
||||
func (a *appGRPCServer) prepareOTEL(spanCtx context.Context) {
|
||||
_, span := a.tracer.Start(spanCtx, "appgrpc.init.prepare.otel", trace.WithAttributes(
|
||||
attribute.Bool("grpc.server.instrumented", a.opts.EnableInstrumentation)))
|
||||
|
Reference in New Issue
Block a user