/
pipelineruns_state_finished.go
71 lines (63 loc) · 2.1 KB
/
pipelineruns_state_finished.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
package metrics
import (
"fmt"
"sync"
stewardapi "github.com/SAP/stewardci-core/pkg/apis/steward/v1alpha1"
"github.com/SAP/stewardci-core/pkg/metrics"
"github.com/prometheus/client_golang/prometheus"
)
var (
// PipelineRunsStateFinished is a metric that observes the state
// of pipeline runs that has just been finished.
PipelineRunsStateFinished StateItemsMetric = &pipelineRunsStateFinished{}
)
func init() {
PipelineRunsStateFinished.(*pipelineRunsStateFinished).init()
}
type pipelineRunsStateFinished struct {
initOnlyOnce sync.Once
metric *prometheus.HistogramVec
// TODO remove when deprecated long enough
metricOld *prometheus.HistogramVec
}
func (m *pipelineRunsStateFinished) init() {
m.initOnlyOnce.Do(func() {
m.metric = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Subsystem: subsystem,
Name: "state_duration_seconds",
Help: "A histogram vector partitioned by pipeline run states counting the pipeline runs that finished a state grouped by the state duration." +
"\n\nThere's one histogram per pipeline run state (label `state`)." +
" A pipeline run gets counted immediately when a state is finished.",
Buckets: prometheus.ExponentialBuckets(0.125, 2, 15),
},
[]string{
"state",
},
)
metrics.Registerer().MustRegister(m.metric)
m.metricOld = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: "steward_pipelinerun_state_duration_seconds",
Help: fmt.Sprintf("Deprecated! Use '%s_state_duration_seconds' instead.", subsystem),
Buckets: prometheus.ExponentialBuckets(0.125, 2, 15),
},
[]string{
"state",
},
)
metrics.Registerer().MustRegister(m.metricOld)
})
}
func (m *pipelineRunsStateFinished) Observe(state *stewardapi.StateItem) {
if state.StartedAt.IsZero() || state.FinishedAt.IsZero() {
// cannot observe state if timestamps are not set
return
}
duration := state.FinishedAt.Sub(state.StartedAt.Time)
if duration < 0 {
return
}
m.metric.WithLabelValues(string(state.State)).Observe(duration.Seconds())
m.metricOld.WithLabelValues(string(state.State)).Observe(duration.Seconds())
}