Skip to content

Commit a7cb5e7

Browse files
authored
Merge pull request #6719 from yangjunmyfm192085/updatestructlog
update migration-to-structured-logging.md
2 parents b576dc0 + d7ab088 commit a7cb5e7

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

contributors/devel/sig-instrumentation/migration-to-structured-logging.md

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,15 @@ func KObj(obj KMetadata) ObjectRef
156156
// >> I1025 00:15:15.525108 1 controller_utils.go:116] "Pod status updated" pod="kube-system/kubedns" status="ready"
157157
func KRef(namespace, name string) ObjectRef
158158

159+
// KObjSlice takes a slice of objects that implement the KMetadata interface
160+
// and returns an object that gets logged as a slice of ObjectRef values or a
161+
// string containing those values, depending on whether the logger prefers text
162+
// output or structured output.
163+
// >> klog.InfoS("Pods status updated", "pods", klog.KObjSlice(pods), "status", "ready")
164+
// output:
165+
// >> I1025 00:15:15.525108 1 controller_utils.go:116] "Pods status updated" pods="[kube-system/kubedns kube-system/metrics-server]" status="ready"
166+
func KObjSlice(arg interface{}) interface{}
167+
159168
// ObjectRef represents a reference to a kubernetes object used for logging purpose
160169
// In text logs it is serialized into "{namespace}/{name}" or "{name}" if namespace is empty
161170
type ObjectRef struct {
@@ -467,16 +476,18 @@ func ChangePodStatus(newStatus, currentStatus string) {
467476
## Good practice for passing values in structured logging
468477

469478
When passing a value for a key-value pair, please use following rules:
470-
* Prefer using Kubernetes objects (for example `*v1.Pod`) and log them using `klog.KObj`
479+
* Prefer using Kubernetes objects and log them using `klog.KObj` or `klog.KObjSlice`
471480
* When the original object is not available, use `klog.KRef` instead
481+
* when only one object (for example `*v1.Pod`), we use`klog.KObj`
482+
* When type is object slice (for example `[]*v1.Pod`), we use `klog.KObjSlice`
472483
* Pass structured values directly (avoid calling `.String()` on them first)
473484
* When the goal is to log a `[]byte` array as string, explicitly convert with `string(<byte array>)`.
474485

475-
### Prefer using Kubernetes objects (for example `*v1.Pod`) and log them using `klog.KObj`
486+
### Prefer using Kubernetes objects (for example `*v1.Pod` or `[]*v1.Pod`) and log them using `klog.KObj` or `klog.KObjSlice`
476487

477488
As part of the structured logging migration, we want to ensure that Kubernetes object references are
478489
consistent within the
479-
codebase. Two new utility functions were introduced to klog: `klog.KObj` and `klog.KRef`.
490+
codebase. Three new utility functions were introduced to klog: `klog.KObj` `klog.KObjSlice` and `klog.KRef`.
480491

481492
Any existing logging code that makes a reference (such as name, namespace) to a Kubernetes
482493
object (for example: Pod, Node, Deployment, CustomResourceDefinition) should be rewritten to

0 commit comments

Comments
 (0)