improve grpc lifecycle

This commit is contained in:
Ryan McGuire 2025-03-07 17:04:46 -05:00
parent c5da5f7887
commit e178956eef
3 changed files with 30 additions and 5 deletions

View File

@ -17,8 +17,6 @@ func (a *App) Done() <-chan any {
func (a *App) MustRun() { func (a *App) MustRun() {
if a.cfg != nil { if a.cfg != nil {
panic(errors.New("already ran app trying to run")) panic(errors.New("already ran app trying to run"))
} else if !a.cfg.HTTP.Enabled && !a.cfg.GRPC.Enabled {
panic(errors.New("neither http nor grpc enabled, nothing to do"))
} }
// Set up app // Set up app
@ -28,17 +26,23 @@ func (a *App) MustRun() {
a.appDone = make(chan any) a.appDone = make(chan any)
a.HTTP.HTTPDone = make(chan any) a.HTTP.HTTPDone = make(chan any)
if !a.cfg.HTTPEnabled() && !a.cfg.GRPCEnabled() {
panic(errors.New("neither http nor grpc enabled, nothing to do"))
}
if len(a.HTTP.Funcs) < 1 { if len(a.HTTP.Funcs) < 1 {
a.l.Warn().Msg("no http funcs provided, only serving health and metrics") a.l.Warn().Msg("no http funcs provided, only serving health and metrics")
} }
// Start OTEL // Start OTEL
// Registers a NO-OP provider if not enabled
a.initOTEL() a.initOTEL()
ctx, initSpan := a.tracer.Start(a.AppContext, "init") ctx, initSpan := a.tracer.Start(a.AppContext, "init")
defer initSpan.End() defer initSpan.End()
// Start HTTP (does not block) // Start HTTP (does not block)
if a.cfg.HTTP.Enabled { if a.cfg.HTTPEnabled() {
if err := a.initHTTP(ctx); err != nil { if err := a.initHTTP(ctx); err != nil {
initSpan.RecordError(err) initSpan.RecordError(err)
initSpan.SetStatus(codes.Error, err.Error()) initSpan.SetStatus(codes.Error, err.Error())
@ -48,7 +52,7 @@ func (a *App) MustRun() {
} }
// Start GRPC (does not block) // Start GRPC (does not block)
if a.cfg.GRPC.Enabled { if a.cfg.GRPCEnabled() {
if err := a.initGRPC(ctx); err != nil { if err := a.initGRPC(ctx); err != nil {
initSpan.RecordError(err) initSpan.RecordError(err)
initSpan.SetStatus(codes.Error, err.Error()) initSpan.SetStatus(codes.Error, err.Error())

View File

@ -22,3 +22,24 @@ type AppConfig struct {
OTEL *OTELConfig `yaml:"otel,omitempty"` OTEL *OTELConfig `yaml:"otel,omitempty"`
GRPC *GRPCConfig `yaml:"grpc,omitempty"` GRPC *GRPCConfig `yaml:"grpc,omitempty"`
} }
func (ac *AppConfig) HTTPEnabled() bool {
if ac.HTTP != nil && ac.HTTP.Enabled {
return true
}
return false
}
func (ac *AppConfig) GRPCEnabled() bool {
if ac.GRPC != nil && ac.GRPC.Enabled {
return true
}
return false
}
func (ac *AppConfig) OTELEnabled() bool {
if ac.OTEL != nil && ac.OTEL.Enabled {
return true
}
return false
}

View File

@ -44,7 +44,7 @@ func Init(ctx context.Context) (context.Context, func(context.Context) error) {
cfg := config.MustFromCtx(ctx) cfg := config.MustFromCtx(ctx)
// Nothing to do here if not enabled // Nothing to do here if not enabled
if !cfg.OTEL.Enabled { if !cfg.OTELEnabled() {
opentelemetry.SetMeterProvider(noopMetric.NewMeterProvider()) opentelemetry.SetMeterProvider(noopMetric.NewMeterProvider())
opentelemetry.SetTracerProvider(noop.NewTracerProvider()) opentelemetry.SetTracerProvider(noop.NewTracerProvider())
// Won't function with noop providers // Won't function with noop providers