-
Notifications
You must be signed in to change notification settings - Fork 434
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix ever-accumulating memory in logger #5284
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -88,14 +88,18 @@ | |
) | ||
} | ||
|
||
func (s *spanLogSink) WithValues(keysAndValues ...interface{}) logr.LogSink { | ||
s.vals = append(s.vals, keysAndValues...) | ||
return s | ||
func (s spanLogSink) WithValues(keysAndValues ...interface{}) logr.LogSink { | ||
// always create a new slice to avoid multiple loggers writing to the same backing array | ||
vals := make([]interface{}, len(s.vals)+len(keysAndValues)) | ||
copy(vals, s.vals) | ||
copy(vals[len(s.vals):], keysAndValues) | ||
s.vals = vals | ||
Comment on lines
+92
to
+96
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Beyond just the memory issue, I noticed a separate issue where a plain |
||
return &s | ||
} | ||
|
||
func (s *spanLogSink) WithName(name string) logr.LogSink { | ||
func (s spanLogSink) WithName(name string) logr.LogSink { | ||
s.name = name | ||
return s | ||
return &s | ||
} | ||
|
||
// NewSpanLogSink is the main entry-point to this implementation. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
Copyright 2024 The Kubernetes Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package tele | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/go-logr/logr" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestSpanLogSinkWithValues(t *testing.T) { | ||
g := NewGomegaWithT(t) | ||
|
||
var log0 logr.LogSink = &spanLogSink{ | ||
// simulating a slice with cap() > len() where an append() will not create a new array | ||
vals: make([]interface{}, 0, 4), | ||
} | ||
|
||
log0 = log0.WithValues("k0", "v0") | ||
|
||
g.Expect(log0.(*spanLogSink).vals).To(HaveExactElements("k0", "v0")) | ||
|
||
log1 := log0.WithValues("k1", "v1") | ||
|
||
g.Expect(log0.(*spanLogSink).vals).To(HaveExactElements("k0", "v0")) | ||
g.Expect(log1.(*spanLogSink).vals).To(HaveExactElements("k0", "v0", "k1", "v1")) | ||
|
||
log2 := log0.WithValues("k2", "v2") | ||
|
||
g.Expect(log0.(*spanLogSink).vals).To(HaveExactElements("k0", "v0")) | ||
g.Expect(log1.(*spanLogSink).vals).To(HaveExactElements("k0", "v0", "k1", "v1")) | ||
g.Expect(log2.(*spanLogSink).vals).To(HaveExactElements("k0", "v0", "k2", "v2")) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The issue with this is that
s
is being mutated through the pointer receiver, so this slice will grow forever and can't be garbage collected. The method receiver here is changed to a value, which fixes the memory issue.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is how the default
klog
implementation is set up: https://github.com/kubernetes/klog/blob/75663bb798999a49e3e4c0f2375ed5cca8164194/klogr.go#L94