diff options
author | Patrick Steinhardt <psteinhardt@gitlab.com> | 2023-09-28 11:01:08 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2023-10-04 11:31:25 +0300 |
commit | 2d170550f1887c7787fa72d01eebd71655d593ca (patch) | |
tree | 4dc9e9de6c449589438b75e95314778735f5840c | |
parent | 1fbdff1e93a3b7234e0f04b3101c7a5358c9c804 (diff) |
middleware: Rename metadatahandler to requestinfohandler
We have two different mechanisms to inject information about the current
gRPC call into the context:
- The metadatahandler injects data about the metadata, e.g. the peer
information and whether a call is an accessor or a mutator.
- The field extractor that gets set up via go-grpc-middleware's tags
interceptor and that injects data about the request.
The tagging mechanism used by the field extractor is going away in the
go-grpc-middleware dependency though without replacement. Furthermore,
both functionalities are related to each other and can thus easily share
the same infrastructure. We are thus about to merge them.
As a first step, let's rename the metadatahandler to requestinfohandler
such that it better reflects the new reponsibility when it will start to
handle both gRPC call metadata and request information.
-rw-r--r-- | internal/gitaly/server/server.go | 6 | ||||
-rw-r--r-- | internal/grpc/middleware/requestinfohandler/requestinfohandler.go (renamed from internal/grpc/middleware/metadatahandler/metadatahandler.go) | 66 | ||||
-rw-r--r-- | internal/grpc/middleware/requestinfohandler/requestinfohandler_test.go (renamed from internal/grpc/middleware/metadatahandler/metadatahandler_test.go) | 46 | ||||
-rw-r--r-- | internal/grpc/middleware/requestinfohandler/testhelper_test.go (renamed from internal/grpc/middleware/metadatahandler/testhelper_test.go) | 2 | ||||
-rw-r--r-- | internal/praefect/server.go | 6 |
5 files changed, 63 insertions, 63 deletions
diff --git a/internal/gitaly/server/server.go b/internal/gitaly/server/server.go index 9ffb88c65..a828c3f03 100644 --- a/internal/gitaly/server/server.go +++ b/internal/gitaly/server/server.go @@ -16,8 +16,8 @@ import ( "gitlab.com/gitlab-org/gitaly/v16/internal/grpc/middleware/cache" "gitlab.com/gitlab-org/gitaly/v16/internal/grpc/middleware/customfieldshandler" "gitlab.com/gitlab-org/gitaly/v16/internal/grpc/middleware/featureflag" - "gitlab.com/gitlab-org/gitaly/v16/internal/grpc/middleware/metadatahandler" "gitlab.com/gitlab-org/gitaly/v16/internal/grpc/middleware/panichandler" + "gitlab.com/gitlab-org/gitaly/v16/internal/grpc/middleware/requestinfohandler" "gitlab.com/gitlab-org/gitaly/v16/internal/grpc/middleware/sentryhandler" "gitlab.com/gitlab-org/gitaly/v16/internal/grpc/middleware/statushandler" "gitlab.com/gitlab-org/gitaly/v16/internal/grpc/protoregistry" @@ -108,7 +108,7 @@ func (s *GitalyServerFactory) New(secure bool, opts ...Option) (*grpc.Server, er streamServerInterceptors := []grpc.StreamServerInterceptor{ grpcmwtags.StreamServerInterceptor(ctxTagOpts...), grpccorrelation.StreamServerCorrelationInterceptor(), // Must be above the metadata handler - metadatahandler.StreamInterceptor, + requestinfohandler.StreamInterceptor, grpcprometheus.StreamServerInterceptor, customfieldshandler.StreamInterceptor, s.logger.WithField("component", "gitaly.StreamServerInterceptor").StreamServerInterceptor( @@ -124,7 +124,7 @@ func (s *GitalyServerFactory) New(secure bool, opts ...Option) (*grpc.Server, er unaryServerInterceptors := []grpc.UnaryServerInterceptor{ grpcmwtags.UnaryServerInterceptor(ctxTagOpts...), grpccorrelation.UnaryServerCorrelationInterceptor(), // Must be above the metadata handler - metadatahandler.UnaryInterceptor, + requestinfohandler.UnaryInterceptor, grpcprometheus.UnaryServerInterceptor, customfieldshandler.UnaryInterceptor, s.logger.WithField("component", "gitaly.UnaryServerInterceptor").UnaryServerInterceptor( diff --git a/internal/grpc/middleware/metadatahandler/metadatahandler.go b/internal/grpc/middleware/requestinfohandler/requestinfohandler.go index 930276999..843508bf3 100644 --- a/internal/grpc/middleware/metadatahandler/metadatahandler.go +++ b/internal/grpc/middleware/requestinfohandler/requestinfohandler.go @@ -1,4 +1,4 @@ -package metadatahandler +package requestinfohandler import ( "context" @@ -34,7 +34,7 @@ var requests = promauto.NewCounterVec( }, ) -type metadataTags struct { +type requestInfo struct { clientName string callSite string authVersion string @@ -86,11 +86,11 @@ func getFromMD(md metadata.MD, header string) string { return values[0] } -// addMetadataTags extracts metadata from the connection headers and add it to the +// newRequestInfo extracts metadata from the connection headers and add it to the // ctx_tags, if it is set. Returns values appropriate for use with prometheus labels, // using `unknown` if a value is not set -func addMetadataTags(ctx context.Context, fullMethod, grpcMethodType string) metadataTags { - metaTags := metadataTags{ +func newRequestInfo(ctx context.Context, fullMethod, grpcMethodType string) requestInfo { + info := requestInfo{ clientName: unknownValue, callSite: unknownValue, authVersion: unknownValue, @@ -101,7 +101,7 @@ func addMetadataTags(ctx context.Context, fullMethod, grpcMethodType string) met md, ok := metadata.FromIncomingContext(ctx) if !ok { - return metaTags + return info } tags := grpcmwtags.Extract(ctx) @@ -119,7 +119,7 @@ func addMetadataTags(ctx context.Context, fullMethod, grpcMethodType string) met operation = unknownValue } - metaTags.methodOperation = operation + info.methodOperation = operation tags.Set(MethodOperationKey, operation) var scope string @@ -132,43 +132,43 @@ func addMetadataTags(ctx context.Context, fullMethod, grpcMethodType string) met scope = unknownValue } - metaTags.methodScope = scope + info.methodScope = scope tags.Set(MethodScopeKey, scope) } metadata := getFromMD(md, "call_site") if metadata != "" { - metaTags.callSite = metadata + info.callSite = metadata tags.Set(CallSiteKey, metadata) } metadata = getFromMD(md, "deadline_type") _, deadlineSet := ctx.Deadline() if !deadlineSet { - metaTags.deadlineType = "none" + info.deadlineType = "none" } else if metadata != "" { - metaTags.deadlineType = metadata + info.deadlineType = metadata } clientName := correlation.ExtractClientNameFromContext(ctx) if clientName != "" { - metaTags.clientName = clientName + info.clientName = clientName tags.Set(ClientNameKey, clientName) } else { metadata = getFromMD(md, "client_name") if metadata != "" { - metaTags.clientName = metadata + info.clientName = metadata tags.Set(ClientNameKey, metadata) } } // Set the deadline and method types in the logs - tags.Set(DeadlineTypeKey, metaTags.deadlineType) + tags.Set(DeadlineTypeKey, info.deadlineType) tags.Set(MethodTypeKey, grpcMethodType) authInfo, _ := gitalyauth.ExtractAuthInfo(ctx) if authInfo != nil { - metaTags.authVersion = authInfo.Version + info.authVersion = authInfo.Version tags.Set(AuthVersionKey, authInfo.Version) } @@ -193,7 +193,7 @@ func addMetadataTags(ctx context.Context, fullMethod, grpcMethodType string) met tags.Set(correlation.FieldName, correlationID) } - return metaTags + return info } func extractServiceAndMethodName(fullMethodName string) (string, string) { @@ -214,43 +214,43 @@ func streamRPCType(info *grpc.StreamServerInfo) string { return "bidi_stream" } -func reportWithPrometheusLabels(metaTags metadataTags, fullMethod string, err error) { +func reportWithPrometheusLabels(info requestInfo, fullMethod string, err error) { grpcCode := structerr.GRPCCode(err) serviceName, methodName := extractServiceAndMethodName(fullMethod) requests.WithLabelValues( - metaTags.clientName, // client_name - serviceName, // grpc_service - methodName, // grpc_method - metaTags.callSite, // call_site - metaTags.authVersion, // auth_version - grpcCode.String(), // grpc_code - metaTags.deadlineType, // deadline_type - metaTags.methodOperation, - metaTags.methodScope, + info.clientName, // client_name + serviceName, // grpc_service + methodName, // grpc_method + info.callSite, // call_site + info.authVersion, // auth_version + grpcCode.String(), // grpc_code + info.deadlineType, // deadline_type + info.methodOperation, + info.methodScope, ).Inc() - grpcprometheus.WithConstLabels(prometheus.Labels{"deadline_type": metaTags.deadlineType}) + grpcprometheus.WithConstLabels(prometheus.Labels{"deadline_type": info.deadlineType}) } // UnaryInterceptor returns a Unary Interceptor -func UnaryInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) { - metaTags := addMetadataTags(ctx, info.FullMethod, "unary") +func UnaryInterceptor(ctx context.Context, req interface{}, serverInfo *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) { + info := newRequestInfo(ctx, serverInfo.FullMethod, "unary") res, err := handler(ctx, req) - reportWithPrometheusLabels(metaTags, info.FullMethod, err) + reportWithPrometheusLabels(info, serverInfo.FullMethod, err) return res, err } // StreamInterceptor returns a Stream Interceptor -func StreamInterceptor(srv interface{}, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error { +func StreamInterceptor(srv interface{}, stream grpc.ServerStream, serverInfo *grpc.StreamServerInfo, handler grpc.StreamHandler) error { ctx := stream.Context() - metaTags := addMetadataTags(ctx, info.FullMethod, streamRPCType(info)) + info := newRequestInfo(ctx, serverInfo.FullMethod, streamRPCType(serverInfo)) err := handler(srv, stream) - reportWithPrometheusLabels(metaTags, info.FullMethod, err) + reportWithPrometheusLabels(info, serverInfo.FullMethod, err) return err } diff --git a/internal/grpc/middleware/metadatahandler/metadatahandler_test.go b/internal/grpc/middleware/requestinfohandler/requestinfohandler_test.go index 460dd2f68..c9c9946d7 100644 --- a/internal/grpc/middleware/metadatahandler/metadatahandler_test.go +++ b/internal/grpc/middleware/requestinfohandler/requestinfohandler_test.go @@ -1,4 +1,4 @@ -package metadatahandler +package requestinfohandler import ( "context" @@ -17,23 +17,23 @@ const ( clientName = "CLIENT_NAME" ) -func TestAddMetadataTags(t *testing.T) { +func TestNewRequestInfo(t *testing.T) { t.Parallel() baseContext := testhelper.Context(t) for _, tc := range []struct { - desc string - fullMethod string - metadata metadata.MD - deadline bool - expectedMetatags metadataTags + desc string + fullMethod string + metadata metadata.MD + deadline bool + expectedInfo requestInfo }{ { desc: "empty metadata", metadata: metadata.Pairs(), deadline: false, - expectedMetatags: metadataTags{ + expectedInfo: requestInfo{ clientName: unknownValue, callSite: unknownValue, authVersion: unknownValue, @@ -46,7 +46,7 @@ func TestAddMetadataTags(t *testing.T) { desc: "context containing metadata", metadata: metadata.Pairs("call_site", "testsite"), deadline: false, - expectedMetatags: metadataTags{ + expectedInfo: requestInfo{ clientName: unknownValue, callSite: "testsite", authVersion: unknownValue, @@ -59,7 +59,7 @@ func TestAddMetadataTags(t *testing.T) { desc: "context containing metadata and a deadline", metadata: metadata.Pairs("call_site", "testsite"), deadline: true, - expectedMetatags: metadataTags{ + expectedInfo: requestInfo{ clientName: unknownValue, callSite: "testsite", authVersion: unknownValue, @@ -72,7 +72,7 @@ func TestAddMetadataTags(t *testing.T) { desc: "context containing metadata and a deadline type", metadata: metadata.Pairs("deadline_type", "regular"), deadline: true, - expectedMetatags: metadataTags{ + expectedInfo: requestInfo{ clientName: unknownValue, callSite: unknownValue, authVersion: unknownValue, @@ -85,7 +85,7 @@ func TestAddMetadataTags(t *testing.T) { desc: "a context without deadline but with deadline type", metadata: metadata.Pairs("deadline_type", "regular"), deadline: false, - expectedMetatags: metadataTags{ + expectedInfo: requestInfo{ clientName: unknownValue, callSite: unknownValue, authVersion: unknownValue, @@ -98,7 +98,7 @@ func TestAddMetadataTags(t *testing.T) { desc: "with a context containing metadata", metadata: metadata.Pairs("deadline_type", "regular", "client_name", "rails"), deadline: true, - expectedMetatags: metadataTags{ + expectedInfo: requestInfo{ clientName: "rails", callSite: unknownValue, authVersion: unknownValue, @@ -112,7 +112,7 @@ func TestAddMetadataTags(t *testing.T) { fullMethod: "/gitaly.RepositoryService/UnknownMethod", metadata: metadata.Pairs(), deadline: false, - expectedMetatags: metadataTags{ + expectedInfo: requestInfo{ clientName: unknownValue, callSite: unknownValue, authVersion: unknownValue, @@ -126,7 +126,7 @@ func TestAddMetadataTags(t *testing.T) { fullMethod: "/gitaly.RepositoryService/ObjectFormat", metadata: metadata.Pairs(), deadline: false, - expectedMetatags: metadataTags{ + expectedInfo: requestInfo{ clientName: unknownValue, callSite: unknownValue, authVersion: unknownValue, @@ -140,7 +140,7 @@ func TestAddMetadataTags(t *testing.T) { fullMethod: "/gitaly.RepositoryService/CreateRepository", metadata: metadata.Pairs(), deadline: false, - expectedMetatags: metadataTags{ + expectedInfo: requestInfo{ clientName: unknownValue, callSite: unknownValue, authVersion: unknownValue, @@ -154,7 +154,7 @@ func TestAddMetadataTags(t *testing.T) { fullMethod: "/gitaly.RepositoryService/OptimizeRepository", metadata: metadata.Pairs(), deadline: false, - expectedMetatags: metadataTags{ + expectedInfo: requestInfo{ clientName: unknownValue, callSite: unknownValue, authVersion: unknownValue, @@ -168,7 +168,7 @@ func TestAddMetadataTags(t *testing.T) { fullMethod: "/gitaly.RepositoryService/OptimizeRepository", metadata: metadata.Pairs(), deadline: false, - expectedMetatags: metadataTags{ + expectedInfo: requestInfo{ clientName: unknownValue, callSite: unknownValue, authVersion: unknownValue, @@ -182,7 +182,7 @@ func TestAddMetadataTags(t *testing.T) { fullMethod: "/gitaly.RemoteService/FindRemoteRepository", metadata: metadata.Pairs(), deadline: false, - expectedMetatags: metadataTags{ + expectedInfo: requestInfo{ clientName: unknownValue, callSite: unknownValue, authVersion: unknownValue, @@ -205,7 +205,7 @@ func TestAddMetadataTags(t *testing.T) { defer cancel() } - require.Equal(t, tc.expectedMetatags, addMetadataTags(ctx, tc.fullMethod, "unary")) + require.Equal(t, tc.expectedInfo, newRequestInfo(ctx, tc.fullMethod, "unary")) }) } } @@ -228,16 +228,16 @@ func TestGRPCTags(t *testing.T) { interceptor := grpcmwtags.UnaryServerInterceptor() _, err := interceptor(ctx, nil, nil, func(ctx context.Context, _ interface{}) (interface{}, error) { - metaTags := addMetadataTags(ctx, "/gitaly.RepositoryService/OptimizeRepository", "unary") + info := newRequestInfo(ctx, "/gitaly.RepositoryService/OptimizeRepository", "unary") - require.Equal(t, metadataTags{ + require.Equal(t, requestInfo{ clientName: clientName, callSite: "unknown", authVersion: "unknown", deadlineType: "none", methodOperation: "maintenance", methodScope: "repository", - }, metaTags) + }, info) require.Equal(t, map[string]interface{}{ "correlation_id": correlationID, diff --git a/internal/grpc/middleware/metadatahandler/testhelper_test.go b/internal/grpc/middleware/requestinfohandler/testhelper_test.go index ee8340678..464eca43d 100644 --- a/internal/grpc/middleware/metadatahandler/testhelper_test.go +++ b/internal/grpc/middleware/requestinfohandler/testhelper_test.go @@ -1,4 +1,4 @@ -package metadatahandler +package requestinfohandler import ( "testing" diff --git a/internal/praefect/server.go b/internal/praefect/server.go index 066f2bf78..4044e8eab 100644 --- a/internal/praefect/server.go +++ b/internal/praefect/server.go @@ -14,8 +14,8 @@ import ( "gitlab.com/gitlab-org/gitaly/v16/internal/grpc/backchannel" "gitlab.com/gitlab-org/gitaly/v16/internal/grpc/grpcstats" "gitlab.com/gitlab-org/gitaly/v16/internal/grpc/listenmux" - "gitlab.com/gitlab-org/gitaly/v16/internal/grpc/middleware/metadatahandler" "gitlab.com/gitlab-org/gitaly/v16/internal/grpc/middleware/panichandler" + "gitlab.com/gitlab-org/gitaly/v16/internal/grpc/middleware/requestinfohandler" "gitlab.com/gitlab-org/gitaly/v16/internal/grpc/middleware/sentryhandler" "gitlab.com/gitlab-org/gitaly/v16/internal/grpc/middleware/statushandler" "gitlab.com/gitlab-org/gitaly/v16/internal/grpc/proxy" @@ -69,7 +69,7 @@ func commonUnaryServerInterceptors(logger log.Logger, messageProducer grpcmwlogr return []grpc.UnaryServerInterceptor{ grpcmwtags.UnaryServerInterceptor(ctxtagsInterceptorOption()), grpccorrelation.UnaryServerCorrelationInterceptor(), // Must be above the metadata handler - metadatahandler.UnaryInterceptor, + requestinfohandler.UnaryInterceptor, grpcprometheus.UnaryServerInterceptor, logger.UnaryServerInterceptor( grpcmwlogrus.WithTimestampFormat(log.LogTimestampFormat), @@ -139,7 +139,7 @@ func NewGRPCServer( grpcmwtags.StreamServerInterceptor(ctxtagsInterceptorOption()), grpccorrelation.StreamServerCorrelationInterceptor(), // Must be above the metadata handler middleware.MethodTypeStreamInterceptor(deps.Registry, deps.Logger), - metadatahandler.StreamInterceptor, + requestinfohandler.StreamInterceptor, grpcprometheus.StreamServerInterceptor, deps.Logger.WithField("component", "praefect.StreamServerInterceptor").StreamServerInterceptor( grpcmwlogrus.WithTimestampFormat(log.LogTimestampFormat), |