Debian Codegraph @

sdk/log/record_test.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package log
5
6 import (
7 "fmt"
8 "slices"
9 "strconv"
10 "strings"
11 "sync"
12 "testing"
13 "time"
14
15 "github.com/stretchr/testify/assert"
16 "github.com/stretchr/testify/require"
17
18 "go.opentelemetry.io/otel/attribute"
19 "go.opentelemetry.io/otel/log"
20 "go.opentelemetry.io/otel/sdk/instrumentation"
21 "go.opentelemetry.io/otel/sdk/resource"
22 "go.opentelemetry.io/otel/trace"
23 )
24
25 func TestRecordEventName(t *testing.T) {
26 const text = "testing text"
27
28 r := new(Record)
29 r.SetEventName(text)
30 assert.Equal(t, text, r.EventName())
31 }
32
33 func TestRecordTimestamp(t *testing.T) {
34 now := time.Now()
35 r := new(Record)
36 r.SetTimestamp(now)
37 assert.Equal(t, now, r.Timestamp())
38 }
39
40 func TestRecordObservedTimestamp(t *testing.T) {
41 now := time.Now()
42 r := new(Record)
43 r.SetObservedTimestamp(now)
44 assert.Equal(t, now, r.ObservedTimestamp())
45 }
46
47 func TestRecordSeverity(t *testing.T) {
48 s := log.SeverityInfo
49 r := new(Record)
50 r.SetSeverity(s)
51 assert.Equal(t, s, r.Severity())
52 }
53
54 func TestRecordSeverityText(t *testing.T) {
55 text := "text"
56 r := new(Record)
57 r.SetSeverityText(text)
58 assert.Equal(t, text, r.SeverityText())
59 }
60
61 func TestRecordBody(t *testing.T) {
62 testcases := []struct {
63 name string
64 allowDuplicates bool
65 body log.Value
66 want log.Value
67 }{
68 {
69 name: "boolean value",
70 body: log.BoolValue(true),
71 want: log.BoolValue(true),
72 },
73 {
74 name: "slice",
75 body: log.SliceValue(log.BoolValue(true), log.BoolValue(false)),
76 want: log.SliceValue(log.BoolValue(true), log.BoolValue(false)),
77 },
78 {
79 name: "map",
80 body: log.MapValue(
81 log.Bool("0", true),
82 log.Int64("1", 2), // This should be removed
83 log.Float64("2", 3.0),
84 log.String("3", "forth"),
85 log.Slice("4", log.Int64Value(1)),
86 log.Map("5", log.Int("key", 2)),
87 log.Bytes("6", []byte("six")),
88 log.Int64("1", 3),
89 ),
90 want: log.MapValue(
91 log.Bool("0", true),
92 log.Float64("2", 3.0),
93 log.String("3", "forth"),
94 log.Slice("4", log.Int64Value(1)),
95 log.Map("5", log.Int("key", 2)),
96 log.Bytes("6", []byte("six")),
97 log.Int64("1", 3),
98 ),
99 },
100 {
101 name: "nested map",
102 body: log.MapValue(
103 log.Map("key",
104 log.Int64("key", 1),
105 log.Int64("key", 2),
106 ),
107 ),
108 want: log.MapValue(
109 log.Map("key",
110 log.Int64("key", 2),
111 ),
112 ),
113 },
114 {
115 name: "map - allow duplicates",
116 allowDuplicates: true,
117 body: log.MapValue(
118 log.Int64("1", 2),
119 log.Int64("1", 3),
120 ),
121 want: log.MapValue(
122 log.Int64("1", 2),
123 log.Int64("1", 3),
124 ),
125 },
126 {
127 name: "slice with nested deduplication",
128 body: log.SliceValue(
129 log.MapValue(log.String("key", "value1"), log.String("key", "value2")),
130 log.StringValue("normal"),
131 log.SliceValue(
132 log.MapValue(log.String("nested", "val1"), log.String("nested", "val2")),
133 ),
134 ),
135 want: log.SliceValue(
136 log.MapValue(log.String("key", "value2")),
137 log.StringValue("normal"),
138 log.SliceValue(
139 log.MapValue(log.String("nested", "val2")),
140 ),
141 ),
142 },
143 {
144 name: "empty slice",
145 body: log.SliceValue(),
146 want: log.SliceValue(),
147 },
148 {
149 name: "empty map",
150 body: log.MapValue(),
151 want: log.MapValue(),
152 },
153 {
154 name: "single key map",
155 body: log.MapValue(log.String("single", "value")),
156 want: log.MapValue(log.String("single", "value")),
157 },
158 {
159 name: "slice with no deduplication needed",
160 body: log.SliceValue(
161 log.StringValue("value1"),
162 log.StringValue("value2"),
163 log.MapValue(log.String("unique1", "val1")),
164 log.MapValue(log.String("unique2", "val2")),
165 ),
166 want: log.SliceValue(
167 log.StringValue("value1"),
168 log.StringValue("value2"),
169 log.MapValue(log.String("unique1", "val1")),
170 log.MapValue(log.String("unique2", "val2")),
171 ),
172 },
173 {
174 name: "deeply nested slice and map structure",
175 body: log.SliceValue(
176 log.MapValue(
177 log.String("outer", "value"),
178 log.Slice("inner_slice",
179 log.MapValue(log.String("deep", "value1"), log.String("deep", "value2")),
180 ),
181 ),
182 ),
183 want: log.SliceValue(
184 log.MapValue(
185 log.String("outer", "value"),
186 log.Slice("inner_slice",
187 log.MapValue(log.String("deep", "value2")),
188 ),
189 ),
190 ),
191 },
192 {
193 name: "slice with duplicates allowed",
194 allowDuplicates: true,
195 body: log.SliceValue(
196 log.MapValue(log.String("key", "value1"), log.String("key", "value2")),
197 ),
198 want: log.SliceValue(
199 log.MapValue(log.String("key", "value1"), log.String("key", "value2")),
200 ),
201 },
202 {
203 name: "string value",
204 body: log.StringValue("test"),
205 want: log.StringValue("test"),
206 },
207 {
208 name: "boolean value without deduplication",
209 body: log.BoolValue(true),
210 want: log.BoolValue(true),
211 },
212 {
213 name: "integer value",
214 body: log.Int64Value(42),
215 want: log.Int64Value(42),
216 },
217 {
218 name: "float value",
219 body: log.Float64Value(3.14),
220 want: log.Float64Value(3.14),
221 },
222 {
223 name: "bytes value",
224 body: log.BytesValue([]byte("test")),
225 want: log.BytesValue([]byte("test")),
226 },
227 {
228 name: "empty slice",
229 body: log.SliceValue(),
230 want: log.SliceValue(),
231 },
232 {
233 name: "slice without nested deduplication",
234 body: log.SliceValue(log.StringValue("test"), log.BoolValue(true)),
235 want: log.SliceValue(log.StringValue("test"), log.BoolValue(true)),
236 },
237 {
238 name: "slice with nested deduplication needed",
239 body: log.SliceValue(log.MapValue(log.String("key", "value1"), log.String("key", "value2"))),
240 want: log.SliceValue(log.MapValue(log.String("key", "value2"))),
241 },
242 {
243 name: "empty map",
244 body: log.MapValue(),
245 want: log.MapValue(),
246 },
247 {
248 name: "single key map",
249 body: log.MapValue(log.String("key", "value")),
250 want: log.MapValue(log.String("key", "value")),
251 },
252 {
253 name: "map with duplicate keys",
254 body: log.MapValue(log.String("key", "value1"), log.String("key", "value2")),
255 want: log.MapValue(log.String("key", "value2")),
256 },
257 {
258 name: "map without duplicates",
259 body: log.MapValue(log.String("key1", "value1"), log.String("key2", "value2")),
260 want: log.MapValue(log.String("key1", "value1"), log.String("key2", "value2")),
261 },
262 {
263 name: "map with nested slice deduplication",
264 body: log.MapValue(
265 log.Slice("slice", log.MapValue(log.String("nested", "val1"), log.String("nested", "val2"))),
266 ),
267 want: log.MapValue(
268 log.Slice("slice", log.MapValue(log.String("nested", "val2"))),
269 ),
270 },
271 {
272 name: "deeply nested structure with deduplication",
273 body: log.SliceValue(
274 log.MapValue(
275 log.Map("nested",
276 log.String("key", "value1"),
277 log.String("key", "value2"),
278 ),
279 ),
280 ),
281 want: log.SliceValue(
282 log.MapValue(
283 log.Map("nested",
284 log.String("key", "value2"),
285 ),
286 ),
287 ),
288 },
289 {
290 name: "deeply nested structure without deduplication",
291 body: log.SliceValue(
292 log.MapValue(
293 log.Map("nested",
294 log.String("key1", "value1"),
295 log.String("key2", "value2"),
296 ),
297 ),
298 ),
299 want: log.SliceValue(
300 log.MapValue(
301 log.Map("nested",
302 log.String("key1", "value1"),
303 log.String("key2", "value2"),
304 ),
305 ),
306 ),
307 },
308 {
309 name: "string value for collection deduplication",
310 body: log.StringValue("test"),
311 want: log.StringValue("test"),
312 },
313 {
314 name: "boolean value for collection deduplication",
315 body: log.BoolValue(true),
316 want: log.BoolValue(true),
317 },
318 {
319 name: "empty slice for collection deduplication",
320 body: log.SliceValue(),
321 want: log.SliceValue(),
322 },
323 {
324 name: "slice without nested deduplication for collection testing",
325 body: log.SliceValue(log.StringValue("test"), log.BoolValue(true)),
326 want: log.SliceValue(log.StringValue("test"), log.BoolValue(true)),
327 },
328 {
329 name: "slice with nested map requiring deduplication",
330 body: log.SliceValue(
331 log.MapValue(log.String("key", "value1"), log.String("key", "value2")),
332 log.StringValue("normal"),
333 ),
334 want: log.SliceValue(
335 log.MapValue(log.String("key", "value2")),
336 log.StringValue("normal"),
337 ),
338 },
339 {
340 name: "deeply nested slice with map deduplication",
341 body: log.SliceValue(
342 log.SliceValue(
343 log.MapValue(log.String("deep", "value1"), log.String("deep", "value2")),
344 ),
345 ),
346 want: log.SliceValue(
347 log.SliceValue(
348 log.MapValue(log.String("deep", "value2")),
349 ),
350 ),
351 },
352 {
353 name: "empty map for collection deduplication",
354 body: log.MapValue(),
355 want: log.MapValue(),
356 },
357 {
358 name: "map with nested slice containing duplicates",
359 body: log.MapValue(
360 log.String("outer", "value"),
361 log.Slice("nested_slice",
362 log.MapValue(log.String("inner", "val1"), log.String("inner", "val2")),
363 ),
364 ),
365 want: log.MapValue(
366 log.String("outer", "value"),
367 log.Slice("nested_slice",
368 log.MapValue(log.String("inner", "val2")),
369 ),
370 ),
371 },
372 {
373 name: "map with key duplication and nested value deduplication",
374 body: log.MapValue(
375 log.String("key1", "value1"),
376 log.String("key1", "value2"), // key dedup
377 log.Slice("slice",
378 log.MapValue(log.String("nested", "val1"), log.String("nested", "val2")), // nested value dedup
379 ),
380 ),
381 want: log.MapValue(
382 log.String("key1", "value2"),
383 log.Slice("slice",
384 log.MapValue(log.String("nested", "val2")),
385 ),
386 ),
387 },
388 }
389 for _, tc := range testcases {
390 t.Run(tc.name, func(t *testing.T) {
391 r := new(Record)
392 r.allowDupKeys = tc.allowDuplicates
393 r.SetBody(tc.body)
394 got := r.Body()
395 if !got.Equal(tc.want) {
396 t.Errorf("r.Body() = %v, want %v", got, tc.want)
397 }
398 })
399 }
400 }
401
402 func TestRecordAttributes(t *testing.T) {
403 attrs := []log.KeyValue{
404 log.Bool("0", true),
405 log.Int64("1", 2),
406 log.Float64("2", 3.0),
407 log.String("3", "forth"),
408 log.Slice("4", log.Int64Value(1)),
409 log.Map("5", log.Int("key", 2)),
410 log.Bytes("6", []byte("six")),
411 }
412 r := new(Record)
413 r.attributeValueLengthLimit = -1
414 r.SetAttributes(attrs...)
415 r.SetAttributes(attrs[:2]...) // Overwrite existing.
416 r.AddAttributes(attrs[2:]...)
417
418 assert.Equal(t, len(attrs), r.AttributesLen(), "attribute length")
419
420 for n := range attrs {
421 var i int
422 r.WalkAttributes(func(log.KeyValue) bool {
423 i++
424 return i <= n
425 })
426 assert.Equalf(t, n+1, i, "WalkAttributes did not stop at %d", n+1)
427 }
428
429 var i int
430 r.WalkAttributes(func(kv log.KeyValue) bool {
431 assert.Truef(t, kv.Equal(attrs[i]), "%d: %v != %v", i, kv, attrs[i])
432 i++
433 return true
434 })
435 }
436
437 func TestRecordTraceID(t *testing.T) {
438 id := trace.TraceID([16]byte{1})
439 r := new(Record)
440 r.SetTraceID(id)
441 assert.Equal(t, id, r.TraceID())
442 }
443
444 func TestRecordSpanID(t *testing.T) {
445 id := trace.SpanID([8]byte{1})
446 r := new(Record)
447 r.SetSpanID(id)
448 assert.Equal(t, id, r.SpanID())
449 }
450
451 func TestRecordTraceFlags(t *testing.T) {
452 flag := trace.FlagsSampled
453 r := new(Record)
454 r.SetTraceFlags(flag)
455 assert.Equal(t, flag, r.TraceFlags())
456 }
457
458 func TestRecordResource(t *testing.T) {
459 r := new(Record)
460 assert.NotPanics(t, func() { r.Resource() })
461
462 res := resource.NewSchemaless(attribute.Bool("key", true))
463 r.resource = res
464 got := r.Resource()
465 assert.Equal(t, res, got)
466 }
467
468 func TestRecordInstrumentationScope(t *testing.T) {
469 r := new(Record)
470 assert.NotPanics(t, func() { r.InstrumentationScope() })
471
472 scope := instrumentation.Scope{Name: "testing"}
473 r.scope = &scope
474 assert.Equal(t, scope, r.InstrumentationScope())
475 }
476
477 func TestRecordClone(t *testing.T) {
478 now0 := time.Now()
479 sev0 := log.SeverityInfo
480 text0 := "text"
481 val0 := log.BoolValue(true)
482 attr0 := log.Bool("0", true)
483 traceID0 := trace.TraceID([16]byte{1})
484 spanID0 := trace.SpanID([8]byte{1})
485 flag0 := trace.FlagsSampled
486
487 r0 := new(Record)
488 r0.SetTimestamp(now0)
489 r0.SetObservedTimestamp(now0)
490 r0.SetSeverity(sev0)
491 r0.SetSeverityText(text0)
492 r0.SetBody(val0)
493 r0.SetAttributes(attr0)
494 r0.SetTraceID(traceID0)
495 r0.SetSpanID(spanID0)
496 r0.SetTraceFlags(flag0)
497
498 now1 := now0.Add(time.Second)
499 sev1 := log.SeverityDebug
500 text1 := "string"
501 val1 := log.IntValue(1)
502 attr1 := log.Int64("1", 2)
503 traceID1 := trace.TraceID([16]byte{2})
504 spanID1 := trace.SpanID([8]byte{2})
505 flag1 := trace.TraceFlags(2)
506
507 r1 := r0.Clone()
508 r1.SetTimestamp(now1)
509 r1.SetObservedTimestamp(now1)
510 r1.SetSeverity(sev1)
511 r1.SetSeverityText(text1)
512 r1.SetBody(val1)
513 r1.SetAttributes(attr1)
514 r1.SetTraceID(traceID1)
515 r1.SetSpanID(spanID1)
516 r1.SetTraceFlags(flag1)
517
518 assert.Equal(t, now0, r0.Timestamp())
519 assert.Equal(t, now0, r0.ObservedTimestamp())
520 assert.Equal(t, sev0, r0.Severity())
521 assert.Equal(t, text0, r0.SeverityText())
522 assert.True(t, val0.Equal(r0.Body()))
523 assert.Equal(t, traceID0, r0.TraceID())
524 assert.Equal(t, spanID0, r0.SpanID())
525 assert.Equal(t, flag0, r0.TraceFlags())
526 r0.WalkAttributes(func(kv log.KeyValue) bool {
527 return assert.Truef(t, kv.Equal(attr0), "%v != %v", kv, attr0)
528 })
529
530 assert.Equal(t, now1, r1.Timestamp())
531 assert.Equal(t, now1, r1.ObservedTimestamp())
532 assert.Equal(t, sev1, r1.Severity())
533 assert.Equal(t, text1, r1.SeverityText())
534 assert.True(t, val1.Equal(r1.Body()))
535 assert.Equal(t, traceID1, r1.TraceID())
536 assert.Equal(t, spanID1, r1.SpanID())
537 assert.Equal(t, flag1, r1.TraceFlags())
538 r1.WalkAttributes(func(kv log.KeyValue) bool {
539 return assert.Truef(t, kv.Equal(attr1), "%v != %v", kv, attr1)
540 })
541 }
542
543 func TestRecordDroppedAttributes(t *testing.T) {
544 orig := logAttrDropped
545 t.Cleanup(func() { logAttrDropped = orig })
546
547 for i := 1; i < attributesInlineCount*5; i++ {
548 var called bool
549 logAttrDropped = func() { called = true }
550
551 r := new(Record)
552 r.attributeCountLimit = 1
553
554 attrs := make([]log.KeyValue, i)
555 attrs[0] = log.Bool("only key different then the rest", true)
556
557 r.AddAttributes(attrs...)
558 // Deduplication doesn't count as dropped.
559 wantDropped := 0
560 if i > 1 {
561 wantDropped = 1
562 }
563 assert.Equalf(t, wantDropped, r.DroppedAttributes(), "%d: AddAttributes", i)
564 if i <= 1 {
565 assert.False(t, called, "%d: dropped attributes logged", i)
566 } else {
567 assert.True(t, called, "%d: dropped attributes not logged", i)
568 }
569
570 r.AddAttributes(attrs...)
571 wantDropped = 0
572 if i > 1 {
573 wantDropped = 2
574 }
575 assert.Equalf(t, wantDropped, r.DroppedAttributes(), "%d: second AddAttributes", i)
576
577 r.SetAttributes(attrs...)
578 wantDropped = 0
579 if i > 1 {
580 wantDropped = 1
581 }
582 assert.Equalf(t, wantDropped, r.DroppedAttributes(), "%d: SetAttributes", i)
583 }
584 }
585
586 func TestRecordAttrAllowDuplicateAttributes(t *testing.T) {
587 testcases := []struct {
588 name string
589 attrs []log.KeyValue
590 want []log.KeyValue
591 }{
592 {
593 name: "EmptyKey",
594 attrs: make([]log.KeyValue, 10),
595 want: make([]log.KeyValue, 10),
596 },
597 {
598 name: "MapKey",
599 attrs: []log.KeyValue{
600 log.Map("key", log.Int("key", 5), log.Int("key", 10)),
601 },
602 want: []log.KeyValue{
603 log.Map("key", log.Int("key", 5), log.Int("key", 10)),
604 },
605 },
606 {
607 name: "NonEmptyKey",
608 attrs: []log.KeyValue{
609 log.Bool("key", true),
610 log.Int64("key", 1),
611 log.Bool("key", false),
612 log.Float64("key", 2.),
613 log.String("key", "3"),
614 log.Slice("key", log.Int64Value(4)),
615 log.Map("key", log.Int("key", 5)),
616 log.Bytes("key", []byte("six")),
617 log.Bool("key", false),
618 },
619 want: []log.KeyValue{
620 log.Bool("key", true),
621 log.Int64("key", 1),
622 log.Bool("key", false),
623 log.Float64("key", 2.),
624 log.String("key", "3"),
625 log.Slice("key", log.Int64Value(4)),
626 log.Map("key", log.Int("key", 5)),
627 log.Bytes("key", []byte("six")),
628 log.Bool("key", false),
629 },
630 },
631 {
632 name: "Multiple",
633 attrs: []log.KeyValue{
634 log.Bool("a", true),
635 log.Int64("b", 1),
636 log.Bool("a", false),
637 log.Float64("c", 2.),
638 log.String("b", "3"),
639 log.Slice("d", log.Int64Value(4)),
640 log.Map("a", log.Int("key", 5)),
641 log.Bytes("d", []byte("six")),
642 log.Bool("e", true),
643 log.Int("f", 1),
644 log.Int("f", 2),
645 log.Int("f", 3),
646 log.Float64("b", 0.0),
647 log.Float64("b", 0.0),
648 log.String("g", "G"),
649 log.String("h", "H"),
650 log.String("g", "GG"),
651 log.Bool("a", false),
652 },
653 want: []log.KeyValue{
654 // Order is important here.
655 log.Bool("a", true),
656 log.Int64("b", 1),
657 log.Bool("a", false),
658 log.Float64("c", 2.),
659 log.String("b", "3"),
660 log.Slice("d", log.Int64Value(4)),
661 log.Map("a", log.Int("key", 5)),
662 log.Bytes("d", []byte("six")),
663 log.Bool("e", true),
664 log.Int("f", 1),
665 log.Int("f", 2),
666 log.Int("f", 3),
667 log.Float64("b", 0.0),
668 log.Float64("b", 0.0),
669 log.String("g", "G"),
670 log.String("h", "H"),
671 log.String("g", "GG"),
672 log.Bool("a", false),
673 },
674 },
675 {
676 name: "NoDuplicate",
677 attrs: func() []log.KeyValue {
678 out := make([]log.KeyValue, attributesInlineCount*2)
679 for i := range out {
680 out[i] = log.Bool(strconv.Itoa(i), true)
681 }
682 return out
683 }(),
684 want: func() []log.KeyValue {
685 out := make([]log.KeyValue, attributesInlineCount*2)
686 for i := range out {
687 out[i] = log.Bool(strconv.Itoa(i), true)
688 }
689 return out
690 }(),
691 },
692 }
693 for _, tc := range testcases {
694 t.Run(tc.name, func(t *testing.T) {
695 validate := func(t *testing.T, r *Record, want []log.KeyValue) {
696 t.Helper()
697
698 var i int
699 r.WalkAttributes(func(kv log.KeyValue) bool {
700 if assert.Lessf(t, i, len(want), "additional: %v", kv) {
701 want := want[i]
702 assert.Truef(t, kv.Equal(want), "%d: want %v, got %v", i, want, kv)
703 }
704 i++
705 return true
706 })
707 }
708
709 t.Run("SetAttributes", func(t *testing.T) {
710 r := new(Record)
711 r.allowDupKeys = true
712 r.attributeValueLengthLimit = -1
713 r.SetAttributes(tc.attrs...)
714 validate(t, r, tc.want)
715 })
716
717 t.Run("AddAttributes/Empty", func(t *testing.T) {
718 r := new(Record)
719 r.allowDupKeys = true
720 r.attributeValueLengthLimit = -1
721 r.AddAttributes(tc.attrs...)
722 validate(t, r, tc.want)
723 })
724
725 t.Run("AddAttributes/Twice", func(t *testing.T) {
726 r := new(Record)
727 r.allowDupKeys = true
728 r.attributeValueLengthLimit = -1
729 r.AddAttributes(tc.attrs...)
730 r.AddAttributes(tc.attrs...)
731 want := append(tc.want, tc.want...)
732 validate(t, r, want)
733 })
734 })
735 }
736 }
737
738 func TestRecordAttrDeduplication(t *testing.T) {
739 testcases := []struct {
740 name string
741 attrs []log.KeyValue
742 want []log.KeyValue
743 }{
744 {
745 name: "EmptyKey",
746 attrs: make([]log.KeyValue, 10),
747 want: make([]log.KeyValue, 1),
748 },
749 {
750 name: "NonEmptyKey",
751 attrs: []log.KeyValue{
752 log.Bool("key", true),
753 log.Int64("key", 1),
754 log.Bool("key", false),
755 log.Float64("key", 2.),
756 log.String("key", "3"),
757 log.Slice("key", log.Int64Value(4)),
758 log.Map("key", log.Int("key", 5)),
759 log.Bytes("key", []byte("six")),
760 log.Bool("key", false),
761 },
762 want: []log.KeyValue{
763 log.Bool("key", false),
764 },
765 },
766 {
767 name: "Multiple",
768 attrs: []log.KeyValue{
769 log.Bool("a", true),
770 log.Int64("b", 1),
771 log.Bool("a", false),
772 log.Float64("c", 2.),
773 log.String("b", "3"),
774 log.Slice("d", log.Int64Value(4)),
775 log.Map("a", log.Int("key", 5)),
776 log.Bytes("d", []byte("six")),
777 log.Bool("e", true),
778 log.Int("f", 1),
779 log.Int("f", 2),
780 log.Int("f", 3),
781 log.Float64("b", 0.0),
782 log.Float64("b", 0.0),
783 log.String("g", "G"),
784 log.String("h", "H"),
785 log.String("g", "GG"),
786 log.Bool("a", false),
787 },
788 want: []log.KeyValue{
789 // Order is important here.
790 log.Bool("a", false),
791 log.Float64("b", 0.0),
792 log.Float64("c", 2.),
793 log.Bytes("d", []byte("six")),
794 log.Bool("e", true),
795 log.Int("f", 3),
796 log.String("g", "GG"),
797 log.String("h", "H"),
798 },
799 },
800 {
801 name: "NoDuplicate",
802 attrs: func() []log.KeyValue {
803 out := make([]log.KeyValue, attributesInlineCount*2)
804 for i := range out {
805 out[i] = log.Bool(strconv.Itoa(i), true)
806 }
807 return out
808 }(),
809 want: func() []log.KeyValue {
810 out := make([]log.KeyValue, attributesInlineCount*2)
811 for i := range out {
812 out[i] = log.Bool(strconv.Itoa(i), true)
813 }
814 return out
815 }(),
816 },
817 {
818 name: "AttributeWithDuplicateKeys",
819 attrs: []log.KeyValue{
820 log.String("duplicate", "first"),
821 log.String("unique", "value"),
822 log.String("duplicate", "second"),
823 },
824 want: []log.KeyValue{
825 log.String("duplicate", "second"),
826 log.String("unique", "value"),
827 },
828 },
829 {
830 name: "ManyDuplicateKeys",
831 attrs: []log.KeyValue{
832 log.String("key", "value1"),
833 log.String("key", "value2"),
834 log.String("key", "value3"),
835 log.String("key", "value4"),
836 log.String("key", "value5"),
837 },
838 want: []log.KeyValue{
839 log.String("key", "value5"),
840 },
841 },
842 {
843 name: "InterleavedDuplicates",
844 attrs: []log.KeyValue{
845 log.String("a", "a1"),
846 log.String("b", "b1"),
847 log.String("a", "a2"),
848 log.String("c", "c1"),
849 log.String("b", "b2"),
850 },
851 want: []log.KeyValue{
852 log.String("a", "a2"),
853 log.String("b", "b2"),
854 log.String("c", "c1"),
855 },
856 },
857 }
858
859 for _, tc := range testcases {
860 t.Run(tc.name, func(t *testing.T) {
861 validate := func(t *testing.T, r *Record) {
862 t.Helper()
863
864 var i int
865 r.WalkAttributes(func(kv log.KeyValue) bool {
866 if assert.Lessf(t, i, len(tc.want), "additional: %v", kv) {
867 want := tc.want[i]
868 assert.Truef(t, kv.Equal(want), "%d: want %v, got %v", i, want, kv)
869 }
870 i++
871 return true
872 })
873 }
874
875 t.Run("SetAttributes", func(t *testing.T) {
876 r := new(Record)
877 r.attributeValueLengthLimit = -1
878 r.SetAttributes(tc.attrs...)
879 validate(t, r)
880 })
881
882 t.Run("AddAttributes/Empty", func(t *testing.T) {
883 r := new(Record)
884 r.attributeValueLengthLimit = -1
885 r.AddAttributes(tc.attrs...)
886 validate(t, r)
887 })
888
889 t.Run("AddAttributes/Duplicates", func(t *testing.T) {
890 r := new(Record)
891 r.attributeValueLengthLimit = -1
892 r.AddAttributes(tc.attrs...)
893 r.AddAttributes(tc.attrs...)
894 validate(t, r)
895 })
896 })
897 }
898 }
899
900 func TestApplyAttrLimitsDeduplication(t *testing.T) {
901 testcases := []struct {
902 name string
903 limit int
904 input, want log.Value
905 wantDroppedAttrs int
906 }{
907 {
908 // No de-duplication
909 name: "Slice",
910 input: log.SliceValue(
911 log.BoolValue(true),
912 log.BoolValue(true),
913 log.Float64Value(1.3),
914 log.Float64Value(1.3),
915 log.Int64Value(43),
916 log.Int64Value(43),
917 log.BytesValue([]byte("hello")),
918 log.BytesValue([]byte("hello")),
919 log.StringValue("foo"),
920 log.StringValue("foo"),
921 log.SliceValue(log.StringValue("baz")),
922 log.SliceValue(log.StringValue("baz")),
923 log.MapValue(log.String("a", "qux")),
924 log.MapValue(log.String("a", "qux")),
925 ),
926 want: log.SliceValue(
927 log.BoolValue(true),
928 log.BoolValue(true),
929 log.Float64Value(1.3),
930 log.Float64Value(1.3),
931 log.Int64Value(43),
932 log.Int64Value(43),
933 log.BytesValue([]byte("hello")),
934 log.BytesValue([]byte("hello")),
935 log.StringValue("foo"),
936 log.StringValue("foo"),
937 log.SliceValue(log.StringValue("baz")),
938 log.SliceValue(log.StringValue("baz")),
939 log.MapValue(log.String("a", "qux")),
940 log.MapValue(log.String("a", "qux")),
941 ),
942 },
943 {
944 name: "Map",
945 input: log.MapValue(
946 log.Bool("a", true),
947 log.Int64("b", 1),
948 log.Bool("a", false),
949 log.Float64("c", 2.),
950 log.String("b", "3"),
951 log.Slice("d", log.Int64Value(4)),
952 log.Map("a", log.Int("key", 5)),
953 log.Bytes("d", []byte("six")),
954 log.Bool("e", true),
955 log.Int("f", 1),
956 log.Int("f", 2),
957 log.Int("f", 3),
958 log.Float64("b", 0.0),
959 log.Float64("b", 0.0),
960 log.String("g", "G"),
961 log.String("h", "H"),
962 log.String("g", "GG"),
963 log.Bool("a", false),
964 ),
965 want: log.MapValue(
966 // Order is important here.
967 log.Bool("a", false),
968 log.Float64("b", 0.0),
969 log.Float64("c", 2.),
970 log.Bytes("d", []byte("six")),
971 log.Bool("e", true),
972 log.Int("f", 3),
973 log.String("g", "GG"),
974 log.String("h", "H"),
975 ),
976 wantDroppedAttrs: 0, // Deduplication doesn't count as dropped
977 },
978 {
979 name: "EmptyMap",
980 input: log.MapValue(),
981 want: log.MapValue(),
982 wantDroppedAttrs: 0,
983 },
984 {
985 name: "SingleKeyMap",
986 input: log.MapValue(log.String("key1", "value1")),
987 want: log.MapValue(log.String("key1", "value1")),
988 wantDroppedAttrs: 0,
989 },
990 {
991 name: "EmptySlice",
992 input: log.SliceValue(),
993 want: log.SliceValue(),
994 wantDroppedAttrs: 0,
995 },
996 {
997 name: "SliceWithNestedDedup",
998 input: log.SliceValue(
999 log.MapValue(log.String("key", "value1"), log.String("key", "value2")),
1000 log.StringValue("normal"),
1001 ),
1002 want: log.SliceValue(
1003 log.MapValue(log.String("key", "value2")),
1004 log.StringValue("normal"),
1005 ),
1006 wantDroppedAttrs: 0, // Nested deduplication doesn't count as dropped
1007 },
1008 {
1009 name: "NestedSliceInMap",
1010 input: log.MapValue(
1011 log.Slice("slice_key",
1012 log.MapValue(log.String("nested", "value1"), log.String("nested", "value2")),
1013 ),
1014 ),
1015 want: log.MapValue(
1016 log.Slice("slice_key",
1017 log.MapValue(log.String("nested", "value2")),
1018 ),
1019 ),
1020 wantDroppedAttrs: 0, // Nested deduplication doesn't count as dropped
1021 },
1022 {
1023 name: "DeeplyNestedStructure",
1024 input: log.MapValue(
1025 log.Map("level1",
1026 log.Map("level2",
1027 log.Slice("level3",
1028 log.MapValue(log.String("deep", "value1"), log.String("deep", "value2")),
1029 ),
1030 ),
1031 ),
1032 ),
1033 want: log.MapValue(
1034 log.Map("level1",
1035 log.Map("level2",
1036 log.Slice("level3",
1037 log.MapValue(log.String("deep", "value2")),
1038 ),
1039 ),
1040 ),
1041 ),
1042 wantDroppedAttrs: 0, // Deeply nested deduplication doesn't count as dropped
1043 },
1044 {
1045 name: "NestedMapWithoutDuplicateKeys",
1046 input: log.SliceValue((log.MapValue(
1047 log.String("key1", "value1"),
1048 log.String("key2", "value2"),
1049 ))),
1050 want: log.SliceValue(log.MapValue(
1051 log.String("key1", "value1"),
1052 log.String("key2", "value2"),
1053 )),
1054 wantDroppedAttrs: 0,
1055 },
1056 }
1057
1058 for _, tc := range testcases {
1059 t.Run(tc.name, func(t *testing.T) {
1060 const key = "key"
1061 kv := log.KeyValue{Key: key, Value: tc.input}
1062 r := Record{attributeValueLengthLimit: -1}
1063
1064 t.Run("AddAttributes", func(t *testing.T) {
1065 r.AddAttributes(kv)
1066 assertKV(t, r, log.KeyValue{Key: key, Value: tc.want})
1067 assert.Equal(t, tc.wantDroppedAttrs, r.DroppedAttributes())
1068 })
1069
1070 t.Run("SetAttributes", func(t *testing.T) {
1071 r.SetAttributes(kv)
1072 assertKV(t, r, log.KeyValue{Key: key, Value: tc.want})
1073 assert.Equal(t, tc.wantDroppedAttrs, r.DroppedAttributes())
1074 })
1075 })
1076 }
1077 }
1078
1079 func TestDeduplicationBehavior(t *testing.T) {
1080 origKeyValueDropped := logKeyValuePairDropped
1081 origAttrDropped := logAttrDropped
1082 t.Cleanup(func() {
1083 logKeyValuePairDropped = origKeyValueDropped
1084 logAttrDropped = origAttrDropped
1085 })
1086
1087 testCases := []struct {
1088 name string
1089 attributeCountLimit int
1090 allowDupKeys bool
1091 attrs []log.KeyValue
1092 wantKeyValueDropped bool
1093 wantAttrDropped bool
1094 wantDroppedCount int
1095 wantAttributeCount int
1096 }{
1097 {
1098 name: "Duplicate keys only",
1099 attrs: []log.KeyValue{log.String("key", "v1"), log.String("key", "v2")},
1100 wantKeyValueDropped: true,
1101 wantDroppedCount: 0, // Deduplication doesn't count
1102 wantAttributeCount: 1,
1103 },
1104 {
1105 name: "Limit exceeded only",
1106 attributeCountLimit: 2,
1107 attrs: []log.KeyValue{log.String("a", "v1"), log.String("b", "v2"), log.String("c", "v3")},
1108 wantAttrDropped: true,
1109 wantDroppedCount: 1,
1110 wantAttributeCount: 2,
1111 },
1112 {
1113 name: "Both duplicates and limit",
1114 attributeCountLimit: 2,
1115 attrs: []log.KeyValue{
1116 log.String("a", "v1"),
1117 log.String("a", "v2"),
1118 log.String("b", "v3"),
1119 log.String("c", "v4"),
1120 },
1121 wantKeyValueDropped: true,
1122 wantAttrDropped: true,
1123 wantDroppedCount: 1, // Only limit drops count
1124 wantAttributeCount: 2,
1125 },
1126 {
1127 name: "allowDupKeys=true",
1128 allowDupKeys: true,
1129 attrs: []log.KeyValue{log.String("key", "v1"), log.String("key", "v2")},
1130 wantKeyValueDropped: false,
1131 wantDroppedCount: 0,
1132 wantAttributeCount: 2,
1133 },
1134 {
1135 name: "Nested map duplicates",
1136 attrs: []log.KeyValue{
1137 log.Map("outer", log.String("nested", "v1"), log.String("nested", "v2")),
1138 },
1139 wantKeyValueDropped: true,
1140 wantDroppedCount: 0,
1141 wantAttributeCount: 1,
1142 },
1143 }
1144
1145 for _, tc := range testCases {
1146 t.Run(tc.name, func(t *testing.T) {
1147 keyValueDroppedCalled := false
1148 attrDroppedCalled := false
1149
1150 logKeyValuePairDropped = sync.OnceFunc(func() { keyValueDroppedCalled = true })
1151 logAttrDropped = sync.OnceFunc(func() { attrDroppedCalled = true })
1152
1153 r := &Record{
1154 attributeValueLengthLimit: -1,
1155 attributeCountLimit: tc.attributeCountLimit,
1156 allowDupKeys: tc.allowDupKeys,
1157 }
1158
1159 r.SetAttributes(tc.attrs...)
1160
1161 assert.Equal(t, tc.wantKeyValueDropped, keyValueDroppedCalled)
1162 assert.Equal(t, tc.wantAttrDropped, attrDroppedCalled)
1163 assert.Equal(t, tc.wantDroppedCount, r.DroppedAttributes())
1164 assert.Equal(t, tc.wantAttributeCount, r.AttributesLen())
1165 })
1166 }
1167 }
1168
1169 func TestApplyAttrLimitsTruncation(t *testing.T) {
1170 testcases := []struct {
1171 name string
1172 limit int
1173 input, want log.Value
1174 }{
1175 {
1176 name: "Empty",
1177 limit: 0,
1178 input: log.Value{},
1179 want: log.Value{},
1180 },
1181 {
1182 name: "Bool",
1183 limit: 0,
1184 input: log.BoolValue(true),
1185 want: log.BoolValue(true),
1186 },
1187 {
1188 name: "Float64",
1189 limit: 0,
1190 input: log.Float64Value(1.3),
1191 want: log.Float64Value(1.3),
1192 },
1193 {
1194 name: "Int64",
1195 limit: 0,
1196 input: log.Int64Value(43),
1197 want: log.Int64Value(43),
1198 },
1199 {
1200 name: "Bytes",
1201 limit: 0,
1202 input: log.BytesValue([]byte("foo")),
1203 want: log.BytesValue([]byte("foo")),
1204 },
1205 {
1206 name: "String",
1207 limit: 0,
1208 input: log.StringValue("foo"),
1209 want: log.StringValue(""),
1210 },
1211 {
1212 name: "Slice",
1213 limit: 0,
1214 input: log.SliceValue(
1215 log.BoolValue(true),
1216 log.Float64Value(1.3),
1217 log.Int64Value(43),
1218 log.BytesValue([]byte("hello")),
1219 log.StringValue("foo"),
1220 log.StringValue("bar"),
1221 log.SliceValue(log.StringValue("baz")),
1222 log.MapValue(log.String("a", "qux")),
1223 ),
1224 want: log.SliceValue(
1225 log.BoolValue(true),
1226 log.Float64Value(1.3),
1227 log.Int64Value(43),
1228 log.BytesValue([]byte("hello")),
1229 log.StringValue(""),
1230 log.StringValue(""),
1231 log.SliceValue(log.StringValue("")),
1232 log.MapValue(log.String("a", "")),
1233 ),
1234 },
1235 {
1236 name: "Map",
1237 limit: 0,
1238 input: log.MapValue(
1239 log.Bool("0", true),
1240 log.Float64("1", 1.3),
1241 log.Int64("2", 43),
1242 log.Bytes("3", []byte("hello")),
1243 log.String("4", "foo"),
1244 log.String("5", "bar"),
1245 log.Slice("6", log.StringValue("baz")),
1246 log.Map("7", log.String("a", "qux")),
1247 ),
1248 want: log.MapValue(
1249 log.Bool("0", true),
1250 log.Float64("1", 1.3),
1251 log.Int64("2", 43),
1252 log.Bytes("3", []byte("hello")),
1253 log.String("4", ""),
1254 log.String("5", ""),
1255 log.Slice("6", log.StringValue("")),
1256 log.Map("7", log.String("a", "")),
1257 ),
1258 },
1259 {
1260 name: "LongStringTruncated",
1261 limit: 5,
1262 input: log.StringValue("This is a very long string that should be truncated"),
1263 want: log.StringValue("This "),
1264 },
1265 {
1266 name: "LongBytesNotTruncated",
1267 limit: 5,
1268 input: log.BytesValue([]byte("This is a very long byte array")),
1269 want: log.BytesValue([]byte("This is a very long byte array")),
1270 },
1271 {
1272 name: "TruncationInNestedMap",
1273 limit: 3,
1274 input: log.MapValue(
1275 log.String("short", "ok"),
1276 log.String("long", "toolong"),
1277 ),
1278 want: log.MapValue(
1279 log.String("short", "ok"),
1280 log.String("long", "too"),
1281 ),
1282 },
1283 {
1284 name: "TruncationInNestedSlice",
1285 limit: 4,
1286 input: log.SliceValue(
1287 log.StringValue("good"),
1288 log.StringValue("toolong"),
1289 ),
1290 want: log.SliceValue(
1291 log.StringValue("good"),
1292 log.StringValue("tool"),
1293 ),
1294 },
1295 }
1296
1297 for _, tc := range testcases {
1298 t.Run(tc.name, func(t *testing.T) {
1299 const key = "key"
1300 kv := log.KeyValue{Key: key, Value: tc.input}
1301 r := Record{attributeValueLengthLimit: tc.limit}
1302
1303 t.Run("AddAttributes", func(t *testing.T) {
1304 r.AddAttributes(kv)
1305 assertKV(t, r, log.KeyValue{Key: key, Value: tc.want})
1306 })
1307
1308 t.Run("SetAttributes", func(t *testing.T) {
1309 r.SetAttributes(kv)
1310 assertKV(t, r, log.KeyValue{Key: key, Value: tc.want})
1311 })
1312 })
1313 }
1314 }
1315
1316 func assertKV(t *testing.T, r Record, kv log.KeyValue) {
1317 t.Helper()
1318
1319 var kvs []log.KeyValue
1320 r.WalkAttributes(func(kv log.KeyValue) bool {
1321 kvs = append(kvs, kv)
1322 return true
1323 })
1324
1325 require.Len(t, kvs, 1)
1326 assert.Truef(t, kv.Equal(kvs[0]), "%s != %s", kv, kvs[0])
1327 }
1328
1329 func TestTruncate(t *testing.T) {
1330 type group struct {
1331 limit int
1332 input string
1333 expected string
1334 }
1335
1336 tests := []struct {
1337 name string
1338 groups []group
1339 }{
1340 // Edge case: limit is negative, no truncation should occur
1341 {
1342 name: "NoTruncation",
1343 groups: []group{
1344 {-1, "No truncation!", "No truncation!"},
1345 },
1346 },
1347
1348 // Edge case: string is already shorter than the limit, no truncation
1349 // should occur
1350 {
1351 name: "ShortText",
1352 groups: []group{
1353 {10, "Short text", "Short text"},
1354 {15, "Short text", "Short text"},
1355 {100, "Short text", "Short text"},
1356 },
1357 },
1358
1359 // Edge case: truncation happens with ASCII characters only
1360 {
1361 name: "ASCIIOnly",
1362 groups: []group{
1363 {1, "Hello World!", "H"},
1364 {5, "Hello World!", "Hello"},
1365 {12, "Hello World!", "Hello World!"},
1366 },
1367 },
1368
1369 // Truncation including multi-byte characters (UTF-8)
1370 {
1371 name: "ValidUTF-8",
1372 groups: []group{
1373 {7, "Hello, 世界", "Hello, "},
1374 {8, "Hello, 世界", "Hello, 世"},
1375 {2, "こんにちは", "こん"},
1376 {3, "こんにちは", "こんに"},
1377 {5, "こんにちは", "こんにちは"},
1378 {12, "こんにちは", "こんにちは"},
1379 },
1380 },
1381
1382 // Truncation with invalid UTF-8 characters
1383 {
1384 name: "InvalidUTF-8",
1385 groups: []group{
1386 {11, "Invalid\x80text", "Invalidtext"},
1387 // Do not modify invalid text if equal to limit.
1388 {11, "Valid text\x80", "Valid text\x80"},
1389 // Do not modify invalid text if under limit.
1390 {15, "Valid text\x80", "Valid text\x80"},
1391 {5, "Hello\x80World", "Hello"},
1392 {11, "Hello\x80World\x80!", "HelloWorld!"},
1393 {15, "Hello\x80World\x80Test", "HelloWorldTest"},
1394 {15, "Hello\x80\x80\x80World\x80Test", "HelloWorldTest"},
1395 {15, "\x80\x80\x80Hello\x80\x80\x80World\x80Test\x80\x80", "HelloWorldTest"},
1396 },
1397 },
1398
1399 // Truncation with mixed validn and invalid UTF-8 characters
1400 {
1401 name: "MixedUTF-8",
1402 groups: []group{
1403 {6, "€"[0:2] + "hello€€", "hello€"},
1404 {6, "€" + "€"[0:2] + "hello", "€hello"},
1405 {11, "Valid text\x80📜", "Valid text📜"},
1406 {11, "Valid text📜\x80", "Valid text📜"},
1407 {14, "😊 Hello\x80World🌍🚀", "😊 HelloWorld🌍🚀"},
1408 {14, "😊\x80 Hello\x80World🌍🚀", "😊 HelloWorld🌍🚀"},
1409 {14, "😊\x80 Hello\x80World🌍\x80🚀", "😊 HelloWorld🌍🚀"},
1410 {14, "😊\x80 Hello\x80World🌍\x80🚀\x80", "😊 HelloWorld🌍🚀"},
1411 {14, "\x80😊\x80 Hello\x80World🌍\x80🚀\x80", "😊 HelloWorld🌍🚀"},
1412 },
1413 },
1414
1415 // Edge case: empty string, should return empty string
1416 {
1417 name: "Empty",
1418 groups: []group{
1419 {5, "", ""},
1420 },
1421 },
1422
1423 // Edge case: limit is 0, should return an empty string
1424 {
1425 name: "Zero",
1426 groups: []group{
1427 {0, "Some text", ""},
1428 {0, "", ""},
1429 },
1430 },
1431 }
1432
1433 for _, tt := range tests {
1434 for _, g := range tt.groups {
1435 t.Run(tt.name, func(t *testing.T) {
1436 t.Parallel()
1437
1438 got := truncate(g.limit, g.input)
1439 assert.Equalf(
1440 t, g.expected, got,
1441 "input: %q([]rune%v))\ngot: %q([]rune%v)\nwant %q([]rune%v)",
1442 g.input, []rune(g.input),
1443 got, []rune(got),
1444 g.expected, []rune(g.expected),
1445 )
1446 })
1447 }
1448 }
1449 }
1450
1451 func TestRecordAddAttributesDoesNotMutateInput(t *testing.T) {
1452 attrs := []log.KeyValue{
1453 log.String("attr1", "very long value that will be truncated"),
1454 log.String("attr2", "another very long value that will be truncated"),
1455 log.String("attr3", "yet another very long value that will be truncated"),
1456 log.String("attr4", "more very long value that will be truncated"),
1457 log.String("attr5", "extra very long value that will be truncated"),
1458 log.String("attr6", "additional very long value that will be truncated"),
1459 log.String("attr7", "more additional very long value that will be truncated"),
1460 }
1461
1462 originalValues := make([]string, len(attrs))
1463 for i, kv := range attrs {
1464 originalValues[i] = kv.Value.AsString()
1465 }
1466
1467 r := &Record{
1468 attributeValueLengthLimit: 20, // Short limit to trigger truncation.
1469 attributeCountLimit: -1, // No count limit.
1470 allowDupKeys: false,
1471 }
1472
1473 r.AddAttributes(attrs...)
1474
1475 // Verify that the original shared slice was not mutated
1476 for i, kv := range attrs {
1477 if kv.Value.AsString() != originalValues[i] {
1478 t.Errorf("Input slice was mutated! Attribute %d: original=%q, current=%q",
1479 i, originalValues[i], kv.Value.AsString())
1480 }
1481 }
1482
1483 // Verify that the record has the truncated values
1484 var gotAttrs []log.KeyValue
1485 r.WalkAttributes(func(kv log.KeyValue) bool {
1486 gotAttrs = append(gotAttrs, kv)
1487 return true
1488 })
1489 wantAttr := []log.KeyValue{
1490 log.String("attr1", "very long value that"),
1491 log.String("attr2", "another very long va"),
1492 log.String("attr3", "yet another very lon"),
1493 log.String("attr4", "more very long value"),
1494 log.String("attr5", "extra very long valu"),
1495 log.String("attr6", "additional very long"),
1496 log.String("attr7", "more additional very"),
1497 }
1498 if !slices.EqualFunc(gotAttrs, wantAttr, func(a, b log.KeyValue) bool { return a.Equal(b) }) {
1499 t.Errorf("Attributes do not match.\ngot:\n%v\nwant:\n%v", printKVs(gotAttrs), printKVs(wantAttr))
1500 }
1501 }
1502
1503 func TestRecordMethodsInputConcurrentSafe(t *testing.T) {
1504 nestedSlice := log.Slice("nested_slice",
1505 log.SliceValue(log.StringValue("nested_inner1"), log.StringValue("nested_inner2")),
1506 log.StringValue("nested_outer"),
1507 )
1508
1509 nestedMap := log.Map("nested_map",
1510 log.String("nested_key1", "nested_value1"),
1511 log.Map("nested_map", log.String("nested_inner_key", "nested_inner_value")),
1512 log.String("nested_key1", "duplicate"), // This will trigger dedup.
1513 )
1514
1515 dedupAttributes := []log.KeyValue{
1516 log.String("dedup_key1", "dedup_value1"),
1517 log.String("dedup_key2", "dedup_value2"),
1518 log.String("dedup_key1", "duplicate"), // This will trigger the dedup.
1519 log.String("dedup_key3", "dedup_value3"), // This will trigger attr count limit.
1520 }
1521
1522 var wg sync.WaitGroup
1523 for range 10 {
1524 wg.Go(func() {
1525 r := &Record{
1526 attributeValueLengthLimit: 10,
1527 attributeCountLimit: 4,
1528 allowDupKeys: false,
1529 }
1530
1531 r.SetAttributes(nestedSlice)
1532 r.AddAttributes(nestedMap)
1533 r.AddAttributes(dedupAttributes...)
1534 r.SetBody(nestedMap.Value)
1535
1536 var gotAttrs []log.KeyValue
1537 r.WalkAttributes(func(kv log.KeyValue) bool {
1538 gotAttrs = append(gotAttrs, kv)
1539 return true
1540 })
1541 wantAttr := []log.KeyValue{
1542 log.Slice("nested_slice",
1543 log.SliceValue(log.StringValue("nested_inn"), log.StringValue("nested_inn")),
1544 log.StringValue("nested_out"),
1545 ),
1546 log.Map("nested_map",
1547 log.String("nested_key1", "duplicate"),
1548 log.Map("nested_map", log.String("nested_inner_key", "nested_inn")),
1549 ),
1550 log.String("dedup_key1", "duplicate"),
1551 log.String("dedup_key2", "dedup_valu"),
1552 }
1553 if !slices.EqualFunc(gotAttrs, wantAttr, func(a, b log.KeyValue) bool { return a.Equal(b) }) {
1554 t.Errorf("Attributes do not match.\ngot:\n%v\nwant:\n%v", printKVs(gotAttrs), printKVs(wantAttr))
1555 }
1556
1557 gotBody := r.Body()
1558 wantBody := log.MapValue(
1559 log.String("nested_key1", "duplicate"),
1560 log.Map("nested_map", log.String("nested_inner_key", "nested_inner_value")),
1561 )
1562 if !gotBody.Equal(wantBody) {
1563 t.Errorf("Body does not match.\ngot:\n%v\nwant:\n%v", gotBody, wantBody)
1564 }
1565 })
1566 }
1567
1568 wg.Wait()
1569 }
1570
1571 func printKVs(kvs []log.KeyValue) string {
1572 var sb strings.Builder
1573 for _, kv := range kvs {
1574 _, _ = fmt.Fprintf(&sb, "%s: %s\n", kv.Key, kv.Value)
1575 }
1576 return sb.String()
1577 }
1578
1579 func BenchmarkTruncate(b *testing.B) {
1580 run := func(limit int, input string) func(b *testing.B) {
1581 return func(b *testing.B) {
1582 b.ReportAllocs()
1583 b.RunParallel(func(pb *testing.PB) {
1584 var out string
1585 for pb.Next() {
1586 out = truncate(limit, input)
1587 }
1588 _ = out
1589 })
1590 }
1591 }
1592 b.Run("Unlimited", run(-1, "hello 😊 world 🌍🚀"))
1593 b.Run("Zero", run(0, "Some text"))
1594 b.Run("Short", run(10, "Short Text"))
1595 b.Run("ASCII", run(5, "Hello, World!"))
1596 b.Run("ValidUTF-8", run(10, "hello 😊 world 🌍🚀"))
1597 b.Run("InvalidUTF-8", run(6, "€"[0:2]+"hello€€"))
1598 b.Run("MixedUTF-8", run(14, "\x80😊\x80 Hello\x80World🌍\x80🚀\x80"))
1599 }
1600
1601 func BenchmarkWalkAttributes(b *testing.B) {
1602 for _, tt := range []struct {
1603 attrCount int
1604 }{
1605 {attrCount: 1},
1606 {attrCount: 10},
1607 {attrCount: 100},
1608 {attrCount: 1000},
1609 } {
1610 b.Run(fmt.Sprintf("%d attributes", tt.attrCount), func(b *testing.B) {
1611 record := &Record{}
1612 for i := 0; i < tt.attrCount; i++ {
1613 record.SetAttributes(
1614 log.String(fmt.Sprintf("key-%d", tt.attrCount), "value"),
1615 )
1616 }
1617
1618 b.ReportAllocs()
1619 b.ResetTimer()
1620
1621 for i := 0; i < b.N; i++ {
1622 record.WalkAttributes(func(log.KeyValue) bool {
1623 return true
1624 })
1625 }
1626 })
1627 }
1628 }
1629
1630 func BenchmarkAddAttributes(b *testing.B) {
1631 // Simple attribute (no deduplication or limits).
1632 singleKV := log.String("key", "value")
1633
1634 // Attributes with no duplicates.
1635 uniqueAttrs := []log.KeyValue{
1636 log.String("key1", "value1"),
1637 log.String("key2", "value2"),
1638 log.String("key3", "value3"),
1639 log.String("key4", "value4"),
1640 log.String("key5", "value5"),
1641 }
1642
1643 // Attributes with duplicates that trigger deduplication.
1644 dupAttrs := []log.KeyValue{
1645 log.String("key1", "value1"),
1646 log.String("key2", "value2"),
1647 log.String("key1", "duplicate1"), // duplicate key
1648 log.String("key3", "value3"),
1649 log.String("key2", "duplicate2"), // duplicate key
1650 }
1651
1652 // Large number of attributes to trigger count limits.
1653 manyAttrs := make([]log.KeyValue, 20)
1654 for i := range manyAttrs {
1655 manyAttrs[i] = log.String(fmt.Sprintf("key%d", i), "value")
1656 }
1657
1658 // Attributes with long values to trigger value length limits.
1659 longValueAttrs := []log.KeyValue{
1660 log.String("short", "short"),
1661 log.String("long1", strings.Repeat("a", 50)),
1662 log.String("long2", strings.Repeat("b", 100)),
1663 }
1664
1665 // Attributes with nested maps that have duplicates (triggers recursive deduplication).
1666 nestedDupAttrs := []log.KeyValue{
1667 log.String("simple", "value"),
1668 log.Map("map1",
1669 log.String("inner1", "value1"),
1670 log.String("inner2", "value2"),
1671 log.String("inner1", "duplicate"), // duplicate in nested map
1672 ),
1673 log.Map("map2",
1674 log.String("key", "original"),
1675 log.Map("deeply_nested",
1676 log.String("deep1", "value1"),
1677 log.String("deep2", "value2"),
1678 log.String("deep1", "duplicate_deep"), // duplicate in deeply nested map
1679 ),
1680 log.String("key", "overwrite"), // duplicate key at this level
1681 ),
1682 log.Slice("slice_with_maps",
1683 log.MapValue(
1684 log.String("slice_key", "value1"),
1685 log.String("slice_key", "duplicate"), // duplicate in slice element
1686 ),
1687 ),
1688 }
1689
1690 // Adding a single attribute with no limits applied.
1691 b.Run("Single/NoLimits", func(b *testing.B) {
1692 records := make([]Record, b.N)
1693 for i := range records {
1694 records[i].attributeValueLengthLimit = -1
1695 records[i].attributeCountLimit = -1
1696 }
1697 b.ResetTimer()
1698 b.ReportAllocs()
1699 for i := range b.N {
1700 records[i].AddAttributes(singleKV)
1701 }
1702 })
1703
1704 // Adding a single attribute with duplicate keys allowed (faster path).
1705 b.Run("Single/AllowDuplicates", func(b *testing.B) {
1706 records := make([]Record, b.N)
1707 for i := range records {
1708 records[i].allowDupKeys = true
1709 records[i].attributeValueLengthLimit = -1
1710 records[i].attributeCountLimit = -1
1711 }
1712 b.ResetTimer()
1713 b.ReportAllocs()
1714 for i := range b.N {
1715 records[i].AddAttributes(singleKV)
1716 }
1717 })
1718
1719 // Adding multiple unique attributes with no limits applied.
1720 b.Run("Unique/NoLimits", func(b *testing.B) {
1721 records := make([]Record, b.N)
1722 for i := range records {
1723 records[i].attributeValueLengthLimit = -1
1724 records[i].attributeCountLimit = -1
1725 }
1726 b.ResetTimer()
1727 b.ReportAllocs()
1728 for i := range b.N {
1729 records[i].AddAttributes(uniqueAttrs...)
1730 }
1731 })
1732
1733 // Adding multiple unique attributes with duplicate keys allowed (faster path).
1734 b.Run("Unique/AllowDuplicates", func(b *testing.B) {
1735 records := make([]Record, b.N)
1736 for i := range records {
1737 records[i].allowDupKeys = true
1738 records[i].attributeValueLengthLimit = -1
1739 records[i].attributeCountLimit = -1
1740 }
1741 b.ResetTimer()
1742 b.ReportAllocs()
1743 for i := range b.N {
1744 records[i].AddAttributes(uniqueAttrs...)
1745 }
1746 })
1747
1748 // Adding attributes with duplicates that trigger deduplication logic.
1749 b.Run("Deduplication/Enabled", func(b *testing.B) {
1750 records := make([]Record, b.N)
1751 for i := range records {
1752 records[i].attributeValueLengthLimit = -1
1753 records[i].attributeCountLimit = -1
1754 }
1755 b.ResetTimer()
1756 b.ReportAllocs()
1757 for i := range b.N {
1758 records[i].AddAttributes(dupAttrs...)
1759 }
1760 })
1761
1762 // Adding nested maps with duplicates that trigger recursive deduplication.
1763 b.Run("NestedDeduplication/Enabled", func(b *testing.B) {
1764 records := make([]Record, b.N)
1765 for i := range records {
1766 records[i].attributeValueLengthLimit = -1
1767 records[i].attributeCountLimit = -1
1768 }
1769 b.ResetTimer()
1770 b.ReportAllocs()
1771 for i := range b.N {
1772 records[i].AddAttributes(nestedDupAttrs...)
1773 }
1774 })
1775
1776 // Adding nested maps with duplicates with deduplication disabled.
1777 b.Run("NestedDeduplication/Disabled", func(b *testing.B) {
1778 records := make([]Record, b.N)
1779 for i := range records {
1780 records[i].allowDupKeys = true
1781 records[i].attributeValueLengthLimit = -1
1782 records[i].attributeCountLimit = -1
1783 }
1784 b.ResetTimer()
1785 b.ReportAllocs()
1786 for i := range b.N {
1787 records[i].AddAttributes(nestedDupAttrs...)
1788 }
1789 })
1790
1791 // Adding attributes with duplicates with deduplication disabled.
1792 b.Run("Deduplication/Disabled", func(b *testing.B) {
1793 records := make([]Record, b.N)
1794 for i := range records {
1795 records[i].allowDupKeys = true
1796 records[i].attributeValueLengthLimit = -1
1797 records[i].attributeCountLimit = -1
1798 }
1799 b.ResetTimer()
1800 b.ReportAllocs()
1801 for i := range b.N {
1802 records[i].AddAttributes(dupAttrs...)
1803 }
1804 })
1805
1806 // Adding more attributes than the count limit allows (triggers dropping).
1807 b.Run("CountLimit/Hit", func(b *testing.B) {
1808 records := make([]Record, b.N)
1809 for i := range records {
1810 records[i].attributeValueLengthLimit = -1
1811 records[i].attributeCountLimit = 10 // Less than manyAttrs length.
1812 }
1813 b.ResetTimer()
1814 b.ReportAllocs()
1815 for i := range b.N {
1816 records[i].AddAttributes(manyAttrs...)
1817 }
1818 })
1819
1820 // Adding attributes within the count limit (no dropping).
1821 b.Run("CountLimit/NotHit", func(b *testing.B) {
1822 records := make([]Record, b.N)
1823 for i := range records {
1824 records[i].attributeValueLengthLimit = -1
1825 records[i].attributeCountLimit = 100 // More than manyAttrs length.
1826 }
1827 b.ResetTimer()
1828 b.ReportAllocs()
1829 for i := range b.N {
1830 records[i].AddAttributes(manyAttrs...)
1831 }
1832 })
1833
1834 // Adding attributes with long values that trigger truncation.
1835 b.Run("ValueLimit/Hit", func(b *testing.B) {
1836 records := make([]Record, b.N)
1837 for i := range records {
1838 records[i].attributeValueLengthLimit = 20 // Less than long values.
1839 records[i].attributeCountLimit = -1
1840 }
1841 b.ResetTimer()
1842 b.ReportAllocs()
1843 for i := range b.N {
1844 records[i].AddAttributes(longValueAttrs...)
1845 }
1846 })
1847
1848 // Adding attributes with values within the length limit (no truncation).
1849 b.Run("ValueLimit/NotHit", func(b *testing.B) {
1850 records := make([]Record, b.N)
1851 for i := range records {
1852 records[i].attributeValueLengthLimit = 200 // More than long values.
1853 records[i].attributeCountLimit = -1
1854 }
1855 b.ResetTimer()
1856 b.ReportAllocs()
1857 for i := range b.N {
1858 records[i].AddAttributes(longValueAttrs...)
1859 }
1860 })
1861 }
1862
1863 func BenchmarkSetAttributes(b *testing.B) {
1864 // Simple attribute (no deduplication or limits).
1865 singleKV := log.String("key", "value")
1866
1867 // Attributes with no duplicates.
1868 uniqueAttrs := []log.KeyValue{
1869 log.String("key1", "value1"),
1870 log.String("key2", "value2"),
1871 log.String("key3", "value3"),
1872 log.String("key4", "value4"),
1873 log.String("key5", "value5"),
1874 }
1875
1876 // Attributes with duplicates that trigger deduplication.
1877 dupAttrs := []log.KeyValue{
1878 log.String("key1", "value1"),
1879 log.String("key2", "value2"),
1880 log.String("key1", "duplicate1"), // duplicate key
1881 log.String("key3", "value3"),
1882 log.String("key2", "duplicate2"), // duplicate key
1883 }
1884
1885 // Large number of attributes to trigger count limits.
1886 manyAttrs := make([]log.KeyValue, 20)
1887 for i := range manyAttrs {
1888 manyAttrs[i] = log.String(fmt.Sprintf("key%d", i), "value")
1889 }
1890
1891 // Attributes with long values to trigger value length limits.
1892 longValueAttrs := []log.KeyValue{
1893 log.String("short", "short"),
1894 log.String("long1", strings.Repeat("a", 50)),
1895 log.String("long2", strings.Repeat("b", 100)),
1896 }
1897
1898 // Attributes with nested maps that have duplicates (triggers recursive deduplication).
1899 nestedDupAttrs := []log.KeyValue{
1900 log.String("simple", "value"),
1901 log.Map("map1",
1902 log.String("inner1", "value1"),
1903 log.String("inner2", "value2"),
1904 log.String("inner1", "duplicate"), // duplicate in nested map
1905 ),
1906 log.Map("map2",
1907 log.String("key", "original"),
1908 log.Map("deeply_nested",
1909 log.String("deep1", "value1"),
1910 log.String("deep2", "value2"),
1911 log.String("deep1", "duplicate_deep"), // duplicate in deeply nested map
1912 ),
1913 log.String("key", "overwrite"), // duplicate key at this level
1914 ),
1915 log.Slice("slice_with_maps",
1916 log.MapValue(
1917 log.String("slice_key", "value1"),
1918 log.String("slice_key", "duplicate"), // duplicate in slice element
1919 ),
1920 ),
1921 }
1922
1923 // Setting a single attribute with no limits applied.
1924 b.Run("Single/NoLimits", func(b *testing.B) {
1925 records := make([]Record, b.N)
1926 for i := range records {
1927 records[i].attributeValueLengthLimit = -1
1928 records[i].attributeCountLimit = -1
1929 }
1930 b.ResetTimer()
1931 b.ReportAllocs()
1932 for i := range b.N {
1933 records[i].SetAttributes(singleKV)
1934 }
1935 })
1936
1937 // Setting a single attribute with duplicate keys allowed (faster path).
1938 b.Run("Single/AllowDuplicates", func(b *testing.B) {
1939 records := make([]Record, b.N)
1940 for i := range records {
1941 records[i].allowDupKeys = true
1942 records[i].attributeValueLengthLimit = -1
1943 records[i].attributeCountLimit = -1
1944 }
1945 b.ResetTimer()
1946 b.ReportAllocs()
1947 for i := range b.N {
1948 records[i].SetAttributes(singleKV)
1949 }
1950 })
1951
1952 // Setting multiple unique attributes with no limits applied.
1953 b.Run("Unique/NoLimits", func(b *testing.B) {
1954 records := make([]Record, b.N)
1955 for i := range records {
1956 records[i].attributeValueLengthLimit = -1
1957 records[i].attributeCountLimit = -1
1958 }
1959 b.ResetTimer()
1960 b.ReportAllocs()
1961 for i := range b.N {
1962 records[i].SetAttributes(uniqueAttrs...)
1963 }
1964 })
1965
1966 // Setting multiple unique attributes with duplicate keys allowed (faster path).
1967 b.Run("Unique/AllowDuplicates", func(b *testing.B) {
1968 records := make([]Record, b.N)
1969 for i := range records {
1970 records[i].allowDupKeys = true
1971 records[i].attributeValueLengthLimit = -1
1972 records[i].attributeCountLimit = -1
1973 }
1974 b.ResetTimer()
1975 b.ReportAllocs()
1976 for i := range b.N {
1977 records[i].SetAttributes(uniqueAttrs...)
1978 }
1979 })
1980
1981 // Setting attributes with duplicates that trigger deduplication logic.
1982 b.Run("Deduplication/Enabled", func(b *testing.B) {
1983 records := make([]Record, b.N)
1984 for i := range records {
1985 records[i].attributeValueLengthLimit = -1
1986 records[i].attributeCountLimit = -1
1987 }
1988 b.ResetTimer()
1989 b.ReportAllocs()
1990 for i := range b.N {
1991 records[i].SetAttributes(dupAttrs...)
1992 }
1993 })
1994
1995 // Setting attributes with duplicates with deduplication disabled.
1996 b.Run("Deduplication/Disabled", func(b *testing.B) {
1997 records := make([]Record, b.N)
1998 for i := range records {
1999 records[i].allowDupKeys = true
2000 records[i].attributeValueLengthLimit = -1
2001 records[i].attributeCountLimit = -1
2002 }
2003 b.ResetTimer()
2004 b.ReportAllocs()
2005 for i := range b.N {
2006 records[i].SetAttributes(dupAttrs...)
2007 }
2008 })
2009
2010 // Setting nested maps with duplicates that trigger recursive deduplication.
2011 b.Run("NestedDeduplication/Enabled", func(b *testing.B) {
2012 records := make([]Record, b.N)
2013 for i := range records {
2014 records[i].attributeValueLengthLimit = -1
2015 records[i].attributeCountLimit = -1
2016 }
2017 b.ResetTimer()
2018 b.ReportAllocs()
2019 for i := range b.N {
2020 records[i].SetAttributes(nestedDupAttrs...)
2021 }
2022 })
2023
2024 // Setting nested maps with duplicates with deduplication disabled.
2025 b.Run("NestedDeduplication/Disabled", func(b *testing.B) {
2026 records := make([]Record, b.N)
2027 for i := range records {
2028 records[i].allowDupKeys = true
2029 records[i].attributeValueLengthLimit = -1
2030 records[i].attributeCountLimit = -1
2031 }
2032 b.ResetTimer()
2033 b.ReportAllocs()
2034 for i := range b.N {
2035 records[i].SetAttributes(nestedDupAttrs...)
2036 }
2037 })
2038
2039 // Setting more attributes than the count limit allows (triggers dropping).
2040 b.Run("CountLimit/Hit", func(b *testing.B) {
2041 records := make([]Record, b.N)
2042 for i := range records {
2043 records[i].attributeValueLengthLimit = -1
2044 records[i].attributeCountLimit = 10 // Less than manyAttrs length.
2045 }
2046 b.ResetTimer()
2047 b.ReportAllocs()
2048 for i := range b.N {
2049 records[i].SetAttributes(manyAttrs...)
2050 }
2051 })
2052
2053 // Setting attributes within the count limit (no dropping).
2054 b.Run("CountLimit/NotHit", func(b *testing.B) {
2055 records := make([]Record, b.N)
2056 for i := range records {
2057 records[i].attributeValueLengthLimit = -1
2058 records[i].attributeCountLimit = 100 // More than manyAttrs length.
2059 }
2060 b.ResetTimer()
2061 b.ReportAllocs()
2062 for i := range b.N {
2063 records[i].SetAttributes(manyAttrs...)
2064 }
2065 })
2066
2067 // Setting attributes with long values that trigger truncation.
2068 b.Run("ValueLimit/Hit", func(b *testing.B) {
2069 records := make([]Record, b.N)
2070 for i := range records {
2071 records[i].attributeValueLengthLimit = 20 // Less than long values.
2072 records[i].attributeCountLimit = -1
2073 }
2074 b.ResetTimer()
2075 b.ReportAllocs()
2076 for i := range b.N {
2077 records[i].SetAttributes(longValueAttrs...)
2078 }
2079 })
2080
2081 // Setting attributes with values within the length limit (no truncation).
2082 b.Run("ValueLimit/NotHit", func(b *testing.B) {
2083 records := make([]Record, b.N)
2084 for i := range records {
2085 records[i].attributeValueLengthLimit = 200 // More than long values.
2086 records[i].attributeCountLimit = -1
2087 }
2088 b.ResetTimer()
2089 b.ReportAllocs()
2090 for i := range b.N {
2091 records[i].SetAttributes(longValueAttrs...)
2092 }
2093 })
2094
2095 // Setting attributes on a record that already has existing attributes (tests overwrite behavior).
2096 b.Run("Overwrite/Existing", func(b *testing.B) {
2097 records := make([]Record, b.N)
2098 for i := range records {
2099 records[i].attributeValueLengthLimit = -1
2100 records[i].attributeCountLimit = -1
2101 // Pre-populate with existing attributes
2102 records[i].AddAttributes(
2103 log.String("existing1", "value1"),
2104 log.String("existing2", "value2"),
2105 )
2106 }
2107 b.ResetTimer()
2108 b.ReportAllocs()
2109 for i := range b.N {
2110 records[i].SetAttributes(uniqueAttrs...)
2111 }
2112 })
2113 }
2114
2115 func BenchmarkSetBody(b *testing.B) {
2116 // Simple value (no deduplication or limits).
2117 simpleValue := log.StringValue("simple string value")
2118
2119 // Map with unique keys (no deduplication needed).
2120 uniqueMapValue := log.MapValue(
2121 log.Bool("bool_key", true),
2122 log.Float64("float_key", 3.14),
2123 log.String("string_key", "value"),
2124 log.Slice("slice_key", log.Int64Value(1), log.Int64Value(2)),
2125 log.Map("nested_key", log.Int("inner", 42)),
2126 log.Bytes("bytes_key", []byte("data")),
2127 )
2128
2129 // Map with duplicate keys (triggers deduplication).
2130 dupMapValue := log.MapValue(
2131 log.String("key1", "value1"),
2132 log.String("key2", "value2"),
2133 log.String("key1", "duplicate1"), // duplicate key
2134 log.String("key3", "value3"),
2135 log.String("key2", "duplicate2"), // duplicate key
2136 )
2137
2138 // Nested map with duplicates.
2139 nestedDupMapValue := log.MapValue(
2140 log.String("outer1", "value1"),
2141 log.Map("nested",
2142 log.String("inner1", "value1"),
2143 log.String("inner2", "value2"),
2144 log.String("inner1", "duplicate"), // duplicate in nested map
2145 ),
2146 log.Slice("slice_with_maps",
2147 log.MapValue(
2148 log.String("slice_key", "value1"),
2149 log.String("slice_key", "duplicate"), // duplicate in slice element
2150 ),
2151 ),
2152 )
2153
2154 // Map with long string values (triggers value length limits).
2155 longValueMapValue := log.MapValue(
2156 log.String("short", "short"),
2157 log.String("long1", strings.Repeat("a", 50)),
2158 log.String("long2", strings.Repeat("b", 100)),
2159 )
2160
2161 // Setting a simple string value with no limits applied.
2162 b.Run("Simple/NoLimits", func(b *testing.B) {
2163 records := make([]Record, b.N)
2164 for i := range records {
2165 records[i].attributeValueLengthLimit = -1
2166 }
2167 b.ResetTimer()
2168 b.ReportAllocs()
2169 for i := range b.N {
2170 records[i].SetBody(simpleValue)
2171 }
2172 })
2173
2174 // Setting a simple string value with limits applied.
2175 b.Run("Simple/WithLimits", func(b *testing.B) {
2176 records := make([]Record, b.N)
2177 for i := range records {
2178 records[i].attributeValueLengthLimit = 20
2179 }
2180 b.ResetTimer()
2181 b.ReportAllocs()
2182 for i := range b.N {
2183 records[i].SetBody(simpleValue)
2184 }
2185 })
2186
2187 // Setting a map value with unique keys and no limits applied.
2188 b.Run("UniqueMap/NoLimits", func(b *testing.B) {
2189 records := make([]Record, b.N)
2190 for i := range records {
2191 records[i].attributeValueLengthLimit = -1
2192 }
2193 b.ResetTimer()
2194 b.ReportAllocs()
2195 for i := range b.N {
2196 records[i].SetBody(uniqueMapValue)
2197 }
2198 })
2199
2200 // Setting a map value with duplicate keys allowed (faster path).
2201 b.Run("UniqueMap/AllowDuplicates", func(b *testing.B) {
2202 records := make([]Record, b.N)
2203 for i := range records {
2204 records[i].allowDupKeys = true
2205 records[i].attributeValueLengthLimit = -1
2206 }
2207 b.ResetTimer()
2208 b.ReportAllocs()
2209 for i := range b.N {
2210 records[i].SetBody(uniqueMapValue)
2211 }
2212 })
2213
2214 // Setting a map with duplicate keys that triggers deduplication logic.
2215 b.Run("Deduplication/Enabled", func(b *testing.B) {
2216 records := make([]Record, b.N)
2217 for i := range records {
2218 records[i].attributeValueLengthLimit = -1
2219 }
2220 b.ResetTimer()
2221 b.ReportAllocs()
2222 for i := range b.N {
2223 records[i].SetBody(dupMapValue)
2224 }
2225 })
2226
2227 // Setting a map with duplicate keys with deduplication disabled.
2228 b.Run("Deduplication/Disabled", func(b *testing.B) {
2229 records := make([]Record, b.N)
2230 for i := range records {
2231 records[i].allowDupKeys = true
2232 records[i].attributeValueLengthLimit = -1
2233 }
2234 b.ResetTimer()
2235 b.ReportAllocs()
2236 for i := range b.N {
2237 records[i].SetBody(dupMapValue)
2238 }
2239 })
2240
2241 // Setting nested maps with duplicates (tests recursive deduplication).
2242 b.Run("NestedDeduplication/Enabled", func(b *testing.B) {
2243 records := make([]Record, b.N)
2244 for i := range records {
2245 records[i].attributeValueLengthLimit = -1
2246 }
2247 b.ResetTimer()
2248 b.ReportAllocs()
2249 for i := range b.N {
2250 records[i].SetBody(nestedDupMapValue)
2251 }
2252 })
2253
2254 // Setting nested maps with duplicates with deduplication disabled.
2255 b.Run("NestedDeduplication/Disabled", func(b *testing.B) {
2256 records := make([]Record, b.N)
2257 for i := range records {
2258 records[i].allowDupKeys = true
2259 records[i].attributeValueLengthLimit = -1
2260 }
2261 b.ResetTimer()
2262 b.ReportAllocs()
2263 for i := range b.N {
2264 records[i].SetBody(nestedDupMapValue)
2265 }
2266 })
2267
2268 // Setting map with long string values that trigger truncation.
2269 b.Run("ValueLimit/Hit", func(b *testing.B) {
2270 records := make([]Record, b.N)
2271 for i := range records {
2272 records[i].attributeValueLengthLimit = 30 // Less than long values.
2273 }
2274 b.ResetTimer()
2275 b.ReportAllocs()
2276 for i := range b.N {
2277 records[i].SetBody(longValueMapValue)
2278 }
2279 })
2280
2281 // Setting map with values within the length limit (no truncation).
2282 b.Run("ValueLimit/NoHit", func(b *testing.B) {
2283 records := make([]Record, b.N)
2284 for i := range records {
2285 records[i].attributeValueLengthLimit = 200 // More than long values.
2286 }
2287 b.ResetTimer()
2288 b.ReportAllocs()
2289 for i := range b.N {
2290 records[i].SetBody(longValueMapValue)
2291 }
2292 })
2293 }