From 27e38e96ea330d05d18bd3170a1904b630e36715 Mon Sep 17 00:00:00 2001 From: "Benjamin A. Petersen" Date: Tue, 10 Dec 2019 09:59:08 -0500 Subject: [PATCH] Ensure route.host has a value before tracking console_url metric to a "" --- pkg/console/metrics/console_url.go | 47 ++++++++++++++++++++++++++++++ pkg/console/metrics/register.go | 31 ++++++++++++++++++++ pkg/console/operator/sync_v400.go | 15 +++------- 3 files changed, 82 insertions(+), 11 deletions(-) create mode 100644 pkg/console/metrics/console_url.go create mode 100644 pkg/console/metrics/register.go diff --git a/pkg/console/metrics/console_url.go b/pkg/console/metrics/console_url.go new file mode 100644 index 000000000..6e6dc1a2f --- /dev/null +++ b/pkg/console/metrics/console_url.go @@ -0,0 +1,47 @@ +package metrics + +import "k8s.io/klog" + +func HandleConsoleURL(oldURL, newURL string) { + // if neither have been set, there is nothing to update + if noHost(oldURL, newURL) { + klog.V(4).Infof("metric console_url has no host") + return + } + + // only a new URL + if isNewHost(oldURL, newURL) { + klog.V(4).Infof("metric console_url new host: %s %s", oldURL, newURL) + singleton.ConsoleURL.WithLabelValues(newURL).Set(1) + return + } + + // route or ingress update + if isHostTransition(oldURL, newURL) { + klog.V(4).Infof("metric console_url host transition: %s to %s", oldURL, newURL) + singleton.ConsoleURL.WithLabelValues(oldURL).Set(0) + singleton.ConsoleURL.WithLabelValues(newURL).Set(1) + return + } + + // something went wrong and we no longer have a route or ingress with a host + if hostDied(oldURL, newURL) { + klog.V(4).Infof("metric console_url host lost: %s %s", oldURL, newURL) + singleton.ConsoleURL.WithLabelValues(oldURL).Set(0) + return + } + klog.Error("metric console_url unhandled") +} + +func noHost(old, new string) bool { + return len(old) == 0 && len(new) == 0 +} +func isNewHost(old, new string) bool { + return len(old) == 0 && len(new) != 0 +} +func isHostTransition(old, new string) bool { + return len(old) != 0 && len(new) != 0 +} +func hostDied(old, new string) bool { + return len(old) != 0 && len(new) == 0 +} diff --git a/pkg/console/metrics/register.go b/pkg/console/metrics/register.go new file mode 100644 index 000000000..6f8670117 --- /dev/null +++ b/pkg/console/metrics/register.go @@ -0,0 +1,31 @@ +package metrics + +import ( + "sync" + + "github.com/prometheus/client_golang/prometheus" +) + +type ConsoleMetrics struct { + ConsoleURL *prometheus.GaugeVec +} + +var singleton *ConsoleMetrics +var once sync.Once + +func Register() *ConsoleMetrics { + // thread safe + once.Do(func() { + singleton = &ConsoleMetrics{} + + // metric: console_url{url="https://"} 1 + singleton.ConsoleURL = prometheus.NewGaugeVec(prometheus.GaugeOpts{ + Name: "console_url", + Help: "URL of the console exposed on the cluster", + // one label + }, []string{"url"}) + + prometheus.MustRegister(singleton.ConsoleURL) + }) + return singleton +} diff --git a/pkg/console/operator/sync_v400.go b/pkg/console/operator/sync_v400.go index c93724aef..5a7affdff 100644 --- a/pkg/console/operator/sync_v400.go +++ b/pkg/console/operator/sync_v400.go @@ -5,6 +5,8 @@ import ( "fmt" "os" + "github.com/openshift/console-operator/pkg/console/metrics" + "github.com/prometheus/client_golang/prometheus" // kube @@ -212,22 +214,13 @@ func (co *consoleOperator) sync_v400(updatedOperatorConfig *operatorv1.Console, } func (co *consoleOperator) SyncConsoleConfig(consoleConfig *configv1.Console, consoleURL string) (*configv1.Console, error) { + oldURL := consoleConfig.Status.ConsoleURL updated := consoleConfig.DeepCopy() - - // track the URL state in prometheus before we update it - if consoleConfig.Status.ConsoleURL != consoleURL { - // not using this URL anymore - consoleURLMetric.WithLabelValues(consoleConfig.Status.ConsoleURL).Set(0) - } - if len(consoleURL) != 0 { - // only update to new if we have a url - consoleURLMetric.WithLabelValues(consoleURL).Set(1) - } - if updated.Status.ConsoleURL != consoleURL { klog.V(4).Infof("updating console.config.openshift.io with url: %v", consoleURL) updated.Status.ConsoleURL = consoleURL } + metrics.HandleConsoleURL(oldURL, consoleURL) return co.consoleConfigClient.UpdateStatus(updated) }