Skip to content

Commit 61798ff

Browse files
author
Jim Minter
committed
WIP2
1 parent 6db905c commit 61798ff

File tree

1 file changed

+33
-27
lines changed

1 file changed

+33
-27
lines changed

pkg/template/controller/metrics.go

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,71 @@
11
package controller
22

33
import (
4+
"time"
5+
46
templateapi "github.com/openshift/origin/pkg/template/apis/template"
57
"github.com/prometheus/client_golang/prometheus"
68
"k8s.io/apimachinery/pkg/labels"
79
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
810
kapi "k8s.io/kubernetes/pkg/api"
911
)
1012

11-
var (
12-
templateInstancesTotal = prometheus.NewDesc(
13-
"TemplateInstanceController_TemplateInstances_total",
14-
"Counts TemplateInstance objects by condition type and status",
13+
func newTemplateInstancesTotal() *prometheus.GaugeVec {
14+
return prometheus.NewGaugeVec(
15+
prometheus.GaugeOpts{
16+
Name: "TemplateInstanceController_TemplateInstances_total",
17+
Help: "Counts TemplateInstance objects by condition type and status",
18+
},
1519
[]string{"type", "status"},
16-
nil,
1720
)
18-
templateInstancesWaiting = prometheus.NewDesc(
19-
"TemplateInstanceController_TemplateInstances_waiting_start_time_seconds",
20-
"Show the start time in unix epoch form of waiting TemplateInstances by namespace and name",
21-
[]string{"namespace", "name"},
22-
nil,
21+
}
22+
23+
func newTemplateInstancesWaiting() prometheus.Histogram {
24+
return prometheus.NewHistogram(
25+
prometheus.HistogramOpts{
26+
Name: "TemplateInstanceController_TemplateInstances_active_waiting_time_seconds",
27+
Help: "Show the distribution of active TemplateInstance waiting times",
28+
Buckets: []float64{60, 300, 600, 1200, 3600},
29+
},
2330
)
24-
)
31+
}
2532

2633
func (c *TemplateInstanceController) Describe(ch chan<- *prometheus.Desc) {
27-
ch <- templateInstancesTotal
28-
ch <- templateInstancesWaiting
34+
newTemplateInstancesTotal().Describe(ch)
35+
newTemplateInstancesWaiting().Describe(ch)
2936
}
3037

3138
func (c *TemplateInstanceController) Collect(ch chan<- prometheus.Metric) {
39+
now := time.Now()
40+
3241
templateInstances, err := c.lister.List(labels.Everything())
3342
if err != nil {
3443
utilruntime.HandleError(err)
3544
return
3645
}
3746

38-
m := map[templateapi.TemplateInstanceCondition]uint64{}
47+
templateInstancesTotal := newTemplateInstancesTotal()
48+
templateInstancesWaiting := newTemplateInstancesWaiting()
3949

4050
for _, templateInstance := range templateInstances {
4151
waiting := true
4252

43-
m[templateapi.TemplateInstanceCondition{}]++
53+
templateInstancesTotal.WithLabelValues("", "").Inc()
54+
4455
for _, cond := range templateInstance.Status.Conditions {
45-
m[templateapi.TemplateInstanceCondition{Type: cond.Type, Status: cond.Status}]++
46-
if cond.Type == templateapi.TemplateInstanceInstantiateFailure && cond.Status == kapi.ConditionTrue ||
47-
cond.Type == templateapi.TemplateInstanceReady && cond.Status == kapi.ConditionTrue {
56+
templateInstancesTotal.WithLabelValues(string(cond.Type), string(cond.Status)).Inc()
57+
58+
if cond.Status == kapi.ConditionTrue &&
59+
(cond.Type == templateapi.TemplateInstanceInstantiateFailure || cond.Type == templateapi.TemplateInstanceReady) {
4860
waiting = false
4961
}
5062
}
5163

5264
if waiting {
53-
ch <- prometheus.MustNewConstMetric(templateInstancesWaiting,
54-
prometheus.GaugeValue,
55-
float64(templateInstance.CreationTimestamp.Unix()),
56-
templateInstance.Namespace,
57-
templateInstance.Name,
58-
)
65+
templateInstancesWaiting.Observe(float64(now.Sub(templateInstance.CreationTimestamp.Time) / time.Second))
5966
}
6067
}
6168

62-
for cond, count := range m {
63-
ch <- prometheus.MustNewConstMetric(templateInstancesTotal, prometheus.GaugeValue, float64(count), string(cond.Type), string(cond.Status))
64-
}
69+
templateInstancesTotal.Collect(ch)
70+
templateInstancesWaiting.Collect(ch)
6571
}

0 commit comments

Comments
 (0)