Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
340cf67515 | |||
f5cb3456b1 | |||
ede5bc92f7 | |||
063ff0f1d0 | |||
2310ac574c | |||
cb0c87d200 |
15
CHANGELOG.md
15
CHANGELOG.md
@@ -1,3 +1,18 @@
|
|||||||
|
# v0.12.1
|
||||||
|
* feat: Implement custom OpenTelemetry span name formatting for HTTP requests.
|
||||||
|
* refactor: Streamline OpenTelemetry handler integration by removing custom wrapper.
|
||||||
|
|
||||||
|
# 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:
|
# v0.11.0:
|
||||||
* Updated `github.com/prometheus/client_golang` dependency to v1.23.0.
|
* Updated `github.com/prometheus/client_golang` dependency to v1.23.0.
|
||||||
* Updated `google.golang.org/grpc` dependency to v1.74.2.
|
* Updated `google.golang.org/grpc` dependency to v1.74.2.
|
||||||
|
@@ -10,6 +10,7 @@ import (
|
|||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"regexp"
|
||||||
"runtime/debug"
|
"runtime/debug"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -105,6 +106,12 @@ 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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -1,6 +1,10 @@
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
import "time"
|
import (
|
||||||
|
"regexp"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
var defaultHTTPConfig = &HTTPConfig{
|
var defaultHTTPConfig = &HTTPConfig{
|
||||||
Enabled: true,
|
Enabled: true,
|
||||||
@@ -14,13 +18,23 @@ var defaultHTTPConfig = &HTTPConfig{
|
|||||||
|
|
||||||
// HTTPConfig provides HTTP server 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
|
||||||
}
|
}
|
||||||
|
@@ -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,
|
||||||
) {
|
) {
|
||||||
|
@@ -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()
|
||||||
|
Reference in New Issue
Block a user