From 7960ff1adc87f5693b79c1d8b117e5895afef33c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20Naveiras?= Date: Thu, 15 Jun 2023 22:12:45 +0100 Subject: [PATCH] Add Prometheus build_info metrics ``` theatre_build_info{commit="none",goarch="arm64",goos="linux",goversion="go1.20.5",version="dev"} 1 ``` This is a good practice that should be in all our binaries --- Makefile | 2 +- apis/vault/v1alpha1/secretsinjector_webhook.go | 6 ------ cmd/helpers.go | 18 ++++++++++++++++++ cmd/rbac-manager/main.go | 4 ++++ cmd/vault-manager/main.go | 6 ++++++ cmd/workloads-manager/main.go | 4 ++++ 6 files changed, 33 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index 13c8cd9a..87f5adde 100644 --- a/Makefile +++ b/Makefile @@ -51,7 +51,7 @@ acceptance-prepare: install-tools go run cmd/acceptance/main.go prepare --verbose acceptance-destroy: install-tools - go run cmd/acceptance/main.go prepare --verbose + go run cmd/acceptance/main.go destroy generate: install-tools controller-gen object paths="./apis/rbac/..." diff --git a/apis/vault/v1alpha1/secretsinjector_webhook.go b/apis/vault/v1alpha1/secretsinjector_webhook.go index a961d12a..3201871e 100644 --- a/apis/vault/v1alpha1/secretsinjector_webhook.go +++ b/apis/vault/v1alpha1/secretsinjector_webhook.go @@ -15,7 +15,6 @@ import ( corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/resource" "sigs.k8s.io/controller-runtime/pkg/client" - "sigs.k8s.io/controller-runtime/pkg/metrics" "sigs.k8s.io/controller-runtime/pkg/webhook/admission" ) @@ -88,11 +87,6 @@ var ( ) ) -func init() { - // Register custom metrics with the global controller runtime prometheus registry - metrics.Registry.MustRegister(handleTotal, mutateTotal, skipTotal, errorsTotal) -} - func (i *SecretsInjector) Handle(ctx context.Context, req admission.Request) (resp admission.Response) { labels := prometheus.Labels{"pod_namespace": req.Namespace} logger := i.logger.WithValues("uuid", string(req.UID)) diff --git a/cmd/helpers.go b/cmd/helpers.go index e674fba1..230c694c 100644 --- a/cmd/helpers.go +++ b/cmd/helpers.go @@ -7,6 +7,7 @@ import ( "github.com/alecthomas/kingpin" "github.com/go-logr/logr" + "github.com/prometheus/client_golang/prometheus" zaplogfmt "github.com/sykesm/zap-logfmt" zapopt "go.uber.org/zap" "go.uber.org/zap/zapcore" @@ -77,3 +78,20 @@ func VersionStanza() string { Version, Commit, GoVersion, runtime.GOOS, runtime.GOARCH, Date, ) } + +var ( + BuildInfo = prometheus.NewGaugeFunc( + prometheus.GaugeOpts{ + Name: "theatre_build_info", + Help: "A metric with a constant '1' value labeled by version, commit, goversion from which %s was built, and the goos and goarch for the build.", + ConstLabels: prometheus.Labels{ + "version": Version, + "commit": Commit, + "goversion": GoVersion, + "goos": runtime.GOOS, + "goarch": runtime.GOARCH, + }, + }, + func() float64 { return 1 }, + ) +) diff --git a/cmd/rbac-manager/main.go b/cmd/rbac-manager/main.go index dbe9ad22..99fb82e9 100644 --- a/cmd/rbac-manager/main.go +++ b/cmd/rbac-manager/main.go @@ -14,6 +14,7 @@ import ( clientgoscheme "k8s.io/client-go/kubernetes/scheme" _ "k8s.io/client-go/plugin/pkg/client/auth/gcp" // this is required to auth against GCP ctrl "sigs.k8s.io/controller-runtime" + "sigs.k8s.io/controller-runtime/pkg/metrics" rbacv1alpha1 "github.com/gocardless/theatre/v3/apis/rbac/v1alpha1" "github.com/gocardless/theatre/v3/cmd" @@ -39,6 +40,9 @@ var ( func init() { _ = clientgoscheme.AddToScheme(scheme) _ = rbacv1alpha1.AddToScheme(scheme) + + // Register custom metrics with the global controller runtime prometheus registry + metrics.Registry.MustRegister(cmd.BuildInfo) } func main() { diff --git a/cmd/vault-manager/main.go b/cmd/vault-manager/main.go index d00885db..f86326e0 100644 --- a/cmd/vault-manager/main.go +++ b/cmd/vault-manager/main.go @@ -8,6 +8,7 @@ import ( _ "k8s.io/client-go/plugin/pkg/client/auth/gcp" // this is required to auth against GCP ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/metrics" "sigs.k8s.io/controller-runtime/pkg/webhook/admission" vaultv1alpha1 "github.com/gocardless/theatre/v3/apis/vault/v1alpha1" @@ -43,6 +44,11 @@ var ( serviceAccountTokenAudience = app.Flag("service-account-token-audience", "Audience for the projected service account token").String() ) +func init() { + // Register custom metrics with the global controller runtime prometheus registry + metrics.Registry.MustRegister(cmd.BuildInfo) +} + func main() { kingpin.MustParse(app.Parse(os.Args[1:])) logger := commonOpts.Logger() diff --git a/cmd/workloads-manager/main.go b/cmd/workloads-manager/main.go index 43150f02..b1ab4627 100644 --- a/cmd/workloads-manager/main.go +++ b/cmd/workloads-manager/main.go @@ -10,6 +10,7 @@ import ( clientgoscheme "k8s.io/client-go/kubernetes/scheme" _ "k8s.io/client-go/plugin/pkg/client/auth/gcp" // this is required to auth against GCP ctrl "sigs.k8s.io/controller-runtime" + "sigs.k8s.io/controller-runtime/pkg/metrics" "sigs.k8s.io/controller-runtime/pkg/webhook/admission" rbacv1alpha1 "github.com/gocardless/theatre/v3/apis/rbac/v1alpha1" @@ -39,6 +40,9 @@ func init() { _ = clientgoscheme.AddToScheme(scheme) _ = workloadsv1alpha1.AddToScheme(scheme) _ = rbacv1alpha1.AddToScheme(scheme) + + // Register custom metrics with the global controller runtime prometheus registry + metrics.Registry.MustRegister(cmd.BuildInfo) } func main() {