@@ -10,6 +10,8 @@ import (
10
10
gitpod_grpc "github.com/gitpod-io/gitpod/common-go/grpc"
11
11
"github.com/gitpod-io/gitpod/common-go/pprof"
12
12
grpc_logrus "github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus"
13
+ grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
14
+ "github.com/prometheus/client_golang/prometheus"
13
15
"github.com/prometheus/client_golang/prometheus/promhttp"
14
16
"github.com/sirupsen/logrus"
15
17
"google.golang.org/grpc"
@@ -165,6 +167,10 @@ func (s *Server) GRPC() *grpc.Server {
165
167
return s .grpc
166
168
}
167
169
170
+ func (s * Server ) MetricsRegistry () * prometheus.Registry {
171
+ return s .cfg .metricsRegistry
172
+ }
173
+
168
174
func (s * Server ) close (ctx context.Context ) error {
169
175
if s .listening == nil {
170
176
return fmt .Errorf ("server is not running, invalid close operation" )
@@ -221,14 +227,9 @@ func (s *Server) newHTTPMux() *http.ServeMux {
221
227
mux .HandleFunc ("/live" , s .cfg .healthHandler .LiveEndpoint )
222
228
s .Logger ().WithField ("protocol" , "http" ).Debug ("Serving liveliness handler on /live" )
223
229
224
- // Metrics endpoint
225
- metricsHandler := promhttp .Handler ()
226
- if s .cfg .metricsRegistry != nil {
227
- metricsHandler = promhttp .InstrumentMetricHandler (
228
- s .cfg .metricsRegistry , promhttp .HandlerFor (s .cfg .metricsRegistry , promhttp.HandlerOpts {}),
229
- )
230
- }
231
- mux .Handle ("/metrics" , metricsHandler )
230
+ mux .Handle ("/metrics" , promhttp .InstrumentMetricHandler (
231
+ s .cfg .metricsRegistry , promhttp .HandlerFor (s .cfg .metricsRegistry , promhttp.HandlerOpts {}),
232
+ ))
232
233
s .Logger ().WithField ("protocol" , "http" ).Debug ("Serving metrics on /metrics" )
233
234
234
235
mux .Handle (pprof .Path , pprof .Handler ())
@@ -240,11 +241,19 @@ func (s *Server) newHTTPMux() *http.ServeMux {
240
241
func (s * Server ) initializeGRPC () error {
241
242
gitpod_grpc .SetupLogging ()
242
243
244
+ grpcMetrics := grpc_prometheus .NewServerMetrics ()
245
+ grpcMetrics .EnableHandlingTimeHistogram ()
246
+ if err := s .MetricsRegistry ().Register (grpcMetrics ); err != nil {
247
+ return fmt .Errorf ("failed to register grpc metrics: %w" , err )
248
+ }
249
+
243
250
unary := []grpc.UnaryServerInterceptor {
244
251
grpc_logrus .UnaryServerInterceptor (s .Logger ()),
252
+ grpcMetrics .UnaryServerInterceptor (),
245
253
}
246
254
stream := []grpc.StreamServerInterceptor {
247
255
grpc_logrus .StreamServerInterceptor (s .Logger ()),
256
+ grpcMetrics .StreamServerInterceptor (),
248
257
}
249
258
250
259
s .grpc = grpc .NewServer (gitpod_grpc .ServerOptionsWithInterceptors (stream , unary )... )
0 commit comments