Document how to register Server or change init() to register the same way
cstockton opened this issue · comments
I needed to change the namespace so I checked here first and found #29. It took a few minutes to figure out because all the metrics are private on *Server and the init() registers them explicitly, and my grep found no other calls to MustRegister for a *Server type. I realized after digging into *Server that it was itself a collector so registering that worked.
To give this hint to users I propose changing:
func init() {
prom.MustRegister(DefaultServerMetrics.serverStartedCounter)
prom.MustRegister(DefaultServerMetrics.serverHandledCounter)
prom.MustRegister(DefaultServerMetrics.serverStreamMsgReceived)
prom.MustRegister(DefaultServerMetrics.serverStreamMsgSent)
}
To:
func init() {
prom.MustRegister(DefaultServerMetrics)
}
Or adding a basic custom namespace example in the README if there is a technical reason the above would be incorrect:
myCustomMetrics := grpcprom.NewServerMetrics(
grpcprom.CounterOption(func(o *prometheus.CounterOpts) {
o.Namespace = "my_custom_namespace"
}),
)
prom.MustRegister(myCustomMetrics)
srv := grpc.NewServer(...)
myCustomMetrics.InitializeMetrics(srv)
I think we should generally recommend not to use the globals/init, so the latter sounds good to me.