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

protocol.go « git « internal - gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ac86873b83a3835983cee75472e03464af7ab77e (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package git

import (
	"context"
	"fmt"
	"strings"

	grpc_ctxtags "github.com/grpc-ecosystem/go-grpc-middleware/tags"
	"github.com/prometheus/client_golang/prometheus"
	"gitlab.com/gitlab-org/gitaly/internal/config"
)

const (
	// ProtocolV2 is the special value used by Git clients to request protocol v2
	ProtocolV2 = "version=2"
)

var (
	gitProtocolRequests = prometheus.NewCounterVec(
		prometheus.CounterOpts{
			Name: "gitaly_git_protocol_requests_total",
			Help: "Counter of Git protocol requests",
		},
		[]string{"grpc_service", "grpc_method", "git_protocol"},
	)
)

func init() {
	prometheus.MustRegister(gitProtocolRequests)
}

// RequestWithGitProtocol holds requests that respond to GitProtocol
type RequestWithGitProtocol interface {
	GetGitProtocol() string
}

// AddGitProtocolEnv checks whether the request has Git protocol v2
// and sets this in the environment.
func AddGitProtocolEnv(ctx context.Context, req RequestWithGitProtocol, env []string) []string {
	service, method := methodFromContext(ctx)

	if req.GetGitProtocol() == ProtocolV2 {
		if config.Config.Git.ProtocolV2Enabled {
			env = append(env, fmt.Sprintf("GIT_PROTOCOL=%s", ProtocolV2))

			gitProtocolRequests.WithLabelValues(service, method, "v2").Inc()
		} else {
			gitProtocolRequests.WithLabelValues(service, method, "v2-rejected").Inc()
		}
	} else {
		gitProtocolRequests.WithLabelValues(service, method, "v0").Inc()
	}

	return env
}

func methodFromContext(ctx context.Context) (service string, method string) {
	tags := grpc_ctxtags.Extract(ctx)
	ctxValue := tags.Values()["grpc.request.fullMethod"]
	if ctxValue == nil {
		return "", ""
	}

	if s, ok := ctxValue.(string); ok {
		// Expect: "/foo.BarService/Qux"
		split := strings.Split(s, "/")
		if len(split) != 3 {
			return "", ""
		}

		return split[1], split[2]
	}

	return "", ""
}