Debian Codegraph @

log/keyvalue_test.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 // Copyright 2022 The Go Authors. All rights reserved.
5 // Use of this source code is governed by a BSD-style
6 // license that can be found in the LICENSE file.
7
8 package log_test
9
10 import (
11 "testing"
12
13 "github.com/go-logr/logr"
14 "github.com/go-logr/logr/testr"
15 "github.com/stretchr/testify/assert"
16 "github.com/stretchr/testify/require"
17
18 "go.opentelemetry.io/otel/attribute"
19 "go.opentelemetry.io/otel/internal/global"
20 "go.opentelemetry.io/otel/log"
21 )
22
23 func TestKind(t *testing.T) {
24 testCases := []struct {
25 kind log.Kind
26 str string
27 value int
28 }{
29 {log.KindBool, "Bool", 1},
30 {log.KindBytes, "Bytes", 5},
31 {log.KindEmpty, "Empty", 0},
32 {log.KindFloat64, "Float64", 2},
33 {log.KindInt64, "Int64", 3},
34 {log.KindSlice, "Slice", 6},
35 {log.KindMap, "Map", 7},
36 {log.KindString, "String", 4},
37 }
38 for _, tc := range testCases {
39 t.Run(tc.str, func(t *testing.T) {
40 assert.Equal(t, tc.value, int(tc.kind), "Kind value")
41 assert.Equal(t, tc.str, tc.kind.String(), "Kind string")
42 })
43 }
44 }
45
46 func TestValueEqual(t *testing.T) {
47 vals := []log.Value{
48 {},
49 log.Int64Value(1),
50 log.Int64Value(2),
51 log.Int64Value(-2),
52 log.Float64Value(3.5),
53 log.Float64Value(3.7),
54 log.BoolValue(true),
55 log.BoolValue(false),
56 log.StringValue("hi"),
57 log.StringValue("bye"),
58 log.BytesValue([]byte{1, 3, 5}),
59 log.SliceValue(log.StringValue("foo")),
60 log.SliceValue(log.IntValue(3), log.StringValue("foo")),
61 log.MapValue(log.Bool("b", true), log.Int("i", 3)),
62 log.MapValue(
63 log.Slice("l", log.IntValue(3), log.StringValue("foo")),
64 log.Bytes("b", []byte{3, 5, 7}),
65 log.Empty("e"),
66 ),
67 }
68 for i, v1 := range vals {
69 for j, v2 := range vals {
70 assert.Equal(t, i == j, v1.Equal(v2), "%v.Equal(%v)", v1, v2)
71 }
72 }
73 }
74
75 func TestSortedValueEqual(t *testing.T) {
76 testCases := []struct {
77 value log.Value
78 value2 log.Value
79 }{
80 {
81 value: log.MapValue(
82 log.Slice("l", log.IntValue(3), log.StringValue("foo")),
83 log.Bytes("b", []byte{3, 5, 7}),
84 log.Empty("e"),
85 ),
86 value2: log.MapValue(
87 log.Bytes("b", []byte{3, 5, 7}),
88 log.Slice("l", log.IntValue(3), log.StringValue("foo")),
89 log.Empty("e"),
90 ),
91 },
92 }
93 for _, tc := range testCases {
94 t.Run(tc.value.String(), func(t *testing.T) {
95 assert.True(t, tc.value.Equal(tc.value2), "%v.Equal(%v)", tc.value, tc.value2)
96 })
97 }
98 }
99
100 func TestValueEmpty(t *testing.T) {
101 v := log.Value{}
102 t.Run("Value.Empty", func(t *testing.T) {
103 assert.True(t, v.Empty())
104 })
105
106 t.Run("Bytes", func(t *testing.T) {
107 assert.Nil(t, log.Bytes("b", nil).Value.AsBytes())
108 })
109 t.Run("Slice", func(t *testing.T) {
110 assert.Nil(t, log.Slice("s").Value.AsSlice())
111 })
112 t.Run("Map", func(t *testing.T) {
113 assert.Nil(t, log.Map("m").Value.AsMap())
114 })
115 }
116
117 func TestEmptyGroupsPreserved(t *testing.T) {
118 t.Run("Map", func(t *testing.T) {
119 assert.Equal(t, []log.KeyValue{
120 log.Int("a", 1),
121 log.Map("g1", log.Map("g2")),
122 log.Map("g3", log.Map("g4", log.Int("b", 2))),
123 }, log.MapValue(
124 log.Int("a", 1),
125 log.Map("g1", log.Map("g2")),
126 log.Map("g3", log.Map("g4", log.Int("b", 2))),
127 ).AsMap())
128 })
129
130 t.Run("Slice", func(t *testing.T) {
131 assert.Equal(t, []log.Value{{}}, log.SliceValue(log.Value{}).AsSlice())
132 })
133 }
134
135 func TestBool(t *testing.T) {
136 const key, val = "boolKey", true
137 kv := log.Bool(key, val)
138 testKV(t, key, kv)
139
140 v, k := kv.Value, log.KindBool
141 t.Run("AsBool", func(t *testing.T) {
142 assert.Equal(t, val, kv.Value.AsBool(), "AsBool")
143 })
144 t.Run("AsFloat64", testErrKind(v.AsFloat64, "AsFloat64", k))
145 t.Run("AsInt64", testErrKind(v.AsInt64, "AsInt64", k))
146 t.Run("AsString", testErrKind(v.AsString, "AsString", k))
147 t.Run("AsBytes", testErrKind(v.AsBytes, "AsBytes", k))
148 t.Run("AsSlice", testErrKind(v.AsSlice, "AsSlice", k))
149 t.Run("AsMap", testErrKind(v.AsMap, "AsMap", k))
150 }
151
152 func TestFloat64(t *testing.T) {
153 const key, val = "float64Key", 3.0
154 kv := log.Float64(key, val)
155 testKV(t, key, kv)
156
157 v, k := kv.Value, log.KindFloat64
158 t.Run("AsBool", testErrKind(v.AsBool, "AsBool", k))
159 t.Run("AsFloat64", func(t *testing.T) {
160 assert.Equal(t, val, v.AsFloat64(), "AsFloat64")
161 })
162 t.Run("AsInt64", testErrKind(v.AsInt64, "AsInt64", k))
163 t.Run("AsString", testErrKind(v.AsString, "AsString", k))
164 t.Run("AsBytes", testErrKind(v.AsBytes, "AsBytes", k))
165 t.Run("AsSlice", testErrKind(v.AsSlice, "AsSlice", k))
166 t.Run("AsMap", testErrKind(v.AsMap, "AsMap", k))
167 }
168
169 func TestInt(t *testing.T) {
170 const key, val = "intKey", 1
171 kv := log.Int(key, val)
172 testKV(t, key, kv)
173
174 v, k := kv.Value, log.KindInt64
175 t.Run("AsBool", testErrKind(v.AsBool, "AsBool", k))
176 t.Run("AsFloat64", testErrKind(v.AsFloat64, "AsFloat64", k))
177 t.Run("AsInt64", func(t *testing.T) {
178 assert.Equal(t, int64(val), v.AsInt64(), "AsInt64")
179 })
180 t.Run("AsString", testErrKind(v.AsString, "AsString", k))
181 t.Run("AsBytes", testErrKind(v.AsBytes, "AsBytes", k))
182 t.Run("AsSlice", testErrKind(v.AsSlice, "AsSlice", k))
183 t.Run("AsMap", testErrKind(v.AsMap, "AsMap", k))
184 }
185
186 func TestInt64(t *testing.T) {
187 const key, val = "int64Key", 1
188 kv := log.Int64(key, val)
189 testKV(t, key, kv)
190
191 v, k := kv.Value, log.KindInt64
192 t.Run("AsBool", testErrKind(v.AsBool, "AsBool", k))
193 t.Run("AsFloat64", testErrKind(v.AsFloat64, "AsFloat64", k))
194 t.Run("AsInt64", func(t *testing.T) {
195 assert.Equal(t, int64(val), v.AsInt64(), "AsInt64")
196 })
197 t.Run("AsString", testErrKind(v.AsString, "AsString", k))
198 t.Run("AsBytes", testErrKind(v.AsBytes, "AsBytes", k))
199 t.Run("AsSlice", testErrKind(v.AsSlice, "AsSlice", k))
200 t.Run("AsMap", testErrKind(v.AsMap, "AsMap", k))
201 }
202
203 func TestString(t *testing.T) {
204 const key, val = "stringKey", "test string value"
205 kv := log.String(key, val)
206 testKV(t, key, kv)
207
208 v, k := kv.Value, log.KindString
209 t.Run("AsBool", testErrKind(v.AsBool, "AsBool", k))
210 t.Run("AsFloat64", testErrKind(v.AsFloat64, "AsFloat64", k))
211 t.Run("AsInt64", testErrKind(v.AsInt64, "AsInt64", k))
212 t.Run("AsString", func(t *testing.T) {
213 assert.Equal(t, val, v.AsString(), "AsString")
214 })
215 t.Run("AsBytes", testErrKind(v.AsBytes, "AsBytes", k))
216 t.Run("AsSlice", testErrKind(v.AsSlice, "AsSlice", k))
217 t.Run("AsMap", testErrKind(v.AsMap, "AsMap", k))
218 }
219
220 func TestBytes(t *testing.T) {
221 const key = "bytesKey"
222 val := []byte{3, 2, 1}
223 kv := log.Bytes(key, val)
224 testKV(t, key, kv)
225
226 v, k := kv.Value, log.KindBytes
227 t.Run("AsBool", testErrKind(v.AsBool, "AsBool", k))
228 t.Run("AsFloat64", testErrKind(v.AsFloat64, "AsFloat64", k))
229 t.Run("AsInt64", testErrKind(v.AsInt64, "AsInt64", k))
230 t.Run("AsString", testErrKind(v.AsString, "AsString", k))
231 t.Run("AsBytes", func(t *testing.T) {
232 assert.Equal(t, val, v.AsBytes(), "AsBytes")
233 })
234 t.Run("AsSlice", testErrKind(v.AsSlice, "AsSlice", k))
235 t.Run("AsMap", testErrKind(v.AsMap, "AsMap", k))
236 }
237
238 func TestSlice(t *testing.T) {
239 const key = "sliceKey"
240 val := []log.Value{log.IntValue(3), log.StringValue("foo")}
241 kv := log.Slice(key, val...)
242 testKV(t, key, kv)
243
244 v, k := kv.Value, log.KindSlice
245 t.Run("AsBool", testErrKind(v.AsBool, "AsBool", k))
246 t.Run("AsFloat64", testErrKind(v.AsFloat64, "AsFloat64", k))
247 t.Run("AsInt64", testErrKind(v.AsInt64, "AsInt64", k))
248 t.Run("AsString", testErrKind(v.AsString, "AsString", k))
249 t.Run("AsBytes", testErrKind(v.AsBytes, "AsBytes", k))
250 t.Run("AsSlice", func(t *testing.T) {
251 assert.Equal(t, val, v.AsSlice(), "AsSlice")
252 })
253 t.Run("AsMap", testErrKind(v.AsMap, "AsMap", k))
254 }
255
256 func TestMap(t *testing.T) {
257 const key = "mapKey"
258 val := []log.KeyValue{
259 log.Slice("l", log.IntValue(3), log.StringValue("foo")),
260 log.Bytes("b", []byte{3, 5, 7}),
261 }
262 kv := log.Map(key, val...)
263 testKV(t, key, kv)
264
265 v, k := kv.Value, log.KindMap
266 t.Run("AsBool", testErrKind(v.AsBool, "AsBool", k))
267 t.Run("AsFloat64", testErrKind(v.AsFloat64, "AsFloat64", k))
268 t.Run("AsInt64", testErrKind(v.AsInt64, "AsInt64", k))
269 t.Run("AsString", testErrKind(v.AsString, "AsString", k))
270 t.Run("AsBytes", testErrKind(v.AsBytes, "AsBytes", k))
271 t.Run("AsSlice", testErrKind(v.AsSlice, "AsSlice", k))
272 t.Run("AsMap", func(t *testing.T) {
273 assert.Equal(t, val, v.AsMap(), "AsMap")
274 })
275 }
276
277 func TestEmpty(t *testing.T) {
278 const key = "key"
279 kv := log.Empty(key)
280
281 assert.Equal(t, key, kv.Key, "incorrect key")
282 assert.True(t, kv.Value.Empty(), "value not empty")
283
284 v, k := kv.Value, log.KindEmpty
285 t.Run("AsBool", testErrKind(v.AsBool, "AsBool", k))
286 t.Run("AsFloat64", testErrKind(v.AsFloat64, "AsFloat64", k))
287 t.Run("AsInt64", testErrKind(v.AsInt64, "AsInt64", k))
288 t.Run("AsString", testErrKind(v.AsString, "AsString", k))
289 t.Run("AsBytes", testErrKind(v.AsBytes, "AsBytes", k))
290 t.Run("AsSlice", testErrKind(v.AsSlice, "AsSlice", k))
291 t.Run("AsMap", testErrKind(v.AsMap, "AsMap", k))
292 }
293
294 func TestValueString(t *testing.T) {
295 for _, test := range []struct {
296 v log.Value
297 want string
298 }{
299 {log.Int64Value(-3), "-3"},
300 {log.Float64Value(.15), "0.15"},
301 {log.BoolValue(true), "true"},
302 {log.StringValue("foo"), "foo"},
303 {log.BytesValue([]byte{2, 4, 6}), "[2 4 6]"},
304 {log.SliceValue(log.IntValue(3), log.StringValue("foo")), "[3 foo]"},
305 {log.MapValue(log.Int("a", 1), log.Bool("b", true)), "[a:1 b:true]"},
306 {log.Value{}, "<nil>"},
307 } {
308 got := test.v.String()
309 assert.Equal(t, test.want, got)
310 }
311 }
312
313 func TestValueFromAttribute(t *testing.T) {
314 testCases := []struct {
315 desc string
316 v attribute.Value
317 want log.Value
318 }{
319 {
320 desc: "Empty",
321 v: attribute.Value{},
322 want: log.Value{},
323 },
324 {
325 desc: "Bool",
326 v: attribute.BoolValue(true),
327 want: log.BoolValue(true),
328 },
329 {
330 desc: "BoolSlice",
331 v: attribute.BoolSliceValue([]bool{true, false}),
332 want: log.SliceValue(log.BoolValue(true), log.BoolValue(false)),
333 },
334 {
335 desc: "Int64",
336 v: attribute.Int64Value(13),
337 want: log.Int64Value(13),
338 },
339 {
340 desc: "Int64Slice",
341 v: attribute.Int64SliceValue([]int64{12, 34}),
342 want: log.SliceValue(log.Int64Value(12), log.Int64Value(34)),
343 },
344 {
345 desc: "Float64",
346 v: attribute.Float64Value(3.14),
347 want: log.Float64Value(3.14),
348 },
349 {
350 desc: "Float64Slice",
351 v: attribute.Float64SliceValue([]float64{3.14, 2.72}),
352 want: log.SliceValue(log.Float64Value(3.14), log.Float64Value(2.72)),
353 },
354 {
355 desc: "String",
356 v: attribute.StringValue("foo"),
357 want: log.StringValue("foo"),
358 },
359 {
360 desc: "StringSlice",
361 v: attribute.StringSliceValue([]string{"foo", "bar"}),
362 want: log.SliceValue(log.StringValue("foo"), log.StringValue("bar")),
363 },
364 }
365 for _, tc := range testCases {
366 t.Run(tc.desc, func(t *testing.T) {
367 got := log.ValueFromAttribute(tc.v)
368 if !got.Equal(tc.want) {
369 t.Errorf("got: %v; want:%v", got, tc.want)
370 }
371 })
372 }
373 }
374
375 func TestKeyValueFromAttribute(t *testing.T) {
376 testCases := []struct {
377 desc string
378 kv attribute.KeyValue
379 want log.KeyValue
380 }{
381 {
382 desc: "Empty",
383 kv: attribute.KeyValue{},
384 want: log.KeyValue{},
385 },
386 {
387 desc: "Bool",
388 kv: attribute.Bool("k", true),
389 want: log.Bool("k", true),
390 },
391 {
392 desc: "BoolSlice",
393 kv: attribute.BoolSlice("k", []bool{true, false}),
394 want: log.Slice("k", log.BoolValue(true), log.BoolValue(false)),
395 },
396 {
397 desc: "Int64",
398 kv: attribute.Int64("k", 13),
399 want: log.Int64("k", 13),
400 },
401 {
402 desc: "Int64Slice",
403 kv: attribute.Int64Slice("k", []int64{12, 34}),
404 want: log.Slice("k", log.Int64Value(12), log.Int64Value(34)),
405 },
406 {
407 desc: "Float64",
408 kv: attribute.Float64("k", 3.14),
409 want: log.Float64("k", 3.14),
410 },
411 {
412 desc: "Float64Slice",
413 kv: attribute.Float64Slice("k", []float64{3.14, 2.72}),
414 want: log.Slice("k", log.Float64Value(3.14), log.Float64Value(2.72)),
415 },
416 {
417 desc: "String",
418 kv: attribute.String("k", "foo"),
419 want: log.String("k", "foo"),
420 },
421 {
422 desc: "StringSlice",
423 kv: attribute.StringSlice("k", []string{"foo", "bar"}),
424 want: log.Slice("k", log.StringValue("foo"), log.StringValue("bar")),
425 },
426 }
427 for _, tc := range testCases {
428 t.Run(tc.desc, func(t *testing.T) {
429 got := log.KeyValueFromAttribute(tc.kv)
430 if !got.Equal(tc.want) {
431 t.Errorf("got: %v; want:%v", got, tc.want)
432 }
433 })
434 }
435 }
436
437 type logSink struct {
438 logr.LogSink
439
440 err error
441 msg string
442 keysAndValues []any
443 }
444
445 func (l *logSink) Error(err error, msg string, keysAndValues ...any) {
446 l.err, l.msg, l.keysAndValues = err, msg, keysAndValues
447 l.LogSink.Error(err, msg, keysAndValues...)
448 }
449
450 func testErrKind[T any](f func() T, msg string, k log.Kind) func(*testing.T) {
451 return func(t *testing.T) {
452 t.Cleanup(func(l logr.Logger) func() {
453 return func() { global.SetLogger(l) }
454 }(global.GetLogger()))
455
456 l := &logSink{LogSink: testr.New(t).GetSink()}
457 global.SetLogger(logr.New(l))
458
459 assert.Zero(t, f())
460
461 assert.ErrorContains(t, l.err, "invalid Kind")
462 assert.Equal(t, msg, l.msg)
463 require.Len(t, l.keysAndValues, 2, "logged attributes")
464 assert.Equal(t, l.keysAndValues[1], k)
465 }
466 }
467
468 func testKV(t *testing.T, key string, kv log.KeyValue) {
469 t.Helper()
470
471 assert.Equal(t, key, kv.Key, "incorrect key")
472 assert.False(t, kv.Value.Empty(), "value empty")
473 }
474
475 func TestAllocationLimits(t *testing.T) {
476 const (
477 runs = 5
478 key = "key"
479 )
480
481 // Assign testing results to external scope so the compiler doesn't
482 // optimize away the testing statements.
483 var (
484 i int64
485 f float64
486 b bool
487 by []byte
488 s string
489 slice []log.Value
490 m []log.KeyValue
491 )
492
493 assert.Equal(t, 0.0, testing.AllocsPerRun(runs, func() {
494 b = log.Bool(key, true).Value.AsBool()
495 }), "Bool.AsBool")
496
497 assert.Equal(t, 0.0, testing.AllocsPerRun(runs, func() {
498 f = log.Float64(key, 3.0).Value.AsFloat64()
499 }), "Float.AsFloat64")
500
501 assert.Equal(t, 0.0, testing.AllocsPerRun(runs, func() {
502 i = log.Int(key, 9).Value.AsInt64()
503 }), "Int.AsInt64")
504
505 assert.Equal(t, 0.0, testing.AllocsPerRun(runs, func() {
506 i = log.Int64(key, 8).Value.AsInt64()
507 }), "Int64.AsInt64")
508
509 assert.Equal(t, 0.0, testing.AllocsPerRun(runs, func() {
510 s = log.String(key, "value").Value.AsString()
511 }), "String.AsString")
512
513 byteVal := []byte{1, 3, 4}
514 assert.Equal(t, 0.0, testing.AllocsPerRun(runs, func() {
515 by = log.Bytes(key, byteVal).Value.AsBytes()
516 }), "Byte.AsBytes")
517
518 sliceVal := []log.Value{log.BoolValue(true), log.IntValue(32)}
519 assert.Equal(t, 0.0, testing.AllocsPerRun(runs, func() {
520 slice = log.Slice(key, sliceVal...).Value.AsSlice()
521 }), "Slice.AsSlice")
522
523 mapVal := []log.KeyValue{log.Bool("b", true), log.Int("i", 32)}
524 assert.Equal(t, 0.0, testing.AllocsPerRun(runs, func() {
525 m = log.Map(key, mapVal...).Value.AsMap()
526 }), "Map.AsMap")
527
528 // Convince the linter these values are used.
529 _, _, _, _, _, _, _ = i, f, b, by, s, slice, m
530 }