Skip to content
Merged
Show file tree
Hide file tree
Changes from 14 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

## [Unreleased]

### Added

- Add `ConvertAttributeValue` and `ConvertAttributeKeyValue` in `go.opentelemetry.io/otel/log`. (#6180)

<!-- Released section -->
<!-- Don't change this section unless doing release -->

Expand Down
56 changes: 56 additions & 0 deletions log/keyvalue.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"strconv"
"unsafe"

"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/internal/global"
)

Expand Down Expand Up @@ -385,3 +386,58 @@ func Empty(key string) KeyValue {
func (a KeyValue) String() string {
return fmt.Sprintf("%s:%s", a.Key, a.Value)
}

// ConvertAttributeValue converts [attribute.Value) to [Value].
func ConvertAttributeValue(value attribute.Value) Value {
switch value.Type() {
case attribute.INVALID:
return Value{}
case attribute.BOOL:
return BoolValue(value.AsBool())
case attribute.BOOLSLICE:
val := value.AsBoolSlice()
res := make([]Value, 0, len(val))
for _, v := range val {
res = append(res, BoolValue(v))
}
return SliceValue(res...)
case attribute.INT64:
return Int64Value(value.AsInt64())
case attribute.INT64SLICE:
val := value.AsInt64Slice()
res := make([]Value, 0, len(val))
for _, v := range val {
res = append(res, Int64Value(v))
}
return SliceValue(res...)
case attribute.FLOAT64:
return Float64Value(value.AsFloat64())
case attribute.FLOAT64SLICE:
val := value.AsFloat64Slice()
res := make([]Value, 0, len(val))
for _, v := range val {
res = append(res, Float64Value(v))
}
return SliceValue(res...)
case attribute.STRING:
return StringValue(value.AsString())
case attribute.STRINGSLICE:
val := value.AsStringSlice()
res := make([]Value, 0, len(val))
for _, v := range val {
res = append(res, StringValue(v))
}
return SliceValue(res...)
}
// This code should never be reached
// as log attributes are a superset of standard attributes.
panic("unknown attribute type")
}

// ConvertAttributeKeyValue converts [attribute.KeyValue) to [KeyValue].
func ConvertAttributeKeyValue(kv attribute.KeyValue) KeyValue {
return KeyValue{
Key: string(kv.Key),
Value: ConvertAttributeValue(kv.Value),
}
}
63 changes: 63 additions & 0 deletions log/keyvalue_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package log_test
import (
"testing"

"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/log"
)

Expand Down Expand Up @@ -232,3 +233,65 @@ func BenchmarkValueEqual(b *testing.B) {
}
}
}

func BenchmarkConvertAttributeKeyValue(b *testing.B) {
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 {
b.Run(tc.desc, func(b *testing.B) {
b.ReportAllocs()
for range b.N {
outKV = log.ConvertAttributeKeyValue(tc.kv)
}
})
}
}
125 changes: 125 additions & 0 deletions log/keyvalue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -309,6 +310,130 @@ func TestValueString(t *testing.T) {
}
}

func TestConvertAttributeValue(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.ConvertAttributeValue(tc.v)
if !got.Equal(tc.want) {
t.Errorf("got: %v; want:%v", got, tc.want)
}
})
}
}

func TestConvertAttributeKeyValue(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.ConvertAttributeKeyValue(tc.kv)
if !got.Equal(tc.want) {
t.Errorf("got: %v; want:%v", got, tc.want)
}
})
}
}

type logSink struct {
logr.LogSink

Expand Down
Loading