Skip to content

Commit

Permalink
apps: add rollouts metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
mfojtik committed Sep 8, 2017
1 parent 9b053a4 commit e8c06b5
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

deployapi "github.com/openshift/origin/pkg/apps/apis/apps"
appsclient "github.com/openshift/origin/pkg/apps/generated/internalclientset/typed/apps/internalversion"
appsinternallisters "github.com/openshift/origin/pkg/apps/generated/listers/apps/internalversion"
deployutil "github.com/openshift/origin/pkg/apps/util"
oscache "github.com/openshift/origin/pkg/client/cache"
)
Expand Down Expand Up @@ -65,6 +66,10 @@ type DeploymentConfigController struct {

// dcStore provides a local cache for deployment configs.
dcStore oscache.StoreToDeploymentConfigLister

// dcLister provides lister for the prometheus metrics
dcLister appsinternallisters.DeploymentConfigLister

// dcStoreSynced makes sure the dc store is synced before reconcling any deployment config.
dcStoreSynced func() bool
// rcLister can list/get replication controllers from a shared informer's cache
Expand Down
3 changes: 3 additions & 0 deletions pkg/apps/controller/deploymentconfig/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

deployapi "github.com/openshift/origin/pkg/apps/apis/apps"
appsclient "github.com/openshift/origin/pkg/apps/generated/internalclientset"
metrics "github.com/openshift/origin/pkg/apps/metrics/prometheus"
)

const (
Expand Down Expand Up @@ -93,6 +94,8 @@ func (c *DeploymentConfigController) Run(workers int, stopCh <-chan struct{}) {
go wait.Until(c.worker, time.Second, stopCh)
}

metrics.IntializeMetricsCollector(c.rcLister)

<-stopCh

glog.Infof("Shutting down deploymentconfig controller")
Expand Down
3 changes: 3 additions & 0 deletions pkg/apps/metrics/prometheus/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Package prometheus contains code for injecting apps related metrics
// into the prometheus registry running in the openshift master
package prometheus
104 changes: 104 additions & 0 deletions pkg/apps/metrics/prometheus/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package prometheus

import (
"fmt"
"strings"
"time"

"github.com/golang/glog"
"github.com/prometheus/client_golang/prometheus"

"k8s.io/apimachinery/pkg/labels"
kcorelisters "k8s.io/kubernetes/pkg/client/listers/core/v1"

"github.com/openshift/origin/pkg/apps/util"
)

const (
completeRolloutCount = "complete_rollouts_total"
activeRolloutDurationSeconds = "active_rollouts_duration_seconds"

availablePhase = "available"
failedPhase = "failed"
cancelledPhase = "cancelled"
)

var (
nameToQuery = func(name string) string {
return strings.Join([]string{"openshift_apps", name}, "_")
}

completeRolloutCountDesc = prometheus.NewDesc(
nameToQuery(completeRolloutCount),
"Counts total complete rollouts",
[]string{"phase"}, nil,
)

activeRolloutDurationSecondsDesc = prometheus.NewDesc(
nameToQuery(activeRolloutDurationSeconds),
"Counts total active rollouts",
[]string{"namespace", "name", "phase", "generation"}, nil,
)

apps = appsCollector{}
registered = false
)

type appsCollector struct {
lister kcorelisters.ReplicationControllerLister
}

func IntializeMetricsCollector(rcLister kcorelisters.ReplicationControllerLister) {
apps.lister = rcLister
if !registered {
prometheus.MustRegister(&apps)
registered = true
}
glog.V(4).Info("apps metrics registered with prometheus")
}

func (c *appsCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- completeRolloutCountDesc
ch <- activeRolloutDurationSecondsDesc
}

// Collect implements the prometheus.Collector interface.
func (c *appsCollector) Collect(ch chan<- prometheus.Metric) {
result, err := c.lister.List(labels.Everything())
if err != nil {
glog.V(4).Infof("Collecting metrics for apps failed: %v", err)
return
}

var available, failed, cancelled float64

for _, d := range result {
if util.IsTerminatedDeployment(d) {
if util.IsDeploymentCancelled(d) {
cancelled++
continue
}
if util.IsFailedDeployment(d) {
failed++
continue
}
if util.IsCompleteDeployment(d) {
available++
continue
}
}

// TODO: possible time screw?
durationSeconds := time.Now().Unix() - d.CreationTimestamp.Unix()
ch <- prometheus.MustNewConstMetric(activeRolloutDurationSecondsDesc, prometheus.GaugeValue, float64(durationSeconds), []string{
d.Namespace,
util.DeploymentConfigNameFor(d),
strings.ToLower(string(util.DeploymentStatusFor(d))),
fmt.Sprintf("%d", d.Status.ObservedGeneration),
}...)
}

ch <- prometheus.MustNewConstMetric(completeRolloutCountDesc, prometheus.GaugeValue, available, []string{availablePhase}...)
ch <- prometheus.MustNewConstMetric(completeRolloutCountDesc, prometheus.GaugeValue, failed, []string{failedPhase}...)
ch <- prometheus.MustNewConstMetric(completeRolloutCountDesc, prometheus.GaugeValue, cancelled, []string{cancelledPhase}...)
}

0 comments on commit e8c06b5

Please sign in to comment.