|
1 | 1 | package controller
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "time" |
| 5 | + |
4 | 6 | templateapi "github.com/openshift/origin/pkg/template/apis/template"
|
5 | 7 | "github.com/prometheus/client_golang/prometheus"
|
6 | 8 | "k8s.io/apimachinery/pkg/labels"
|
7 | 9 | utilruntime "k8s.io/apimachinery/pkg/util/runtime"
|
8 | 10 | kapi "k8s.io/kubernetes/pkg/api"
|
9 | 11 | )
|
10 | 12 |
|
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 | + }, |
15 | 19 | []string{"type", "status"},
|
16 |
| - nil, |
17 | 20 | )
|
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 | + }, |
23 | 30 | )
|
24 |
| -) |
| 31 | +} |
25 | 32 |
|
26 | 33 | func (c *TemplateInstanceController) Describe(ch chan<- *prometheus.Desc) {
|
27 |
| - ch <- templateInstancesTotal |
28 |
| - ch <- templateInstancesWaiting |
| 34 | + newTemplateInstancesTotal().Describe(ch) |
| 35 | + newTemplateInstancesWaiting().Describe(ch) |
29 | 36 | }
|
30 | 37 |
|
31 | 38 | func (c *TemplateInstanceController) Collect(ch chan<- prometheus.Metric) {
|
| 39 | + now := time.Now() |
| 40 | + |
32 | 41 | templateInstances, err := c.lister.List(labels.Everything())
|
33 | 42 | if err != nil {
|
34 | 43 | utilruntime.HandleError(err)
|
35 | 44 | return
|
36 | 45 | }
|
37 | 46 |
|
38 |
| - m := map[templateapi.TemplateInstanceCondition]uint64{} |
| 47 | + templateInstancesTotal := newTemplateInstancesTotal() |
| 48 | + templateInstancesWaiting := newTemplateInstancesWaiting() |
39 | 49 |
|
40 | 50 | for _, templateInstance := range templateInstances {
|
41 | 51 | waiting := true
|
42 | 52 |
|
43 |
| - m[templateapi.TemplateInstanceCondition{}]++ |
| 53 | + templateInstancesTotal.WithLabelValues("", "").Inc() |
| 54 | + |
44 | 55 | 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) { |
48 | 60 | waiting = false
|
49 | 61 | }
|
50 | 62 | }
|
51 | 63 |
|
52 | 64 | 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)) |
59 | 66 | }
|
60 | 67 | }
|
61 | 68 |
|
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) |
65 | 71 | }
|
0 commit comments