-
Notifications
You must be signed in to change notification settings - Fork 67
/
metrics.go
243 lines (208 loc) · 6.63 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
/*
* 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 (
"sync"
"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/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"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)
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"},
)
)
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(ptype, zone string) {
ZoneCacheDiscardings.WithLabelValues(ptype, zone).Add(float64(1))
}
type ZoneProviderTypes struct {
lock sync.Mutex
providers map[string]string
}
func (this *ZoneProviderTypes) Add(ptype, zone string) {
this.lock.Lock()
defer this.lock.Unlock()
this.providers[zone] = ptype
}
func (this *ZoneProviderTypes) Remove(zone string) string {
this.lock.Lock()
defer this.lock.Unlock()
ptype := this.providers[zone]
delete(this.providers, zone)
return ptype
}
var zoneProviders = &ZoneProviderTypes{providers: map[string]string{}}
func ReportZoneEntries(ptype, zone string, amount int, stale int) {
Entries.WithLabelValues(ptype, zone).Set(float64(amount))
StaleEntries.WithLabelValues(ptype, zone).Set(float64(stale))
zoneProviders.Add(ptype, zone)
}
func DeleteZone(zone string) {
ptype := zoneProviders.Remove(zone)
if ptype != "" {
Entries.DeleteLabelValues(ptype, zone)
}
}
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)
}
}
}