-
Notifications
You must be signed in to change notification settings - Fork 99
/
app_metrics.go
133 lines (119 loc) · 4.45 KB
/
app_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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Copyright 2021 VMware, Inc.
// SPDX-License-Identifier: Apache-2.0
package metrics
import (
"sync"
"github.com/prometheus/client_golang/prometheus"
"sigs.k8s.io/controller-runtime/pkg/metrics"
)
// AppMetrics holds server metrics
type AppMetrics struct {
reconcileAttemptTotal *prometheus.CounterVec
reconcileSuccessTotal *prometheus.CounterVec
reconcileFailureTotal *prometheus.CounterVec
reconcileDeleteAttemptTotal *prometheus.CounterVec
reconcileDeleteFailedTotal *prometheus.CounterVec
}
var (
once sync.Once
)
// NewAppMetrics creates AppMetrics object
func NewAppMetrics() *AppMetrics {
const (
metricNamespace = "kappctrl"
kappNameLabel = "app_name"
kappNamespaceLabel = "namespace"
)
return &AppMetrics{
reconcileAttemptTotal: prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: metricNamespace,
Name: "app_reconcile_attempt_total",
Help: "Total number of attempted reconciles",
},
[]string{kappNameLabel, kappNamespaceLabel},
),
reconcileSuccessTotal: prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: metricNamespace,
Name: "app_reconcile_success_total",
Help: "Total number of succeeded reconciles",
},
[]string{kappNameLabel, kappNamespaceLabel},
),
reconcileFailureTotal: prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: metricNamespace,
Name: "app_reconcile_failure_total",
Help: "Total number of failed reconciles",
},
[]string{kappNameLabel, kappNamespaceLabel},
),
reconcileDeleteAttemptTotal: prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: metricNamespace,
Name: "app_reconcile_delete_attempt_total",
Help: "Total number of attempted reconcile deletion",
},
[]string{kappNameLabel, kappNamespaceLabel},
),
reconcileDeleteFailedTotal: prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: metricNamespace,
Name: "app_reconcile_delete_failed_total",
Help: "Total number of failed reconcile deletion",
},
[]string{kappNameLabel, kappNamespaceLabel},
),
}
}
// RegisterAllMetrics registers all prometheus metrics.
func (am *AppMetrics) RegisterAllMetrics() {
once.Do(func() {
metrics.Registry.MustRegister(
am.reconcileAttemptTotal,
am.reconcileSuccessTotal,
am.reconcileFailureTotal,
am.reconcileDeleteAttemptTotal,
am.reconcileDeleteFailedTotal,
)
})
}
// InitMetrics initializes metrics
func (am *AppMetrics) InitMetrics(appName string, namespace string) {
// Initializes counter metrics
am.reconcileAttemptTotal.WithLabelValues(appName, namespace).Add(0)
am.reconcileSuccessTotal.WithLabelValues(appName, namespace).Add(0)
am.reconcileFailureTotal.WithLabelValues(appName, namespace).Add(0)
am.reconcileDeleteAttemptTotal.WithLabelValues(appName, namespace).Add(0)
am.reconcileDeleteFailedTotal.WithLabelValues(appName, namespace).Add(0)
}
// DeleteMetrics deletes metrics
func (am *AppMetrics) DeleteMetrics(appName string, namespace string) {
// Delete counter metrics
am.reconcileAttemptTotal.DeleteLabelValues(appName, namespace)
am.reconcileSuccessTotal.DeleteLabelValues(appName, namespace)
am.reconcileFailureTotal.DeleteLabelValues(appName, namespace)
am.reconcileDeleteAttemptTotal.DeleteLabelValues(appName, namespace)
am.reconcileDeleteFailedTotal.DeleteLabelValues(appName, namespace)
}
// RegisterReconcileAttempt increments reconcileAttemptTotal
func (am *AppMetrics) RegisterReconcileAttempt(appName string, namespace string) {
am.reconcileAttemptTotal.WithLabelValues(appName, namespace).Inc()
}
// RegisterReconcileSuccess increments reconcileSuccessTotal
func (am *AppMetrics) RegisterReconcileSuccess(appName string, namespace string) {
am.reconcileSuccessTotal.WithLabelValues(appName, namespace).Inc()
}
// RegisterReconcileFailure increments reconcileFailureTotal
func (am *AppMetrics) RegisterReconcileFailure(appName string, namespace string) {
am.reconcileFailureTotal.WithLabelValues(appName, namespace).Inc()
}
// RegisterReconcileDeleteAttempt increments reconcileDeleteAttemptTotal
func (am *AppMetrics) RegisterReconcileDeleteAttempt(appName string, namespace string) {
am.reconcileDeleteAttemptTotal.WithLabelValues(appName, namespace).Inc()
}
// RegisterReconcileDeleteFailed increments reconcileDeleteFailedTotal
func (am *AppMetrics) RegisterReconcileDeleteFailed(appName string, namespace string) {
am.reconcileDeleteFailedTotal.WithLabelValues(appName, namespace).Inc()
}