Skip to content
Closed
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
47 changes: 47 additions & 0 deletions pkg/console/metrics/console_url.go
Original file line number Diff line number Diff line change
@@ -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
}
31 changes: 31 additions & 0 deletions pkg/console/metrics/register.go
Original file line number Diff line number Diff line change
@@ -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://<url>"} 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
}
15 changes: 4 additions & 11 deletions pkg/console/operator/sync_v400.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"os"

"github.com/openshift/console-operator/pkg/console/metrics"

"github.com/prometheus/client_golang/prometheus"

// kube
Expand Down Expand Up @@ -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)
}

Expand Down