13 Commits

Author SHA1 Message Date
f0c3bc6b9e feat: standardize OpenTelemetry HTTP span names to include method and route pattern 2025-08-27 10:17:24 -04:00
00cb0d0421 refactors OpenTelemetry HTTP instrumentation by centralizing handler wrapping, adding span name formatting, and filtering health and metrics paths. 2025-08-27 10:04:05 -04:00
ede5bc92f7 update changelog 2025-08-26 14:53:03 -04:00
063ff0f1d0 feat: adds configurable HTTP request path exclusion from logging 2025-08-26 14:51:39 -04:00
2310ac574c update changelog 2025-08-24 11:52:08 -04:00
cb0c87d200 refactors HTTP server initialization, improves logging middleware, and updates documentation (#1)
Reviewed-on: #1
2025-08-24 15:50:34 +00:00
92e78f533a add CHANGELOG 2025-08-14 09:14:14 -04:00
d4c6ecbefd upgrade deps, pin annoying otlptranslator 2025-08-14 09:12:17 -04:00
e1d58c0094 improves godoc comments for gRPC server and logger components 2025-08-14 09:03:44 -04:00
9cee9037e4 Merge branch 'main' of gitea.libretechconsulting.com:rmcguire/go-app 2025-08-13 16:02:00 -04:00
318115690d Exports OTEL initialization function and adds extensive documentation across packages for improved clarity. 2025-08-13 16:01:51 -04:00
8b4bb890be update TODO 2025-08-10 17:46:09 -04:00
e841e11168 updates dummy health check to ensure positive duration 2025-08-02 11:01:25 -04:00
24 changed files with 202 additions and 94 deletions

20
CHANGELOG.md Normal file
View File

@@ -0,0 +1,20 @@
# v0.12.0
* feat: Add support for excluding HTTP request paths from logging using configurable regular expressions.
# v0.11.1:
* docs: Add comprehensive package-level documentation for the `http` package.
* docs: Improve documentation for the `InitHTTPServer` function.
* feat: Introduce specific logging for HTTP 204 (No Content) responses, omitting body logging.
* fix: Enhance error handling and logging for empty or unreadable HTTP response bodies.
* chore: Refine HTTP response log messages for improved clarity and consistency.
* docs: Update documentation for the `Flush` method in `LoggingResponseWriter`
# v0.11.0:
* Updated `github.com/prometheus/client_golang` dependency to v1.23.0.
* Updated `google.golang.org/grpc` dependency to v1.74.2.
* Added a `replace` directive for `github.com/prometheus/otlptranslator`.
* Renamed `initOTEL` function to `InitOTEL` and added a check to prevent re-initialization.
* Improved `dummyHealthCheck` to ensure a positive random duration.
* Added comprehensive package, struct, and function comments across `pkg/app`, `pkg/config`, `pkg/logging`, `pkg/otel`, `pkg/srv/grpc`, and `pkg/srv/http` for improved clarity.
* Corrected typos in `TODO.md` for "implementing" and "support".
* Added a new item to `TODO.md` for early OpenTelemetry initialization.

View File

@@ -1,14 +1,15 @@
# TODO # TODO
- [ ] Add ability to initialize OTEL early (before app runs)
- [ ] Spruce up the README - [ ] Spruce up the README
- [ ] Create generic interface for implenting a service - [ ] Create generic interface for implementing a service
- [ ] Create config sample not called demo, so it is more easily reused - [ ] Create config sample not called demo, so it is more easily reused
- [ ] Expand config test case to cover GRPC config - [ ] Expand config test case to cover GRPC config
- [ ] Expand tracing - [ ] Expand tracing
## Done ## Done
- [x] Test and troubleshoot GRPC Gateway supprot - [x] Test and troubleshoot GRPC Gateway support
- [x] Finish implementing GRPC service support - [x] Finish implementing GRPC service support
- [x] Unit tests - [x] Unit tests
- [x] Pattern for extending config - [x] Pattern for extending config

18
go.mod
View File

@@ -2,6 +2,8 @@ module gitea.libretechconsulting.com/rmcguire/go-app
go 1.24.2 go 1.24.2
replace github.com/prometheus/otlptranslator => github.com/prometheus/otlptranslator v0.0.0-20250717125610-8549f4ab4f8f
require ( require (
buf.build/go/protovalidate v0.14.0 buf.build/go/protovalidate v0.14.0
github.com/caarlos0/env/v11 v11.3.1 github.com/caarlos0/env/v11 v11.3.1
@@ -27,7 +29,7 @@ require (
) )
require ( require (
buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.36.6-20250717185734-6c6e0d3c608e.1 // indirect buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.36.7-20250717185734-6c6e0d3c608e.1 // indirect
cel.dev/expr v0.24.0 // indirect cel.dev/expr v0.24.0 // indirect
github.com/antlr4-go/antlr/v4 v4.13.1 // indirect github.com/antlr4-go/antlr/v4 v4.13.1 // indirect
github.com/beorn7/perks v1.0.1 // indirect github.com/beorn7/perks v1.0.1 // indirect
@@ -51,11 +53,11 @@ require (
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.37.0 // indirect go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.37.0 // indirect
go.opentelemetry.io/proto/otlp v1.7.1 // indirect go.opentelemetry.io/proto/otlp v1.7.1 // indirect
golang.org/x/exp v0.0.0-20250718183923-645b1fa84792 // indirect golang.org/x/exp v0.0.0-20250813145105-42675adae3e6 // indirect
golang.org/x/net v0.42.0 // indirect golang.org/x/net v0.43.0 // indirect
golang.org/x/sys v0.34.0 // indirect golang.org/x/sys v0.35.0 // indirect
golang.org/x/text v0.27.0 // indirect golang.org/x/text v0.28.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20250728155136-f173205681a0 // indirect google.golang.org/genproto/googleapis/api v0.0.0-20250811230008-5f3141c8851a // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20250728155136-f173205681a0 // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20250811230008-5f3141c8851a // indirect
google.golang.org/protobuf v1.36.6 // indirect google.golang.org/protobuf v1.36.7 // indirect
) )

32
go.sum
View File

@@ -1,5 +1,5 @@
buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.36.6-20250717185734-6c6e0d3c608e.1 h1:Lg6klmCi3v7VvpqeeLEER9/m5S8y9e9DjhqQnSCNy4k= buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.36.7-20250717185734-6c6e0d3c608e.1 h1:/AZH8sVB6LHv8G+hZlAMCP31NevnesHwYgnlgS5Vt14=
buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.36.6-20250717185734-6c6e0d3c608e.1/go.mod h1:avRlCjnFzl98VPaeCtJ24RrV/wwHFzB8sWXhj26+n/U= buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.36.7-20250717185734-6c6e0d3c608e.1/go.mod h1:eva/VCrd8X7xuJw+JtwCEyrCKiRRASukFqmirnWBvFU=
buf.build/go/protovalidate v0.14.0 h1:kr/rC/no+DtRyYX+8KXLDxNnI1rINz0imk5K44ZpZ3A= buf.build/go/protovalidate v0.14.0 h1:kr/rC/no+DtRyYX+8KXLDxNnI1rINz0imk5K44ZpZ3A=
buf.build/go/protovalidate v0.14.0/go.mod h1:+F/oISho9MO7gJQNYC2VWLzcO1fTPmaTA08SDYJZncA= buf.build/go/protovalidate v0.14.0/go.mod h1:+F/oISho9MO7gJQNYC2VWLzcO1fTPmaTA08SDYJZncA=
cel.dev/expr v0.24.0 h1:56OvJKSH3hDGL0ml5uSxZmz3/3Pq4tJ+fb1unVLAFcY= cel.dev/expr v0.24.0 h1:56OvJKSH3hDGL0ml5uSxZmz3/3Pq4tJ+fb1unVLAFcY=
@@ -135,25 +135,25 @@ go.opentelemetry.io/proto/otlp v1.7.1 h1:gTOMpGDb0WTBOP8JaO72iL3auEZhVmAQg4ipjOV
go.opentelemetry.io/proto/otlp v1.7.1/go.mod h1:b2rVh6rfI/s2pHWNlB7ILJcRALpcNDzKhACevjI+ZnE= go.opentelemetry.io/proto/otlp v1.7.1/go.mod h1:b2rVh6rfI/s2pHWNlB7ILJcRALpcNDzKhACevjI+ZnE=
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/exp v0.0.0-20250718183923-645b1fa84792 h1:R9PFI6EUdfVKgwKjZef7QIwGcBKu86OEFpJ9nUEP2l4= golang.org/x/exp v0.0.0-20250813145105-42675adae3e6 h1:SbTAbRFnd5kjQXbczszQ0hdk3ctwYf3qBNH9jIsGclE=
golang.org/x/exp v0.0.0-20250718183923-645b1fa84792/go.mod h1:A+z0yzpGtvnG90cToK5n2tu8UJVP2XUATh+r+sfOOOc= golang.org/x/exp v0.0.0-20250813145105-42675adae3e6/go.mod h1:4QTo5u+SEIbbKW1RacMZq1YEfOBqeXa19JeshGi+zc4=
golang.org/x/net v0.42.0 h1:jzkYrhi3YQWD6MLBJcsklgQsoAcw89EcZbJw8Z614hs= golang.org/x/net v0.43.0 h1:lat02VYK2j4aLzMzecihNvTlJNQUq316m2Mr9rnM6YE=
golang.org/x/net v0.42.0/go.mod h1:FF1RA5d3u7nAYA4z2TkclSCKh68eSXtiFwcWQpPXdt8= golang.org/x/net v0.43.0/go.mod h1:vhO1fvI4dGsIjh73sWfUVjj3N7CA9WkKJNQm2svM6Jg=
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=
golang.org/x/sys v0.34.0 h1:H5Y5sJ2L2JRdyv7ROF1he/lPdvFsd0mJHFw2ThKHxLA= golang.org/x/sys v0.35.0 h1:vz1N37gP5bs89s7He8XuIYXpyY0+QlsKmzipCbUtyxI=
golang.org/x/sys v0.34.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k= golang.org/x/sys v0.35.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
golang.org/x/text v0.27.0 h1:4fGWRpyh641NLlecmyl4LOe6yDdfaYNrGb2zdfo4JV4= golang.org/x/text v0.28.0 h1:rhazDwis8INMIwQ4tpjLDzUhx6RlXqZNPEM0huQojng=
golang.org/x/text v0.27.0/go.mod h1:1D28KMCvyooCX9hBiosv5Tz/+YLxj0j7XhWjpSUF7CU= golang.org/x/text v0.28.0/go.mod h1:U8nCwOR8jO/marOQ0QbDiOngZVEBB7MAiitBuMjXiNU=
google.golang.org/genproto/googleapis/api v0.0.0-20250728155136-f173205681a0 h1:0UOBWO4dC+e51ui0NFKSPbkHHiQ4TmrEfEZMLDyRmY8= google.golang.org/genproto/googleapis/api v0.0.0-20250811230008-5f3141c8851a h1:DMCgtIAIQGZqJXMVzJF4MV8BlWoJh2ZuFiRdAleyr58=
google.golang.org/genproto/googleapis/api v0.0.0-20250728155136-f173205681a0/go.mod h1:8ytArBbtOy2xfht+y2fqKd5DRDJRUQhqbyEnQ4bDChs= google.golang.org/genproto/googleapis/api v0.0.0-20250811230008-5f3141c8851a/go.mod h1:y2yVLIE/CSMCPXaHnSKXxu1spLPnglFLegmgdY23uuE=
google.golang.org/genproto/googleapis/rpc v0.0.0-20250728155136-f173205681a0 h1:MAKi5q709QWfnkkpNQ0M12hYJ1+e8qYVDyowc4U1XZM= google.golang.org/genproto/googleapis/rpc v0.0.0-20250811230008-5f3141c8851a h1:tPE/Kp+x9dMSwUm/uM0JKK0IfdiJkwAbSMSeZBXXJXc=
google.golang.org/genproto/googleapis/rpc v0.0.0-20250728155136-f173205681a0/go.mod h1:qQ0YXyHHx3XkvlzUtpXDkS29lDSafHMZBAZDc03LQ3A= google.golang.org/genproto/googleapis/rpc v0.0.0-20250811230008-5f3141c8851a/go.mod h1:gw1tLEfykwDz2ET4a12jcXt4couGAm7IwsVaTy0Sflo=
google.golang.org/grpc v1.74.2 h1:WoosgB65DlWVC9FqI82dGsZhWFNBSLjQ84bjROOpMu4= google.golang.org/grpc v1.74.2 h1:WoosgB65DlWVC9FqI82dGsZhWFNBSLjQ84bjROOpMu4=
google.golang.org/grpc v1.74.2/go.mod h1:CtQ+BGjaAIXHs/5YS3i473GqwBBa1zGQNevxdeBEXrM= google.golang.org/grpc v1.74.2/go.mod h1:CtQ+BGjaAIXHs/5YS3i473GqwBBa1zGQNevxdeBEXrM=
google.golang.org/protobuf v1.36.6 h1:z1NpPI8ku2WgiWnf+t9wTPsn6eP1L7ksHUlkfLvd9xY= google.golang.org/protobuf v1.36.7 h1:IgrO7UwFQGJdRNXH/sQux4R1Dj1WAKcLElzeeRaXV2A=
google.golang.org/protobuf v1.36.6/go.mod h1:jduwjTPXsFjZGTmRluh+L6NjiWu7pchiJ2/5YcXBHnY= google.golang.org/protobuf v1.36.7/go.mod h1:jduwjTPXsFjZGTmRluh+L6NjiWu7pchiJ2/5YcXBHnY=
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=

View File

@@ -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

View File

@@ -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)

View File

@@ -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() {

View File

@@ -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")

View File

@@ -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

View File

@@ -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) {

View File

@@ -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.

View File

@@ -10,6 +10,7 @@ import (
"flag" "flag"
"fmt" "fmt"
"os" "os"
"regexp"
"runtime/debug" "runtime/debug"
"time" "time"
@@ -17,15 +18,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 +45,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 +76,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
@@ -101,15 +106,23 @@ func prepareConfig(cfg *AppConfig) error {
} }
} }
// Prepare user-provided expressions, panic up-front if invalid
cfg.HTTP.excludeRegexps = make([]*regexp.Regexp, len(cfg.HTTP.LogExcludePathRegexps))
for i, re := range cfg.HTTP.LogExcludePathRegexps {
cfg.HTTP.excludeRegexps[i] = regexp.MustCompile(re)
}
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

View File

@@ -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 {

View File

@@ -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

View File

@@ -1,6 +1,10 @@
package config package config
import "time" import (
"regexp"
"sync"
"time"
)
var defaultHTTPConfig = &HTTPConfig{ var defaultHTTPConfig = &HTTPConfig{
Enabled: true, Enabled: true,
@@ -12,15 +16,25 @@ 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"`
LogRequests bool `yaml:"logRequests" env:"APP_HTTP_LOG_REQUESTS" json:"logRequests,omitempty"` LogRequests bool `yaml:"logRequests" env:"APP_HTTP_LOG_REQUESTS" json:"logRequests,omitempty"`
ReadTimeout string `yaml:"readTimeout" env:"APP_HTTP_READ_TIMEOUT" json:"readTimeout,omitempty"` // Go duration (e.g. 10s) LogExcludePathRegexps []string `yaml:"logExcludePathRegexps" env:"APP_HTTP_LOG_EXCLUDE_PATH_REGEXPS" json:"logExcludePathRegexps,omitempty"`
WriteTimeout string `yaml:"writeTimeout" env:"APP_HTTP_WRITE_TIMEOUT" json:"writeTimeout,omitempty"` // Go duration (e.g. 10s) ReadTimeout string `yaml:"readTimeout" env:"APP_HTTP_READ_TIMEOUT" json:"readTimeout,omitempty"` // Go duration (e.g. 10s)
IdleTimeout string `yaml:"idleTimeout" env:"APP_HTTP_IDLE_TIMEOUT" json:"idleTimeout,omitempty"` // Go duration (e.g. 10s) WriteTimeout string `yaml:"writeTimeout" env:"APP_HTTP_WRITE_TIMEOUT" json:"writeTimeout,omitempty"` // Go duration (e.g. 10s)
rT *time.Duration IdleTimeout string `yaml:"idleTimeout" env:"APP_HTTP_IDLE_TIMEOUT" json:"idleTimeout,omitempty"` // Go duration (e.g. 10s)
wT *time.Duration excludeRegexps []*regexp.Regexp
iT *time.Duration rT *time.Duration
wT *time.Duration
iT *time.Duration
lock sync.RWMutex
}
func (h *HTTPConfig) GetExcludeRegexps() []*regexp.Regexp {
h.lock.RLock()
defer h.lock.RUnlock()
return h.excludeRegexps
} }

View File

@@ -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"`

View File

@@ -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"`

View File

@@ -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 (

View File

@@ -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"),

View File

@@ -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 (
@@ -29,8 +31,9 @@ type appGRPCServer struct {
doneChan chan error doneChan chan error
} }
// Returns a shutdown func, a channel indicating done / error, // InitGRPCServer initializes and starts a gRPC server.
// and an up-front error if server fails to start // It returns a shutdown function, a channel to signal completion or error,
// and an error if the server fails to start.
func InitGRPCServer(ctx context.Context, opts *opts.GRPCOpts) ( func InitGRPCServer(ctx context.Context, opts *opts.GRPCOpts) (
func(context.Context) error, <-chan error, error, func(context.Context) error, <-chan error, error,
) { ) {

View File

@@ -8,19 +8,19 @@ import (
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
) )
// A grpc logging middleware compatible logger // GRPCLogger is a grpc logging middleware compatible logger
// implementation using a zerolog logger, either from // implementation using a zerolog logger, either from
// the global logger or a context logger // the global logger or a context logger.
type GRPCLogger struct { type GRPCLogger struct {
logger zerolog.Logger logger zerolog.Logger
} }
// Returns a grpcLogger using the global zerolog Logger // NewGRPCLogger returns a grpcLogger using the global zerolog Logger.
func NewGRPCLogger() *GRPCLogger { func NewGRPCLogger() *GRPCLogger {
return &GRPCLogger{logger: log.Logger} return &GRPCLogger{logger: log.Logger}
} }
// Returns a grpcLogger using the zerolog logger in the provided context // NewGRPCContextLogger returns a grpcLogger using the zerolog logger in the provided context.
func NewGRPCContextLogger(ctx context.Context) *GRPCLogger { func NewGRPCContextLogger(ctx context.Context) *GRPCLogger {
logger := log.Ctx(ctx) logger := log.Ctx(ctx)
return &GRPCLogger{logger: *logger} return &GRPCLogger{logger: *logger}

View File

@@ -1,3 +1,6 @@
// Package http provides functionality for setting up and managing HTTP servers.
// It includes features for health checks, Prometheus metrics, OpenTelemetry
// tracing, and custom middleware integration.
package http package http
import ( import (
@@ -7,6 +10,7 @@ import (
"net" "net"
"net/http" "net/http"
"os" "os"
"regexp"
"strings" "strings"
"time" "time"
@@ -22,11 +26,12 @@ import (
) )
var ( var (
httpMeter metric.Meter httpMeter metric.Meter
httpTracer trace.Tracer httpTracer trace.Tracer
defReadTimeout = 10 * time.Second httpPatternWithMethodRegexp = regexp.MustCompile(`(\w+) .*`)
defWriteTimeout = 10 * time.Second defReadTimeout = 10 * time.Second
defIdleTimeout = 15 * time.Second defWriteTimeout = 10 * time.Second
defIdleTimeout = 15 * time.Second
) )
func prepHTTPServer(opts *opts.AppHTTP) *http.Server { func prepHTTPServer(opts *opts.AppHTTP) *http.Server {
@@ -36,19 +41,12 @@ func prepHTTPServer(opts *opts.AppHTTP) *http.Server {
mux = &http.ServeMux{} mux = &http.ServeMux{}
) )
// NOTE: Wraps handle func with otelhttp handler and
// inserts route tag
otelHandleFunc := func(pattern string, handlerFunc func(http.ResponseWriter, *http.Request)) {
handler := otelhttp.WithRouteTag(pattern, http.HandlerFunc(handlerFunc))
mux.Handle(pattern, handler) // Associate pattern with handler
}
healthChecks := handleHealthCheckFunc(opts.Ctx, opts.HealthChecks...) healthChecks := handleHealthCheckFunc(opts.Ctx, opts.HealthChecks...)
otelHandleFunc("/health", healthChecks) mux.HandleFunc("/health", healthChecks)
otelHandleFunc("/", healthChecks) mux.HandleFunc("/", healthChecks)
for _, f := range opts.Funcs { for _, f := range opts.Funcs {
otelHandleFunc(f.Path, f.HandlerFunc) mux.HandleFunc(f.Path, f.HandlerFunc)
} }
// Prometheus metrics endpoint // Prometheus metrics endpoint
@@ -71,12 +69,12 @@ func prepHTTPServer(opts *opts.AppHTTP) *http.Server {
if h.StripPrefix { if h.StripPrefix {
h.Handler = http.StripPrefix(h.Prefix[:len(h.Prefix)-1], h.Handler) h.Handler = http.StripPrefix(h.Prefix[:len(h.Prefix)-1], h.Handler)
} }
mux.Handle(h.Prefix, h.Handler) mux.Handle(h.Prefix, otelhttp.WithRouteTag(h.Prefix, h.Handler))
} }
// Add OTEL, skip health-check spans // Add OTEL instrumentation, filter noise, set span names
// NOTE: Add any other span exclusions here
handler := otelhttp.NewHandler(mux, "/", handler := otelhttp.NewHandler(mux, "/",
// TODO: Make configurable similar to config.http.LogExcludePathRegexps
otelhttp.WithFilter(func(r *http.Request) bool { otelhttp.WithFilter(func(r *http.Request) bool {
switch r.URL.Path { switch r.URL.Path {
case "/health": case "/health":
@@ -86,6 +84,18 @@ func prepHTTPServer(opts *opts.AppHTTP) *http.Server {
default: default:
return true return true
} }
}),
otelhttp.WithSpanNameFormatter(func(operation string, r *http.Request) string {
endpoint := r.URL.Path
if _, pattern := mux.Handler(r); pattern != "" {
endpoint = pattern
}
if httpPatternWithMethodRegexp.MatchString(endpoint) {
return endpoint
}
return fmt.Sprintf("%s %s", r.Method, endpoint)
})) }))
// Set timeouts from defaults, override // Set timeouts from defaults, override
@@ -133,8 +143,8 @@ func prepHTTPServer(opts *opts.AppHTTP) *http.Server {
} }
} }
// Returns a shutdown func and a done channel if the // InitHTTPServer 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(opts *opts.AppHTTP) ( func InitHTTPServer(opts *opts.AppHTTP) (
func(context.Context) error, <-chan any, error, func(context.Context) error, <-chan any, error,
) { ) {

View File

@@ -55,7 +55,7 @@ func handleHealthCheckFunc(ctx context.Context, hcFuncs ...opts.HealthCheckFunc)
} }
func dummyHealthCheck(ctx context.Context) error { func dummyHealthCheck(ctx context.Context) error {
workFor := rand.Intn(750) workFor := rand.Intn(740) + 10 // ensure positive
ticker := time.NewTicker(time.Duration(time.Duration(workFor) * time.Millisecond)) ticker := time.NewTicker(time.Duration(time.Duration(workFor) * time.Millisecond))
select { select {

View File

@@ -3,12 +3,15 @@ package http
import ( import (
"bytes" "bytes"
"context" "context"
"errors" "fmt"
"io"
"net/http" "net/http"
"regexp" "regexp"
"time" "time"
"github.com/rs/zerolog" "github.com/rs/zerolog"
"gitea.libretechconsulting.com/rmcguire/go-app/pkg/config"
) )
var ExcludeFromLogging = regexp.MustCompile(`\/(ready|live|metrics)$`) var ExcludeFromLogging = regexp.MustCompile(`\/(ready|live|metrics)$`)
@@ -20,12 +23,22 @@ type LoggingResponseWriter struct {
} }
func loggingMiddleware(appCtx context.Context, next http.Handler) http.Handler { func loggingMiddleware(appCtx context.Context, next http.Handler) http.Handler {
appConfig := config.MustFromCtx(appCtx)
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if ExcludeFromLogging.Match([]byte(r.URL.Path)) { if ExcludeFromLogging.Match([]byte(r.URL.Path)) {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
return return
} }
// User-configurable logging exclusions
for _, re := range appConfig.HTTP.GetExcludeRegexps() {
if re.MatchString(r.URL.Path) {
next.ServeHTTP(w, r)
return
}
}
log := zerolog.Ctx(appCtx) log := zerolog.Ctx(appCtx)
start := time.Now() start := time.Now()
@@ -41,29 +54,42 @@ func loggingMiddleware(appCtx context.Context, next http.Handler) http.Handler {
Dur("duration", time.Since(start)). Dur("duration", time.Since(start)).
Msg("http request served") Msg("http request served")
// Log response body // Log response with body if not 204
if lrr.statusCode == http.StatusNoContent {
trcLog := log.Trace().
Str("path", r.URL.Path).
Int("statusCode", lrr.statusCode)
trcLog.Msg("http response (no content)") // Explicitly log 204
return // No body to log for 204 No Content
}
trcLog := log.Trace(). trcLog := log.Trace().
Str("path", r.URL.Path). Str("path", r.URL.Path).
Int("statusCode", lrr.statusCode) Int("statusCode", lrr.statusCode)
// Check if it's JSON
firstByte, err := lrr.body.ReadByte() firstByte, err := lrr.body.ReadByte()
if err != nil { if err != nil {
trcLog.Err(errors.New("invalid response body")).Send() if err == io.EOF {
return // Body is empty, which might be valid for some non-204 responses.
trcLog.Msg("http response (empty body)")
} else {
// Other error reading the body. Wrap the original error for context.
trcLog.Err(fmt.Errorf("error reading response body: %w", err)).Send()
}
return // No further body processing if there was an error or it was empty
} }
lrr.body.UnreadByte() lrr.body.UnreadByte() // Put the byte back for Bytes() to read
if firstByte == '{' { if firstByte == '{' {
trcLog = trcLog.RawJSON("response", lrr.body.Bytes()) trcLog = trcLog.RawJSON("response", lrr.body.Bytes())
} else { } else {
trcLog = trcLog.Bytes("response", lrr.body.Bytes()) trcLog = trcLog.Bytes("response", lrr.body.Bytes())
} }
trcLog.Msg("response payload") trcLog.Msg("http response")
}) })
} }
// Implement Flush to support the http.Flusher interface // Flush implements the http.Flusher interface to allow flushing buffered data.
func (w *LoggingResponseWriter) Flush() { func (w *LoggingResponseWriter) Flush() {
if flusher, ok := w.ResponseWriter.(http.Flusher); ok { if flusher, ok := w.ResponseWriter.(http.Flusher); ok {
flusher.Flush() flusher.Flush()