Skip to content

Commit

Permalink
replace KObjs with KObjSlice
Browse files Browse the repository at this point in the history
KObjSlice can be used in log calls without causing unnecessary work when the
log event does not get logged.
  • Loading branch information
pohly committed Apr 27, 2022
1 parent ff92e06 commit 7724404
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 1 deletion.
2 changes: 1 addition & 1 deletion examples/benchmarks/benchmarks_test.go
Expand Up @@ -75,7 +75,7 @@ func BenchmarkOutput(b *testing.B) {
tests = append(tests, testcase{
name: fmt.Sprintf("objects/%d", length),
generate: func() interface{} {
return klog.KObjs(arg)
return klog.KObjSlice(arg)
},
})
}
Expand Down
56 changes: 56 additions & 0 deletions k8s_references.go
Expand Up @@ -77,6 +77,8 @@ func KRef(namespace, name string) ObjectRef {
}

// KObjs returns slice of ObjectRef from an slice of ObjectMeta
//
// DEPRECATED: Use KObjSlice instead, it has better performance.
func KObjs(arg interface{}) []ObjectRef {
s := reflect.ValueOf(arg)
if s.Kind() != reflect.Slice {
Expand All @@ -92,3 +94,57 @@ func KObjs(arg interface{}) []ObjectRef {
}
return objectRefs
}

// KObjSlice takes a slice of objects that implement the KMetadata interface
// and returns an object that gets logged as a slice of ObjectRef values or a
// string containing those values, depending on whether the logger prefers text
// output or structured output.
//
// An error string is logged when KObjSlice is not passed a suitable slice.
//
// Processing of the argument is delayed until the value actually gets logged,
// in contrast to KObjs where that overhead is incurred regardless of whether
// the result is needed.
func KObjSlice(arg interface{}) interface{} {
return kobjSlice{arg: arg}
}

type kobjSlice struct {
arg interface{}
}

var _ fmt.Stringer = kobjSlice{}
var _ logr.Marshaler = kobjSlice{}

func (ks kobjSlice) String() string {
objectRefs, err := ks.process()
if err != nil {
return err.Error()
}
return fmt.Sprintf("%v", objectRefs)
}

func (ks kobjSlice) MarshalLog() interface{} {
objectRefs, err := ks.process()
if err != nil {
return err.Error()
}
return objectRefs
}

func (ks kobjSlice) process() ([]ObjectRef, error) {
s := reflect.ValueOf(ks.arg)
if s.Kind() != reflect.Slice {
return nil, fmt.Errorf("<KObjSlice needs a slice, got type %T>", ks.arg)
}
objectRefs := make([]ObjectRef, 0, s.Len())
for i := 0; i < s.Len(); i++ {
item := s.Index(i).Interface()
if v, ok := item.(KMetadata); ok {
objectRefs = append(objectRefs, KObj(v))
} else {
return nil, fmt.Errorf("<KObjSlice needs a slice of values implementing KMetadata, got type %T>", item)
}
}
return objectRefs, nil
}
34 changes: 34 additions & 0 deletions test/output.go
Expand Up @@ -268,6 +268,40 @@ I output.go:<LINE>] "test" firstKey=1 secondKey=3
&kmeta{Name: "pod-2", Namespace: "kube-system"},
})},
expectedOutput: `I output.go:<LINE>] "test" pods=[kube-system/pod-1 kube-system/pod-2]
`,
},
"KObjSlice okay": {
text: "test",
values: []interface{}{"pods",
klog.KObjSlice([]interface{}{
&kmeta{Name: "pod-1", Namespace: "kube-system"},
&kmeta{Name: "pod-2", Namespace: "kube-system"},
})},
expectedOutput: `I output.go:<LINE>] "test" pods="[kube-system/pod-1 kube-system/pod-2]"
`,
},
"KObjSlice nil arg": {
text: "test",
values: []interface{}{"pods",
klog.KObjSlice(nil)},
expectedOutput: `I output.go:<LINE>] "test" pods="<KObjSlice needs a slice, got type <nil>>"
`,
},
"KObjSlice nil entry": {
text: "test",
values: []interface{}{"pods",
klog.KObjSlice([]interface{}{
&kmeta{Name: "pod-1", Namespace: "kube-system"},
nil,
})},
expectedOutput: `I output.go:<LINE>] "test" pods="<KObjSlice needs a slice of values implementing KMetadata, got type <nil>>"
`,
},
"KObjSlice ints": {
text: "test",
values: []interface{}{"ints",
klog.KObjSlice([]int{1, 2, 3})},
expectedOutput: `I output.go:<LINE>] "test" ints="<KObjSlice needs a slice of values implementing KMetadata, got type int>"
`,
},
"regular error types as value": {
Expand Down
16 changes: 16 additions & 0 deletions test/zapr.go
Expand Up @@ -67,6 +67,22 @@ func ZaprOutputMappingDirect() map[string]string {

`I output.go:<LINE>] "test" pods=[kube-system/pod-1 kube-system/pod-2]
`: `{"caller":"test/output.go:<LINE>","msg":"test","v":0,"pods":[{"name":"pod-1","namespace":"kube-system"},{"name":"pod-2","namespace":"kube-system"}]}
`,

`I output.go:<LINE>] "test" pods="[kube-system/pod-1 kube-system/pod-2]"
`: `{"caller":"test/output.go:<LINE>","msg":"test","v":0,"pods":[{"name":"pod-1","namespace":"kube-system"},{"name":"pod-2","namespace":"kube-system"}]}
`,

`I output.go:<LINE>] "test" pods="<KObjSlice needs a slice, got type <nil>>"
`: `{"caller":"test/output.go:<LINE>","msg":"test","v":0,"pods":"<KObjSlice needs a slice, got type <nil>>"}
`,

`I output.go:<LINE>] "test" ints="<KObjSlice needs a slice of values implementing KMetadata, got type int>"
`: `{"caller":"test/output.go:<LINE>","msg":"test","v":0,"ints":"<KObjSlice needs a slice of values implementing KMetadata, got type int>"}
`,

`I output.go:<LINE>] "test" pods="<KObjSlice needs a slice of values implementing KMetadata, got type <nil>>"
`: `{"caller":"test/output.go:<LINE>","msg":"test","v":0,"pods":"<KObjSlice needs a slice of values implementing KMetadata, got type <nil>>"}
`,

`I output.go:<LINE>] "test" akey="avalue"
Expand Down

0 comments on commit 7724404

Please sign in to comment.