diff --git a/client/transport.go b/client/transport.go index 900c6ed1a1f6e134af01abb55fe29e6f5463eaf1..b5fc6b6df4e0c5ad76340501a7dd6639140dc22c 100644 --- a/client/transport.go +++ b/client/transport.go @@ -2,12 +2,14 @@ package client import ( + "log/slog" "net/http" "time" "gitlab.com/gitlab-org/labkit/correlation" - "gitlab.com/gitlab-org/labkit/log" "gitlab.com/gitlab-org/labkit/tracing" + "gitlab.com/gitlab-org/labkit/v2/log" + "gitlab.com/gitlab-org/labkit/v2/meta" ) type transport struct { @@ -17,6 +19,9 @@ type transport struct { // RoundTrip executes a single HTTP transaction, adding logging and tracing capabilities. func (rt *transport) RoundTrip(request *http.Request) (*http.Response, error) { ctx := request.Context() + correlationID := correlation.ExtractFromContext(ctx) + ctx = meta.WithCorrelationID(ctx, correlationID) + logger := log.ContextLogger(ctx) originalRemoteIP, ok := ctx.Value(OriginalRemoteIPContextKey{}).(string) if ok { @@ -29,31 +34,27 @@ func (rt *transport) RoundTrip(request *http.Request) (*http.Response, error) { response, err := rt.next.RoundTrip(request) - fields := log.Fields{ - "method": request.Method, - "url": request.URL.String(), - "duration_ms": time.Since(start) / time.Millisecond, - } - logger := log.WithContextFields(ctx, fields) + logger = logger.With(ctx, + slog.String("method", request.Method), + slog.String("url", request.URL.String()), + slog.Any("duration_ms", time.Since(start)/time.Millisecond), + ) if err != nil { - logger.WithError(err).Error("Internal API unreachable") + logger.Error("Internal API unreachable", slog.String("error_message", err.Error())) return response, err } - logger = logger.WithField("status", response.StatusCode) - + logger = logger.With( + slog.Int("status", response.StatusCode), + slog.Int("content_length_bytes", int(response.ContentLength)), + ) if response.StatusCode >= 400 { - logger.WithError(err).Error("Internal API error") + logger.Error("Internal API unreachable", slog.String("error_message", err.Error())) return response, err } - if response.ContentLength >= 0 { - logger = logger.WithField("content_length_bytes", response.ContentLength) - } - logger.Info("Finished HTTP request") - return response, nil } diff --git a/go.mod b/go.mod index 005c18a95c5c09eb65ad21939c98955b2f5e081e..e651d90391db4f7d58814307c881d099f2e44f64 100644 --- a/go.mod +++ b/go.mod @@ -23,7 +23,7 @@ require ( // Please do not override. Once v16.11.1 is released, this comment // can be removed. gitlab.com/gitlab-org/gitaly/v16 v16.11.0-rc1.0.20250408053233-c6d43513e93c - gitlab.com/gitlab-org/labkit v1.27.1 + gitlab.com/gitlab-org/labkit v1.32.1-0.20251125141554-84ae5e332213 golang.org/x/crypto v0.41.0 golang.org/x/sync v0.16.0 google.golang.org/grpc v1.72.0 @@ -65,7 +65,7 @@ require ( github.com/googleapis/enterprise-certificate-proxy v0.3.4 // indirect github.com/googleapis/gax-go/v2 v2.13.0 // indirect github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 // indirect - github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.3.1 // indirect + github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.3.2 // indirect github.com/hashicorp/go-cleanhttp v0.5.2 // indirect github.com/hashicorp/yamux v0.1.2-0.20220728231024-8f49b6f63f18 // indirect github.com/jmespath/go-jmespath v0.4.0 // indirect diff --git a/go.sum b/go.sum index f068899fdf5a11306875f61d2d3ea100ce003b80..15b032f6ac6303ae9c003250f876e90bedf47766 100644 --- a/go.sum +++ b/go.sum @@ -350,6 +350,8 @@ github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 h1:UH//fgunKIs4JdUbpDl1VZCDa github.com/grpc-ecosystem/go-grpc-middleware v1.4.0/go.mod h1:g5qyo/la0ALbONm6Vbp88Yd8NsDy6rZz+RcrMPxvld8= github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.3.1 h1:KcFzXwzM/kGhIRHvc8jdixfIJjVzuUJdnv+5xsPutog= github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.3.1/go.mod h1:qOchhhIlmRcqk/O9uCo/puJlyo07YINaIqdZfZG3Jkc= +github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.3.2 h1:sGm2vDRFUrQJO/Veii4h4zG2vvqG6uWNkBHSTqXOZk0= +github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.3.2/go.mod h1:wd1YpapPLivG6nQgbf7ZkG1hhSOXDhhn4MLTknx2aAc= github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 h1:Ovs26xHkKqVztRpIrF/92BcuyuQ/YW4NSIpoGtfXNho= github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk= github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw= @@ -556,6 +558,8 @@ gitlab.com/gitlab-org/go/reopen v1.0.0 h1:6BujZ0lkkjGIejTUJdNO1w56mN1SI10qcVQyQl gitlab.com/gitlab-org/go/reopen v1.0.0/go.mod h1:D6OID8YJDzEVZNYW02R/Pkj0v8gYFSIhXFTArAsBQw8= gitlab.com/gitlab-org/labkit v1.27.1 h1:c4gL4qfHPMZwetbFZO5HDam98MOS1Ul/CC8QTPon5/c= gitlab.com/gitlab-org/labkit v1.27.1/go.mod h1:ZHOQIOVQKeOEKvQ/GhGBjUNbV3zWsx8nty6D/SRCyd4= +gitlab.com/gitlab-org/labkit v1.32.1-0.20251125141554-84ae5e332213 h1:OklZVT0RxBsAUuCG8h5Bz3u3m21pKwf7RvZElW4sYtI= +gitlab.com/gitlab-org/labkit v1.32.1-0.20251125141554-84ae5e332213/go.mod h1:JqQLdgjV/KKAZJ6gvNodaLStmWeTT9mxgJPIEi66VHI= go.etcd.io/raft/v3 v3.6.0 h1:5NtvbDVYpnfZWcIHgGRk9DyzkBIXOi8j+DDp1IcnUWQ= go.etcd.io/raft/v3 v3.6.0/go.mod h1:nLvLevg6+xrVtHUmVaTcTz603gQPHfh7kUAwV6YpfGo= go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=