Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
896225effc | |||
06bf7ebca7 | |||
15a597de1d | |||
56037c4b05 |
2
TODO.md
2
TODO.md
@ -1,6 +1,6 @@
|
|||||||
# TODO
|
# TODO
|
||||||
|
|
||||||
- [ ] Pattern for extending config
|
- [x] Pattern for extending config
|
||||||
|
|
||||||
## Done
|
## Done
|
||||||
- [x] Unit tests
|
- [x] Unit tests
|
||||||
|
12
go.mod
12
go.mod
@ -30,7 +30,7 @@ require (
|
|||||||
github.com/google/uuid v1.6.0 // indirect
|
github.com/google/uuid v1.6.0 // indirect
|
||||||
github.com/grpc-ecosystem/grpc-gateway/v2 v2.25.1 // indirect
|
github.com/grpc-ecosystem/grpc-gateway/v2 v2.25.1 // indirect
|
||||||
github.com/klauspost/compress v1.17.11 // indirect
|
github.com/klauspost/compress v1.17.11 // indirect
|
||||||
github.com/mattn/go-colorable v0.1.13 // indirect
|
github.com/mattn/go-colorable v0.1.14 // indirect
|
||||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||||
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
|
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
|
||||||
github.com/prometheus/client_model v0.6.1 // indirect
|
github.com/prometheus/client_model v0.6.1 // indirect
|
||||||
@ -38,12 +38,12 @@ require (
|
|||||||
github.com/prometheus/procfs v0.15.1 // indirect
|
github.com/prometheus/procfs v0.15.1 // indirect
|
||||||
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
|
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
|
||||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.33.0 // indirect
|
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.33.0 // indirect
|
||||||
go.opentelemetry.io/proto/otlp v1.4.0 // indirect
|
go.opentelemetry.io/proto/otlp v1.5.0 // indirect
|
||||||
golang.org/x/net v0.33.0 // indirect
|
golang.org/x/net v0.34.0 // indirect
|
||||||
golang.org/x/sys v0.29.0 // indirect
|
golang.org/x/sys v0.29.0 // indirect
|
||||||
golang.org/x/text v0.21.0 // indirect
|
golang.org/x/text v0.21.0 // indirect
|
||||||
google.golang.org/genproto/googleapis/api v0.0.0-20250102185135-69823020774d // indirect
|
google.golang.org/genproto/googleapis/api v0.0.0-20250106144421-5f5ef82da422 // indirect
|
||||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20250102185135-69823020774d // indirect
|
google.golang.org/genproto/googleapis/rpc v0.0.0-20250106144421-5f5ef82da422 // indirect
|
||||||
google.golang.org/grpc v1.69.2 // indirect
|
google.golang.org/grpc v1.69.2 // indirect
|
||||||
google.golang.org/protobuf v1.36.1 // indirect
|
google.golang.org/protobuf v1.36.2 // indirect
|
||||||
)
|
)
|
||||||
|
12
go.sum
12
go.sum
@ -35,6 +35,8 @@ github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0
|
|||||||
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
|
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
|
||||||
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
|
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
|
||||||
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
|
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
|
||||||
|
github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE=
|
||||||
|
github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8=
|
||||||
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
|
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
|
||||||
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||||
@ -87,10 +89,14 @@ go.opentelemetry.io/otel/trace v1.33.0 h1:cCJuF7LRjUFso9LPnEAHJDB2pqzp+hbO8eu1qq
|
|||||||
go.opentelemetry.io/otel/trace v1.33.0/go.mod h1:uIcdVUZMpTAmz0tI1z04GoVSezK37CbGV4fr1f2nBck=
|
go.opentelemetry.io/otel/trace v1.33.0/go.mod h1:uIcdVUZMpTAmz0tI1z04GoVSezK37CbGV4fr1f2nBck=
|
||||||
go.opentelemetry.io/proto/otlp v1.4.0 h1:TA9WRvW6zMwP+Ssb6fLoUIuirti1gGbP28GcKG1jgeg=
|
go.opentelemetry.io/proto/otlp v1.4.0 h1:TA9WRvW6zMwP+Ssb6fLoUIuirti1gGbP28GcKG1jgeg=
|
||||||
go.opentelemetry.io/proto/otlp v1.4.0/go.mod h1:PPBWZIP98o2ElSqI35IHfu7hIhSwvc5N38Jw8pXuGFY=
|
go.opentelemetry.io/proto/otlp v1.4.0/go.mod h1:PPBWZIP98o2ElSqI35IHfu7hIhSwvc5N38Jw8pXuGFY=
|
||||||
|
go.opentelemetry.io/proto/otlp v1.5.0 h1:xJvq7gMzB31/d406fB8U5CBdyQGw4P399D1aQWU/3i4=
|
||||||
|
go.opentelemetry.io/proto/otlp v1.5.0/go.mod h1:keN8WnHxOy8PG0rQZjJJ5A2ebUoafqWp0eVQ4yIXvJ4=
|
||||||
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
|
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
|
||||||
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
|
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
|
||||||
golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I=
|
golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I=
|
||||||
golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
|
golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
|
||||||
|
golang.org/x/net v0.34.0 h1:Mb7Mrk043xzHgnRM88suvJFwzVrRfHEHJEl5/71CKw0=
|
||||||
|
golang.org/x/net v0.34.0/go.mod h1:di0qlW3YNM5oh6GqDGQr92MyTozJPmybPK4Ev/Gm31k=
|
||||||
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
@ -100,12 +106,18 @@ golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
|
|||||||
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
|
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
|
||||||
google.golang.org/genproto/googleapis/api v0.0.0-20250102185135-69823020774d h1:H8tOf8XM88HvKqLTxe755haY6r1fqqzLbEnfrmLXlSA=
|
google.golang.org/genproto/googleapis/api v0.0.0-20250102185135-69823020774d h1:H8tOf8XM88HvKqLTxe755haY6r1fqqzLbEnfrmLXlSA=
|
||||||
google.golang.org/genproto/googleapis/api v0.0.0-20250102185135-69823020774d/go.mod h1:2v7Z7gP2ZUOGsaFyxATQSRoBnKygqVq2Cwnvom7QiqY=
|
google.golang.org/genproto/googleapis/api v0.0.0-20250102185135-69823020774d/go.mod h1:2v7Z7gP2ZUOGsaFyxATQSRoBnKygqVq2Cwnvom7QiqY=
|
||||||
|
google.golang.org/genproto/googleapis/api v0.0.0-20250106144421-5f5ef82da422 h1:GVIKPyP/kLIyVOgOnTwFOrvQaQUzOzGMCxgFUOEmm24=
|
||||||
|
google.golang.org/genproto/googleapis/api v0.0.0-20250106144421-5f5ef82da422/go.mod h1:b6h1vNKhxaSoEI+5jc3PJUCustfli/mRab7295pY7rw=
|
||||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20250102185135-69823020774d h1:xJJRGY7TJcvIlpSrN3K6LAWgNFUILlO+OMAqtg9aqnw=
|
google.golang.org/genproto/googleapis/rpc v0.0.0-20250102185135-69823020774d h1:xJJRGY7TJcvIlpSrN3K6LAWgNFUILlO+OMAqtg9aqnw=
|
||||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20250102185135-69823020774d/go.mod h1:3ENsm/5D1mzDyhpzeRi1NR784I0BcofWBoSc5QqqMK4=
|
google.golang.org/genproto/googleapis/rpc v0.0.0-20250102185135-69823020774d/go.mod h1:3ENsm/5D1mzDyhpzeRi1NR784I0BcofWBoSc5QqqMK4=
|
||||||
|
google.golang.org/genproto/googleapis/rpc v0.0.0-20250106144421-5f5ef82da422 h1:3UsHvIr4Wc2aW4brOaSCmcxh9ksica6fHEr8P1XhkYw=
|
||||||
|
google.golang.org/genproto/googleapis/rpc v0.0.0-20250106144421-5f5ef82da422/go.mod h1:3ENsm/5D1mzDyhpzeRi1NR784I0BcofWBoSc5QqqMK4=
|
||||||
google.golang.org/grpc v1.69.2 h1:U3S9QEtbXC0bYNvRtcoklF3xGtLViumSYxWykJS+7AU=
|
google.golang.org/grpc v1.69.2 h1:U3S9QEtbXC0bYNvRtcoklF3xGtLViumSYxWykJS+7AU=
|
||||||
google.golang.org/grpc v1.69.2/go.mod h1:vyjdE6jLBI76dgpDojsFGNaHlxdjXN9ghpnd2o7JGZ4=
|
google.golang.org/grpc v1.69.2/go.mod h1:vyjdE6jLBI76dgpDojsFGNaHlxdjXN9ghpnd2o7JGZ4=
|
||||||
google.golang.org/protobuf v1.36.1 h1:yBPeRvTftaleIgM3PZ/WBIZ7XM/eEYAaEyCwvyjq/gk=
|
google.golang.org/protobuf v1.36.1 h1:yBPeRvTftaleIgM3PZ/WBIZ7XM/eEYAaEyCwvyjq/gk=
|
||||||
google.golang.org/protobuf v1.36.1/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
|
google.golang.org/protobuf v1.36.1/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
|
||||||
|
google.golang.org/protobuf v1.36.2 h1:R8FeyR1/eLmkutZOM5CWghmo5itiG9z0ktFlTVLuTmU=
|
||||||
|
google.golang.org/protobuf v1.36.2/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
||||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
||||||
|
@ -3,6 +3,7 @@ package app
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
"go.opentelemetry.io/otel/codes"
|
"go.opentelemetry.io/otel/codes"
|
||||||
@ -25,6 +26,7 @@ type App struct {
|
|||||||
|
|
||||||
type AppHTTP struct {
|
type AppHTTP struct {
|
||||||
Funcs []srv.HTTPFunc
|
Funcs []srv.HTTPFunc
|
||||||
|
Middleware []http.Handler
|
||||||
HealthChecks []srv.HealthCheckFunc
|
HealthChecks []srv.HealthCheckFunc
|
||||||
httpDone <-chan interface{}
|
httpDone <-chan interface{}
|
||||||
}
|
}
|
||||||
@ -78,9 +80,12 @@ func (a *App) MustRun() {
|
|||||||
func (a *App) initHTTP() {
|
func (a *App) initHTTP() {
|
||||||
var httpShutdown shutdownFunc
|
var httpShutdown shutdownFunc
|
||||||
httpShutdown, a.HTTP.httpDone = srv.MustInitHTTPServer(
|
httpShutdown, a.HTTP.httpDone = srv.MustInitHTTPServer(
|
||||||
a.AppContext,
|
&srv.HTTPServerOpts{
|
||||||
a.HTTP.Funcs,
|
Ctx: a.AppContext,
|
||||||
a.HTTP.HealthChecks...,
|
HandleFuncs: a.HTTP.Funcs,
|
||||||
|
Middleware: a.HTTP.Middleware,
|
||||||
|
HealthCheckFuncs: a.HTTP.HealthChecks,
|
||||||
|
},
|
||||||
)
|
)
|
||||||
a.shutdownFuncs = append(a.shutdownFuncs, httpShutdown)
|
a.shutdownFuncs = append(a.shutdownFuncs, httpShutdown)
|
||||||
}
|
}
|
||||||
|
@ -23,3 +23,10 @@ func MustSetupConfigAndLogging(ctx context.Context) context.Context {
|
|||||||
zerolog.Ctx(ctx).Trace().Any("config", *cfg).Send()
|
zerolog.Ctx(ctx).Trace().Any("config", *cfg).Send()
|
||||||
return ctx
|
return ctx
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Unmarshal config into a custom type
|
||||||
|
// Type MUST include *config.AppConfig
|
||||||
|
// Stored in context as *config.AppConfig but can be asserted back
|
||||||
|
func MustSetupConfigAndLoggingInto[T any](ctx context.Context, into T) (context.Context, T) {
|
||||||
|
return ctx, into
|
||||||
|
}
|
||||||
|
107
pkg/app/setup_custom.go
Normal file
107
pkg/app/setup_custom.go
Normal file
@ -0,0 +1,107 @@
|
|||||||
|
package app
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"reflect"
|
||||||
|
|
||||||
|
"gopkg.in/yaml.v3"
|
||||||
|
|
||||||
|
"gitea.libretechconsulting.com/rmcguire/go-app/pkg/config"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Used to unmarshal config and environment into a custom type
|
||||||
|
// that overloads *config.AppConfig. Will perform normal env
|
||||||
|
// substitutions for AppConfig, but env overrides for custom type
|
||||||
|
// are up to the caller.
|
||||||
|
func MustLoadConfigInto[T any](ctx context.Context, into T) (context.Context, T) {
|
||||||
|
// Step 1: Check our custom type for required *config.AppConfig
|
||||||
|
if err := hasAppConfig(into); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Step 2: Do the normal thing
|
||||||
|
ctx = MustSetupConfigAndLogging(ctx)
|
||||||
|
|
||||||
|
// Step 3: Extract the config
|
||||||
|
cfg := config.MustFromCtx(ctx)
|
||||||
|
|
||||||
|
// Step 4: Unmarshal custom config
|
||||||
|
configPath := flag.Lookup("config")
|
||||||
|
if configPath != nil && configPath.Value.String() != "" {
|
||||||
|
file, err := os.Open(configPath.Value.String())
|
||||||
|
if err != nil {
|
||||||
|
panic(fmt.Errorf("could not open config file: %w", err))
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
decoder := yaml.NewDecoder(file)
|
||||||
|
if err := decoder.Decode(into); err != nil {
|
||||||
|
panic(fmt.Errorf("could not decode config file: %w", err))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Step 5: Re-apply AppConfig to custom type
|
||||||
|
if err := setAppConfig(into, cfg); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Step 6: Update context, return custom type
|
||||||
|
return ctx, into
|
||||||
|
}
|
||||||
|
|
||||||
|
func setAppConfig[T any](target T, appConfig *config.AppConfig) error {
|
||||||
|
// Ensure target is a pointer to a struct
|
||||||
|
v := reflect.ValueOf(target)
|
||||||
|
if v.Kind() != reflect.Ptr || v.IsNil() {
|
||||||
|
return errors.New("target must be a non-nil pointer to a struct")
|
||||||
|
}
|
||||||
|
|
||||||
|
v = v.Elem() // Dereference the pointer
|
||||||
|
if v.Kind() != reflect.Struct {
|
||||||
|
return errors.New("target must be a pointer to a struct")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Replace *config.AppConfig
|
||||||
|
for i := 0; i < v.NumField(); i++ {
|
||||||
|
field := v.Field(i)
|
||||||
|
if field.Type() == reflect.TypeOf((*config.AppConfig)(nil)) {
|
||||||
|
if !field.CanSet() {
|
||||||
|
return fmt.Errorf("field %q cannot be set", v.Type().Field(i).Name)
|
||||||
|
}
|
||||||
|
field.Set(reflect.ValueOf(appConfig))
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return errors.New("no *config.AppConfig field found in target struct")
|
||||||
|
}
|
||||||
|
|
||||||
|
func hasAppConfig[T any](target T) error {
|
||||||
|
v := reflect.ValueOf(target)
|
||||||
|
if v.Kind() != reflect.Ptr || v.IsNil() {
|
||||||
|
return errors.New("target must be a non-nil pointer to a struct")
|
||||||
|
}
|
||||||
|
v = v.Elem()
|
||||||
|
|
||||||
|
if v.Kind() != reflect.Struct {
|
||||||
|
return errors.New("target must be a pointer to a struct")
|
||||||
|
}
|
||||||
|
|
||||||
|
hasAppConfig := false
|
||||||
|
for i := 0; i < v.NumField(); i++ {
|
||||||
|
field := v.Type().Field(i)
|
||||||
|
if field.Type == reflect.TypeOf((*config.AppConfig)(nil)) {
|
||||||
|
hasAppConfig = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !hasAppConfig {
|
||||||
|
return errors.New("struct does not contain a *config.AppConfig field")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
@ -8,6 +8,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
opentelemetry "go.opentelemetry.io/otel"
|
opentelemetry "go.opentelemetry.io/otel"
|
||||||
|
"go.opentelemetry.io/otel/attribute"
|
||||||
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc"
|
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc"
|
||||||
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
|
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
|
||||||
"go.opentelemetry.io/otel/exporters/prometheus"
|
"go.opentelemetry.io/otel/exporters/prometheus"
|
||||||
@ -139,7 +140,7 @@ func newPropagator() propagation.TextMapPropagator {
|
|||||||
|
|
||||||
func (s *settings) newTracerProvider(ctx context.Context) (traceProvider *traceSDK.TracerProvider, err error) {
|
func (s *settings) newTracerProvider(ctx context.Context) (traceProvider *traceSDK.TracerProvider, err error) {
|
||||||
traceOpts := []traceSDK.TracerProviderOption{
|
traceOpts := []traceSDK.TracerProviderOption{
|
||||||
traceSDK.WithResource(newResource()),
|
traceSDK.WithResource(newResource(ctx)),
|
||||||
}
|
}
|
||||||
|
|
||||||
host, set := os.LookupEnv("OTEL_EXPORTER_OTLP_ENDPOINT")
|
host, set := os.LookupEnv("OTEL_EXPORTER_OTLP_ENDPOINT")
|
||||||
@ -164,8 +165,15 @@ func (s *settings) newTracerProvider(ctx context.Context) (traceProvider *traceS
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func newResource() *resource.Resource {
|
func newResource(ctx context.Context) *resource.Resource {
|
||||||
return resource.NewWithAttributes(semconv.SchemaURL)
|
cfg := config.MustFromCtx(ctx)
|
||||||
|
attributes := []attribute.KeyValue{
|
||||||
|
semconv.ServiceName(cfg.Name),
|
||||||
|
semconv.ServiceVersion(cfg.Version),
|
||||||
|
semconv.K8SPodName(os.Getenv("HOSTNAME")),
|
||||||
|
}
|
||||||
|
|
||||||
|
return resource.NewWithAttributes(semconv.SchemaURL, attributes...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Configures meter provider
|
// Configures meter provider
|
||||||
@ -173,7 +181,9 @@ func newResource() *resource.Resource {
|
|||||||
// Conditionally provides an OTLP metrics exporter
|
// Conditionally provides an OTLP metrics exporter
|
||||||
func (s *settings) newMeterProvider(ctx context.Context) (*metric.MeterProvider, error) {
|
func (s *settings) newMeterProvider(ctx context.Context) (*metric.MeterProvider, error) {
|
||||||
// OTEL Prometheus Exporter
|
// OTEL Prometheus Exporter
|
||||||
exporter, err := prometheus.New()
|
exporter, err := prometheus.New(
|
||||||
|
prometheus.WithResourceAsConstantLabels(attribute.NewDenyKeysFilter()),
|
||||||
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -202,6 +212,7 @@ func (s *settings) newMeterProvider(ctx context.Context) (*metric.MeterProvider,
|
|||||||
var meterProvider *metric.MeterProvider
|
var meterProvider *metric.MeterProvider
|
||||||
if otlpExporter != nil {
|
if otlpExporter != nil {
|
||||||
metricOptions = append(metricOptions,
|
metricOptions = append(metricOptions,
|
||||||
|
metric.WithResource(newResource(ctx)),
|
||||||
metric.WithReader(exporter),
|
metric.WithReader(exporter),
|
||||||
metric.WithReader(
|
metric.WithReader(
|
||||||
metric.NewPeriodicReader(
|
metric.NewPeriodicReader(
|
||||||
@ -209,12 +220,11 @@ func (s *settings) newMeterProvider(ctx context.Context) (*metric.MeterProvider,
|
|||||||
metric.WithInterval(s.MetricExportInterval),
|
metric.WithInterval(s.MetricExportInterval),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
metric.WithResource(newResource()),
|
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
metricOptions = append(metricOptions,
|
metricOptions = append(metricOptions,
|
||||||
|
metric.WithResource(newResource(ctx)),
|
||||||
metric.WithReader(exporter),
|
metric.WithReader(exporter),
|
||||||
metric.WithResource(newResource()),
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -29,10 +29,17 @@ type HTTPFunc struct {
|
|||||||
HandlerFunc http.HandlerFunc
|
HandlerFunc http.HandlerFunc
|
||||||
}
|
}
|
||||||
|
|
||||||
func prepHTTPServer(ctx context.Context, handleFuncs []HTTPFunc, hcFuncs ...HealthCheckFunc) *http.Server {
|
type HTTPServerOpts struct {
|
||||||
|
Ctx context.Context
|
||||||
|
HandleFuncs []HTTPFunc
|
||||||
|
Middleware []http.Handler
|
||||||
|
HealthCheckFuncs []HealthCheckFunc
|
||||||
|
}
|
||||||
|
|
||||||
|
func prepHTTPServer(opts *HTTPServerOpts) *http.Server {
|
||||||
var (
|
var (
|
||||||
cfg = config.MustFromCtx(ctx)
|
cfg = config.MustFromCtx(opts.Ctx)
|
||||||
l = zerolog.Ctx(ctx)
|
l = zerolog.Ctx(opts.Ctx)
|
||||||
mux = &http.ServeMux{}
|
mux = &http.ServeMux{}
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -43,11 +50,11 @@ func prepHTTPServer(ctx context.Context, handleFuncs []HTTPFunc, hcFuncs ...Heal
|
|||||||
mux.Handle(pattern, handler) // Associate pattern with handler
|
mux.Handle(pattern, handler) // Associate pattern with handler
|
||||||
}
|
}
|
||||||
|
|
||||||
healthChecks := handleHealthCheckFunc(ctx, hcFuncs...)
|
healthChecks := handleHealthCheckFunc(opts.Ctx, opts.HealthCheckFuncs...)
|
||||||
otelHandleFunc("/health", healthChecks)
|
otelHandleFunc("/health", healthChecks)
|
||||||
otelHandleFunc("/", healthChecks)
|
otelHandleFunc("/", healthChecks)
|
||||||
|
|
||||||
for _, f := range handleFuncs {
|
for _, f := range opts.HandleFuncs {
|
||||||
otelHandleFunc(f.Path, f.HandlerFunc)
|
otelHandleFunc(f.Path, f.HandlerFunc)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -89,9 +96,19 @@ func prepHTTPServer(ctx context.Context, handleFuncs []HTTPFunc, hcFuncs ...Heal
|
|||||||
idleTimeout = *iT
|
idleTimeout = *iT
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Inject any supplied middleware
|
||||||
|
for i := len(opts.Middleware) - 1; i >= 0; i-- {
|
||||||
|
mw := opts.Middleware[i]
|
||||||
|
next := handler
|
||||||
|
handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
mw.ServeHTTP(w, r)
|
||||||
|
next.ServeHTTP(w, r)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// Inject logging middleware
|
// Inject logging middleware
|
||||||
if cfg.HTTP.LogRequests {
|
if cfg.HTTP.LogRequests {
|
||||||
handler = loggingMiddleware(ctx, handler)
|
handler = loggingMiddleware(opts.Ctx, handler)
|
||||||
}
|
}
|
||||||
|
|
||||||
return &http.Server{
|
return &http.Server{
|
||||||
@ -101,17 +118,17 @@ func prepHTTPServer(ctx context.Context, handleFuncs []HTTPFunc, hcFuncs ...Heal
|
|||||||
IdleTimeout: idleTimeout,
|
IdleTimeout: idleTimeout,
|
||||||
Handler: handler,
|
Handler: handler,
|
||||||
BaseContext: func(_ net.Listener) context.Context {
|
BaseContext: func(_ net.Listener) context.Context {
|
||||||
return ctx
|
return opts.Ctx
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns a shutdown func and a done channel if the
|
// Returns a shutdown func and a done channel if the
|
||||||
// server aborts abnormally. Panics on error.
|
// server aborts abnormally. Panics on error.
|
||||||
func MustInitHTTPServer(ctx context.Context, funcs []HTTPFunc, hcFuncs ...HealthCheckFunc) (
|
func MustInitHTTPServer(opts *HTTPServerOpts) (
|
||||||
func(context.Context) error, <-chan interface{},
|
func(context.Context) error, <-chan interface{},
|
||||||
) {
|
) {
|
||||||
shutdownFunc, doneChan, err := InitHTTPServer(ctx, funcs, hcFuncs...)
|
shutdownFunc, doneChan, err := InitHTTPServer(opts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
@ -120,18 +137,18 @@ func MustInitHTTPServer(ctx context.Context, funcs []HTTPFunc, hcFuncs ...Health
|
|||||||
|
|
||||||
// Returns a shutdown func and a done channel if the
|
// Returns a shutdown func and a done channel if the
|
||||||
// server aborts abnormally. Returns error on failure to start
|
// server aborts abnormally. Returns error on failure to start
|
||||||
func InitHTTPServer(ctx context.Context, funcs []HTTPFunc, hcFuncs ...HealthCheckFunc) (
|
func InitHTTPServer(opts *HTTPServerOpts) (
|
||||||
func(context.Context) error, <-chan interface{}, error,
|
func(context.Context) error, <-chan interface{}, error,
|
||||||
) {
|
) {
|
||||||
l := zerolog.Ctx(ctx)
|
l := zerolog.Ctx(opts.Ctx)
|
||||||
doneChan := make(chan interface{})
|
doneChan := make(chan interface{})
|
||||||
|
|
||||||
var server *http.Server
|
var server *http.Server
|
||||||
|
|
||||||
httpMeter = otel.GetMeter(ctx, "http")
|
httpMeter = otel.GetMeter(opts.Ctx, "http")
|
||||||
httpTracer = otel.GetTracer(ctx, "http")
|
httpTracer = otel.GetTracer(opts.Ctx, "http")
|
||||||
|
|
||||||
server = prepHTTPServer(ctx, funcs, hcFuncs...)
|
server = prepHTTPServer(opts)
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
l.Debug().Msg("HTTP Server Started")
|
l.Debug().Msg("HTTP Server Started")
|
||||||
|
Reference in New Issue
Block a user