add grpc support

This commit is contained in:
2025-03-07 13:00:45 -05:00
parent 98fba4eac8
commit 2cf15a4837
4 changed files with 68 additions and 19 deletions

View File

@ -16,18 +16,22 @@ import (
)
type appGRPCServer struct {
ctx context.Context
tracer trace.Tracer
meter metric.Meter
opts *opts.GRPCOpts
serverOpts []grpc.ServerOption
logger *zerolog.Logger
server *grpc.Server
ctx context.Context
tracer trace.Tracer
meter metric.Meter
opts *opts.GRPCOpts
serverOpts []grpc.ServerOption
logger *zerolog.Logger
server *grpc.Server
shutdownFunc func(context.Context) error
doneChan chan error
}
// TODO: This probably needs to pass back an error chan and a shutdown
// func like the http server does
func InitGRPCServer(ctx context.Context, opts *opts.GRPCOpts) error {
// Returns a shutdown func, a channel indicating done / error,
// and an up-front error if server fails to start
func InitGRPCServer(ctx context.Context, opts *opts.GRPCOpts) (
func(context.Context) error, <-chan error, error,
) {
appGRPC := &appGRPCServer{
ctx: ctx,
tracer: appotel.GetTracer(ctx, "grpc"),
@ -35,6 +39,7 @@ func InitGRPCServer(ctx context.Context, opts *opts.GRPCOpts) error {
opts: opts,
serverOpts: make([]grpc.ServerOption, 0),
logger: zerolog.Ctx(ctx),
doneChan: make(chan error),
}
ctx, span := appGRPC.tracer.Start(ctx, "appgrpc.init")
@ -44,23 +49,21 @@ func InitGRPCServer(ctx context.Context, opts *opts.GRPCOpts) error {
if err := appGRPC.prepGRPCServer(ctx); err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, "failed to prepare GRPC Server")
return err
return nil, nil, err
}
// Load span with server info
span.SetAttributes(appGRPC.getServerAttributes()...)
// Run, returning a shutdown func and an error chan
// TODO: Implement shutdown func and error chan
err := appGRPC.runGRPCServer(ctx)
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, "failed to start GRPC Server")
return err
return nil, nil, err
}
span.SetStatus(codes.Ok, "")
return nil
return appGRPC.shutdownFunc, appGRPC.doneChan, nil
}
// Convert grpc.ServiceInfo map to []attribute.KeyValue