Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Instrument gRPC server with go-grpc-prometheus middleware (#1678) #1692

Merged
merged 4 commits into from
Oct 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/contour/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,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
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
github.com/google/uuid v1.0.0
github.com/googleapis/gnostic v0.2.0 // indirect
github.com/gordonklaus/ineffassign v0.0.0-20190601041439-ed7b1b5ee0f8
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0
github.com/hashicorp/golang-lru v0.5.1 // indirect
github.com/imdario/mergo v0.3.7 // indirect
github.com/kisielk/errcheck v1.2.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ github.com/gordonklaus/ineffassign v0.0.0-20190601041439-ed7b1b5ee0f8/go.mod h1:
github.com/gorilla/context v1.1.1/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51q0aT7Yg=
github.com/gorilla/mux v1.6.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
github.com/gregjones/httpcache v0.0.0-20170728041850-787624de3eb7/go.mod h1:FecbI9+v66THATjSRHfNgh1IVFe/9kFxbXtjV0ctIMA=
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 h1:Ovs26xHkKqVztRpIrF/92BcuyuQ/YW4NSIpoGtfXNho=
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1 h1:0hERBMJE1eitiLkihrMvRVBYAkpHzc/J3QdDN+dAcgU=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
Expand Down Expand Up @@ -245,8 +247,6 @@ golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3
golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
golang.org/x/tools v0.0.0-20190621195816-6e04913cbbac/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
golang.org/x/tools v0.0.0-20190719005602-e377ae9d6386/go.mod h1:jcCCGcm9btYwXyDqrUWc6MKQKKGJCWEQ3AfLSRIbEuI=
golang.org/x/tools v0.0.0-20190918220241-b8f1ca6a929d h1:509WdJuGomQvj505oUgekhS5hb41+tboOY87t2t1Vko=
golang.org/x/tools v0.0.0-20190918220241-b8f1ca6a929d/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20190929041059-e7abfedfabcf h1:NvypsVlesF+lEDKVK5RNkww4fzArJXChZxNin79j05M=
golang.org/x/tools v0.0.0-20190929041059-e7abfedfabcf/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
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
14 changes: 11 additions & 3 deletions internal/grpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,41 @@ import (
"google.golang.org/grpc/codes"
"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"
)

// 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 {
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)
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