Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick Steinhardt <psteinhardt@gitlab.com>2023-09-28 11:10:59 +0300
committerPatrick Steinhardt <psteinhardt@gitlab.com>2023-10-04 11:31:25 +0300
commit95b7dc4842e80847f5ea141f96ec51ca8bb486f9 (patch)
treefec293e2c71f2886425c78ce85e247b922788b16
parent2d170550f1887c7787fa72d01eebd71655d593ca (diff)
requestinfohandler: Make Prometheus reporting a receiver function
Convert the function that reports Prometheus metrics a receiver function of the `requestInfo`. This makes the logic more self-contained as we have to pass less variables into it and can readily reuse what we store inside of the struct already anyway.
-rw-r--r--internal/grpc/middleware/requestinfohandler/requestinfohandler.go60
-rw-r--r--internal/grpc/middleware/requestinfohandler/requestinfohandler_test.go7
2 files changed, 38 insertions, 29 deletions
diff --git a/internal/grpc/middleware/requestinfohandler/requestinfohandler.go b/internal/grpc/middleware/requestinfohandler/requestinfohandler.go
index 843508bf3..7e59b6ce1 100644
--- a/internal/grpc/middleware/requestinfohandler/requestinfohandler.go
+++ b/internal/grpc/middleware/requestinfohandler/requestinfohandler.go
@@ -35,6 +35,7 @@ var requests = promauto.NewCounterVec(
)
type requestInfo struct {
+ fullMethod string
clientName string
callSite string
authVersion string
@@ -91,6 +92,7 @@ func getFromMD(md metadata.MD, header string) string {
// using `unknown` if a value is not set
func newRequestInfo(ctx context.Context, fullMethod, grpcMethodType string) requestInfo {
info := requestInfo{
+ fullMethod: fullMethod,
clientName: unknownValue,
callSite: unknownValue,
authVersion: unknownValue,
@@ -196,40 +198,31 @@ func newRequestInfo(ctx context.Context, fullMethod, grpcMethodType string) requ
return info
}
-func extractServiceAndMethodName(fullMethodName string) (string, string) {
- fullMethodName = strings.TrimPrefix(fullMethodName, "/") // remove leading slash
- service, method, ok := strings.Cut(fullMethodName, "/")
- if !ok {
- return unknownValue, unknownValue
- }
- return service, method
-}
-
-func streamRPCType(info *grpc.StreamServerInfo) string {
- if info.IsClientStream && !info.IsServerStream {
- return "client_stream"
- } else if !info.IsClientStream && info.IsServerStream {
- return "server_stream"
- }
- return "bidi_stream"
-}
-
-func reportWithPrometheusLabels(info requestInfo, fullMethod string, err error) {
+func (i requestInfo) reportPrometheusMetrics(err error) {
grpcCode := structerr.GRPCCode(err)
- serviceName, methodName := extractServiceAndMethodName(fullMethod)
+ serviceName, methodName := extractServiceAndMethodName(i.fullMethod)
requests.WithLabelValues(
- info.clientName, // client_name
+ i.clientName, // client_name
serviceName, // grpc_service
methodName, // grpc_method
- info.callSite, // call_site
- info.authVersion, // auth_version
+ i.callSite, // call_site
+ i.authVersion, // auth_version
grpcCode.String(), // grpc_code
- info.deadlineType, // deadline_type
- info.methodOperation,
- info.methodScope,
+ i.deadlineType, // deadline_type
+ i.methodOperation,
+ i.methodScope,
).Inc()
- grpcprometheus.WithConstLabels(prometheus.Labels{"deadline_type": info.deadlineType})
+ grpcprometheus.WithConstLabels(prometheus.Labels{"deadline_type": i.deadlineType})
+}
+
+func extractServiceAndMethodName(fullMethodName string) (string, string) {
+ fullMethodName = strings.TrimPrefix(fullMethodName, "/") // remove leading slash
+ service, method, ok := strings.Cut(fullMethodName, "/")
+ if !ok {
+ return unknownValue, unknownValue
+ }
+ return service, method
}
// UnaryInterceptor returns a Unary Interceptor
@@ -238,7 +231,7 @@ func UnaryInterceptor(ctx context.Context, req interface{}, serverInfo *grpc.Una
res, err := handler(ctx, req)
- reportWithPrometheusLabels(info, serverInfo.FullMethod, err)
+ info.reportPrometheusMetrics(err)
return res, err
}
@@ -250,7 +243,16 @@ func StreamInterceptor(srv interface{}, stream grpc.ServerStream, serverInfo *gr
err := handler(srv, stream)
- reportWithPrometheusLabels(info, serverInfo.FullMethod, err)
+ info.reportPrometheusMetrics(err)
return err
}
+
+func streamRPCType(info *grpc.StreamServerInfo) string {
+ if info.IsClientStream && !info.IsServerStream {
+ return "client_stream"
+ } else if !info.IsClientStream && info.IsServerStream {
+ return "server_stream"
+ }
+ return "bidi_stream"
+}
diff --git a/internal/grpc/middleware/requestinfohandler/requestinfohandler_test.go b/internal/grpc/middleware/requestinfohandler/requestinfohandler_test.go
index c9c9946d7..bad0e39ee 100644
--- a/internal/grpc/middleware/requestinfohandler/requestinfohandler_test.go
+++ b/internal/grpc/middleware/requestinfohandler/requestinfohandler_test.go
@@ -113,6 +113,7 @@ func TestNewRequestInfo(t *testing.T) {
metadata: metadata.Pairs(),
deadline: false,
expectedInfo: requestInfo{
+ fullMethod: "/gitaly.RepositoryService/UnknownMethod",
clientName: unknownValue,
callSite: unknownValue,
authVersion: unknownValue,
@@ -127,6 +128,7 @@ func TestNewRequestInfo(t *testing.T) {
metadata: metadata.Pairs(),
deadline: false,
expectedInfo: requestInfo{
+ fullMethod: "/gitaly.RepositoryService/ObjectFormat",
clientName: unknownValue,
callSite: unknownValue,
authVersion: unknownValue,
@@ -141,6 +143,7 @@ func TestNewRequestInfo(t *testing.T) {
metadata: metadata.Pairs(),
deadline: false,
expectedInfo: requestInfo{
+ fullMethod: "/gitaly.RepositoryService/CreateRepository",
clientName: unknownValue,
callSite: unknownValue,
authVersion: unknownValue,
@@ -155,6 +158,7 @@ func TestNewRequestInfo(t *testing.T) {
metadata: metadata.Pairs(),
deadline: false,
expectedInfo: requestInfo{
+ fullMethod: "/gitaly.RepositoryService/OptimizeRepository",
clientName: unknownValue,
callSite: unknownValue,
authVersion: unknownValue,
@@ -169,6 +173,7 @@ func TestNewRequestInfo(t *testing.T) {
metadata: metadata.Pairs(),
deadline: false,
expectedInfo: requestInfo{
+ fullMethod: "/gitaly.RepositoryService/OptimizeRepository",
clientName: unknownValue,
callSite: unknownValue,
authVersion: unknownValue,
@@ -183,6 +188,7 @@ func TestNewRequestInfo(t *testing.T) {
metadata: metadata.Pairs(),
deadline: false,
expectedInfo: requestInfo{
+ fullMethod: "/gitaly.RemoteService/FindRemoteRepository",
clientName: unknownValue,
callSite: unknownValue,
authVersion: unknownValue,
@@ -231,6 +237,7 @@ func TestGRPCTags(t *testing.T) {
info := newRequestInfo(ctx, "/gitaly.RepositoryService/OptimizeRepository", "unary")
require.Equal(t, requestInfo{
+ fullMethod: "/gitaly.RepositoryService/OptimizeRepository",
clientName: clientName,
callSite: "unknown",
authVersion: "unknown",