Skip to content

Commit 3732311

Browse files
committed
instrument gRPC server with go-grpc-prometheus middleware (projectcontour#1678)
1 parent b0a268b commit 3732311

File tree

4 files changed

+11
-2
lines changed

4 files changed

+11
-2
lines changed

cmd/contour/serve.go

+1
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ func doServe(log logrus.FieldLogger, ctx *serveContext) error {
216216
registry := prometheus.NewRegistry()
217217
registry.MustRegister(prometheus.NewProcessCollector(prometheus.ProcessCollectorOpts{}))
218218
registry.MustRegister(prometheus.NewGoCollector())
219+
registry.MustRegister(cgrpc.ServerMetrics)
219220

220221
// step 9. create metrics service and register with workgroup.
221222
metricsvc := metrics.Service{

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ require (
1515
github.com/google/uuid v1.0.0
1616
github.com/googleapis/gnostic v0.2.0 // indirect
1717
github.com/gordonklaus/ineffassign v0.0.0-20190601041439-ed7b1b5ee0f8
18+
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0
1819
github.com/hashicorp/golang-lru v0.5.1 // indirect
1920
github.com/imdario/mergo v0.3.7 // indirect
2021
github.com/kisielk/errcheck v1.2.0

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ github.com/gordonklaus/ineffassign v0.0.0-20190601041439-ed7b1b5ee0f8/go.mod h1:
8686
github.com/gorilla/context v1.1.1/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51q0aT7Yg=
8787
github.com/gorilla/mux v1.6.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
8888
github.com/gregjones/httpcache v0.0.0-20170728041850-787624de3eb7/go.mod h1:FecbI9+v66THATjSRHfNgh1IVFe/9kFxbXtjV0ctIMA=
89+
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 h1:Ovs26xHkKqVztRpIrF/92BcuyuQ/YW4NSIpoGtfXNho=
90+
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
8991
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
9092
github.com/hashicorp/golang-lru v0.5.1 h1:0hERBMJE1eitiLkihrMvRVBYAkpHzc/J3QdDN+dAcgU=
9193
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
@@ -245,8 +247,6 @@ golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3
245247
golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
246248
golang.org/x/tools v0.0.0-20190621195816-6e04913cbbac/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
247249
golang.org/x/tools v0.0.0-20190719005602-e377ae9d6386/go.mod h1:jcCCGcm9btYwXyDqrUWc6MKQKKGJCWEQ3AfLSRIbEuI=
248-
golang.org/x/tools v0.0.0-20190918220241-b8f1ca6a929d h1:509WdJuGomQvj505oUgekhS5hb41+tboOY87t2t1Vko=
249-
golang.org/x/tools v0.0.0-20190918220241-b8f1ca6a929d/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
250250
golang.org/x/tools v0.0.0-20190929041059-e7abfedfabcf h1:NvypsVlesF+lEDKVK5RNkww4fzArJXChZxNin79j05M=
251251
golang.org/x/tools v0.0.0-20190929041059-e7abfedfabcf/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
252252
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

internal/grpc/server.go

+7
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,20 @@ import (
1919
"google.golang.org/grpc/codes"
2020
"google.golang.org/grpc/status"
2121

22+
"github.com/grpc-ecosystem/go-grpc-prometheus"
23+
2224
v2 "github.com/envoyproxy/go-control-plane/envoy/api/v2"
2325
discovery "github.com/envoyproxy/go-control-plane/envoy/service/discovery/v2"
2426
loadstats "github.com/envoyproxy/go-control-plane/envoy/service/load_stats/v2"
2527
"github.com/sirupsen/logrus"
2628
)
2729

30+
var ServerMetrics = grpc_prometheus.NewServerMetrics()
31+
2832
// NewAPI returns a *grpc.Server which responds to the Envoy v2 xDS gRPC API.
2933
func NewAPI(log logrus.FieldLogger, resources map[string]Resource, opts ...grpc.ServerOption) *grpc.Server {
34+
opts = append(opts, grpc.StreamInterceptor(ServerMetrics.StreamServerInterceptor()),
35+
grpc.UnaryInterceptor(ServerMetrics.UnaryServerInterceptor()))
3036
g := grpc.NewServer(opts...)
3137
s := &grpcServer{
3238
xdsHandler{
@@ -40,6 +46,7 @@ func NewAPI(log logrus.FieldLogger, resources map[string]Resource, opts ...grpc.
4046
v2.RegisterListenerDiscoveryServiceServer(g, s)
4147
v2.RegisterRouteDiscoveryServiceServer(g, s)
4248
discovery.RegisterSecretDiscoveryServiceServer(g, s)
49+
ServerMetrics.InitializeMetrics(g)
4350
return g
4451
}
4552

0 commit comments

Comments
 (0)