Skip to content

Commit 8051afb

Browse files
committed
[baseserver] Add metric with version of the server, use in public api and usage
1 parent ac16572 commit 8051afb

File tree

7 files changed

+65
-5
lines changed

7 files changed

+65
-5
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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 baseserver
6+
7+
import (
8+
"fmt"
9+
"github.com/prometheus/client_golang/prometheus"
10+
)
11+
12+
var (
13+
serverVersionGauge = prometheus.NewGaugeVec(prometheus.GaugeOpts{
14+
Namespace: "gitpod",
15+
Subsystem: "server",
16+
Name: "version",
17+
Help: "Gauge of the current version of a gitpod server",
18+
}, []string{"version"})
19+
)
20+
21+
func registerMetrics(reg *prometheus.Registry) error {
22+
metrics := []prometheus.Collector{
23+
serverVersionGauge,
24+
}
25+
for _, metric := range metrics {
26+
err := reg.Register(metric)
27+
if err != nil {
28+
return fmt.Errorf("failed to register metric: %w", err)
29+
}
30+
}
31+
32+
return nil
33+
}
34+
35+
func reportServerVersion(version string) {
36+
serverVersionGauge.WithLabelValues(version).Set(1)
37+
}

components/common-go/baseserver/options.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ import (
1919
type options struct {
2020
logger *logrus.Entry
2121

22+
// version is the version of this application
23+
version string
24+
2225
config *Configuration
2326

2427
// closeTimeout is the amount we allow for the server to shut down cleanly
@@ -36,7 +39,8 @@ type options struct {
3639

3740
func defaultOptions() *options {
3841
return &options{
39-
logger: log.New(),
42+
logger: log.New(),
43+
version: "unknown",
4044
config: &Configuration{
4145
Services: ServicesConfiguration{
4246
GRPC: nil, // disabled by default
@@ -61,6 +65,13 @@ func WithConfig(config *Configuration) Option {
6165
}
6266
}
6367

68+
func WithVersion(version string) Option {
69+
return func(opts *options) error {
70+
opts.version = version
71+
return nil
72+
}
73+
}
74+
6475
func WithUnderTest() Option {
6576
return func(opts *options) error {
6677
opts.underTest = true

components/common-go/baseserver/options_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ func TestOptions(t *testing.T) {
3232
WithMetricsRegistry(registry),
3333
WithHealthHandler(health),
3434
WithGRPCHealthService(grpcHealthService),
35+
WithVersion("foo-bar"),
3536
}
3637
actual, err := evaluateOptions(defaultOptions(), opts...)
3738
require.NoError(t, err)
@@ -48,6 +49,7 @@ func TestOptions(t *testing.T) {
4849
metricsRegistry: registry,
4950
healthHandler: health,
5051
grpcHealthCheck: grpcHealthService,
52+
version: "foo-bar",
5153
}
5254

5355
require.Equal(t, expected, actual)

components/common-go/baseserver/server.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,13 @@ func (s *Server) initializeMetrics() error {
320320
return fmt.Errorf("failed to register process collectors: %w", err)
321321
}
322322

323+
err = registerMetrics(s.MetricsRegistry())
324+
if err != nil {
325+
return fmt.Errorf("failed to register baseserver metrics: %w", err)
326+
}
327+
328+
reportServerVersion(s.options.version)
329+
323330
return nil
324331
}
325332

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
"github.com/sirupsen/logrus"
2626
)
2727

28-
func Start(logger *logrus.Entry, cfg *config.Configuration) error {
28+
func Start(logger *logrus.Entry, version string, cfg *config.Configuration) error {
2929
logger.WithField("config", cfg).Info("Starting public-api.")
3030

3131
gitpodAPI, err := url.Parse(cfg.GitpodServiceURL)
@@ -39,6 +39,7 @@ func Start(logger *logrus.Entry, cfg *config.Configuration) error {
3939
baseserver.WithLogger(logger),
4040
baseserver.WithConfig(cfg.Server),
4141
baseserver.WithMetricsRegistry(registry),
42+
baseserver.WithVersion(version),
4243
)
4344
if err != nil {
4445
return fmt.Errorf("failed to initialize public api server: %w", err)

components/usage/cmd/run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func run() *cobra.Command {
3737
log.WithError(err).Fatal("Failed to get config. Did you specify --config correctly?")
3838
}
3939

40-
err = server.Start(cfg)
40+
err = server.Start(cfg, Version)
4141
if err != nil {
4242
log.WithError(err).Fatal("Failed to start usage server.")
4343
}

components/usage/pkg/server/server.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ type Config struct {
4040
DefaultSpendingLimit db.DefaultSpendingLimit `json:"defaultSpendingLimit"`
4141
}
4242

43-
func Start(cfg Config) error {
43+
func Start(cfg Config, version string) error {
4444
log.WithField("config", cfg).Info("Starting usage component.")
4545

4646
conn, err := db.Connect(db.ConnectionParams{
@@ -53,7 +53,9 @@ func Start(cfg Config) error {
5353
return fmt.Errorf("failed to establish database connection: %w", err)
5454
}
5555

56-
var serverOpts []baseserver.Option
56+
serverOpts := []baseserver.Option{
57+
baseserver.WithVersion(version),
58+
}
5759
if cfg.Server != nil {
5860
serverOpts = append(serverOpts, baseserver.WithConfig(cfg.Server))
5961
}

0 commit comments

Comments
 (0)