Repository: etcd-io/etcd
File: server/etcdmain/grpc_proxy.go (Go)

Code before:
		client.KV = namespace.NewKV(client.KV, grpcProxyNamespace)
		client.Watcher = namespace.NewWatcher(client.Watcher, grpcProxyNamespace)
		client.Lease = namespace.NewLease(client.Lease, grpcProxyNamespace)
	}

	if len(grpcProxyLeasing) > 0 {
		client.KV, _, _ = leasing.NewKV(client, grpcProxyLeasing)
	}

	kvp, _ := grpcproxy.NewKvProxy(client)
	watchp, _ := grpcproxy.NewWatchProxy(client.Ctx(), lg, client)
	if grpcProxyResolverPrefix != "" {
		grpcproxy.Register(lg, client, grpcProxyResolverPrefix, grpcProxyAdvertiseClientURL, grpcProxyResolverTTL)
	}
	clusterp, _ := grpcproxy.NewClusterProxy(lg, client, grpcProxyAdvertiseClientURL, grpcProxyResolverPrefix)
	leasep, _ := grpcproxy.NewLeaseProxy(client.Ctx(), client)

	mainp := grpcproxy.NewMaintenanceProxy(client)
	authp := grpcproxy.NewAuthProxy(client)
	electionp := grpcproxy.NewElectionProxy(client)
	lockp := grpcproxy.NewLockProxy(client)

	alwaysLoggingDeciderServer := func(ctx context.Context, fullMethodName string, servingObject any) bool { return true }

	serverMetrics := grpc_prometheus.NewServerMetrics()
	prometheus.Register(serverMetrics)

	grpcChainStreamList := []grpc.StreamServerInterceptor{
		serverMetrics.StreamServerInterceptor(),
	}
	grpcChainUnaryList := []grpc.UnaryServerInterceptor{
		serverMetrics.UnaryServerInterceptor(),
	}
	if grpcProxyEnableLogging {
		grpcChainStreamList = append(grpcChainStreamList,
			grpc_ctxtags.StreamServerInterceptor(),
			grpc_zap.PayloadStreamServerInterceptor(lg, alwaysLoggingDeciderServer),
		)
		grpcChainUnaryList = append(grpcChainUnaryList,
			grpc_ctxtags.UnaryServerInterceptor(),
			grpc_zap.PayloadUnaryServerInterceptor(lg, alwaysLoggingDeciderServer),
		)
	}

	gopts := []grpc.ServerOption{
		grpc.ChainStreamInterceptor(grpcChainStreamList...),
		grpc.ChainUnaryInterceptor(grpcChainUnaryList...),
		grpc.MaxConcurrentStreams(math.MaxUint32),
	}
	if grpcKeepAliveMinTime > time.Duration(0) {
		gopts = append(gopts, grpc.KeepaliveEnforcementPolicy(keepalive.EnforcementPolicy{

Review comment (suggestion):
```suggestion
	prometheus.MustRegister(serverMetrics)
```

Or we need to handle the error returned by `prometheus.Register`

Code after:
		client.KV = namespace.NewKV(client.KV, grpcProxyNamespace)
		client.Watcher = namespace.NewWatcher(client.Watcher, grpcProxyNamespace)
		client.Lease = namespace.NewLease(client.Lease, grpcProxyNamespace)
	}

	if len(grpcProxyLeasing) > 0 {
		client.KV, _, _ = leasing.NewKV(client, grpcProxyLeasing)
	}

	kvp, _ := grpcproxy.NewKvProxy(client)
	watchp, _ := grpcproxy.NewWatchProxy(client.Ctx(), lg, client)
	if grpcProxyResolverPrefix != "" {
		grpcproxy.Register(lg, client, grpcProxyResolverPrefix, grpcProxyAdvertiseClientURL, grpcProxyResolverTTL)
	}
	clusterp, _ := grpcproxy.NewClusterProxy(lg, client, grpcProxyAdvertiseClientURL, grpcProxyResolverPrefix)
	leasep, _ := grpcproxy.NewLeaseProxy(client.Ctx(), client)

	mainp := grpcproxy.NewMaintenanceProxy(client)
	authp := grpcproxy.NewAuthProxy(client)
	electionp := grpcproxy.NewElectionProxy(client)
	lockp := grpcproxy.NewLockProxy(client)

	alwaysLoggingDeciderServer := func(ctx context.Context, fullMethodName string, servingObject any) bool { return true }

	serverMetrics := grpc_prometheus.NewServerMetrics()
	prometheus.MustRegister(serverMetrics)

	grpcChainStreamList := []grpc.StreamServerInterceptor{
		serverMetrics.StreamServerInterceptor(),
	}
	grpcChainUnaryList := []grpc.UnaryServerInterceptor{
		serverMetrics.UnaryServerInterceptor(),
	}
	if grpcProxyEnableLogging {
		grpcChainStreamList = append(grpcChainStreamList,
			grpc_ctxtags.StreamServerInterceptor(),
			grpc_zap.PayloadStreamServerInterceptor(lg, alwaysLoggingDeciderServer),
		)
		grpcChainUnaryList = append(grpcChainUnaryList,
			grpc_ctxtags.UnaryServerInterceptor(),
			grpc_zap.PayloadUnaryServerInterceptor(lg, alwaysLoggingDeciderServer),
		)
	}

	gopts := []grpc.ServerOption{
		grpc.ChainStreamInterceptor(grpcChainStreamList...),
		grpc.ChainUnaryInterceptor(grpcChainUnaryList...),
		grpc.MaxConcurrentStreams(math.MaxUint32),
	}
	if grpcKeepAliveMinTime > time.Duration(0) {
		gopts = append(gopts, grpc.KeepaliveEnforcementPolicy(keepalive.EnforcementPolicy{