Skip to content

Commit

Permalink
move grpc ServerMetrics to private struct
Browse files Browse the repository at this point in the history
pass in the Prometheus Registry to the constructor so that grpcServer
can remain an internal struct

Signed-off-by: Benoit Gagnon <benoit.gagnon@ubisoft.com>
  • Loading branch information
bgagnon authored and youngnick committed Oct 16, 2019
1 parent 3faa067 commit 31bdfd9
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 13 deletions.
3 changes: 1 addition & 2 deletions cmd/contour/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,6 @@ func doServe(log logrus.FieldLogger, ctx *serveContext) error {
registry := prometheus.NewRegistry()
registry.MustRegister(prometheus.NewProcessCollector(prometheus.ProcessCollectorOpts{}))
registry.MustRegister(prometheus.NewGoCollector())
registry.MustRegister(cgrpc.ServerMetrics)

// step 9. create metrics service and register with workgroup.
metricsvc := metrics.Service{
Expand Down Expand Up @@ -289,7 +288,7 @@ func doServe(log logrus.FieldLogger, ctx *serveContext) error {
et.TypeURL(): et,
}
opts := ctx.grpcOptions()
s := cgrpc.NewAPI(log, resources, opts...)
s := cgrpc.NewAPI(log, resources, registry, opts...)
addr := net.JoinHostPort(ctx.xdsAddr, strconv.Itoa(ctx.xdsPort))
l, err := net.Listen("tcp", addr)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/e2e/e2e.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func setup(t *testing.T, opts ...func(*contour.EventHandler)) (cache.ResourceEve
ch.ListenerCache.TypeURL(): &ch.ListenerCache,
ch.SecretCache.TypeURL(): &ch.SecretCache,
et.TypeURL(): et,
})
}, r)

var g workgroup.Group

Expand Down
2 changes: 1 addition & 1 deletion internal/featuretests/featuretests.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func setup(t *testing.T, opts ...func(*contour.EventHandler)) (cache.ResourceEve
ch.ListenerCache.TypeURL(): &ch.ListenerCache,
ch.SecretCache.TypeURL(): &ch.SecretCache,
et.TypeURL(): et,
})
}, r)

var g workgroup.Group

Expand Down
17 changes: 9 additions & 8 deletions internal/grpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,39 +20,40 @@ import (
"google.golang.org/grpc/status"

"github.com/grpc-ecosystem/go-grpc-prometheus"
"github.com/prometheus/client_golang/prometheus"

v2 "github.com/envoyproxy/go-control-plane/envoy/api/v2"
discovery "github.com/envoyproxy/go-control-plane/envoy/service/discovery/v2"
loadstats "github.com/envoyproxy/go-control-plane/envoy/service/load_stats/v2"
"github.com/sirupsen/logrus"
)

var ServerMetrics = grpc_prometheus.NewServerMetrics()

// NewAPI returns a *grpc.Server which responds to the Envoy v2 xDS gRPC API.
func NewAPI(log logrus.FieldLogger, resources map[string]Resource, opts ...grpc.ServerOption) *grpc.Server {
opts = append(opts, grpc.StreamInterceptor(ServerMetrics.StreamServerInterceptor()),
grpc.UnaryInterceptor(ServerMetrics.UnaryServerInterceptor()))
g := grpc.NewServer(opts...)
func NewAPI(log logrus.FieldLogger, resources map[string]Resource, registry *prometheus.Registry, opts ...grpc.ServerOption) *grpc.Server {
s := &grpcServer{
xdsHandler{
FieldLogger: log,
resources: resources,
},
grpc_prometheus.NewServerMetrics(),
}

registry.MustRegister(s.metrics)
opts = append(opts, grpc.StreamInterceptor(s.metrics.StreamServerInterceptor()),
grpc.UnaryInterceptor(s.metrics.UnaryServerInterceptor()))
g := grpc.NewServer(opts...)
v2.RegisterClusterDiscoveryServiceServer(g, s)
v2.RegisterEndpointDiscoveryServiceServer(g, s)
v2.RegisterListenerDiscoveryServiceServer(g, s)
v2.RegisterRouteDiscoveryServiceServer(g, s)
discovery.RegisterSecretDiscoveryServiceServer(g, s)
ServerMetrics.InitializeMetrics(g)
s.metrics.InitializeMetrics(g)
return g
}

// grpcServer implements the LDS, RDS, CDS, and EDS, gRPC endpoints.
type grpcServer struct {
xdsHandler
metrics *grpc_prometheus.ServerMetrics
}

func (s *grpcServer) FetchClusters(_ context.Context, req *v2.DiscoveryRequest) (*v2.DiscoveryResponse, error) {
Expand Down
3 changes: 2 additions & 1 deletion internal/grpc/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,14 @@ func TestGRPC(t *testing.T) {
Metrics: ch.Metrics,
FieldLogger: log,
}
r := prometheus.NewRegistry()
srv := NewAPI(log, map[string]Resource{
ch.ClusterCache.TypeURL(): &ch.ClusterCache,
ch.RouteCache.TypeURL(): &ch.RouteCache,
ch.ListenerCache.TypeURL(): &ch.ListenerCache,
ch.SecretCache.TypeURL(): &ch.SecretCache,
et.TypeURL(): et,
})
}, r)
l, err := net.Listen("tcp", "127.0.0.1:0")
check(t, err)
done := make(chan error, 1)
Expand Down

0 comments on commit 31bdfd9

Please sign in to comment.