Exports OTEL initialization function and adds extensive documentation across packages for improved clarity.

This commit is contained in:
2025-08-13 16:01:51 -04:00
parent 31fc6dca16
commit 318115690d
16 changed files with 50 additions and 23 deletions

View File

@@ -17,15 +17,15 @@ import (
"gopkg.in/yaml.v3"
)
// To be set by ldflags in go build command or
// retrieved from build meta below
// Version is to be set by ldflags in go build command or
// retrieved from build meta below.
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
// on-top of configuration defaults.
// 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) {
configPath := flag.String("config", "", "Path to the configuration file")
flag.Parse()
@@ -44,6 +44,8 @@ func LoadConfig(ctx context.Context) (context.Context, error) {
return ctx, nil
}
// loadConfig loads the application configuration from the specified path,
// applying environment variable overrides.
func loadConfig(configPath string) (*AppConfig, error) {
cfg := *DefaultConfig
@@ -73,6 +75,8 @@ func loadConfig(configPath string) (*AppConfig, error) {
return &cfg, nil
}
// prepareConfig enriches and validates the AppConfig, parsing duration strings
// for HTTP timeouts.
func prepareConfig(cfg *AppConfig) error {
var errs error
@@ -104,12 +108,14 @@ func prepareConfig(cfg *AppConfig) error {
return errs
}
// Returns read timeout, write timeout, and idle timeout, in that order
// nil if unset
// Timeouts returns read timeout, write timeout, and idle timeout, in that order.
// Returns nil if unset.
func (h *HTTPConfig) Timeouts() (*time.Duration, *time.Duration, *time.Duration) {
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 {
if info, ok := debug.ReadBuildInfo(); ok && info.Main.Version != "(devel)" {
return info.Main.Version

View File

@@ -9,10 +9,12 @@ type appConfigKey uint8
const appConfigCtxKey appConfigKey = iota
func (a *AppConfig) AddToCtx(ctx context.Context) context.Context {
return context.WithValue(ctx, appConfigCtxKey, a)
// AddToCtx adds the AppConfig to the provided context.
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 {
cfg, err := FromCtx(ctx)
if err != nil {
@@ -21,6 +23,7 @@ func MustFromCtx(ctx context.Context) *AppConfig {
return cfg
}
// FromCtx retrieves the AppConfig from the context.
func FromCtx(ctx context.Context) (*AppConfig, error) {
ctxData := ctx.Value(appConfigCtxKey)
if ctxData == nil {

View File

@@ -1,6 +1,6 @@
package config
// Default Settings
// DefaultConfig provides a default application configuration.
var DefaultConfig = &AppConfig{
Environment: "development",
Version: getVersion(),
@@ -23,6 +23,7 @@ type AppConfig struct {
GRPC *GRPCConfig `yaml:"grpc,omitempty" json:"grpc,omitempty"`
}
// HTTPEnabled returns true if HTTP is enabled in the AppConfig.
func (ac *AppConfig) HTTPEnabled() bool {
if ac.HTTP != nil && ac.HTTP.Enabled {
return true
@@ -30,6 +31,7 @@ func (ac *AppConfig) HTTPEnabled() bool {
return false
}
// GRPCEnabled returns true if gRPC is enabled in the AppConfig.
func (ac *AppConfig) GRPCEnabled() bool {
if ac.GRPC != nil && ac.GRPC.Enabled {
return true
@@ -37,6 +39,7 @@ func (ac *AppConfig) GRPCEnabled() bool {
return false
}
// OTELEnabled returns true if OpenTelemetry is enabled in the AppConfig.
func (ac *AppConfig) OTELEnabled() bool {
if ac.OTEL != nil && ac.OTEL.Enabled {
return true

View File

@@ -12,7 +12,7 @@ var defaultHTTPConfig = &HTTPConfig{
IdleTimeout: "1m",
}
// HTTP Configuration
// HTTPConfig provides HTTP server Configuration
type HTTPConfig struct {
Enabled bool `yaml:"enabled" env:"APP_HTTP_ENABLED" json:"enabled,omitempty"`
Listen string `yaml:"listen,omitempty" env:"APP_HTTP_LISTEN" json:"listen,omitempty"`

View File

@@ -8,7 +8,7 @@ var defaultLoggingConfig = &LogConfig{
TimeFormat: TimeFormatLong,
}
// Logging Configuration
// LogConfig provides Logging Configuration
type LogConfig struct {
Enabled bool `yaml:"enabled,omitempty" env:"APP_LOG_ENABLED" json:"enabled,omitempty"`
Level string `yaml:"level,omitempty" env:"APP_LOG_LEVEL" json:"level,omitempty"`

View File

@@ -8,7 +8,7 @@ var defaultOTELConfig = &OTELConfig{
MetricIntervalSecs: 30,
}
// OTEL Configuration
// OtelConfig provides OTEL Configuration
type OTELConfig struct {
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"`