Skip to content

[baseserver] Expose gRPC prometheus metrics #9879

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 10, 2022
Merged
Show file tree
Hide file tree
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
13 changes: 7 additions & 6 deletions components/common-go/baseserver/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@ type config struct {

func defaultConfig() *config {
return &config{
logger: log.New(),
hostname: "localhost",
httpPort: 9000,
grpcPort: 9001,
closeTimeout: 5 * time.Second,
healthHandler: healthcheck.NewHandler(),
logger: log.New(),
hostname: "localhost",
httpPort: 9000,
grpcPort: 9001,
closeTimeout: 5 * time.Second,
healthHandler: healthcheck.NewHandler(),
metricsRegistry: prometheus.NewRegistry(),
}
}

Expand Down
25 changes: 17 additions & 8 deletions components/common-go/baseserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
gitpod_grpc "github.com/gitpod-io/gitpod/common-go/grpc"
"github.com/gitpod-io/gitpod/common-go/pprof"
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/promhttp"
"github.com/sirupsen/logrus"
"google.golang.org/grpc"
Expand Down Expand Up @@ -165,6 +167,10 @@ func (s *Server) GRPC() *grpc.Server {
return s.grpc
}

func (s *Server) MetricsRegistry() *prometheus.Registry {
return s.cfg.metricsRegistry
}

func (s *Server) close(ctx context.Context) error {
if s.listening == nil {
return fmt.Errorf("server is not running, invalid close operation")
Expand Down Expand Up @@ -221,14 +227,9 @@ func (s *Server) newHTTPMux() *http.ServeMux {
mux.HandleFunc("/live", s.cfg.healthHandler.LiveEndpoint)
s.Logger().WithField("protocol", "http").Debug("Serving liveliness handler on /live")

// Metrics endpoint
metricsHandler := promhttp.Handler()
if s.cfg.metricsRegistry != nil {
metricsHandler = promhttp.InstrumentMetricHandler(
s.cfg.metricsRegistry, promhttp.HandlerFor(s.cfg.metricsRegistry, promhttp.HandlerOpts{}),
)
}
mux.Handle("/metrics", metricsHandler)
mux.Handle("/metrics", promhttp.InstrumentMetricHandler(
s.cfg.metricsRegistry, promhttp.HandlerFor(s.cfg.metricsRegistry, promhttp.HandlerOpts{}),
))
s.Logger().WithField("protocol", "http").Debug("Serving metrics on /metrics")

mux.Handle(pprof.Path, pprof.Handler())
Expand All @@ -240,11 +241,19 @@ func (s *Server) newHTTPMux() *http.ServeMux {
func (s *Server) initializeGRPC() error {
gitpod_grpc.SetupLogging()

grpcMetrics := grpc_prometheus.NewServerMetrics()
grpcMetrics.EnableHandlingTimeHistogram()
if err := s.MetricsRegistry().Register(grpcMetrics); err != nil {
return fmt.Errorf("failed to register grpc metrics: %w", err)
}

unary := []grpc.UnaryServerInterceptor{
grpc_logrus.UnaryServerInterceptor(s.Logger()),
grpcMetrics.UnaryServerInterceptor(),
}
stream := []grpc.StreamServerInterceptor{
grpc_logrus.StreamServerInterceptor(s.Logger()),
grpcMetrics.StreamServerInterceptor(),
}

s.grpc = grpc.NewServer(gitpod_grpc.ServerOptionsWithInterceptors(stream, unary)...)
Expand Down
45 changes: 45 additions & 0 deletions components/common-go/baseserver/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@
package baseserver_test

import (
"context"
"fmt"
"github.com/gitpod-io/gitpod/common-go/baseserver"
"github.com/gitpod-io/gitpod/common-go/pprof"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/testutil"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/health/grpc_health_v1"
"net/http"
"testing"
)
Expand Down Expand Up @@ -77,3 +82,43 @@ func TestServer_ServesPprof(t *testing.T) {
require.NoError(t, err)
require.Equalf(t, http.StatusOK, resp.StatusCode, "must serve pprof on %s", pprof.Path)
}

func TestServer_Metrics_gRPC(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this test and the comments 🔥

ctx := context.Background()
srv := baseserver.NewForTests(t)

// At this point, there must be metrics registry available for use
require.NotNil(t, srv.MetricsRegistry())

// To actually get gRPC metrics, we need to invoke an RPC, let's use a built-in health service as a mock
grpc_health_v1.RegisterHealthServer(srv.GRPC(), &HealthService{})

// Let's start our server up
baseserver.StartServerForTests(t, srv)

// We need a client to be able to invoke the RPC, let's construct one
conn, err := grpc.DialContext(ctx, srv.GRPCAddress(), grpc.WithTransportCredentials(insecure.NewCredentials()))
require.NoError(t, err)
client := grpc_health_v1.NewHealthClient(conn)

// Invoke the RPC
_, err = client.Check(ctx, &grpc_health_v1.HealthCheckRequest{})
require.NoError(t, err)

// Finally, we can assert that some metrics were produced.
registry := srv.MetricsRegistry()
// We expect at least the following. It's not the full set, but a good baseline to sanity check.
expected := []string{"grpc_server_handled_total", "grpc_server_handling_seconds", "grpc_server_started_total"}

count, err := testutil.GatherAndCount(registry, expected...)
require.NoError(t, err)
require.Equal(t, len(expected)*1, count, "expected 1 count for each metric")
}

type HealthService struct {
grpc_health_v1.UnimplementedHealthServer
}

func (h *HealthService) Check(_ context.Context, _ *grpc_health_v1.HealthCheckRequest) (*grpc_health_v1.HealthCheckResponse, error) {
return &grpc_health_v1.HealthCheckResponse{Status: grpc_health_v1.HealthCheckResponse_SERVING}, nil
}