Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore: prometheus metrics for grpc apis (#15914)

Danny Xu46f8e43f48f6
diff --git a/backend/server/server.go b/backend/server/server.go
index 01fea3ad0a..77eb6a6155 100644
--- a/backend/server/server.go
+++ b/backend/server/server.go
@@ -11,9 +11,11 @@ import (
"sync"
"time"
+ grpcprom "github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus"
grpcruntime "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/labstack/echo/v4"
"github.com/pkg/errors"
+ "github.com/prometheus/client_golang/prometheus"
"github.com/soheilhy/cmux"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
@@ -259,12 +261,14 @@ func NewServer(ctx context.Context, profile *config.Profile) (*Server, error) {
recoveryUnaryInterceptor := recovery.UnaryServerInterceptor(recovery.WithRecoveryHandler(onPanic))
recoveryStreamInterceptor := recovery.StreamServerInterceptor(recovery.WithRecoveryHandler(onPanic))
grpc.EnableTracing = true
+ srvMetrics := grpcprom.NewServerMetrics(grpcprom.WithServerHandlingTimeHistogram())
s.grpcServer = grpc.NewServer(
// Override the maximum receiving message size to 100M for uploading large sheets.
grpc.MaxRecvMsgSize(100*1024*1024),
grpc.InitialWindowSize(100000000),
grpc.InitialConnWindowSize(100000000),
grpc.ChainUnaryInterceptor(
+ srvMetrics.UnaryServerInterceptor(),
debugProvider.DebugInterceptor,
authProvider.AuthenticationInterceptor,
aclProvider.ACLInterceptor,
@@ -272,6 +276,7 @@ func NewServer(ctx context.Context, profile *config.Profile) (*Server, error) {
recoveryUnaryInterceptor,
),
grpc.ChainStreamInterceptor(
+ srvMetrics.StreamServerInterceptor(),
debugProvider.DebugStreamInterceptor,
authProvider.AuthenticationStreamInterceptor,
aclProvider.ACLStreamInterceptor,
@@ -306,6 +311,14 @@ func NewServer(ctx context.Context, profile *config.Profile) (*Server, error) {
// Configure echo server routes.
configureEchoRouters(s.echoServer, s.grpcServer, s.lspServer, directorySyncServer, mux, profile)
+ // Configure grpc prometheus metrics.
+ if err := prometheus.DefaultRegisterer.Register(srvMetrics); err != nil {
+ if _, ok := err.(prometheus.AlreadyRegisteredError); !ok {
+ return nil, err
+ }
+ }
+ srvMetrics.InitializeMetrics(s.grpcServer)
+
serverStarted = true
return s, nil
}
diff --git a/go.mod b/go.mod
index dee72714c3..244e7054a5 100644
--- a/go.mod
+++ b/go.mod
@@ -53,6 +53,7 @@ require (
github.com/google/uuid v1.6.0
github.com/gorilla/websocket v1.5.3
github.com/gosimple/slug v1.15.0
+ github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus v1.0.1
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.3.1
github.com/grpc-ecosystem/grpc-gateway/v2 v2.26.3
github.com/hashicorp/golang-lru/v2 v2.0.7
@@ -338,7 +339,7 @@ require (
github.com/power-devops/perfstat v0.0.0-20221212215047-62379fc7944b // indirect
github.com/pquerna/cachecontrol v0.2.0 // indirect
github.com/pquerna/otp v1.4.0
- github.com/prometheus/client_golang v1.21.1 // indirect
+ github.com/prometheus/client_golang v1.21.1
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.63.0 // indirect
github.com/prometheus/procfs v0.16.0 // indirect
diff --git a/go.sum b/go.sum
index 7a904c23c0..c957ccd3c9 100644
--- a/go.sum
+++ b/go.sum
@@ -1340,6 +1340,8 @@ github.com/grpc-ecosystem/go-grpc-middleware v1.0.1-0.20190118093823-f849b5445de
github.com/grpc-ecosystem/go-grpc-middleware v1.2.2/go.mod h1:EaizFBKfUKtMIF5iaDEhniwNedqGo9FuLFzppDr3uwI=
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 h1:UH//fgunKIs4JdUbpDl1VZCDaL56wXCB/5+wF6uHfaI=
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0/go.mod h1:g5qyo/la0ALbONm6Vbp88Yd8NsDy6rZz+RcrMPxvld8=
+github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus v1.0.1 h1:qnpSQwGEnkcRpTqNOIR6bJbR0gAorgP9CSALpRcKoAA=
+github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus v1.0.1/go.mod h1:lXGCsh6c22WGtjr+qGHj1otzZpV/1kwTMAqkwZsnWRU=
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.3.1 h1:KcFzXwzM/kGhIRHvc8jdixfIJjVzuUJdnv+5xsPutog=
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.3.1/go.mod h1:qOchhhIlmRcqk/O9uCo/puJlyo07YINaIqdZfZG3Jkc=
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=