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

methodtype.go « middleware « praefect « internal - gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c8a980dc090a6aae6b8efe50622b1373c42e281b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package middleware

import (
	"context"

	"github.com/sirupsen/logrus"
	"gitlab.com/gitlab-org/gitaly/internal/praefect/metrics"
	"gitlab.com/gitlab-org/gitaly/internal/praefect/protoregistry"
	"google.golang.org/grpc"
)

// MethodTypeUnaryInterceptor returns a Unary Interceptor that records the method type of incoming RPC requests
func MethodTypeUnaryInterceptor(r *protoregistry.Registry) grpc.UnaryServerInterceptor {
	return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
		observeMethodType(r, info.FullMethod)

		res, err := handler(ctx, req)

		return res, err
	}
}

// MethodTypeStreamInterceptor returns a Stream Interceptor that records the method type of incoming RPC requests
func MethodTypeStreamInterceptor(r *protoregistry.Registry) grpc.StreamServerInterceptor {
	return func(srv interface{}, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
		observeMethodType(r, info.FullMethod)

		err := handler(srv, stream)

		return err
	}
}

func observeMethodType(registry *protoregistry.Registry, fullMethod string) {
	if registry.IsInterceptedMethod(fullMethod) {
		return
	}

	mi, err := registry.LookupMethod(fullMethod)
	if err != nil {
		logrus.WithField("full_method_name", fullMethod).WithError(err).Warn("error when looking up method info")
	}

	var opType string
	switch mi.Operation {
	case protoregistry.OpAccessor:
		opType = "accessor"
	case protoregistry.OpMutator:
		opType = "mutator"
	default:
		return
	}

	metrics.MethodTypeCounter.WithLabelValues(opType).Inc()
}