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

Drop keyvals with duplicated keys #10

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
94 changes: 47 additions & 47 deletions sdk/log/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestZeroAllocsSimple(t *testing.T) {
slog.Float64("float", testFloat),
slog.Int("int", testInt),
slog.Bool("bool", testBool),
slog.String("string", testString),
slog.String("string2", testString),
)
}))
}
Expand All @@ -55,7 +55,7 @@ func TestZeroAllocsModifyProcessor(t *testing.T) {
slog.Float64("float", testFloat),
slog.Int("int", testInt),
slog.Bool("bool", testBool),
slog.String("string", testString),
slog.String("string2", testString),
)
}))
}
Expand All @@ -71,7 +71,7 @@ func TestZeroAllocsBatch(t *testing.T) {
slog.Float64("float", testFloat),
slog.Int("int", testInt),
slog.Bool("bool", testBool),
slog.String("string", testString),
slog.String("string2", testString),
)
}))
}
Expand All @@ -87,7 +87,7 @@ func TestZeroAllocsNoSpan(t *testing.T) {
slog.Float64("float", testFloat),
slog.Int("int", testInt),
slog.Bool("bool", testBool),
slog.String("string", testString),
slog.String("string2", testString),
)
}))
}
Expand Down Expand Up @@ -121,7 +121,7 @@ func Benchmark(b *testing.B) {
slog.Float64("float", testFloat),
slog.Int("int", testInt),
slog.Bool("bool", testBool),
slog.String("string", testString),
slog.String("string2", testString),
)
},
},
Expand All @@ -133,12 +133,12 @@ func Benchmark(b *testing.B) {
slog.Float64("float", testFloat),
slog.Int("int", testInt),
slog.Bool("bool", testBool),
slog.String("string", testString),
slog.String("string", testString),
slog.Float64("float", testFloat),
slog.Int("int", testInt),
slog.Bool("bool", testBool),
slog.String("string", testString),
slog.String("string2", testString),
slog.String("string3", testString),
slog.Float64("float4", testFloat),
slog.Int("int5", testInt),
slog.Bool("bool6", testBool),
slog.String("string7", testString),
)
},
},
Expand All @@ -150,42 +150,42 @@ func Benchmark(b *testing.B) {
slog.Float64("float", testFloat),
slog.Int("int", testInt),
slog.Bool("bool", testBool),
slog.String("string", testString),
slog.String("string", testString),
slog.Float64("float", testFloat),
slog.Int("int", testInt),
slog.Bool("bool", testBool),
slog.String("string", testString),
slog.String("string", testString),
slog.Float64("float", testFloat),
slog.Int("int", testInt),
slog.Bool("bool", testBool),
slog.String("string", testString),
slog.String("string", testString),
slog.Float64("float", testFloat),
slog.Int("int", testInt),
slog.Bool("bool", testBool),
slog.String("string", testString),
slog.String("string", testString),
slog.Float64("float", testFloat),
slog.Int("int", testInt),
slog.Bool("bool", testBool),
slog.String("string", testString),
slog.String("string", testString),
slog.Float64("float", testFloat),
slog.Int("int", testInt),
slog.Bool("bool", testBool),
slog.String("string", testString),
slog.String("string", testString),
slog.Float64("float", testFloat),
slog.Int("int", testInt),
slog.Bool("bool", testBool),
slog.String("string", testString),
slog.String("string", testString),
slog.Float64("float", testFloat),
slog.Int("int", testInt),
slog.Bool("bool", testBool),
slog.String("string", testString),
slog.String("string2", testString),
slog.String("string3", testString),
slog.Float64("float4", testFloat),
slog.Int("int5", testInt),
slog.Bool("bool6", testBool),
slog.String("string6", testString),
slog.String("string7", testString),
slog.Float64("float8", testFloat),
slog.Int("int9", testInt),
slog.Bool("bool10", testBool),
slog.String("string11", testString),
slog.String("string12", testString),
slog.Float64("float13", testFloat),
slog.Int("int14", testInt),
slog.Bool("bool15", testBool),
slog.String("string16", testString),
slog.String("string17", testString),
slog.Float64("float18", testFloat),
slog.Int("int19", testInt),
slog.Bool("bool20", testBool),
slog.String("string21", testString),
slog.String("string22", testString),
slog.Float64("float23", testFloat),
slog.Int("int24", testInt),
slog.Bool("bool25", testBool),
slog.String("string26", testString),
slog.String("string27", testString),
slog.Float64("float28", testFloat),
slog.Int("int29", testInt),
slog.Bool("bool30", testBool),
slog.String("string31", testString),
slog.String("string32", testString),
slog.Float64("float33", testFloat),
slog.Int("int34", testInt),
slog.Bool("bool35", testBool),
slog.String("string36", testString),
)
},
},
Expand Down
51 changes: 51 additions & 0 deletions sdk/log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package log // import "go.opentelemetry.io/otel/sdk/log"

import (
"context"
"sync"
"time"

"go.opentelemetry.io/otel"
Expand All @@ -17,6 +18,13 @@ import (
// Compile-time check logger implements metric.log.Logger.
var _ log.Logger = (*logger)(nil)

var keysPool = sync.Pool{
New: func() any {
keys := map[string]struct{}{}
return &keys
},
}

type logger struct {
embedded.Logger

Expand Down Expand Up @@ -49,7 +57,24 @@ func (l *logger) Emit(ctx context.Context, r log.Record) {
record.traceFlags = span.TraceFlags()
}

keysPtr := keysPool.Get().(*map[string]struct{})
defer func() {
clear(*keysPtr)
keysPool.Put(keysPtr)
}()
keys := *keysPtr
r.WalkAttributes(func(kv log.KeyValue) bool {
if _, ok := keys[kv.Key]; ok {
// Drop duplicated log attribute.
return true
}

keys[kv.Key] = struct{}{}

if hasDuplicatedKey(kv.Value) {
// Drop attribute which contains any key-value with duplicated key.
return true
}
record.AddAttributes(kv)
return true
})
Expand All @@ -60,3 +85,29 @@ func (l *logger) Emit(ctx context.Context, r log.Record) {
}
}
}

func hasDuplicatedKey(value log.Value) bool {
if value.Kind() != log.KindMap {
return false
}

keysPtr := keysPool.Get().(*map[string]struct{})
defer func() {
clear(*keysPtr)
keysPool.Put(keysPtr)
}()
keys := *keysPtr
m := value.AsMap()
for _, kv := range m {
if _, ok := keys[kv.Key]; ok {
return true
}
keys[kv.Key] = struct{}{}

if hasDuplicatedKey(kv.Value) {
return true
}
}

return false
Comment on lines +94 to +112
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is not even covered by the benchmark 😉

}
Loading