Skip to content

[baseserver] Add metric with version of the server, use in public api and usage #13022

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions components/common-go/baseserver/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
// Licensed under the GNU Affero General Public License (AGPL).
// See License-AGPL.txt in the project root for license information.

package baseserver

import (
"fmt"
"github.com/prometheus/client_golang/prometheus"
)

var (
serverVersionGauge = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: "gitpod",
Subsystem: "server",
Name: "version",
Help: "Gauge of the current version of a gitpod server",
}, []string{"version"})
)

func registerMetrics(reg *prometheus.Registry) error {
metrics := []prometheus.Collector{
serverVersionGauge,
}
for _, metric := range metrics {
err := reg.Register(metric)
if err != nil {
return fmt.Errorf("failed to register metric: %w", err)
}
}

return nil
}

func reportServerVersion(version string) {
serverVersionGauge.WithLabelValues(version).Set(1)
}
13 changes: 12 additions & 1 deletion components/common-go/baseserver/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import (
type options struct {
logger *logrus.Entry

// version is the version of this application
version string

config *Configuration

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

func defaultOptions() *options {
return &options{
logger: log.New(),
logger: log.New(),
version: "unknown",
config: &Configuration{
Services: ServicesConfiguration{
GRPC: nil, // disabled by default
Expand All @@ -61,6 +65,13 @@ func WithConfig(config *Configuration) Option {
}
}

func WithVersion(version string) Option {
return func(opts *options) error {
opts.version = version
return nil
}
}

func WithUnderTest() Option {
return func(opts *options) error {
opts.underTest = true
Expand Down
2 changes: 2 additions & 0 deletions components/common-go/baseserver/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func TestOptions(t *testing.T) {
WithMetricsRegistry(registry),
WithHealthHandler(health),
WithGRPCHealthService(grpcHealthService),
WithVersion("foo-bar"),
}
actual, err := evaluateOptions(defaultOptions(), opts...)
require.NoError(t, err)
Expand All @@ -48,6 +49,7 @@ func TestOptions(t *testing.T) {
metricsRegistry: registry,
healthHandler: health,
grpcHealthCheck: grpcHealthService,
version: "foo-bar",
}

require.Equal(t, expected, actual)
Expand Down
7 changes: 7 additions & 0 deletions components/common-go/baseserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,13 @@ func (s *Server) initializeMetrics() error {
return fmt.Errorf("failed to register process collectors: %w", err)
}

err = registerMetrics(s.MetricsRegistry())
if err != nil {
return fmt.Errorf("failed to register baseserver metrics: %w", err)
}

reportServerVersion(s.options.version)

return nil
}

Expand Down
2 changes: 1 addition & 1 deletion components/public-api-server/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var runCommand = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
cfg := getConfig()

if err := server.Start(log.Log, cfg); err != nil {
if err := server.Start(log.Log, Version, cfg); err != nil {
log.WithError(err).Fatal("cannot start server")
}
},
Expand Down
3 changes: 2 additions & 1 deletion components/public-api-server/pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"github.com/sirupsen/logrus"
)

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

gitpodAPI, err := url.Parse(cfg.GitpodServiceURL)
Expand All @@ -39,6 +39,7 @@ func Start(logger *logrus.Entry, cfg *config.Configuration) error {
baseserver.WithLogger(logger),
baseserver.WithConfig(cfg.Server),
baseserver.WithMetricsRegistry(registry),
baseserver.WithVersion(version),
)
if err != nil {
return fmt.Errorf("failed to initialize public api server: %w", err)
Expand Down
2 changes: 1 addition & 1 deletion components/usage/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func run() *cobra.Command {
log.WithError(err).Fatal("Failed to get config. Did you specify --config correctly?")
}

err = server.Start(cfg)
err = server.Start(cfg, Version)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that this Version is set during build, but how and where it is set and what is the format of the version string?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've observed either commit-<hash> or main.<build-id>

It's set by the CI job when it builds the images

if err != nil {
log.WithError(err).Fatal("Failed to start usage server.")
}
Expand Down
6 changes: 4 additions & 2 deletions components/usage/pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type Config struct {
DefaultSpendingLimit db.DefaultSpendingLimit `json:"defaultSpendingLimit"`
}

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

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

var serverOpts []baseserver.Option
serverOpts := []baseserver.Option{
baseserver.WithVersion(version),
}
if cfg.Server != nil {
serverOpts = append(serverOpts, baseserver.WithConfig(cfg.Server))
}
Expand Down