-
Notifications
You must be signed in to change notification settings - Fork 779
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add metric reporting to ExpansionTemplate controller (#2276)
* Add metric reporting to ExpansionTemplate controller Signed-off-by: davis-haba <davishaba@google.com> * Update metrics docs with gatekeeper_expansion_templates metric Signed-off-by: davis-haba <davishaba@google.com> Signed-off-by: davis-haba <davishaba@google.com> Co-authored-by: Rita Zhang <rita.z.zhang@gmail.com>
- Loading branch information
1 parent
ee3a992
commit 099d71d
Showing
3 changed files
with
98 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package expansion | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/open-policy-agent/gatekeeper/pkg/metrics" | ||
"go.opencensus.io/stats" | ||
"go.opencensus.io/stats/view" | ||
"k8s.io/apimachinery/pkg/types" | ||
) | ||
|
||
const ( | ||
etMetricName = "expansion_templates" | ||
etDesc = "Number of observed expansion templates" | ||
) | ||
|
||
var ( | ||
etM = stats.Int64(etMetricName, etDesc, stats.UnitDimensionless) | ||
|
||
views = []*view.View{ | ||
{ | ||
Name: etMetricName, | ||
Measure: etM, | ||
Description: etDesc, | ||
Aggregation: view.LastValue(), | ||
}, | ||
} | ||
) | ||
|
||
func init() { | ||
if err := register(); err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
func register() error { | ||
return view.Register(views...) | ||
} | ||
|
||
func newRegistry() *etRegistry { | ||
return &etRegistry{cache: make(map[types.NamespacedName]bool)} | ||
} | ||
|
||
type etRegistry struct { | ||
cache map[types.NamespacedName]bool | ||
dirty bool | ||
} | ||
|
||
func (r *etRegistry) add(key types.NamespacedName) { | ||
r.cache[key] = true | ||
r.dirty = true | ||
} | ||
|
||
func (r *etRegistry) remove(key types.NamespacedName) { | ||
delete(r.cache, key) | ||
r.dirty = true | ||
} | ||
|
||
func (r *etRegistry) report(ctx context.Context) error { | ||
if !r.dirty { | ||
return nil | ||
} | ||
|
||
if err := metrics.Record(ctx, etM.M(int64(len(r.cache)))); err != nil { | ||
r.dirty = false | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters