Documentation
¶
Overview ¶
Package setup wires the full standard stack in one call with correct ordering, eliminating the "register everything in the right order" footgun.
Usage:
mgr := gscore.New(gscore.Config{})
result, err := setup.New().
WithLogger(logcore.Options{Service: "my-service"}).
WithOTel(ctx).
WithGORM(db).
Build(mgr)
if err != nil {
log.Fatal(err)
}
Index ¶
- type Builder
- func (b *Builder) Build(mgr *gscore.Manager) (*Result, error)
- func (b *Builder) WithAutobatch(p *autobatch.Plugin) *Builder
- func (b *Builder) WithAutobatchConfig(cfg autobatch.Config) *Builder
- func (b *Builder) WithFiberV2(app *fiberv2.App) *Builder
- func (b *Builder) WithFiberV3(app *fiberv3.App) *Builder
- func (b *Builder) WithGORM(db *gorm.DB) *Builder
- func (b *Builder) WithHTTPClientLogging() *Builder
- func (b *Builder) WithLogger(opts logcore.Options) *Builder
- func (b *Builder) WithOTel(ctx context.Context) *Builder
- func (b *Builder) WithRedis(client redis.UniversalClient, name string) *Builder
- func (b *Builder) WithRueidis(client rueidis.Client, name string) *Builder
- type Result
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Builder ¶
type Builder struct {
// contains filtered or unexported fields
}
Builder accumulates configuration via functional options before wiring everything in Build. The zero value is valid; calling Build with no options registers nothing and returns an empty Result.
func (*Builder) Build ¶
Build wires all configured components onto mgr in the required order:
- gsfiber.RegisterApp / gsfiberv3.RegisterApp (if WithFiberV2 / WithFiberV3)
- apmcore.SetupOTelSDK (if WithOTel)
- logcore.New + logcore.SetGlobal (if WithLogger)
- apmcore.NewGormPlugin via db.Use (if WithGORM + WithOTel)
- mgr.RegisterDB (if WithGORM)
- txcore.RegisterWithManager (if WithGORM)
- apmcore.RegisterDBPoolMetricsWithManager (if WithGORM + WithOTel)
- autobatch.RegisterWithManager (if WithAutobatch or WithAutobatchConfig)
- go-redis closers + optional OTel hooks (if WithRedis)
- rueidis closers + optional OTel wrapping (if WithRueidis)
- apmcore.RegisterWithManager (if WithOTel)
- logcore.RegisterGlobalWithManager (if WithLogger)
- logcore.InstallHTTPClientHook (if WithHTTPClientLogging)
Build returns an error only if apmcore.SetupOTelSDK, logcore.New, or db.Use(apmcore.NewGormPlugin()) fails.
func (*Builder) WithAutobatch ¶
WithAutobatch configures the builder to register p.Close() as a PhasePostDrain closer on the Manager, draining batched writes before the DB pool closes.
func (*Builder) WithAutobatchConfig ¶
WithAutobatchConfig configures the builder to create an autobatch plugin from cfg and register it with the Manager. When WithOTel is also set, Build automatically injects cfg.SpanEmitter = apmcore.BatchSpanEmitter() before creating the plugin so batched writes appear in APM traces.
func (*Builder) WithFiberV2 ¶
WithFiberV2 registers a Fiber v2 app on the Manager so it is drained during the drain phase of graceful shutdown. Multiple calls are not supported; only the last supplied app is registered.
func (*Builder) WithFiberV3 ¶
WithFiberV3 registers a Fiber v3 app on the Manager so it is drained during the drain phase of graceful shutdown. Multiple calls are not supported; only the last supplied app is registered.
func (*Builder) WithGORM ¶
WithGORM configures the builder to register txcore with the Manager so that in-flight transactions are drained before the DB pool is closed.
func (*Builder) WithHTTPClientLogging ¶
WithHTTPClientLogging configures the builder to install the global logcore HTTP client hook via logcore.InstallHTTPClientHook. The hook is installed after the logger is set up so it inherits the configured global logger.
func (*Builder) WithLogger ¶
WithLogger configures the builder to create a logcore.Logger using opts and set it as the process-global logger.
func (*Builder) WithOTel ¶
WithOTel configures the builder to call apmcore.SetupOTelSDK(ctx) during Build. ctx is typically the application root context or context.Background().
func (*Builder) WithRedis ¶
func (b *Builder) WithRedis(client redis.UniversalClient, name string) *Builder
WithRedis registers a go-redis UniversalClient for graceful shutdown at PhasePostDB and, if WithOTel was also called, instruments it with OTel tracing and metrics. Multiple calls accumulate; each client is registered independently. name is used in shutdown log lines.
func (*Builder) WithRueidis ¶
WithRueidis registers a rueidis Client for graceful shutdown at PhasePostDB and, if WithOTel was also called, wraps it with OTel tracing and metrics. Multiple calls accumulate; each client is registered independently. name is used in shutdown log lines.
type Result ¶
type Result struct {
// Logger is the logcore.Logger created by WithLogger; nil if not configured.
Logger *logcore.Logger
// Shutdown is the OTel/APM shutdown function registered by WithOTel; nil
// if not configured.
Shutdown apmcore.ShutdownFunc
}
Result is returned by Build and carries handles to the resources that were created. Fields are nil/zero when the corresponding With-option was not set.