Skip to content
This repository has been archived by the owner on Jun 26, 2023. It is now read-only.

add metric numbers of overwritten object #1185

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion incubator/hnc/internal/reconcilers/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ func (r *ObjectReconciler) getTopSourceToPropagate(log logr.Logger, inst *unstru
for _, obj := range objs {
// If the source cannot propagate, ignore it.
// TODO: add a webhook rule to prevent e.g. removing a source finalizer that
// would cause overwrting the source objects in the descendents.
// would cause overwriting the source objects in the descendents.
// See https://github.com/kubernetes-sigs/multi-tenancy/issues/1120
if !r.shouldPropagateSource(log, obj, inst.GetNamespace()) {
continue
Expand All @@ -394,6 +394,7 @@ func (r *ObjectReconciler) syncPropagated(log logr.Logger, inst, srcInst *unstru
// Delete this local source object from the forest if it exists. (This could
// only happen when we are trying to overwrite a conflicting source).
ns.DeleteSourceObject(r.GVK, inst.GetName())
stats.OverwriteObject(r.GVK)

// If no source object exists, delete this object. This can happen when the source was deleted by
// users or the admin decided this type should no longer be propagated.
Expand Down
12 changes: 11 additions & 1 deletion incubator/hnc/internal/stats/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var (
objectReconcileConcurrent = ocstats.Int64("object_reconcile_concurrent_peak", "The peak concurrent object reconciliations happened in the last reporting period", "reconciliations")
objectWritesTotal = ocstats.Int64("object_writes_total", "The number of object writes happened during object reconciliations", "writes")
namespaceConditions = ocstats.Int64("namespace_conditions", "The number of namespaces with conditions", "conditions")
objectOverwritesTotal = ocstats.Int64("object_overwrites_total", "The number of overwritten objects", "overwrites")
)

// Create Tags. Tags are used to group and filter collected metrics later on.
Expand Down Expand Up @@ -93,7 +94,7 @@ var (
}

objectWritesView = &ocview.View{
Name: "hnc/reconcilers/object/object_writes_total",
Name: "hnc/reconcilers/object/writes_total",
Measure: objectWritesTotal,
Description: "The number of object writes happened during object reconciliations",
Aggregation: ocview.LastValue(),
Expand All @@ -107,6 +108,14 @@ var (
Aggregation: ocview.LastValue(),
TagKeys: []tag.Key{KeyNamespaceConditionType, KeyNamespaceConditionReason},
}

objectOverwritesTotalView = &ocview.View{
Name: "hnc/reconcilers/object/overwrites_total",
Measure: objectOverwritesTotal,
Description: "The number of overwritten objects",
Aggregation: ocview.LastValue(),
TagKeys: []tag.Key{KeyGroupKind},
}
)

// periodicPeak contains periodic peaks for concurrent reconciliations.
Expand All @@ -133,6 +142,7 @@ func startRecordingMetrics() {
objectReconcileConcurrentView,
objectWritesView,
namespaceConditionsView,
objectOverwritesTotalView,
); err != nil {
log.Error(err, "Failed to register the views")
}
Expand Down
20 changes: 19 additions & 1 deletion incubator/hnc/internal/stats/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type object struct {
totalReconciles counter
curReconciles counter
apiWrites counter
totalOverwrites counter
}

type objects map[schema.GroupKind]*object
Expand Down Expand Up @@ -106,6 +107,14 @@ func WriteObject(gvk schema.GroupVersionKind) {
recordObjectMetric(stats.objects[gk].apiWrites, objectWritesTotal, gk)
}

// OverwriteObject updates the object stats by GK when writing the object.
func OverwriteObject(gvk schema.GroupVersionKind) {
gk := gvk.GroupKind()
stats.objects[gk].totalOverwrites.incr()

recordObjectMetric(stats.objects[gk].totalOverwrites, objectOverwritesTotal, gk)
}

func init() {
objects := make(map[schema.GroupKind]*object)
peak = periodicPeak{
Expand Down Expand Up @@ -166,7 +175,8 @@ func logActivity(log logr.Logger, status string) {
"TotalHierConfigReconciles", stats.totalHierConfigReconciles,
"CurHierConfigReconciles", stats.curHierConfigReconciles,
"TotalObjReconciles", getTotalObjReconciles(),
"CurObjReconciles", getCurObjReconciles())
"CurObjReconciles", getCurObjReconciles(),
"ObjectOverwrites", getObjOverwrites())
}

func getTotalObjReconciles() counter {
Expand All @@ -193,6 +203,14 @@ func getObjWrites() counter {
return writes
}

func getObjOverwrites() counter {
var overwrites counter
for _, obj := range stats.objects {
overwrites += obj.totalOverwrites
}
return overwrites
}

func max(a counter, b counter) counter {
return counter(math.Max(float64(a), float64(b)))
}