garbagecollector: structured logging of objectReference

When using JSON as output format, we want objectReference values to be
represented as a struct. For example, "item" is such a value:

{"ts":1678135015708.349,"caller":"garbagecollector/garbagecollector.go:595","msg":"classify object references","v":5,"item":{"name":"dra-test-driver-g4tkd","namespace":"dra-1830","apiVersion":"v1","uid":"c3f88616-7282-488c-887c-3f04291e6f4f"},"solid":null,"dangling":[{"apiVersion":"apps/v1","kind":"ReplicaSet","name":"dra-test-driver","uid":"dbe9a90c-9dfd-4ad0-8395-e5fa228f9851","controller":true,"blockOwnerDeletion":true}],"waitingForDependentsDeletion":null}
This commit is contained in:
Patrick Ohly 2023-03-06 21:37:31 +01:00 committed by Andy Goldstein
parent 26e3dab78b
commit cbf7d96a85

View File

@ -20,6 +20,8 @@ import (
"fmt"
"sync"
"github.com/go-logr/logr"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
)
@ -30,10 +32,29 @@ type objectReference struct {
Namespace string
}
// String is used when logging an objectReference in text format.
func (s objectReference) String() string {
return fmt.Sprintf("[%s/%s, namespace: %s, name: %s, uid: %s]", s.APIVersion, s.Kind, s.Namespace, s.Name, s.UID)
}
// MarshalLog is used when logging an objectReference in JSON format.
func (s objectReference) MarshalLog() interface{} {
return struct {
Name string `json:"name"`
Namespace string `json:"namespace"`
APIVersion string `json:"apiVersion"`
UID types.UID `json:"uid"`
}{
Namespace: s.Namespace,
Name: s.Name,
APIVersion: s.APIVersion,
UID: s.UID,
}
}
var _ fmt.Stringer = objectReference{}
var _ logr.Marshaler = objectReference{}
// The single-threaded GraphBuilder.processGraphChanges() is the sole writer of the
// nodes. The multi-threaded GarbageCollector.attemptToDeleteItem() reads the nodes.
// WARNING: node has different locks on different fields. setters and getters