diff options
author | Stan Hu <stanhu@gmail.com> | 2023-11-17 09:00:55 +0300 |
---|---|---|
committer | Stan Hu <stanhu@gmail.com> | 2023-11-17 09:16:20 +0300 |
commit | d06cb5f4209eeb10102483e85fd604befaa9b99f (patch) | |
tree | 7e7cba04281724888b91c39ada9f3448ce8c12ee | |
parent | 9b36b0700fe0f1b25bd1105ce20ddf1ab66f3050 (diff) |
requestinfohandler: Restore missing log fields in SSHReceivePack
a497cfc8 replaced the v1 grpc-go-middleware `Tags` with the v2
`logging.Fields`, but this caused fields such as `correlation_id` and
`user_id` to be dropped for calls such as `SSHReceivePack`. This
occurred because the logger still uses the v1
`StreamServerInterceptor` and `UnaryServerInterceptor`, which extracts
logging fields from a different context key than the one used by v2
interceptors.
To restore these logging fields until the v2 middlewares are used,
restore the v1 `Tags`. This means we have a bit of redundancy since
the fields are stored for both v1 and v2 formats. However, once
https://gitlab.com/gitlab-org/gitaly/-/work_items/5661 is complete,
the v1 middleware and `Tags` can be dropped.
Changelog: fixed
-rw-r--r-- | internal/grpc/middleware/requestinfohandler/requestinfohandler.go | 10 | ||||
-rw-r--r-- | internal/grpc/middleware/requestinfohandler/requestinfohandler_test.go | 13 |
2 files changed, 23 insertions, 0 deletions
diff --git a/internal/grpc/middleware/requestinfohandler/requestinfohandler.go b/internal/grpc/middleware/requestinfohandler/requestinfohandler.go index 01ca81929..0d738c98e 100644 --- a/internal/grpc/middleware/requestinfohandler/requestinfohandler.go +++ b/internal/grpc/middleware/requestinfohandler/requestinfohandler.go @@ -4,6 +4,7 @@ import ( "context" "strings" + grpcmwtags "github.com/grpc-ecosystem/go-grpc-middleware/tags" "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging" grpcprometheus "github.com/grpc-ecosystem/go-grpc-prometheus" "github.com/prometheus/client_golang/prometheus" @@ -196,9 +197,18 @@ func (i *RequestInfo) extractRequestInfo(request any) { } func (i *RequestInfo) injectTags(ctx context.Context) context.Context { + tags := grpcmwtags.NewTags() + for key, value := range i.Tags() { ctx = logging.InjectLogField(ctx, key, value) + tags.Set(key, value) } + + // This maintains backward compatibility for tags in the v1 grpc-go-middleware. + // This can be removed when the v1 interceptors are removed: + // https://gitlab.com/gitlab-org/gitaly/-/work_items/5661 + ctx = grpcmwtags.SetInContext(ctx, tags) + return ctx } diff --git a/internal/grpc/middleware/requestinfohandler/requestinfohandler_test.go b/internal/grpc/middleware/requestinfohandler/requestinfohandler_test.go index 5e62707cd..38e40faeb 100644 --- a/internal/grpc/middleware/requestinfohandler/requestinfohandler_test.go +++ b/internal/grpc/middleware/requestinfohandler/requestinfohandler_test.go @@ -7,6 +7,7 @@ import ( "testing" "time" + grpcmwtags "github.com/grpc-ecosystem/go-grpc-middleware/tags" "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging" "github.com/stretchr/testify/require" gitalylog "gitlab.com/gitlab-org/gitaly/v16/internal/log" @@ -283,6 +284,18 @@ func TestGRPCTags(t *testing.T) { "grpc.request.fullMethod": "/gitaly.RepositoryService/OptimizeRepository", }, gitalylog.ConvertLoggingFields(fields)) + legacyFields := grpcmwtags.Extract(ctx).Values() + + require.Equal(t, map[string]any{ + "correlation_id": correlationID, + "grpc.meta.client_name": clientName, + "grpc.meta.deadline_type": "none", + "grpc.meta.method_type": "unary", + "grpc.meta.method_operation": "maintenance", + "grpc.meta.method_scope": "repository", + "grpc.request.fullMethod": "/gitaly.RepositoryService/OptimizeRepository", + }, legacyFields) + return nil, nil }) require.NoError(t, err) |