Skip to content

Commit 043b453

Browse files
easyCZroboquat
authored andcommitted
[baseserver] Add prometheus metrics
1 parent bbfb5a9 commit 043b453

File tree

6 files changed

+443
-7
lines changed

6 files changed

+443
-7
lines changed

components/common-go/baseserver/options.go

+16-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package baseserver
77
import (
88
"fmt"
99
"github.com/gitpod-io/gitpod/common-go/log"
10+
"github.com/prometheus/client_golang/prometheus"
1011
"github.com/sirupsen/logrus"
1112
"time"
1213
)
@@ -23,6 +24,9 @@ type config struct {
2324

2425
// closeTimeout is the amount we allow for the server to shut down cleanly
2526
closeTimeout time.Duration
27+
28+
// metricsRegistry configures the metrics registry to use for exporting metrics. When not set, the default prometheus registry is used.
29+
metricsRegistry *prometheus.Registry
2630
}
2731

2832
func defaultConfig() *config {
@@ -46,7 +50,7 @@ func WithHostname(hostname string) Option {
4650

4751
func WithHTTPPort(port int) Option {
4852
return func(cfg *config) error {
49-
if port <= 0 {
53+
if port < 0 {
5054
return fmt.Errorf("http port must be greater than 0, got: %d", port)
5155
}
5256

@@ -84,6 +88,17 @@ func WithCloseTimeout(d time.Duration) Option {
8488
}
8589
}
8690

91+
func WithMetricsRegistry(r *prometheus.Registry) Option {
92+
return func(cfg *config) error {
93+
if r == nil {
94+
return fmt.Errorf("nil prometheus registry received")
95+
}
96+
97+
cfg.metricsRegistry = r
98+
return nil
99+
}
100+
}
101+
87102
func evaluateOptions(cfg *config, opts ...Option) (*config, error) {
88103
for _, opt := range opts {
89104
if err := opt(cfg); err != nil {

components/common-go/baseserver/options_test.go

+9-5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package baseserver
66

77
import (
88
"github.com/gitpod-io/gitpod/common-go/log"
9+
"github.com/prometheus/client_golang/prometheus"
910
"github.com/stretchr/testify/require"
1011
"testing"
1112
"time"
@@ -17,23 +18,26 @@ func TestOptions(t *testing.T) {
1718
grpcPort := 8081
1819
timeout := 10 * time.Second
1920
hostname := "another_hostname"
21+
registry := prometheus.NewRegistry()
2022

2123
var opts = []Option{
2224
WithHostname(hostname),
2325
WithHTTPPort(httpPort),
2426
WithGRPCPort(grpcPort),
2527
WithLogger(logger),
2628
WithCloseTimeout(timeout),
29+
WithMetricsRegistry(registry),
2730
}
2831
cfg, err := evaluateOptions(defaultConfig(), opts...)
2932
require.NoError(t, err)
3033

3134
require.Equal(t, &config{
32-
logger: logger,
33-
hostname: hostname,
34-
grpcPort: grpcPort,
35-
httpPort: httpPort,
36-
closeTimeout: timeout,
35+
logger: logger,
36+
hostname: hostname,
37+
grpcPort: grpcPort,
38+
httpPort: httpPort,
39+
closeTimeout: timeout,
40+
metricsRegistry: registry,
3741
}, cfg)
3842
}
3943

components/common-go/baseserver/server.go

+10
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package baseserver
77
import (
88
"context"
99
"fmt"
10+
"github.com/prometheus/client_golang/prometheus/promhttp"
1011
"github.com/sirupsen/logrus"
1112
"google.golang.org/grpc"
1213
"net"
@@ -201,6 +202,15 @@ func (s *Server) newHTTPMux() *http.ServeMux {
201202
_, _ = w.Write([]byte(`ready`))
202203
})
203204

205+
// Metrics endpoint
206+
metricsHandler := promhttp.Handler()
207+
if s.cfg.metricsRegistry != nil {
208+
metricsHandler = promhttp.InstrumentMetricHandler(
209+
s.cfg.metricsRegistry, promhttp.HandlerFor(s.cfg.metricsRegistry, promhttp.HandlerOpts{}),
210+
)
211+
}
212+
mux.Handle("/metrics", metricsHandler)
213+
204214
return mux
205215
}
206216

components/common-go/baseserver/server_test.go

+34
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package baseserver_test
77
import (
88
"fmt"
99
"github.com/gitpod-io/gitpod/common-go/baseserver"
10+
"github.com/prometheus/client_golang/prometheus"
1011
"github.com/stretchr/testify/require"
1112
"net/http"
1213
"testing"
@@ -40,3 +41,36 @@ func TestServer_ServesReady(t *testing.T) {
4041
require.NoError(t, err)
4142
require.Equal(t, http.StatusOK, resp.StatusCode)
4243
}
44+
45+
func TestServer_ServesMetricsEndpointWithDefaultConfig(t *testing.T) {
46+
srv := baseserver.NewForTests(t)
47+
48+
go func(t *testing.T) {
49+
require.NoError(t, srv.ListenAndServe())
50+
}(t)
51+
52+
baseserver.WaitForServerToBeReachable(t, srv, 3*time.Second)
53+
54+
readyUR := fmt.Sprintf("%s/metrics", srv.HTTPAddress())
55+
resp, err := http.Get(readyUR)
56+
require.NoError(t, err)
57+
require.Equal(t, http.StatusOK, resp.StatusCode)
58+
}
59+
60+
func TestServer_ServesMetricsEndpointWithCustomMetricsConfig(t *testing.T) {
61+
registry := prometheus.NewRegistry()
62+
srv := baseserver.NewForTests(t,
63+
baseserver.WithMetricsRegistry(registry),
64+
)
65+
66+
go func(t *testing.T) {
67+
require.NoError(t, srv.ListenAndServe())
68+
}(t)
69+
70+
baseserver.WaitForServerToBeReachable(t, srv, 3*time.Second)
71+
72+
readyUR := fmt.Sprintf("%s/metrics", srv.HTTPAddress())
73+
resp, err := http.Get(readyUR)
74+
require.NoError(t, err)
75+
require.Equal(t, http.StatusOK, resp.StatusCode)
76+
}

components/public-api-server/go.mod

+7
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,16 @@ go 1.17
55
require github.com/gitpod-io/gitpod/common-go v0.0.0-00010101000000-000000000000
66

77
require (
8+
github.com/beorn7/perks v1.0.1 // indirect
9+
github.com/cespare/xxhash/v2 v2.1.2 // indirect
810
github.com/davecgh/go-spew v1.1.1 // indirect
911
github.com/golang/protobuf v1.5.2 // indirect
12+
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
1013
github.com/pmezard/go-difflib v1.0.0 // indirect
14+
github.com/prometheus/client_golang v1.12.1 // indirect
15+
github.com/prometheus/client_model v0.2.0 // indirect
16+
github.com/prometheus/common v0.32.1 // indirect
17+
github.com/prometheus/procfs v0.7.3 // indirect
1118
github.com/sirupsen/logrus v1.8.1 // indirect
1219
github.com/stretchr/testify v1.7.0 // indirect
1320
golang.org/x/net v0.0.0-20211209124913-491a49abca63 // indirect

0 commit comments

Comments
 (0)