/
stats_reporter.go
72 lines (59 loc) 路 2.31 KB
/
stats_reporter.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
package mutation
import (
"context"
"github.com/open-policy-agent/gatekeeper/v3/pkg/metrics/exporters/view"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
sdkmetric "go.opentelemetry.io/otel/sdk/metric"
)
const (
mutationSystemIterationsMetricName = "mutation_system_iterations"
systemConvergenceKey = "success"
)
// SystemConvergenceStatus defines the outcomes of the attempted mutation of an object by the
// mutation System. The System is meant to converge on a fully mutated object.
type SystemConvergenceStatus string
const (
// SystemConvergenceTrue denotes a successfully converged mutation system request.
SystemConvergenceTrue SystemConvergenceStatus = "true"
// SystemConvergenceFalse denotes an unsuccessfully converged mutation system request.
SystemConvergenceFalse SystemConvergenceStatus = "false"
)
var systemIterationsM metric.Int64Histogram
func init() {
var err error
meter := otel.GetMeterProvider().Meter("gatekeeper")
systemIterationsM, err = meter.Int64Histogram(
mutationSystemIterationsMetricName,
metric.WithDescription("The distribution of Mutation System iterations before convergence"),
)
if err != nil {
panic(err)
}
view.Register(sdkmetric.NewView(
sdkmetric.Instrument{Name: mutationSystemIterationsMetricName},
sdkmetric.Stream{
Aggregation: sdkmetric.AggregationExplicitBucketHistogram{
Boundaries: []float64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 50, 100, 200, 500},
},
},
))
}
// StatsReporter reports mutator-related metrics.
type StatsReporter interface {
ReportIterationConvergence(scs SystemConvergenceStatus, iterations int) error
}
// reporter implements StatsReporter interface.
type reporter struct{}
// NewStatsReporter creates a reporter for webhook metrics.
func NewStatsReporter() StatsReporter {
return &reporter{}
}
// ReportIterationConvergence reports the success or failure of the mutation system to converge.
// It also records the number of system iterations that were required to reach this end.
func (r *reporter) ReportIterationConvergence(scs SystemConvergenceStatus, iterations int) error {
// No need for an actual Context.
systemIterationsM.Record(context.Background(), int64(iterations), metric.WithAttributes(attribute.String(systemConvergenceKey, string(scs))))
return nil
}