setup

package module
v0.7.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 20, 2026 License: MIT Imports: 14 Imported by: 0

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

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 New

func New() *Builder

New returns an empty Builder.

func (*Builder) Build

func (b *Builder) Build(mgr *gscore.Manager) (*Result, error)

Build wires all configured components onto mgr in the required order:

  1. gsfiber.RegisterApp / gsfiberv3.RegisterApp (if WithFiberV2 / WithFiberV3)
  2. apmcore.SetupOTelSDK (if WithOTel)
  3. logcore.New + logcore.SetGlobal (if WithLogger)
  4. apmcore.NewGormPlugin via db.Use (if WithGORM + WithOTel)
  5. mgr.RegisterDB (if WithGORM)
  6. txcore.RegisterWithManager (if WithGORM)
  7. apmcore.RegisterDBPoolMetricsWithManager (if WithGORM + WithOTel)
  8. autobatch.RegisterWithManager (if WithAutobatch or WithAutobatchConfig)
  9. go-redis closers + optional OTel hooks (if WithRedis)
  10. rueidis closers + optional OTel wrapping (if WithRueidis)
  11. apmcore.RegisterWithManager (if WithOTel)
  12. logcore.RegisterGlobalWithManager (if WithLogger)
  13. logcore.InstallHTTPClientHook (if WithHTTPClientLogging)

Build returns an error only if apmcore.SetupOTelSDK, logcore.New, or db.Use(apmcore.NewGormPlugin()) fails.

func (*Builder) WithAutobatch

func (b *Builder) WithAutobatch(p *autobatch.Plugin) *Builder

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

func (b *Builder) WithAutobatchConfig(cfg autobatch.Config) *Builder

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

func (b *Builder) WithFiberV2(app *fiberv2.App) *Builder

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

func (b *Builder) WithFiberV3(app *fiberv3.App) *Builder

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

func (b *Builder) WithGORM(db *gorm.DB) *Builder

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

func (b *Builder) WithHTTPClientLogging() *Builder

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

func (b *Builder) WithLogger(opts logcore.Options) *Builder

WithLogger configures the builder to create a logcore.Logger using opts and set it as the process-global logger.

func (*Builder) WithOTel

func (b *Builder) WithOTel(ctx context.Context) *Builder

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

func (b *Builder) WithRueidis(client rueidis.Client, name string) *Builder

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL