Added OpenTelemetry and use structured logging

This commit is contained in:
Robin Dittmar
2026-07-07 16:18:14 +02:00
parent 860563b47f
commit 0dec97a865
10 changed files with 191 additions and 30 deletions
+13 -11
View File
@@ -1,7 +1,7 @@
package handler
import (
"log"
"log/slog"
"net/http"
"github.com/robindittmar/dttmr-api/internal/api/response"
@@ -18,15 +18,17 @@ type apiResponse struct {
}
func DefaultHandler(w http.ResponseWriter, r *http.Request) {
var resp apiResponse
ctx := r.Context()
resp.Method = r.Method
resp.Url = r.URL.String()
resp.Proto = r.Proto
resp.Header = make(map[string]string)
resp.Host = r.Host
resp.RemoteAddr = r.RemoteAddr
resp.Form = make(map[string]string)
resp := apiResponse{
Method: r.Method,
Url: r.URL.String(),
Proto: r.Proto,
Header: make(map[string]string),
Host: r.Host,
RemoteAddr: r.RemoteAddr,
Form: make(map[string]string),
}
for k, v := range r.Header {
resp.Header[k] = v[0]
@@ -37,8 +39,8 @@ func DefaultHandler(w http.ResponseWriter, r *http.Request) {
resp.Form[k] = v[0]
}
} else {
log.Println(err)
slog.ErrorContext(ctx, "Error parsing form", slog.Any("error", err))
}
response.JSON(w, http.StatusOK, resp)
response.JSON(ctx, w, http.StatusOK, resp)
}
+3 -2
View File
@@ -11,9 +11,10 @@ type healthResponse struct {
}
func HealthHandler(w http.ResponseWriter, r *http.Request) {
_ = r
ctx := r.Context()
resp := healthResponse{
Status: "ok",
}
response.JSON(w, http.StatusOK, resp)
response.JSON(ctx, w, http.StatusOK, resp)
}