-
Notifications
You must be signed in to change notification settings - Fork 280
/
info.go
400 lines (366 loc) · 15 KB
/
info.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
package metrics
import (
"context"
"fmt"
"time"
"go.opencensus.io/metric/metricproducer"
"go.opencensus.io/stats"
"go.opencensus.io/stats/view"
"go.opencensus.io/tag"
"github.com/pomerium/pomerium/internal/log"
"github.com/pomerium/pomerium/pkg/metrics"
)
var (
// InfoViews contains opencensus views for informational metrics about
// pomerium itself.
InfoViews = []*view.View{
ConfigLastReloadView,
ConfigLastReloadSuccessView,
IdentityManagerLastRefreshView,
IdentityManagerLastUserRefreshErrorTimestampView,
IdentityManagerLastUserRefreshErrorView,
IdentityManagerLastUserRefreshSuccessTimestampView,
IdentityManagerLastUserRefreshSuccessView,
IdentityManagerLastUserGroupRefreshErrorTimestampView,
IdentityManagerLastUserGroupRefreshErrorView,
IdentityManagerLastUserGroupRefreshSuccessTimestampView,
IdentityManagerLastUserGroupRefreshSuccessView,
IdentityManagerLastSessionRefreshErrorTimestampView,
IdentityManagerLastSessionRefreshErrorView,
IdentityManagerLastSessionRefreshSuccessTimestampView,
IdentityManagerLastSessionRefreshSuccessView,
ConfigDBVersionView,
ConfigDBErrorsView,
}
configLastReload = stats.Int64(
metrics.ConfigLastReloadTimestampSeconds,
"Timestamp of last successful config reload",
stats.UnitSeconds)
configDBVersion = stats.Int64(
metrics.ConfigDBVersion,
metrics.ConfigDBVersionHelp,
stats.UnitDimensionless,
)
configDBErrors = stats.Int64(
metrics.ConfigDBErrors,
metrics.ConfigDBErrorsHelp,
stats.UnitDimensionless,
)
configLastReloadSuccess = stats.Int64(
metrics.ConfigLastReloadSuccess,
"Returns 1 if last reload was successful",
stats.UnitDimensionless)
identityManagerLastRefresh = stats.Int64(
metrics.IdentityManagerLastRefreshTimestamp,
"Timestamp of last directory refresh",
"seconds",
)
identityManagerLastUserRefreshSuccessTimestamp = stats.Int64(
metrics.IdentityManagerLastUserRefreshSuccessTimestamp,
"Timestamp of last successful directory user refresh success",
stats.UnitSeconds,
)
identityManagerLastUserRefreshErrorTimestamp = stats.Int64(
metrics.IdentityManagerLastUserRefreshErrorTimestamp,
"Timestamp of last directory user refresh error",
stats.UnitSeconds,
)
identityManagerLastUserRefreshSuccess = stats.Int64(
metrics.IdentityManagerLastUserRefreshSuccess,
"Total successful directory user refresh requests",
stats.UnitDimensionless,
)
identityManagerLastUserRefreshError = stats.Int64(
metrics.IdentityManagerLastUserRefreshError,
"Total successful directory user refresh errors",
stats.UnitDimensionless,
)
identityManagerLastUserGroupRefreshSuccessTimestamp = stats.Int64(
metrics.IdentityManagerLastUserGroupRefreshSuccessTimestamp,
"Timestamp of last successful user group refresh success",
stats.UnitSeconds,
)
identityManagerLastUserGroupRefreshErrorTimestamp = stats.Int64(
metrics.IdentityManagerLastUserGroupRefreshErrorTimestamp,
"Timestamp of last directory user group refresh error",
stats.UnitSeconds,
)
identityManagerLastUserGroupRefreshSuccess = stats.Int64(
metrics.IdentityManagerLastUserGroupRefreshSuccess,
"Total successful directory user group refresh requests",
stats.UnitDimensionless,
)
identityManagerLastUserGroupRefreshError = stats.Int64(
metrics.IdentityManagerLastUserGroupRefreshError,
"Total successful directory user refresh errors",
stats.UnitDimensionless,
)
identityManagerLastSessionRefreshSuccessTimestamp = stats.Int64(
metrics.IdentityManagerLastSessionRefreshSuccessTimestamp,
"Timestamp of last successful session refresh success",
stats.UnitSeconds,
)
identityManagerLastSessionRefreshErrorTimestamp = stats.Int64(
metrics.IdentityManagerLastSessionRefreshErrorTimestamp,
"Timestamp of last session refresh error",
stats.UnitSeconds,
)
identityManagerLastSessionRefreshSuccess = stats.Int64(
metrics.IdentityManagerLastSessionRefreshSuccess,
"Total successful session refresh requests",
stats.UnitDimensionless,
)
identityManagerLastSessionRefreshError = stats.Int64(
metrics.IdentityManagerLastSessionRefreshError,
"Total successful session refresh errors",
stats.UnitDimensionless,
)
// ConfigDBVersionView contains last databroker config version that was processed
ConfigDBVersionView = &view.View{
Name: configDBVersion.Name(),
Description: configDBVersion.Description(),
Measure: configDBVersion,
TagKeys: []tag.Key{TagKeyService, TagConfigID},
Aggregation: view.LastValue(),
}
// ConfigDBErrorsView contains list of errors encountered while parsing this databroker config
ConfigDBErrorsView = &view.View{
Name: configDBErrors.Name(),
Description: configDBErrors.Description(),
Measure: configDBErrors,
TagKeys: []tag.Key{TagKeyService, TagConfigID},
Aggregation: view.LastValue(),
}
// ConfigLastReloadView contains the timestamp the configuration was last
// reloaded, labeled by service.
ConfigLastReloadView = &view.View{
Name: configLastReload.Name(),
Description: configLastReload.Description(),
Measure: configLastReload,
TagKeys: []tag.Key{TagKeyService},
Aggregation: view.LastValue(),
}
// ConfigLastReloadSuccessView contains the result of the last configuration
// reload, labeled by service.
ConfigLastReloadSuccessView = &view.View{
Name: configLastReloadSuccess.Name(),
Description: configLastReloadSuccess.Description(),
Measure: configLastReloadSuccess,
TagKeys: []tag.Key{TagKeyService},
Aggregation: view.LastValue(),
}
// IdentityManagerLastRefreshView contains the timestamp the identity manager
// was last refreshed, labeled by service.
IdentityManagerLastRefreshView = &view.View{
Name: identityManagerLastRefresh.Name(),
Description: identityManagerLastRefresh.Description(),
Measure: identityManagerLastRefresh,
Aggregation: view.LastValue(),
}
// IdentityManagerLastUserRefreshSuccessView contains successful user refresh counter
IdentityManagerLastUserRefreshSuccessView = &view.View{
Name: identityManagerLastUserRefreshSuccess.Name(),
Description: identityManagerLastUserRefreshSuccess.Description(),
Measure: identityManagerLastUserRefreshSuccess,
Aggregation: view.Count(),
}
// IdentityManagerLastUserRefreshErrorView contains user refresh errors counter
IdentityManagerLastUserRefreshErrorView = &view.View{
Name: identityManagerLastUserRefreshError.Name(),
Description: identityManagerLastUserRefreshError.Description(),
Measure: identityManagerLastUserRefreshError,
Aggregation: view.Count(),
}
// IdentityManagerLastUserRefreshSuccessTimestampView contains successful user refresh counter
IdentityManagerLastUserRefreshSuccessTimestampView = &view.View{
Name: identityManagerLastUserRefreshSuccessTimestamp.Name(),
Description: identityManagerLastUserRefreshSuccessTimestamp.Description(),
Measure: identityManagerLastUserRefreshSuccessTimestamp,
Aggregation: view.LastValue(),
}
// IdentityManagerLastUserRefreshErrorTimestampView contains user refresh errors counter
IdentityManagerLastUserRefreshErrorTimestampView = &view.View{
Name: identityManagerLastUserRefreshErrorTimestamp.Name(),
Description: identityManagerLastUserRefreshErrorTimestamp.Description(),
Measure: identityManagerLastUserRefreshErrorTimestamp,
Aggregation: view.LastValue(),
}
// IdentityManagerLastUserGroupRefreshSuccessView contains successful user group refresh counter
IdentityManagerLastUserGroupRefreshSuccessView = &view.View{
Name: identityManagerLastUserGroupRefreshSuccess.Name(),
Description: identityManagerLastUserGroupRefreshSuccess.Description(),
Measure: identityManagerLastUserGroupRefreshSuccess,
Aggregation: view.Count(),
}
// IdentityManagerLastUserGroupRefreshErrorView contains user group refresh errors counter
IdentityManagerLastUserGroupRefreshErrorView = &view.View{
Name: identityManagerLastUserGroupRefreshError.Name(),
Description: identityManagerLastUserGroupRefreshError.Description(),
Measure: identityManagerLastUserGroupRefreshError,
Aggregation: view.Count(),
}
// IdentityManagerLastUserGroupRefreshSuccessTimestampView contains successful user group refresh counter
IdentityManagerLastUserGroupRefreshSuccessTimestampView = &view.View{
Name: identityManagerLastUserGroupRefreshSuccessTimestamp.Name(),
Description: identityManagerLastUserGroupRefreshSuccessTimestamp.Description(),
Measure: identityManagerLastUserGroupRefreshSuccessTimestamp,
Aggregation: view.LastValue(),
}
// IdentityManagerLastUserGroupRefreshErrorTimestampView contains user group refresh errors counter
IdentityManagerLastUserGroupRefreshErrorTimestampView = &view.View{
Name: identityManagerLastUserGroupRefreshErrorTimestamp.Name(),
Description: identityManagerLastUserGroupRefreshErrorTimestamp.Description(),
Measure: identityManagerLastUserGroupRefreshErrorTimestamp,
Aggregation: view.LastValue(),
}
// IdentityManagerLastSessionRefreshSuccessView contains successful user refresh counter
IdentityManagerLastSessionRefreshSuccessView = &view.View{
Name: identityManagerLastSessionRefreshSuccess.Name(),
Description: identityManagerLastSessionRefreshSuccess.Description(),
Measure: identityManagerLastSessionRefreshSuccess,
Aggregation: view.Count(),
}
// IdentityManagerLastSessionRefreshErrorView contains user refresh errors counter
IdentityManagerLastSessionRefreshErrorView = &view.View{
Name: identityManagerLastUserRefreshError.Name(),
Description: identityManagerLastUserRefreshError.Description(),
Measure: identityManagerLastUserRefreshError,
Aggregation: view.Count(),
}
// IdentityManagerLastSessionRefreshSuccessTimestampView contains successful session refresh counter
IdentityManagerLastSessionRefreshSuccessTimestampView = &view.View{
Name: identityManagerLastSessionRefreshSuccessTimestamp.Name(),
Description: identityManagerLastSessionRefreshSuccessTimestamp.Description(),
Measure: identityManagerLastSessionRefreshSuccessTimestamp,
Aggregation: view.LastValue(),
}
// IdentityManagerLastSessionRefreshErrorTimestampView contains session refresh errors counter
IdentityManagerLastSessionRefreshErrorTimestampView = &view.View{
Name: identityManagerLastSessionRefreshErrorTimestamp.Name(),
Description: identityManagerLastSessionRefreshErrorTimestamp.Description(),
Measure: identityManagerLastSessionRefreshErrorTimestamp,
Aggregation: view.LastValue(),
}
)
// RecordIdentityManagerLastRefresh records that the identity manager refreshed users and groups.
func RecordIdentityManagerLastRefresh(ctx context.Context) {
stats.Record(ctx, identityManagerLastRefresh.M(time.Now().Unix()))
}
// RecordIdentityManagerUserRefresh updates timestamp and counter for user refresh
func RecordIdentityManagerUserRefresh(ctx context.Context, err error) {
counter := identityManagerLastUserRefreshSuccess
ts := identityManagerLastUserRefreshSuccessTimestamp
if err != nil {
counter = identityManagerLastUserRefreshError
ts = identityManagerLastUserRefreshErrorTimestamp
}
stats.Record(ctx,
ts.M(time.Now().Unix()),
counter.M(1),
)
}
// RecordIdentityManagerUserGroupRefresh updates timestamp and counter for user group update
func RecordIdentityManagerUserGroupRefresh(ctx context.Context, err error) {
counter := identityManagerLastUserGroupRefreshSuccess
ts := identityManagerLastUserGroupRefreshSuccessTimestamp
if err != nil {
counter = identityManagerLastUserGroupRefreshError
ts = identityManagerLastUserGroupRefreshErrorTimestamp
}
stats.Record(ctx,
ts.M(time.Now().Unix()),
counter.M(1),
)
}
// RecordIdentityManagerSessionRefresh updates timestamp and counter for session refresh
func RecordIdentityManagerSessionRefresh(ctx context.Context, err error) {
counter := identityManagerLastSessionRefreshSuccess
ts := identityManagerLastSessionRefreshSuccessTimestamp
if err != nil {
counter = identityManagerLastSessionRefreshError
ts = identityManagerLastSessionRefreshErrorTimestamp
}
stats.Record(ctx,
ts.M(time.Now().Unix()),
counter.M(1),
)
}
// SetDBConfigInfo records status, databroker version and error count while parsing
// the configuration from a databroker
func SetDBConfigInfo(ctx context.Context, service, configID string, version uint64, errCount int64) {
log.Info(ctx).
Str("service", service).
Str("config_id", configID).
Uint64("version", version).
Int64("err_count", errCount).
Msg("set db config info")
if err := stats.RecordWithTags(
ctx,
[]tag.Mutator{
tag.Insert(TagKeyService, service),
tag.Insert(TagConfigID, configID),
},
configDBVersion.M(int64(version)),
); err != nil {
log.Error(ctx).Err(err).Msg("telemetry/metrics: failed to record config version number")
}
if err := stats.RecordWithTags(
context.Background(),
[]tag.Mutator{
tag.Insert(TagKeyService, service),
tag.Insert(TagConfigID, configID),
},
configDBErrors.M(errCount),
); err != nil {
log.Error(ctx).Err(err).Msg("telemetry/metrics: failed to record config error count")
}
}
// SetDBConfigRejected records that a certain databroker config version has been rejected
func SetDBConfigRejected(ctx context.Context, service, configID string, version uint64, err error) {
log.Warn(ctx).Err(err).Msg("databroker: invalid config detected, ignoring")
SetDBConfigInfo(ctx, service, configID, version, -1)
}
// SetConfigInfo records the status, checksum and timestamp of a configuration
// reload. You must register InfoViews or the related config views before calling
func SetConfigInfo(ctx context.Context, service, configName string, checksum uint64, success bool) {
if success {
registry.setConfigChecksum(service, configName, checksum)
serviceTag := tag.Insert(TagKeyService, service)
if err := stats.RecordWithTags(
context.Background(),
[]tag.Mutator{serviceTag},
configLastReload.M(time.Now().Unix()),
); err != nil {
log.Error(ctx).Err(err).Msg("telemetry/metrics: failed to record config checksum timestamp")
}
if err := stats.RecordWithTags(
context.Background(),
[]tag.Mutator{serviceTag},
configLastReloadSuccess.M(1),
); err != nil {
log.Error(ctx).Err(err).Msg("telemetry/metrics: failed to record config reload")
}
} else {
stats.Record(context.Background(), configLastReloadSuccess.M(0))
}
log.Info(ctx).
Str("service", service).
Str("config", configName).
Str("checksum", fmt.Sprintf("%x", checksum)).
Msg("config: updated config")
}
// SetBuildInfo records the pomerium build info. You must call RegisterInfoMetrics to
// have this exported
func SetBuildInfo(service, hostname, envoyVersion string) {
registry.setBuildInfo(service, hostname, envoyVersion)
}
// RegisterInfoMetrics registers non-view based metrics registry globally for export
func RegisterInfoMetrics() {
metricproducer.GlobalManager().AddProducer(registry.registry)
}
// AddPolicyCountCallback sets the function to call when exporting the
// policy count metric. You must call RegisterInfoMetrics to have this
// exported
func AddPolicyCountCallback(service string, f func() int64) {
registry.addPolicyCountCallback(service, f)
}