Skip to content
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

otelslog: Remove the pooled buffer #5878

Merged
merged 3 commits into from
Jul 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 5 additions & 29 deletions bridges/otelslog/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ import (
"fmt"
"log/slog"
"slices"
"sync"

"go.opentelemetry.io/otel/log"
"go.opentelemetry.io/otel/log/global"
Expand Down Expand Up @@ -180,8 +179,7 @@ func (h *Handler) convertRecord(r slog.Record) log.Record {
n := r.NumAttrs()
if h.group != nil {
if n > 0 {
buf, free := getKVBuffer()
defer free()
buf := newKVBuffer(n)
r.Attrs(buf.AddAttr)
record.AddAttributes(h.group.KeyValue(buf.KeyValues()...))
} else {
Expand All @@ -192,8 +190,7 @@ func (h *Handler) convertRecord(r slog.Record) log.Record {
}
}
} else if n > 0 {
buf, free := getKVBuffer()
defer free()
buf := newKVBuffer(n)
r.Attrs(buf.AddAttr)
record.AddAttributes(buf.KeyValues()...)
}
Expand Down Expand Up @@ -338,27 +335,6 @@ func (g *group) AddAttrs(attrs []slog.Attr) {
g.attrs.AddAttrs(attrs)
}

var kvBufferPool = sync.Pool{
New: func() any {
// Based on slog research (https://go.dev/blog/slog#performance), 95%
// of use-cases will use 5 or less attributes.
return newKVBuffer(5)
},
}

func getKVBuffer() (buf *kvBuffer, free func()) {
buf = kvBufferPool.Get().(*kvBuffer)
return buf, func() {
// TODO: limit returned size so the pool doesn't hold on to very large
// buffers. Idea is based on
// https://cs.opensource.google/go/x/exp/+/814bf88c:slog/internal/buffer/buffer.go;l=27-34

// Do not modify any previously held data.
buf.data = buf.data[:0:0]
kvBufferPool.Put(buf)
}
}

type kvBuffer struct {
data []log.KeyValue
}
Expand Down Expand Up @@ -451,9 +427,9 @@ func convertValue(v slog.Value) log.Value {
case slog.KindUint64:
return log.Int64Value(int64(v.Uint64()))
case slog.KindGroup:
buf, free := getKVBuffer()
defer free()
buf.AddAttrs(v.Group())
g := v.Group()
buf := newKVBuffer(len(g))
buf.AddAttrs(g)
return log.MapValue(buf.data...)
case slog.KindLogValuer:
return convertValue(v.Resolve())
Expand Down