Skip to content

[baseserver] Initialize go metrics automatically #10050

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

Merged
merged 1 commit into from
May 17, 2022
Merged
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
34 changes: 27 additions & 7 deletions components/common-go/baseserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
grpc_logrus "github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus"
grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/sirupsen/logrus"
"golang.org/x/sync/errgroup"
Expand All @@ -43,6 +44,11 @@ func New(name string, opts ...Option) (*Server, error) {
server.httpMux = http.NewServeMux()
server.http = &http.Server{Handler: server.httpMux}

err = server.initializeMetrics()
if err != nil {
return nil, fmt.Errorf("failed to initialize metrics: %w", err)
}

err = server.initializeGRPC()
if err != nil {
return nil, fmt.Errorf("failed to initialize gRPC server: %w", err)
Expand Down Expand Up @@ -291,15 +297,18 @@ func (s *Server) initializeGRPC() error {
return nil
}

func httpAddress(cfg *ServerConfiguration, l net.Listener) string {
if l == nil {
return ""
func (s *Server) initializeMetrics() error {
err := s.MetricsRegistry().Register(collectors.NewGoCollector())
if err != nil {
return fmt.Errorf("faile to register go collectors: %w", err)
}
protocol := "http"
if cfg != nil && cfg.TLS != nil {
protocol = "https"

err = s.MetricsRegistry().Register(collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}))
if err != nil {
return fmt.Errorf("failed to register process collectors: %w", err)
}
return fmt.Sprintf("%s://%s", protocol, l.Addr().String())

return nil
}

func (s *Server) DebugAddress() string {
Expand Down Expand Up @@ -387,3 +396,14 @@ func (s *builtinServices) Close() error {
eg.Go(func() error { return s.Health.Close() })
return eg.Wait()
}

func httpAddress(cfg *ServerConfiguration, l net.Listener) string {
if l == nil {
return ""
}
protocol := "http"
if cfg != nil && cfg.TLS != nil {
protocol = "https"
}
return fmt.Sprintf("%s://%s", protocol, l.Addr().String())
}