Exports OTEL initialization function and adds extensive documentation across packages for improved clarity.
This commit is contained in:
@@ -1,3 +1,5 @@
|
|||||||
|
// Package app provides methods to configure, run, and stop
|
||||||
|
// a go-app.
|
||||||
package app
|
package app
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@@ -11,6 +13,8 @@ import (
|
|||||||
httpopts "gitea.libretechconsulting.com/rmcguire/go-app/pkg/srv/http/opts"
|
httpopts "gitea.libretechconsulting.com/rmcguire/go-app/pkg/srv/http/opts"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// App is the main application struct, holding its context, configurations,
|
||||||
|
// and various services like HTTP and gRPC.
|
||||||
type App struct {
|
type App struct {
|
||||||
AppContext context.Context
|
AppContext context.Context
|
||||||
HTTP *httpopts.AppHTTP
|
HTTP *httpopts.AppHTTP
|
||||||
|
@@ -62,7 +62,12 @@ func (a *App) initHTTP(ctx context.Context) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *App) initOTEL() {
|
func (a *App) InitOTEL() {
|
||||||
|
if a.tracer != nil {
|
||||||
|
a.l.Debug().Msg("superfluous call of already configured app otel")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
var otelShutdown shutdownFunc
|
var otelShutdown shutdownFunc
|
||||||
a.AppContext, otelShutdown = otel.Init(a.AppContext)
|
a.AppContext, otelShutdown = otel.Init(a.AppContext)
|
||||||
a.shutdownFuncs = append(a.shutdownFuncs, otelShutdown)
|
a.shutdownFuncs = append(a.shutdownFuncs, otelShutdown)
|
||||||
|
@@ -27,7 +27,7 @@ func (a *App) monitor() {
|
|||||||
a.appDone <- nil // Notify app
|
a.appDone <- nil // Notify app
|
||||||
}
|
}
|
||||||
|
|
||||||
// Typically invoked when AppContext is done
|
// Shutdown is typically invoked when AppContext is done
|
||||||
// or Server has exited. Not intended to be called
|
// or Server has exited. Not intended to be called
|
||||||
// manually
|
// manually
|
||||||
func (a *App) Shutdown() {
|
func (a *App) Shutdown() {
|
||||||
|
@@ -35,7 +35,7 @@ func (a *App) MustRun() {
|
|||||||
|
|
||||||
// Start OTEL
|
// Start OTEL
|
||||||
// Registers a NO-OP provider if not enabled
|
// Registers a NO-OP provider if not enabled
|
||||||
a.initOTEL()
|
a.InitOTEL()
|
||||||
|
|
||||||
// With OTEL ready, create an init span to track startup
|
// With OTEL ready, create an init span to track startup
|
||||||
ctx, initSpan := a.tracer.Start(a.AppContext, "init")
|
ctx, initSpan := a.tracer.Start(a.AppContext, "init")
|
||||||
|
@@ -6,11 +6,11 @@ import (
|
|||||||
js "github.com/swaggest/jsonschema-go"
|
js "github.com/swaggest/jsonschema-go"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Generates json schema for app's config.AppConfig
|
// Schema generates json schema for app's config.AppConfig
|
||||||
func (app *App) Schema() ([]byte, error) {
|
func (a *App) Schema() ([]byte, error) {
|
||||||
r := js.Reflector{}
|
r := js.Reflector{}
|
||||||
|
|
||||||
s, err := r.Reflect(*app.cfg)
|
s, err := r.Reflect(*a.cfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -18,7 +18,7 @@ func (app *App) Schema() ([]byte, error) {
|
|||||||
return json.MarshalIndent(s, "", " ")
|
return json.MarshalIndent(s, "", " ")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generates json schema for custom config
|
// CustomSchema generates json schema for custom config
|
||||||
// which embeds *config.AppConfig into it
|
// which embeds *config.AppConfig into it
|
||||||
// Panics if no *config.AppConfig is embedded into custom
|
// Panics if no *config.AppConfig is embedded into custom
|
||||||
// config type
|
// config type
|
||||||
|
@@ -9,7 +9,7 @@ import (
|
|||||||
"gitea.libretechconsulting.com/rmcguire/go-app/pkg/logging"
|
"gitea.libretechconsulting.com/rmcguire/go-app/pkg/logging"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Helper function to return a context loaded up with
|
// MustSetupConfigAndLogging is a helper function to return a context loaded up with
|
||||||
// config.AppConfig and a logger
|
// config.AppConfig and a logger
|
||||||
func MustSetupConfigAndLogging(ctx context.Context) context.Context {
|
func MustSetupConfigAndLogging(ctx context.Context) context.Context {
|
||||||
ctx, err := config.LoadConfig(ctx)
|
ctx, err := config.LoadConfig(ctx)
|
||||||
@@ -24,7 +24,7 @@ func MustSetupConfigAndLogging(ctx context.Context) context.Context {
|
|||||||
return ctx
|
return ctx
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unmarshal config into a custom type
|
// MustSetupConfigAndLoggingInto will unmarshal config into a custom type
|
||||||
// Type MUST include *config.AppConfig
|
// Type MUST include *config.AppConfig
|
||||||
// Stored in context as *config.AppConfig but can be asserted back
|
// Stored in context as *config.AppConfig but can be asserted back
|
||||||
func MustSetupConfigAndLoggingInto[T any](ctx context.Context, into T) (context.Context, T) {
|
func MustSetupConfigAndLoggingInto[T any](ctx context.Context, into T) (context.Context, T) {
|
||||||
|
@@ -13,7 +13,7 @@ import (
|
|||||||
"gitea.libretechconsulting.com/rmcguire/go-app/pkg/config"
|
"gitea.libretechconsulting.com/rmcguire/go-app/pkg/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Used to unmarshal config and environment into a custom type
|
// MustLoadConfigInto is used to unmarshal config and environment into a custom type
|
||||||
// that overloads *config.AppConfig. Will perform normal env
|
// that overloads *config.AppConfig. Will perform normal env
|
||||||
// substitutions for AppConfig, but env overrides for custom type
|
// substitutions for AppConfig, but env overrides for custom type
|
||||||
// are up to the caller.
|
// are up to the caller.
|
||||||
|
@@ -17,15 +17,15 @@ import (
|
|||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
// To be set by ldflags in go build command or
|
// Version is to be set by ldflags in go build command or
|
||||||
// retrieved from build meta below
|
// retrieved from build meta below.
|
||||||
var Version = "(devel)"
|
var Version = "(devel)"
|
||||||
|
|
||||||
// Calling this will try to load from config if -config is
|
// LoadConfig will try to load from config if -config is
|
||||||
// provided as a file, and will apply any environment overrides
|
// provided as a file, and will apply any environment overrides
|
||||||
// on-top of configuration defaults.
|
// on-top of configuration defaults.
|
||||||
// Config is stored in returned context, and can be retrieved
|
// Config is stored in returned context, and can be retrieved
|
||||||
// using config.FromCtx(ctx)
|
// using config.FromCtx(ctx).
|
||||||
func LoadConfig(ctx context.Context) (context.Context, error) {
|
func LoadConfig(ctx context.Context) (context.Context, error) {
|
||||||
configPath := flag.String("config", "", "Path to the configuration file")
|
configPath := flag.String("config", "", "Path to the configuration file")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
@@ -44,6 +44,8 @@ func LoadConfig(ctx context.Context) (context.Context, error) {
|
|||||||
return ctx, nil
|
return ctx, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// loadConfig loads the application configuration from the specified path,
|
||||||
|
// applying environment variable overrides.
|
||||||
func loadConfig(configPath string) (*AppConfig, error) {
|
func loadConfig(configPath string) (*AppConfig, error) {
|
||||||
cfg := *DefaultConfig
|
cfg := *DefaultConfig
|
||||||
|
|
||||||
@@ -73,6 +75,8 @@ func loadConfig(configPath string) (*AppConfig, error) {
|
|||||||
return &cfg, nil
|
return &cfg, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// prepareConfig enriches and validates the AppConfig, parsing duration strings
|
||||||
|
// for HTTP timeouts.
|
||||||
func prepareConfig(cfg *AppConfig) error {
|
func prepareConfig(cfg *AppConfig) error {
|
||||||
var errs error
|
var errs error
|
||||||
|
|
||||||
@@ -104,12 +108,14 @@ func prepareConfig(cfg *AppConfig) error {
|
|||||||
return errs
|
return errs
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns read timeout, write timeout, and idle timeout, in that order
|
// Timeouts returns read timeout, write timeout, and idle timeout, in that order.
|
||||||
// nil if unset
|
// Returns nil if unset.
|
||||||
func (h *HTTPConfig) Timeouts() (*time.Duration, *time.Duration, *time.Duration) {
|
func (h *HTTPConfig) Timeouts() (*time.Duration, *time.Duration, *time.Duration) {
|
||||||
return h.rT, h.wT, h.iT
|
return h.rT, h.wT, h.iT
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// getVersion returns the application version, preferring the build info version
|
||||||
|
// over the compile-time set Version variable.
|
||||||
func getVersion() string {
|
func getVersion() string {
|
||||||
if info, ok := debug.ReadBuildInfo(); ok && info.Main.Version != "(devel)" {
|
if info, ok := debug.ReadBuildInfo(); ok && info.Main.Version != "(devel)" {
|
||||||
return info.Main.Version
|
return info.Main.Version
|
||||||
|
@@ -9,10 +9,12 @@ type appConfigKey uint8
|
|||||||
|
|
||||||
const appConfigCtxKey appConfigKey = iota
|
const appConfigCtxKey appConfigKey = iota
|
||||||
|
|
||||||
func (a *AppConfig) AddToCtx(ctx context.Context) context.Context {
|
// AddToCtx adds the AppConfig to the provided context.
|
||||||
return context.WithValue(ctx, appConfigCtxKey, a)
|
func (ac *AppConfig) AddToCtx(ctx context.Context) context.Context {
|
||||||
|
return context.WithValue(ctx, appConfigCtxKey, ac)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MustFromCtx retrieves the AppConfig from the context, or panics if not found.
|
||||||
func MustFromCtx(ctx context.Context) *AppConfig {
|
func MustFromCtx(ctx context.Context) *AppConfig {
|
||||||
cfg, err := FromCtx(ctx)
|
cfg, err := FromCtx(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -21,6 +23,7 @@ func MustFromCtx(ctx context.Context) *AppConfig {
|
|||||||
return cfg
|
return cfg
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FromCtx retrieves the AppConfig from the context.
|
||||||
func FromCtx(ctx context.Context) (*AppConfig, error) {
|
func FromCtx(ctx context.Context) (*AppConfig, error) {
|
||||||
ctxData := ctx.Value(appConfigCtxKey)
|
ctxData := ctx.Value(appConfigCtxKey)
|
||||||
if ctxData == nil {
|
if ctxData == nil {
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
// Default Settings
|
// DefaultConfig provides a default application configuration.
|
||||||
var DefaultConfig = &AppConfig{
|
var DefaultConfig = &AppConfig{
|
||||||
Environment: "development",
|
Environment: "development",
|
||||||
Version: getVersion(),
|
Version: getVersion(),
|
||||||
@@ -23,6 +23,7 @@ type AppConfig struct {
|
|||||||
GRPC *GRPCConfig `yaml:"grpc,omitempty" json:"grpc,omitempty"`
|
GRPC *GRPCConfig `yaml:"grpc,omitempty" json:"grpc,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// HTTPEnabled returns true if HTTP is enabled in the AppConfig.
|
||||||
func (ac *AppConfig) HTTPEnabled() bool {
|
func (ac *AppConfig) HTTPEnabled() bool {
|
||||||
if ac.HTTP != nil && ac.HTTP.Enabled {
|
if ac.HTTP != nil && ac.HTTP.Enabled {
|
||||||
return true
|
return true
|
||||||
@@ -30,6 +31,7 @@ func (ac *AppConfig) HTTPEnabled() bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GRPCEnabled returns true if gRPC is enabled in the AppConfig.
|
||||||
func (ac *AppConfig) GRPCEnabled() bool {
|
func (ac *AppConfig) GRPCEnabled() bool {
|
||||||
if ac.GRPC != nil && ac.GRPC.Enabled {
|
if ac.GRPC != nil && ac.GRPC.Enabled {
|
||||||
return true
|
return true
|
||||||
@@ -37,6 +39,7 @@ func (ac *AppConfig) GRPCEnabled() bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// OTELEnabled returns true if OpenTelemetry is enabled in the AppConfig.
|
||||||
func (ac *AppConfig) OTELEnabled() bool {
|
func (ac *AppConfig) OTELEnabled() bool {
|
||||||
if ac.OTEL != nil && ac.OTEL.Enabled {
|
if ac.OTEL != nil && ac.OTEL.Enabled {
|
||||||
return true
|
return true
|
||||||
|
@@ -12,7 +12,7 @@ var defaultHTTPConfig = &HTTPConfig{
|
|||||||
IdleTimeout: "1m",
|
IdleTimeout: "1m",
|
||||||
}
|
}
|
||||||
|
|
||||||
// HTTP Configuration
|
// HTTPConfig provides HTTP server Configuration
|
||||||
type HTTPConfig struct {
|
type HTTPConfig struct {
|
||||||
Enabled bool `yaml:"enabled" env:"APP_HTTP_ENABLED" json:"enabled,omitempty"`
|
Enabled bool `yaml:"enabled" env:"APP_HTTP_ENABLED" json:"enabled,omitempty"`
|
||||||
Listen string `yaml:"listen,omitempty" env:"APP_HTTP_LISTEN" json:"listen,omitempty"`
|
Listen string `yaml:"listen,omitempty" env:"APP_HTTP_LISTEN" json:"listen,omitempty"`
|
||||||
|
@@ -8,7 +8,7 @@ var defaultLoggingConfig = &LogConfig{
|
|||||||
TimeFormat: TimeFormatLong,
|
TimeFormat: TimeFormatLong,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Logging Configuration
|
// LogConfig provides Logging Configuration
|
||||||
type LogConfig struct {
|
type LogConfig struct {
|
||||||
Enabled bool `yaml:"enabled,omitempty" env:"APP_LOG_ENABLED" json:"enabled,omitempty"`
|
Enabled bool `yaml:"enabled,omitempty" env:"APP_LOG_ENABLED" json:"enabled,omitempty"`
|
||||||
Level string `yaml:"level,omitempty" env:"APP_LOG_LEVEL" json:"level,omitempty"`
|
Level string `yaml:"level,omitempty" env:"APP_LOG_LEVEL" json:"level,omitempty"`
|
||||||
|
@@ -8,7 +8,7 @@ var defaultOTELConfig = &OTELConfig{
|
|||||||
MetricIntervalSecs: 30,
|
MetricIntervalSecs: 30,
|
||||||
}
|
}
|
||||||
|
|
||||||
// OTEL Configuration
|
// OtelConfig provides OTEL Configuration
|
||||||
type OTELConfig struct {
|
type OTELConfig struct {
|
||||||
Enabled bool `yaml:"enabled,omitempty" env:"APP_OTEL_ENABLED" json:"enabled,omitempty"`
|
Enabled bool `yaml:"enabled,omitempty" env:"APP_OTEL_ENABLED" json:"enabled,omitempty"`
|
||||||
PrometheusEnabled bool `yaml:"prometheusEnabled,omitempty" env:"APP_OTEL_PROMETHEUS_ENABLED" json:"prometheusEnabled,omitempty"`
|
PrometheusEnabled bool `yaml:"prometheusEnabled,omitempty" env:"APP_OTEL_PROMETHEUS_ENABLED" json:"prometheusEnabled,omitempty"`
|
||||||
|
@@ -1,3 +1,5 @@
|
|||||||
|
// Package logging provides a centralized logging utility using zerolog,
|
||||||
|
// with configurable output formats and log levels.
|
||||||
package logging
|
package logging
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@@ -1,3 +1,4 @@
|
|||||||
|
// Package otel provides OpenTelemetry initialization and configuration for tracing and metrics.
|
||||||
package otel
|
package otel
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@@ -39,6 +40,7 @@ var (
|
|||||||
|
|
||||||
const defMetricInterval = 15 * time.Second
|
const defMetricInterval = 15 * time.Second
|
||||||
|
|
||||||
|
// Init prepares otel, loading a tracer and meter stored in appp context.
|
||||||
// Context must carry config.AppConfig
|
// Context must carry config.AppConfig
|
||||||
func Init(ctx context.Context) (context.Context, func(context.Context) error) {
|
func Init(ctx context.Context) (context.Context, func(context.Context) error) {
|
||||||
cfg := config.MustFromCtx(ctx)
|
cfg := config.MustFromCtx(ctx)
|
||||||
@@ -232,7 +234,7 @@ func (s *settings) newMeterProvider(ctx context.Context) (*metric.MeterProvider,
|
|||||||
return meterProvider, nil
|
return meterProvider, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Creates a new tracer from the global opentelemetry provider
|
// NewTracer creates a new tracer from the global opentelemetry provider
|
||||||
func NewTracer(options ...trace.TracerOption) trace.Tracer {
|
func NewTracer(options ...trace.TracerOption) trace.Tracer {
|
||||||
return opentelemetry.GetTracerProvider().Tracer(
|
return opentelemetry.GetTracerProvider().Tracer(
|
||||||
os.Getenv("OTEL_SERVICE_NAME"),
|
os.Getenv("OTEL_SERVICE_NAME"),
|
||||||
|
@@ -1,3 +1,5 @@
|
|||||||
|
// Package grpc provides a gRPC server implementation with OpenTelemetry integration
|
||||||
|
// and gRPC Gateway for RESTful API exposure.
|
||||||
package grpc
|
package grpc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
Reference in New Issue
Block a user