A complete example to expose /metrics
huichen opened this issue · comments
Can you provide a complete example to expose /metrics in grpc server? Can't find any in the codebase.
For a more comprehensive guide on how to use golang Prometheus library, see:
https://github.com/prometheus/client_golang
Can you provide a better example? How does gRPC and http /metrics get served? The example doesn't make sense when you are serving like this:
s := grpc.NewServer(
grpc.StreamInterceptor(grpc_prometheus.StreamServerInterceptor),
grpc.UnaryInterceptor(grpc_prometheus.UnaryServerInterceptor),
)
myservice.RegisterMyServiceServer(s, &myServiceImpl{})
grpc_prometheus.Register(s)
http.Handle("/metrics", prometheus.Handler())
if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
I have exactly the same question here. Any good practice to serve on both gRPC and http? @mwitkow
@prestonvanloon, I think the thing you're lacking here is a separate goroutine with http.ListenAndServe
. Unfortunately, the grpc.Server.Serve
is not an HTTP server by itself, so you need to start a gRPC server and an HTTP server separately.
@HTChang in terms of best practices of serving both gRPC and HTTP.. that's a little bit beyond the scope of this repo in particular. Maybe worthwhile bringing it up in upstream? https://github.com/grpc/grpc-go
@mwitkow It looks like they have a method to serveHTTP but it has very poor performance and is mostly undocumented.
grpc/grpc-go#549
One approach is to have the http on a separate goroutine and port, like you suggested, but I'm not sure how that would impact discovery or reporting.
@prestonvanloon Can you share the code? How to use serveHTTP
?