Some checks failed
Deploy / deploy (push) Has been cancelled
Adds Prometheus instrumentation across the checkout funnel, auth flow, Stripe calls, rate limiters, and HTTP middleware (inflight gauge, request and response sizes). Introduces a slog handler that counts log lines by level so the dashboard can show error/warn rates; existing log.Printf calls are routed through slog and counted as info. Backs the new "terminalshop — Service Overview" Grafana dashboard. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
59 lines
1.2 KiB
Go
59 lines
1.2 KiB
Go
package middleware
|
|
|
|
import (
|
|
"log"
|
|
"net/http"
|
|
"time"
|
|
|
|
"go.opentelemetry.io/otel/trace"
|
|
)
|
|
|
|
// responseWriter wraps http.ResponseWriter to capture status code and response size.
|
|
type responseWriter struct {
|
|
http.ResponseWriter
|
|
statusCode int
|
|
bytesWritten int64
|
|
}
|
|
|
|
func newResponseWriter(w http.ResponseWriter) *responseWriter {
|
|
return &responseWriter{ResponseWriter: w, statusCode: http.StatusOK}
|
|
}
|
|
|
|
func (rw *responseWriter) WriteHeader(code int) {
|
|
rw.statusCode = code
|
|
rw.ResponseWriter.WriteHeader(code)
|
|
}
|
|
|
|
func (rw *responseWriter) Write(b []byte) (int, error) {
|
|
n, err := rw.ResponseWriter.Write(b)
|
|
rw.bytesWritten += int64(n)
|
|
return n, err
|
|
}
|
|
|
|
// Logger logs HTTP requests
|
|
func Logger(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
start := time.Now()
|
|
|
|
wrapped := newResponseWriter(w)
|
|
next.ServeHTTP(wrapped, r)
|
|
|
|
sc := trace.SpanContextFromContext(r.Context())
|
|
tid := ""
|
|
if sc.HasTraceID() {
|
|
tid = sc.TraceID().String()
|
|
}
|
|
|
|
duration := time.Since(start)
|
|
|
|
log.Printf(
|
|
"%s %s %d %s trace_id=%s",
|
|
r.Method,
|
|
r.RequestURI,
|
|
wrapped.statusCode,
|
|
duration,
|
|
tid,
|
|
)
|
|
})
|
|
}
|