You've already forked opentelemetry-go
mirror of
https://github.com/open-telemetry/opentelemetry-go.git
synced 2026-06-03 18:35:08 +02:00
log: Add ValueFromAttribute and KeyValueFromAttribute (#6180)
Fixes https://github.com/open-telemetry/opentelemetry-go/issues/6158 Related spec PR: https://github.com/open-telemetry/opentelemetry-specification/pull/4373 Benchmark results: ``` goos: linux goarch: amd64 pkg: go.opentelemetry.io/otel/log cpu: 13th Gen Intel(R) Core(TM) i7-13800H BenchmarkKeyValueFromAttribute/Empty-20 72029505 16.47 ns/op 0 B/op 0 allocs/op BenchmarkKeyValueFromAttribute/Bool-20 68560222 16.99 ns/op 0 B/op 0 allocs/op BenchmarkKeyValueFromAttribute/BoolSlice-20 14647401 76.21 ns/op 50 B/op 2 allocs/op BenchmarkKeyValueFromAttribute/Int64-20 70737378 16.92 ns/op 0 B/op 0 allocs/op BenchmarkKeyValueFromAttribute/Int64Slice-20 16780069 96.87 ns/op 64 B/op 2 allocs/op BenchmarkKeyValueFromAttribute/Float64-20 59299638 16.93 ns/op 0 B/op 0 allocs/op BenchmarkKeyValueFromAttribute/Float64Slice-20 12691222 106.2 ns/op 64 B/op 2 allocs/op BenchmarkKeyValueFromAttribute/String-20 63837711 16.97 ns/op 0 B/op 0 allocs/op BenchmarkKeyValueFromAttribute/StringSlice-20 9251001 114.7 ns/op 80 B/op 2 allocs/op PASS ok go.opentelemetry.io/otel/log 14.776s ```
This commit is contained in:
@@ -15,6 +15,7 @@ import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/internal/global"
|
||||
"go.opentelemetry.io/otel/log"
|
||||
)
|
||||
@@ -309,6 +310,130 @@ func TestValueString(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestValueFromAttribute(t *testing.T) {
|
||||
testCases := []struct {
|
||||
desc string
|
||||
v attribute.Value
|
||||
want log.Value
|
||||
}{
|
||||
{
|
||||
desc: "Empty",
|
||||
v: attribute.Value{},
|
||||
want: log.Value{},
|
||||
},
|
||||
{
|
||||
desc: "Bool",
|
||||
v: attribute.BoolValue(true),
|
||||
want: log.BoolValue(true),
|
||||
},
|
||||
{
|
||||
desc: "BoolSlice",
|
||||
v: attribute.BoolSliceValue([]bool{true, false}),
|
||||
want: log.SliceValue(log.BoolValue(true), log.BoolValue(false)),
|
||||
},
|
||||
{
|
||||
desc: "Int64",
|
||||
v: attribute.Int64Value(13),
|
||||
want: log.Int64Value(13),
|
||||
},
|
||||
{
|
||||
desc: "Int64Slice",
|
||||
v: attribute.Int64SliceValue([]int64{12, 34}),
|
||||
want: log.SliceValue(log.Int64Value(12), log.Int64Value(34)),
|
||||
},
|
||||
{
|
||||
desc: "Float64",
|
||||
v: attribute.Float64Value(3.14),
|
||||
want: log.Float64Value(3.14),
|
||||
},
|
||||
{
|
||||
desc: "Float64Slice",
|
||||
v: attribute.Float64SliceValue([]float64{3.14, 2.72}),
|
||||
want: log.SliceValue(log.Float64Value(3.14), log.Float64Value(2.72)),
|
||||
},
|
||||
{
|
||||
desc: "String",
|
||||
v: attribute.StringValue("foo"),
|
||||
want: log.StringValue("foo"),
|
||||
},
|
||||
{
|
||||
desc: "StringSlice",
|
||||
v: attribute.StringSliceValue([]string{"foo", "bar"}),
|
||||
want: log.SliceValue(log.StringValue("foo"), log.StringValue("bar")),
|
||||
},
|
||||
}
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.desc, func(t *testing.T) {
|
||||
got := log.ValueFromAttribute(tc.v)
|
||||
if !got.Equal(tc.want) {
|
||||
t.Errorf("got: %v; want:%v", got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestKeyValueFromAttribute(t *testing.T) {
|
||||
testCases := []struct {
|
||||
desc string
|
||||
kv attribute.KeyValue
|
||||
want log.KeyValue
|
||||
}{
|
||||
{
|
||||
desc: "Empty",
|
||||
kv: attribute.KeyValue{},
|
||||
want: log.KeyValue{},
|
||||
},
|
||||
{
|
||||
desc: "Bool",
|
||||
kv: attribute.Bool("k", true),
|
||||
want: log.Bool("k", true),
|
||||
},
|
||||
{
|
||||
desc: "BoolSlice",
|
||||
kv: attribute.BoolSlice("k", []bool{true, false}),
|
||||
want: log.Slice("k", log.BoolValue(true), log.BoolValue(false)),
|
||||
},
|
||||
{
|
||||
desc: "Int64",
|
||||
kv: attribute.Int64("k", 13),
|
||||
want: log.Int64("k", 13),
|
||||
},
|
||||
{
|
||||
desc: "Int64Slice",
|
||||
kv: attribute.Int64Slice("k", []int64{12, 34}),
|
||||
want: log.Slice("k", log.Int64Value(12), log.Int64Value(34)),
|
||||
},
|
||||
{
|
||||
desc: "Float64",
|
||||
kv: attribute.Float64("k", 3.14),
|
||||
want: log.Float64("k", 3.14),
|
||||
},
|
||||
{
|
||||
desc: "Float64Slice",
|
||||
kv: attribute.Float64Slice("k", []float64{3.14, 2.72}),
|
||||
want: log.Slice("k", log.Float64Value(3.14), log.Float64Value(2.72)),
|
||||
},
|
||||
{
|
||||
desc: "String",
|
||||
kv: attribute.String("k", "foo"),
|
||||
want: log.String("k", "foo"),
|
||||
},
|
||||
{
|
||||
desc: "StringSlice",
|
||||
kv: attribute.StringSlice("k", []string{"foo", "bar"}),
|
||||
want: log.Slice("k", log.StringValue("foo"), log.StringValue("bar")),
|
||||
},
|
||||
}
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.desc, func(t *testing.T) {
|
||||
got := log.KeyValueFromAttribute(tc.kv)
|
||||
if !got.Equal(tc.want) {
|
||||
t.Errorf("got: %v; want:%v", got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
type logSink struct {
|
||||
logr.LogSink
|
||||
|
||||
|
||||
Reference in New Issue
Block a user