feat(observabillity): #115 integrate otel for metrics and traces
Build Docker Image / Build-Docker-Image (push) Successful in 5m51s
Build and Push Docker Image / Build-And-Push-Docker-Image (push) Successful in 5m46s

This commit was merged in pull request #152.
This commit is contained in:
2025-06-07 12:18:41 +02:00
parent b336b65532
commit 3e7251ef9d
32 changed files with 480 additions and 314 deletions
@@ -3,10 +3,15 @@ package middleware
import (
"net/http"
"strings"
"go.opentelemetry.io/otel"
)
func CacheControl(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
counter, _ := otel.Meter("").Int64Counter("spend.sparrow.test")
counter.Add(r.Context(), 1)
shouldCache := strings.HasPrefix(r.URL.Path, "/static")
if !shouldCache {
@@ -46,7 +46,7 @@ func CrossSiteRequestForgery(auth service.Auth) func(http.Handler) http.Handler
csrfToken := r.Header.Get("Csrf-Token")
if session == nil || csrfToken == "" || !auth.IsCsrfTokenValid(csrfToken, session.Id) {
log.Info("CSRF-Token \"%s\" not correct", csrfToken)
log.L.Info("CSRF-Token not correct", "token", csrfToken)
if r.Header.Get("Hx-Request") == "true" {
utils.TriggerToastWithStatus(w, r, "error", "CSRF-Token not correct", http.StatusBadRequest)
} else {
+1 -1
View File
@@ -34,7 +34,7 @@ func Gzip(next http.Handler) http.Handler {
err := gz.Close()
if err != nil && !errors.Is(err, http.ErrBodyNotAllowed) {
log.Error("Gzip: could not close Writer: %v", err)
log.L.Error("Gzip: could not close Writer", "err", err)
}
})
}
+9 -18
View File
@@ -2,23 +2,8 @@ package middleware
import (
"net/http"
"strconv"
"time"
"spend-sparrow/internal/log"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
var (
metrics = promauto.NewCounterVec(
prometheus.CounterOpts{
Name: "mefit_request_total",
Help: "The total number of requests processed",
},
[]string{"path", "method", "status"},
)
"time"
)
type WrappedWriter struct {
@@ -35,13 +20,19 @@ func Log(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
log.L.Info("request pattern", "pattern", r.Pattern)
wrapped := &WrappedWriter{
ResponseWriter: w,
StatusCode: http.StatusOK,
}
next.ServeHTTP(wrapped, r)
log.Info(r.RemoteAddr + " " + strconv.Itoa(wrapped.StatusCode) + " " + r.Method + " " + r.URL.Path + " " + time.Since(start).String())
metrics.WithLabelValues(r.URL.Path, r.Method, http.StatusText(wrapped.StatusCode)).Inc()
log.L.Info("request",
"remoteAddr", r.RemoteAddr,
"status", wrapped.StatusCode,
"method", r.Method,
"path", r.URL.Path,
"duration", time.Since(start).String())
})
}