-
Notifications
You must be signed in to change notification settings - Fork 67
/
metrics.go
293 lines (251 loc) · 8.58 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
* Copyright 2019 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file
*
* 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
*
*/
package metrics
import (
"strconv"
"sync"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/gardener/controller-manager-library/pkg/resources"
"github.com/gardener/controller-manager-library/pkg/server"
"github.com/gardener/controller-manager-library/pkg/utils"
"github.com/gardener/external-dns-management/pkg/dns"
"github.com/gardener/external-dns-management/pkg/dns/provider/statistic"
)
func init() {
prometheus.MustRegister(Requests)
prometheus.MustRegister(ZoneRequests)
prometheus.MustRegister(ZoneCacheDiscardings)
prometheus.MustRegister(Accounts)
prometheus.MustRegister(Entries)
prometheus.MustRegister(StaleEntries)
prometheus.MustRegister(Owners)
prometheus.MustRegister(RemoteAccessLogins)
prometheus.MustRegister(RemoteAccessRequests)
prometheus.MustRegister(RemoteAccessSeconds)
prometheus.MustRegister(RemoteAccessCertificates)
server.RegisterHandler("/metrics", promhttp.Handler())
}
var (
Requests = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "external_dns_management_total_provider_requests",
Help: "Total requests per provider type and credential set",
},
[]string{"providertype", "accounthash", "requesttype"},
)
ZoneRequests = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "external_dns_management_requests_per_zone",
Help: "Requests per provider type, credential set, and zone",
},
[]string{"providertype", "accounthash", "requesttype", "zone"},
)
ZoneCacheDiscardings = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "external_dns_management_zone_cache_discardings",
Help: "Discardings of zone cache per provider type and zone",
},
[]string{"providertype", "zone"},
)
Accounts = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "external_dns_management_account_providers",
Help: "Total number of providers per account",
},
[]string{"providertype", "accounthash"},
)
Entries = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "external_dns_management_dns_entries",
Help: "Total number of dns entries per hosted zone",
},
[]string{"providertype", "zone"},
)
StaleEntries = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "external_dns_management_dns_entries_stale",
Help: "Total number of stale dns entries per hosted zone",
},
[]string{"providertype", "zone"},
)
Owners = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "external_dns_management_dns_owners",
Help: "Total number of dns entries per owner",
},
[]string{"owner", "providertype", "provider"},
)
RemoteAccessLogins = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "external_dns_management_remoteaccess_logins",
Help: "Total number of remote access logins",
},
[]string{"handler", "client", "success"},
)
RemoteAccessRequests = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "external_dns_management_remoteaccess_requests",
Help: "Total number of remote access requests",
},
[]string{"handler", "client", "type", "zoneid"},
)
RemoteAccessSeconds = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: "external_dns_management_remoteaccess_seconds",
Help: "Duration in seconds of completed remote access requests",
Buckets: []float64{.005, .01, .025, .05, .1, .25, .5, 1, 2.5, 5, 10, 25},
},
[]string{"handler", "client", "type", "zoneid", "error"},
)
RemoteAccessCertificates = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "external_dns_management_remoteaccess_transport_credentials",
Help: "Number of server-side transport credentials of remote access",
},
)
)
var theRequestLabels = &requestLabels{lock: sync.Mutex{}, known: map[ptypeAccount]utils.StringSet{}}
type ptypeAccount struct {
ptype string
account string
}
type requestLabels struct {
lock sync.Mutex
known map[ptypeAccount]utils.StringSet
}
func (this *requestLabels) AddRequestLabel(ptype, account, requestType string) {
this.lock.Lock()
defer this.lock.Unlock()
key := ptypeAccount{ptype, account}
set, ok := this.known[key]
if !ok {
set = utils.StringSet{}
this.known[key] = set
}
set.Add(requestType)
}
func (this *requestLabels) Delete(ptype, account string) utils.StringSet {
this.lock.Lock()
defer this.lock.Unlock()
key := ptypeAccount{ptype, account}
set := this.known[key]
delete(this.known, key)
return set
}
func DeleteAccount(ptype, account string) {
Accounts.DeleteLabelValues(ptype, account)
requestTypes := theRequestLabels.Delete(ptype, account)
for rtype := range requestTypes {
Requests.DeleteLabelValues(ptype, account, rtype)
}
Entries.DeleteLabelValues(ptype, account)
}
func ReportAccountProviders(ptype, account string, amount int) {
Accounts.WithLabelValues(ptype, account).Set(float64(amount))
}
func AddRequests(ptype, account, requestType string, no int, zone *string) {
theRequestLabels.AddRequestLabel(ptype, account, requestType)
Requests.WithLabelValues(ptype, account, requestType).Add(float64(no))
if zone != nil {
ZoneRequests.WithLabelValues(ptype, account, requestType, *zone).Add(float64(no))
}
}
func AddZoneCacheDiscarding(id dns.ZoneID) {
ZoneCacheDiscardings.WithLabelValues(id.ProviderType, id.ID).Add(float64(1))
}
type ZoneProviderTypes struct {
lock sync.Mutex
providers map[dns.ZoneID]struct{}
}
func (this *ZoneProviderTypes) Add(zone dns.ZoneID) {
this.lock.Lock()
defer this.lock.Unlock()
this.providers[zone] = struct{}{}
}
func (this *ZoneProviderTypes) Remove(zone dns.ZoneID) {
this.lock.Lock()
defer this.lock.Unlock()
delete(this.providers, zone)
}
var zoneProviders = &ZoneProviderTypes{providers: map[dns.ZoneID]struct{}{}}
func ReportZoneEntries(zoneid dns.ZoneID, amount int, stale int) {
Entries.WithLabelValues(zoneid.ProviderType, zoneid.ID).Set(float64(amount))
StaleEntries.WithLabelValues(zoneid.ProviderType, zoneid.ID).Set(float64(stale))
zoneProviders.Add(zoneid)
}
func ReportRemoteAccessLogins(namespace, client string, success bool) {
RemoteAccessLogins.WithLabelValues(namespace, client, strconv.FormatBool(success)).Add(float64(1))
}
func ReportRemoteAccessRequests(namespace, client, requestType, zoneid string) {
RemoteAccessRequests.WithLabelValues(namespace, client, requestType, zoneid).Add(float64(1))
}
func ReportRemoteAccessSeconds(namespace, client, requestType, zoneid, error string, duration time.Duration) {
RemoteAccessSeconds.WithLabelValues(namespace, client, requestType, zoneid, error).Observe(duration.Seconds())
}
func ReportRemoteAccessCertificates(count int) {
RemoteAccessCertificates.Set(float64(count))
}
func DeleteZone(zoneid dns.ZoneID) {
zoneProviders.Remove(zoneid)
Entries.DeleteLabelValues(zoneid.ProviderType, zoneid.ID)
}
var currentStatistic = statistic.NewEntryStatistic()
var lock sync.Mutex
func deleteOwnerStatistic(state statistic.WalkingState, owner, ptype string, pname resources.ObjectName, count int) statistic.WalkingState {
types := state.(utils.StringSet)
if types.Contains(ptype) {
Owners.DeleteLabelValues(owner, ptype, pname.String())
}
return state
}
func UpdateOwnerStatistic(statistic *statistic.EntryStatistic, types utils.StringSet) {
lock.Lock()
defer lock.Unlock()
for o := range currentStatistic.Owners {
statistic.Owners.Assure(o)
}
for o, pts := range statistic.Owners {
old_pts := currentStatistic.Owners.Assure(o)
for pt := range types {
ps := pts.Get(pt)
old_ps := old_pts.Assure(pt)
for p, c := range ps {
Owners.WithLabelValues(o, pt, p.String()).Set(float64(c))
old_ps[p] = c
}
for p := range old_ps {
if _, ok := ps[p]; !ok {
Owners.DeleteLabelValues(o, pt, p.String())
delete(old_ps, p)
}
}
if len(old_ps) == 0 {
delete(old_pts, pt)
}
}
for pt, ps := range old_pts {
if pts[pt] == nil && types.Contains(pt) {
ps.Walk(types, deleteOwnerStatistic, o, pt)
delete(old_pts, pt)
}
}
if len(old_pts) == 0 {
delete(currentStatistic.Owners, o)
}
}
}