Files
opencloud/services/groupware/pkg/middleware/traceid.go
Pascal Bleser d5f75ad8e0 groupware: Etag handling
* implement correct Etag and If-None-Match handling, responding with
   304 Not Modified if they match

 * introduce SessionState and State string type aliases to ensure we are
   using the correct fields for those, respectively

 * extract the SessionState from the JMAP response bodies in the
   groupware framework instead of having to do that in every single
   groupware API

 * use uint instead of int in some places to clarify that the values are
   >= 0

 * trace-log how long a Session was held in cache before being evicted

 * add Trace-Id header handling: add to response when specified in
   request, and implement a custom request logger to include it as a
   field

 * implement a more compact trace-logging of all the methods and URIs
   that are served, to put them into a single log entry instead of
   creating one log entry for every URI
2026-01-22 09:39:16 +01:00

43 lines
846 B
Go

package middleware
import (
"context"
"net/http"
)
type ctxKeyTraceID int
const TraceIDKey ctxKeyTraceID = 0
const maxTraceIdLength = 1024
var TraceIDHeader = "Trace-Id"
func TraceID(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
traceID := r.Header.Get(TraceIDHeader)
if traceID != "" {
runes := []rune(traceID)
if len(runes) > maxTraceIdLength {
traceID = string(runes[0:maxTraceIdLength])
}
w.Header().Add(TraceIDHeader, traceID)
ctx := context.WithValue(r.Context(), TraceIDKey, traceID)
next.ServeHTTP(w, r.WithContext(ctx))
} else {
next.ServeHTTP(w, r)
}
}
return http.HandlerFunc(fn)
}
func GetTraceID(ctx context.Context) string {
if ctx == nil {
return ""
}
if traceID, ok := ctx.Value(TraceIDKey).(string); ok {
return traceID
}
return ""
}