diff --git a/pkg/app/app.go b/pkg/app/app.go index 4ee6f8d..ca7d7d4 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -2,6 +2,7 @@ package app import ( "errors" + "sync" "github.com/rs/zerolog" "go.opentelemetry.io/otel/attribute" @@ -41,26 +42,38 @@ func (a *App) MustRun() { ctx, initSpan := a.tracer.Start(a.AppContext, "init") defer initSpan.End() + var serverWG sync.WaitGroup + // Start HTTP (does not block) if a.cfg.HTTPEnabled() { - if err := a.initHTTP(ctx); err != nil { - initSpan.RecordError(err) - initSpan.SetStatus(codes.Error, err.Error()) - } - initSpan.AddEvent("http server started") - initSpan.SetAttributes(attribute.Int("http.handlers", len(a.HTTP.Funcs))) + serverWG.Add(1) + go func() { + defer serverWG.Done() + if err := a.initHTTP(ctx); err != nil { + initSpan.RecordError(err) + initSpan.SetStatus(codes.Error, err.Error()) + } + initSpan.AddEvent("http server started") + initSpan.SetAttributes(attribute.Int("http.handlers", len(a.HTTP.Funcs))) + }() } // Start GRPC (does not block) if a.cfg.GRPCEnabled() { - if err := a.initGRPC(ctx); err != nil { - initSpan.RecordError(err) - initSpan.SetStatus(codes.Error, err.Error()) - } - initSpan.AddEvent("grpc server started") - initSpan.SetAttributes(attribute.Int("grpc.services", len(a.GRPC.Services))) + serverWG.Add(1) + go func() { + defer serverWG.Done() + if err := a.initGRPC(ctx); err != nil { + initSpan.RecordError(err) + initSpan.SetStatus(codes.Error, err.Error()) + } + initSpan.AddEvent("grpc server started") + initSpan.SetAttributes(attribute.Int("grpc.services", len(a.GRPC.Services))) + }() } + serverWG.Wait() + // Monitor app lifecycle go a.run()