forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
metrics.go
79 lines (65 loc) · 2.67 KB
/
metrics.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package controller
import (
"time"
templateapi "github.com/openshift/origin/pkg/template/apis/template"
"github.com/prometheus/client_golang/prometheus"
"k8s.io/apimachinery/pkg/labels"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
kapi "k8s.io/kubernetes/pkg/apis/core"
)
var templateInstanceCompleted = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "openshift_template_instance_completed_total",
Help: "Counts completed TemplateInstance objects by condition",
},
[]string{"condition"},
)
func newTemplateInstanceActiveAge() prometheus.Histogram {
// We recreate a new Histogram object every time Collect is called. This is
// because we are recording a series of point-in-time observations about the
// population of "active" TemplateInstances. Were we to use a singleton
// Histogram, we would only be able to observe TemplateInstances as they
// completed, which would add latency in reporting very long-running
// TemplateInstances and completely prevent reporting of non-completing
// TemplateInstances.
//
// Effectively, the resulting series is to Histogram what Gauge is to
// Counter. In the resulting series, _count and _sum are not monotonically
// increasing (because TemplateInstances are no longer part of the
// population once they terminate or are deleted), therefore it is not valid
// to use counter functions such as rate() on this series.
return prometheus.NewHistogram(
prometheus.HistogramOpts{
Name: "openshift_template_instance_active_age_seconds",
Help: "Shows the instantaneous age distribution of active TemplateInstance objects",
Buckets: prometheus.LinearBuckets(600, 600, 7),
},
)
}
func (c *TemplateInstanceController) Describe(ch chan<- *prometheus.Desc) {
templateInstanceActiveAge := newTemplateInstanceActiveAge()
templateInstanceCompleted.Describe(ch)
templateInstanceActiveAge.Describe(ch)
}
func (c *TemplateInstanceController) Collect(ch chan<- prometheus.Metric) {
templateInstanceCompleted.Collect(ch)
now := c.clock.Now()
templateInstances, err := c.lister.List(labels.Everything())
if err != nil {
utilruntime.HandleError(err)
return
}
templateInstanceActiveAge := newTemplateInstanceActiveAge()
nextTemplateInstance:
for _, templateInstance := range templateInstances {
for _, cond := range templateInstance.Status.Conditions {
if cond.Status == kapi.ConditionTrue &&
(cond.Type == templateapi.TemplateInstanceInstantiateFailure ||
cond.Type == templateapi.TemplateInstanceReady) {
continue nextTemplateInstance
}
}
templateInstanceActiveAge.Observe(float64(now.Sub(templateInstance.CreationTimestamp.Time) / time.Second))
}
templateInstanceActiveAge.Collect(ch)
}