grpc-ecosystem / go-grpc-prometheus

Prometheus monitoring for your gRPC Go servers.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support custom prometheus registerer

katexochen opened this issue · comments

I want to use a custom registry, currently using go-grpc-prometheus in v1.2.0.
In #37 , @brancz suggested the following:

r := prometheus.NewRegistry()
grpcMetrics := grpc_prometheus.NewServerMetrics()
gs := grpc.NewServer(
	grpc.StreamInterceptor(grpcMetrics.StreamServerInterceptor()),
	grpc.UnaryInterceptor(grpcMetrics.UnaryServerInterceptor()),
)

as := api.NewAPIServer()
pb.RegisterAPIServer(gs, as)

grpcMetrics.Register(r)
grpcMetrics.InitializeMetrics(as)

But for v1.2.0, ServerMetrics hasn't got a Register() function.
Is there another way to use a custom registry?

I found the answer to my question in an old commit: ServerMetrics implements Prometheus Collector interface. So you can simply do the following:

r := prometheus.NewRegistry()
grpcMetrics := grpc_prometheus.NewServerMetrics()
gs := grpc.NewServer(
	grpc.StreamInterceptor(grpcMetrics.StreamServerInterceptor()),
	grpc.UnaryInterceptor(grpcMetrics.UnaryServerInterceptor()),
)

as := api.NewAPIServer()
pb.RegisterAPIServer(gs, as)

r.Register(grpcMetrics)
grpcMetrics.InitializeMetrics(as)