Skip to content

Commit 787f953

Browse files
authored
remove duplicate log fields (#52)
The use of `logr.Logger.WithValues` in the `pkg/runtime/log.AdoptResource` function was not actually overwriting previously set key/value pairs -- for instance, for the resource's kind, generation or namespace. Instead, these structured log record fields were simply being appended to the end of the `[]interface{}` that `logr.Logger` uses to track log record fields. See https://pkg.go.dev/github.com/go-logr/logr#readme-why-key-value-pairs-and-not-a-map for information on why a `[]interface{}` is used instead of a `map[string]interface{}`, which would have avoided this bug/problem. This patch removes the use of `logr.Logger.WithValues` in the `ResourceLogger.Debug`, `ResourceLogger.Enter|Exit` and ResourceLogger.Info` methods and replaces with direct passing of the core resource fields into the `logr.Logger.Info` method. Fixes Issue aws-controllers-k8s/runtime#938 By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent 19d9f59 commit 787f953

File tree

1 file changed

+57
-35
lines changed

1 file changed

+57
-35
lines changed

pkg/runtime/log/resource.go

Lines changed: 57 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ func (rl *ResourceLogger) Debug(
4444
msg string,
4545
additionalValues ...interface{},
4646
) {
47-
AdaptResource(rl.log, rl.res, additionalValues...).V(1).Info(msg)
47+
vals := expandResourceFields(rl.res, additionalValues...)
48+
rl.log.V(1).Info(msg, vals...)
4849
}
4950

5051
// Info writes a supplied log message about a resource that includes a
@@ -53,7 +54,8 @@ func (rl *ResourceLogger) Info(
5354
msg string,
5455
additionalValues ...interface{},
5556
) {
56-
AdaptResource(rl.log, rl.res, additionalValues...).V(0).Info(msg)
57+
vals := expandResourceFields(rl.res, additionalValues...)
58+
rl.log.V(0).Info(msg, vals...)
5759
}
5860

5961
// Enter logs an entry to a function or code block
@@ -65,7 +67,8 @@ func (rl *ResourceLogger) Enter(
6567
rl.blockDepth++
6668
depth := strings.Repeat(">", rl.blockDepth)
6769
msg := depth + " " + name
68-
AdaptResource(rl.log, rl.res, additionalValues...).V(1).Info(msg)
70+
vals := expandResourceFields(rl.res, additionalValues...)
71+
rl.log.V(1).Info(msg, vals...)
6972
}
7073
}
7174

@@ -82,8 +85,8 @@ func (rl *ResourceLogger) Exit(
8285
additionalValues = append(additionalValues, "error")
8386
additionalValues = append(additionalValues, err)
8487
}
85-
AdaptResource(rl.log, rl.res, additionalValues...).V(1).Info(msg)
86-
rl.blockDepth--
88+
vals := expandResourceFields(rl.res, additionalValues...)
89+
rl.log.V(1).Info(msg, vals...)
8790
}
8891
}
8992

@@ -121,23 +124,7 @@ func AdaptResource(
121124
res acktypes.AWSResource,
122125
additionalValues ...interface{},
123126
) logr.Logger {
124-
metaObj := res.MetaObject()
125-
ns := metaObj.GetNamespace()
126-
resName := metaObj.GetName()
127-
generation := metaObj.GetGeneration()
128-
rtObj := res.RuntimeObject()
129-
kind := rtObj.GetObjectKind().GroupVersionKind().Kind
130-
vals := []interface{}{
131-
"kind", kind,
132-
"namespace", ns,
133-
"name", resName,
134-
"generation", generation,
135-
}
136-
if len(additionalValues) > 0 {
137-
for _, v := range additionalValues {
138-
vals = append(vals, v)
139-
}
140-
}
127+
vals := expandResourceFields(res, additionalValues...)
141128
return log.WithValues(vals...)
142129
}
143130

@@ -163,28 +150,38 @@ func InfoResource(
163150
AdaptResource(log, res, additionalValues...).V(0).Info(msg)
164151
}
165152

166-
// AdaptAdoptedResource returns a logger with log values set for the adopted
167-
// resource's kind, namespace, name, etc
168-
func AdaptAdoptedResource(
169-
log logr.Logger,
170-
res *v1alpha1.AdoptedResource,
153+
// expandResourceFields returns the key/value pairs for a resource that should
154+
// be used as structured data in log messages about the resource
155+
func expandResourceFields(
156+
res acktypes.AWSResource,
171157
additionalValues ...interface{},
172-
) logr.Logger {
173-
ns := res.Namespace
174-
resName := res.Name
175-
generation := res.Generation
176-
group := res.Spec.Kubernetes.Group
177-
kind := res.Spec.Kubernetes.Kind
158+
) []interface{} {
159+
metaObj := res.MetaObject()
160+
ns := metaObj.GetNamespace()
161+
resName := metaObj.GetName()
162+
generation := metaObj.GetGeneration()
163+
rtObj := res.RuntimeObject()
164+
kind := rtObj.GetObjectKind().GroupVersionKind().Kind
178165
vals := []interface{}{
179-
"target_group", group,
180-
"target_kind", kind,
166+
"kind", kind,
181167
"namespace", ns,
182168
"name", resName,
183169
"generation", generation,
184170
}
185171
if len(additionalValues) > 0 {
186172
vals = append(vals, additionalValues...)
187173
}
174+
return vals
175+
}
176+
177+
// AdaptAdoptedResource returns a logger with log values set for the adopted
178+
// resource's kind, namespace, name, etc
179+
func AdaptAdoptedResource(
180+
log logr.Logger,
181+
res *v1alpha1.AdoptedResource,
182+
additionalValues ...interface{},
183+
) logr.Logger {
184+
vals := expandAdoptedResourceFields(res, additionalValues...)
188185
return log.WithValues(vals...)
189186
}
190187

@@ -209,3 +206,28 @@ func InfoAdoptedResource(
209206
) {
210207
AdaptAdoptedResource(log, res, additionalValues...).V(0).Info(msg)
211208
}
209+
210+
// expandAdoptedResourceFields returns the key/value pairs for an adopted
211+
// resource that should be used as structured data in log messages about the
212+
// adopted resource
213+
func expandAdoptedResourceFields(
214+
res *v1alpha1.AdoptedResource,
215+
additionalValues ...interface{},
216+
) []interface{} {
217+
ns := res.Namespace
218+
resName := res.Name
219+
generation := res.Generation
220+
group := res.Spec.Kubernetes.Group
221+
kind := res.Spec.Kubernetes.Kind
222+
vals := []interface{}{
223+
"target_group", group,
224+
"target_kind", kind,
225+
"namespace", ns,
226+
"name", resName,
227+
"generation", generation,
228+
}
229+
if len(additionalValues) > 0 {
230+
vals = append(vals, additionalValues...)
231+
}
232+
return vals
233+
}

0 commit comments

Comments
 (0)