add grpc support

This commit is contained in:
2025-03-06 17:16:27 -05:00
parent 004c1b1ee6
commit e11c563c3a
10 changed files with 225 additions and 142 deletions

View File

@ -1,42 +1,15 @@
package app
import (
"context"
"errors"
"net/http"
"github.com/rs/zerolog"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
"gitea.libretechconsulting.com/rmcguire/go-app/pkg/config"
"gitea.libretechconsulting.com/rmcguire/go-app/pkg/otel"
"gitea.libretechconsulting.com/rmcguire/go-app/pkg/srv"
)
type App struct {
AppContext context.Context
HTTP *AppHTTP
cfg *config.AppConfig
l *zerolog.Logger
tracer trace.Tracer
shutdownFuncs []shutdownFunc
appDone chan interface{}
}
type AppHTTP struct {
Funcs []srv.HTTPFunc
Middleware []http.Handler
HealthChecks []srv.HealthCheckFunc
httpDone <-chan interface{}
}
type (
healthCheckFunc func(context.Context) error
shutdownFunc func(context.Context) error
)
func (a *App) Done() <-chan interface{} {
func (a *App) Done() <-chan any {
return a.appDone
}
@ -49,8 +22,8 @@ func (a *App) MustRun() {
a.cfg = config.MustFromCtx(a.AppContext)
a.l = zerolog.Ctx(a.AppContext)
a.shutdownFuncs = make([]shutdownFunc, 0)
a.appDone = make(chan interface{})
a.HTTP.httpDone = make(chan interface{})
a.appDone = make(chan any)
a.HTTP.httpDone = make(chan any)
if len(a.HTTP.Funcs) < 1 {
a.l.Warn().Msg("no http funcs provided, only serving health and metrics")
@ -58,11 +31,17 @@ func (a *App) MustRun() {
// Start OTEL
a.initOTEL()
var initSpan trace.Span
_, initSpan = a.tracer.Start(a.AppContext, "init")
ctx, initSpan := a.tracer.Start(a.AppContext, "init")
defer initSpan.End()
// Start HTTP
a.initHTTP()
if err := a.initHTTP(ctx); err != nil {
initSpan.RecordError(err)
initSpan.SetStatus(codes.Error, err.Error())
}
// Start GRPC
a.initGRPC()
// Monitor app lifecycle
go a.run()
@ -74,25 +53,4 @@ func (a *App) MustRun() {
Str("logLevel", a.cfg.Logging.Level).
Msg("app initialized")
initSpan.SetStatus(codes.Ok, "")
initSpan.End()
}
func (a *App) initHTTP() {
var httpShutdown shutdownFunc
httpShutdown, a.HTTP.httpDone = srv.MustInitHTTPServer(
&srv.HTTPServerOpts{
Ctx: a.AppContext,
HandleFuncs: a.HTTP.Funcs,
Middleware: a.HTTP.Middleware,
HealthCheckFuncs: a.HTTP.HealthChecks,
},
)
a.shutdownFuncs = append(a.shutdownFuncs, httpShutdown)
}
func (a *App) initOTEL() {
var otelShutdown shutdownFunc
a.AppContext, otelShutdown = otel.Init(a.AppContext)
a.shutdownFuncs = append(a.shutdownFuncs, otelShutdown)
a.tracer = otel.MustTracerFromCtx(a.AppContext)
}

55
pkg/app/app_init.go Normal file
View File

@ -0,0 +1,55 @@
package app
import (
"context"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"gitea.libretechconsulting.com/rmcguire/go-app/pkg/otel"
"gitea.libretechconsulting.com/rmcguire/go-app/pkg/srv"
)
func (a *App) initGRPC() {
}
func (a *App) initHTTP(ctx context.Context) error {
var err error
var httpShutdown shutdownFunc
_, span := a.tracer.Start(ctx, "init.http")
defer span.End()
span.SetAttributes(
attribute.Int("numHTTPFuncs", len(a.HTTP.Funcs)),
attribute.Int("numHTTPMiddlewares", len(a.HTTP.Middleware)),
attribute.Int("numHTTPHealthChecks", len(a.HTTP.HealthChecks)),
)
httpShutdown, a.HTTP.httpDone, err = srv.InitHTTPServer(
&srv.HTTPServerOpts{
Ctx: a.AppContext,
HandleFuncs: a.HTTP.Funcs,
Middleware: a.HTTP.Middleware,
HealthCheckFuncs: a.HTTP.HealthChecks,
},
)
a.shutdownFuncs = append(a.shutdownFuncs, httpShutdown)
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
} else {
span.SetStatus(codes.Ok, "")
}
return err
}
func (a *App) initOTEL() {
var otelShutdown shutdownFunc
a.AppContext, otelShutdown = otel.Init(a.AppContext)
a.shutdownFuncs = append(a.shutdownFuncs, otelShutdown)
a.tracer = otel.MustTracerFromCtx(a.AppContext)
}

47
pkg/app/app_types.go Normal file
View File

@ -0,0 +1,47 @@
package app
import (
"context"
"net/http"
"github.com/rs/zerolog"
"go.opentelemetry.io/otel/trace"
"google.golang.org/grpc"
"gitea.libretechconsulting.com/rmcguire/go-app/pkg/config"
"gitea.libretechconsulting.com/rmcguire/go-app/pkg/srv"
)
type App struct {
AppContext context.Context
HTTP *AppHTTP
GRPC *AppGRPC
cfg *config.AppConfig
l *zerolog.Logger
tracer trace.Tracer
shutdownFuncs []shutdownFunc
appDone chan any
}
type AppGRPC struct {
Services []*GRPCService
GRPCOpts []grpc.ServerOption
}
type GRPCService struct {
Name string // Descriptive name of service
Type *grpc.ServiceDesc // Type (from protoc generated code)
Service any // Implementation of GRPCService.Type (ptr)
}
type AppHTTP struct {
Funcs []srv.HTTPFunc
Middleware []http.Handler
HealthChecks []srv.HealthCheckFunc
httpDone <-chan any
}
type (
healthCheckFunc func(context.Context) error
shutdownFunc func(context.Context) error
)