Skip to content

Commit abf0e1e

Browse files
committed
[public-api] Cleanup registration of metrics
1 parent 262cba9 commit abf0e1e

File tree

6 files changed

+89
-59
lines changed

6 files changed

+89
-59
lines changed

components/public-api-server/pkg/proxy/conn.go

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,32 @@ type ServerConnectionPool interface {
1919
Get(ctx context.Context, token string) (gitpod.APIInterface, error)
2020
}
2121

22-
// NoConnectionPool is a simple version of the ServerConnectionPool which always creates a new connection.
23-
type NoConnectionPool struct {
22+
type connectionConstructor func(endpoint string, opts gitpod.ConnectToServerOpts) (gitpod.APIInterface, error)
23+
24+
func NewAlwaysNewConnectionPool(address *url.URL) (*AlwaysNewConnectionPool, error) {
25+
return &AlwaysNewConnectionPool{
26+
ServerAPI: address,
27+
constructor: func(endpoint string, opts gitpod.ConnectToServerOpts) (gitpod.APIInterface, error) {
28+
return gitpod.ConnectToServer(endpoint, opts)
29+
},
30+
}, nil
31+
}
32+
33+
// AlwaysNewConnectionPool is a simple version of the ServerConnectionPool which always creates a new connection.
34+
type AlwaysNewConnectionPool struct {
2435
ServerAPI *url.URL
36+
37+
constructor connectionConstructor
2538
}
2639

27-
func (p *NoConnectionPool) Get(ctx context.Context, token string) (gitpod.APIInterface, error) {
40+
func (p *AlwaysNewConnectionPool) Get(ctx context.Context, token string) (gitpod.APIInterface, error) {
2841
logger := ctxlogrus.Extract(ctx)
2942

3043
start := time.Now()
3144
defer func() {
3245
reportConnectionDuration(time.Since(start))
3346
}()
34-
server, err := gitpod.ConnectToServer(p.ServerAPI.String(), gitpod.ConnectToServerOpts{
47+
server, err := p.constructor(p.ServerAPI.String(), gitpod.ConnectToServerOpts{
3548
Context: ctx,
3649
Token: token,
3750
Log: logger,
@@ -42,3 +55,20 @@ func (p *NoConnectionPool) Get(ctx context.Context, token string) (gitpod.APIInt
4255

4356
return server, nil
4457
}
58+
59+
func WrapConnectionPoolWithMetrics(pool ServerConnectionPool) ServerConnectionPool {
60+
return &connectionPoolWithMetrics{pool: pool}
61+
}
62+
63+
type connectionPoolWithMetrics struct {
64+
pool ServerConnectionPool
65+
}
66+
67+
func (c *connectionPoolWithMetrics) Get(ctx context.Context, token string) (gitpod.APIInterface, error) {
68+
start := time.Now()
69+
defer func() {
70+
reportConnectionDuration(time.Since(start))
71+
}()
72+
73+
return c.pool.Get(ctx, token)
74+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2+
// Licensed under the GNU Affero General Public License (AGPL).
3+
// See License-AGPL.txt in the project root for license information.
4+
5+
package proxy
6+
7+
import (
8+
"context"
9+
"errors"
10+
gitpod "github.com/gitpod-io/gitpod/gitpod-protocol"
11+
"github.com/prometheus/client_golang/prometheus"
12+
"github.com/prometheus/client_golang/prometheus/testutil"
13+
"github.com/stretchr/testify/require"
14+
"net/url"
15+
"testing"
16+
)
17+
18+
func TestConnectionPoolWithMetrics(t *testing.T) {
19+
registry := prometheus.NewRegistry()
20+
RegisterMetrics(registry)
21+
22+
address, err := url.Parse("http://gitpod.test.address.io")
23+
require.NoError(t, err)
24+
25+
_, err = WrapConnectionPoolWithMetrics(&AlwaysNewConnectionPool{
26+
ServerAPI: address,
27+
constructor: func(endpoint string, opts gitpod.ConnectToServerOpts) (gitpod.APIInterface, error) {
28+
// We don't actually need the connection, so just returning an error
29+
return nil, errors.New("failed to create connection pool")
30+
},
31+
}).Get(context.Background(), "some-token")
32+
require.Error(t, err)
33+
34+
count, err := testutil.GatherAndCount(registry, "gitpod_public_api_proxy_connection_create_duration_seconds")
35+
require.NoError(t, err)
36+
require.Equal(t, 1, count)
37+
38+
}
39+
40+
type MockConnectionPool struct {
41+
api gitpod.APIInterface
42+
}
43+
44+
func (m *MockConnectionPool) Get(ctx context.Context, token string) (gitpod.APIInterface, error) {
45+
return m.api, nil
46+
}

components/public-api-server/pkg/proxy/prometheusmetrics_test.go

Lines changed: 0 additions & 42 deletions
This file was deleted.

components/public-api-server/pkg/server/integration_test.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"context"
99
"github.com/gitpod-io/gitpod/common-go/baseserver"
1010
v1 "github.com/gitpod-io/gitpod/public-api/v1"
11-
"github.com/prometheus/client_golang/prometheus"
1211
"github.com/stretchr/testify/require"
1312
"google.golang.org/grpc"
1413
"google.golang.org/grpc/codes"
@@ -22,12 +21,11 @@ import (
2221
func TestPublicAPIServer_v1_WorkspaceService(t *testing.T) {
2322
ctx := metadata.AppendToOutgoingContext(context.Background(), "authorization", "some-token")
2423
srv := baseserver.NewForTests(t)
25-
registry := prometheus.NewRegistry()
2624

2725
gitpodAPI, err := url.Parse("wss://main.preview.gitpod-dev.com/api/v1")
2826
require.NoError(t, err)
2927

30-
require.NoError(t, register(srv, Config{GitpodAPI: gitpodAPI}, registry))
28+
require.NoError(t, register(srv, Config{GitpodAPI: gitpodAPI}))
3129
baseserver.StartServerForTests(t, srv)
3230

3331
conn, err := grpc.Dial(srv.GRPCAddress(), grpc.WithTransportCredentials(insecure.NewCredentials()))
@@ -69,12 +67,11 @@ func TestPublicAPIServer_v1_WorkspaceService(t *testing.T) {
6967
func TestPublicAPIServer_v1_PrebuildService(t *testing.T) {
7068
ctx := context.Background()
7169
srv := baseserver.NewForTests(t)
72-
registry := prometheus.NewRegistry()
7370

7471
gitpodAPI, err := url.Parse("wss://main.preview.gitpod-dev.com/api/v1")
7572
require.NoError(t, err)
7673

77-
require.NoError(t, register(srv, Config{GitpodAPI: gitpodAPI}, registry))
74+
require.NoError(t, register(srv, Config{GitpodAPI: gitpodAPI}))
7875

7976
baseserver.StartServerForTests(t, srv)
8077

components/public-api-server/pkg/server/server.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,21 @@ import (
1212
"github.com/gitpod-io/gitpod/public-api-server/pkg/apiv1"
1313
"github.com/gitpod-io/gitpod/public-api-server/pkg/proxy"
1414
v1 "github.com/gitpod-io/gitpod/public-api/v1"
15-
"github.com/prometheus/client_golang/prometheus"
1615
"github.com/sirupsen/logrus"
1716
"net/http"
1817
)
1918

2019
func Start(logger *logrus.Entry, cfg Config) error {
21-
registry := prometheus.NewRegistry()
22-
2320
srv, err := baseserver.New("public_api_server",
2421
baseserver.WithLogger(logger),
2522
baseserver.WithHTTPPort(cfg.HTTPPort),
2623
baseserver.WithGRPCPort(cfg.GRPCPort),
27-
baseserver.WithMetricsRegistry(registry),
2824
)
2925
if err != nil {
3026
return fmt.Errorf("failed to initialize public api server: %w", err)
3127
}
3228

33-
if registerErr := register(srv, cfg, registry); registerErr != nil {
29+
if registerErr := register(srv, cfg); registerErr != nil {
3430
return fmt.Errorf("failed to register services: %w", registerErr)
3531
}
3632

@@ -41,14 +37,17 @@ func Start(logger *logrus.Entry, cfg Config) error {
4137
return nil
4238
}
4339

44-
func register(srv *baseserver.Server, cfg Config, registry *prometheus.Registry) error {
45-
proxy.RegisterMetrics(registry)
40+
func register(srv *baseserver.Server, cfg Config) error {
41+
proxy.RegisterMetrics(srv.MetricsRegistry())
4642

4743
logger := log.New()
4844
m := middleware.NewLoggingMiddleware(logger)
4945
srv.HTTPMux().Handle("/", m(http.HandlerFunc(HelloWorldHandler)))
5046

51-
connPool := &proxy.NoConnectionPool{ServerAPI: cfg.GitpodAPI}
47+
connPool, err := proxy.NewAlwaysNewConnectionPool(cfg.GitpodAPI)
48+
if err != nil {
49+
return fmt.Errorf("failed to create connection pool: %w", err)
50+
}
5251

5352
v1.RegisterWorkspacesServiceServer(srv.GRPC(), apiv1.NewWorkspaceService(connPool))
5453
v1.RegisterPrebuildsServiceServer(srv.GRPC(), v1.UnimplementedPrebuildsServiceServer{})

0 commit comments

Comments
 (0)