This repository has been archived by the owner on Mar 20, 2024. It is now read-only.
forked from cilium/cilium
/
metrics.go
174 lines (149 loc) · 5.8 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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Copyright 2018-2019 Authors of Cilium
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package endpoint
import (
"time"
"github.com/cilium/cilium/api/v1/models"
"github.com/cilium/cilium/pkg/datapath/loader"
"github.com/cilium/cilium/pkg/lock"
"github.com/cilium/cilium/pkg/logging/logfields"
"github.com/cilium/cilium/pkg/metrics"
"github.com/cilium/cilium/pkg/spanstat"
"github.com/prometheus/client_golang/prometheus"
)
var endpointPolicyStatus endpointPolicyStatusMap
func init() {
endpointPolicyStatus = newEndpointPolicyStatusMap()
}
type statistics interface {
GetMap() map[string]*spanstat.SpanStat
}
func sendMetrics(stats statistics, metric prometheus.ObserverVec) {
for scope, stat := range stats.GetMap() {
// Skip scopes that have not been hit (zero duration), so the count in
// the histogram accurately reflects the number of times each scope is
// hit, and the distribution is not incorrectly skewed towards zero.
if stat.SuccessTotal() != time.Duration(0) {
metric.WithLabelValues(scope, "success").Observe(stat.SuccessTotal().Seconds())
}
if stat.FailureTotal() != time.Duration(0) {
metric.WithLabelValues(scope, "failure").Observe(stat.FailureTotal().Seconds())
}
}
}
type regenerationStatistics struct {
success bool
endpointID uint16
policyStatus models.EndpointPolicyEnabled
totalTime spanstat.SpanStat
waitingForLock spanstat.SpanStat
waitingForCTClean spanstat.SpanStat
policyCalculation spanstat.SpanStat
proxyConfiguration spanstat.SpanStat
proxyPolicyCalculation spanstat.SpanStat
proxyWaitForAck spanstat.SpanStat
datapathRealization loader.SpanStat
mapSync spanstat.SpanStat
prepareBuild spanstat.SpanStat
}
// SendMetrics sends the regeneration statistics for this endpoint to
// Prometheus.
func (s *regenerationStatistics) SendMetrics() {
endpointPolicyStatus.Update(s.endpointID, s.policyStatus)
if !s.success {
// Endpoint regeneration failed, increase on failed metrics
metrics.EndpointRegenerationCount.WithLabelValues(metrics.LabelValueOutcomeFail).Inc()
return
}
metrics.EndpointRegenerationCount.WithLabelValues(metrics.LabelValueOutcomeSuccess).Inc()
sendMetrics(s, metrics.EndpointRegenerationTimeStats)
}
// GetMap returns a map which key is the stat name and the value is the stat
func (s *regenerationStatistics) GetMap() map[string]*spanstat.SpanStat {
result := map[string]*spanstat.SpanStat{
"waitingForLock": &s.waitingForLock,
"waitingForCTClean": &s.waitingForCTClean,
"policyCalculation": &s.policyCalculation,
"proxyConfiguration": &s.proxyConfiguration,
"proxyPolicyCalculation": &s.proxyPolicyCalculation,
"proxyWaitForAck": &s.proxyWaitForAck,
"mapSync": &s.mapSync,
"prepareBuild": &s.prepareBuild,
logfields.BuildDuration: &s.totalTime,
}
for k, v := range s.datapathRealization.GetMap() {
result[k] = v
}
return result
}
type policyRegenerationStatistics struct {
success bool
totalTime spanstat.SpanStat
waitingForIdentityCache spanstat.SpanStat
waitingForPolicyRepository spanstat.SpanStat
policyCalculation spanstat.SpanStat
}
func (ps *policyRegenerationStatistics) SendMetrics() {
metrics.PolicyRegenerationCount.Inc()
sendMetrics(ps, metrics.PolicyRegenerationTimeStats)
}
func (ps *policyRegenerationStatistics) GetMap() map[string]*spanstat.SpanStat {
return map[string]*spanstat.SpanStat{
"waitingForIdentityCache": &ps.waitingForIdentityCache,
"waitingForPolicyRepository": &ps.waitingForPolicyRepository,
"policyCalculation": &ps.policyCalculation,
logfields.BuildDuration: &ps.totalTime,
}
}
// endpointPolicyStatusMap is a map to store the endpoint id and the policy
// enforcement status. It is used only to send metrics to prometheus.
type endpointPolicyStatusMap struct {
mutex lock.Mutex
m map[uint16]models.EndpointPolicyEnabled
}
func newEndpointPolicyStatusMap() endpointPolicyStatusMap {
return endpointPolicyStatusMap{m: make(map[uint16]models.EndpointPolicyEnabled)}
}
// Update adds or updates a new endpoint to the map and update the metrics
// related
func (epPolicyMaps *endpointPolicyStatusMap) Update(endpointID uint16, policyStatus models.EndpointPolicyEnabled) {
epPolicyMaps.mutex.Lock()
epPolicyMaps.m[endpointID] = policyStatus
epPolicyMaps.mutex.Unlock()
endpointPolicyStatus.UpdateMetrics()
}
// Remove deletes the given endpoint from the map and update the metrics
func (epPolicyMaps *endpointPolicyStatusMap) Remove(endpointID uint16) {
epPolicyMaps.mutex.Lock()
delete(epPolicyMaps.m, endpointID)
epPolicyMaps.mutex.Unlock()
epPolicyMaps.UpdateMetrics()
}
// UpdateMetrics update the policy enforcement metrics statistics for the endpoints.
func (epPolicyMaps *endpointPolicyStatusMap) UpdateMetrics() {
policyStatus := map[models.EndpointPolicyEnabled]float64{
models.EndpointPolicyEnabledNone: 0,
models.EndpointPolicyEnabledEgress: 0,
models.EndpointPolicyEnabledIngress: 0,
models.EndpointPolicyEnabledBoth: 0,
}
epPolicyMaps.mutex.Lock()
for _, value := range epPolicyMaps.m {
policyStatus[value]++
}
epPolicyMaps.mutex.Unlock()
for k, v := range policyStatus {
metrics.PolicyEndpointStatus.WithLabelValues(string(k)).Set(v)
}
}