From 225fc2c137e57f47cc2f7589a321940f60d03774 Mon Sep 17 00:00:00 2001 From: Ziqi Zhao Date: Sun, 2 Oct 2022 14:00:40 +0800 Subject: [PATCH 01/12] attribute: fix slice related function bug Signed-off-by: Ziqi Zhao --- attribute/value.go | 85 +++++++++++++++++++++++++++-------------- attribute/value_test.go | 81 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 137 insertions(+), 29 deletions(-) diff --git a/attribute/value.go b/attribute/value.go index 57899f682e7..4c5bf74c529 100644 --- a/attribute/value.go +++ b/attribute/value.go @@ -17,6 +17,7 @@ package attribute // import "go.opentelemetry.io/otel/attribute" import ( "encoding/json" "fmt" + "reflect" "strconv" "go.opentelemetry.io/otel/internal" @@ -66,11 +67,11 @@ func BoolValue(v bool) Value { // BoolSliceValue creates a BOOLSLICE Value. func BoolSliceValue(v []bool) Value { - cp := make([]bool, len(v)) - copy(cp, v) + cp := reflect.New(reflect.ArrayOf(len(v), reflect.TypeOf(false))) + copy(cp.Elem().Slice(0, len(v)).Interface().([]bool), v) return Value{ vtype: BOOLSLICE, - slice: &cp, + slice: cp.Elem().Interface(), } } @@ -79,15 +80,15 @@ func IntValue(v int) Value { return Int64Value(int64(v)) } -// IntSliceValue creates an INTSLICE Value. func IntSliceValue(v []int) Value { - cp := make([]int64, 0, len(v)) - for _, i := range v { - cp = append(cp, int64(i)) + var int64Val int64 + cp := reflect.New(reflect.ArrayOf(len(v), reflect.TypeOf(int64Val))) + for i, val := range v { + cp.Elem().Index(i).SetInt(int64(val)) } return Value{ vtype: INT64SLICE, - slice: &cp, + slice: cp.Elem().Interface(), } } @@ -101,11 +102,12 @@ func Int64Value(v int64) Value { // Int64SliceValue creates an INT64SLICE Value. func Int64SliceValue(v []int64) Value { - cp := make([]int64, len(v)) - copy(cp, v) + var int64Val int64 + cp := reflect.New(reflect.ArrayOf(len(v), reflect.TypeOf(int64Val))) + copy(cp.Elem().Slice(0, len(v)).Interface().([]int64), v) return Value{ vtype: INT64SLICE, - slice: &cp, + slice: cp.Elem().Interface(), } } @@ -119,11 +121,12 @@ func Float64Value(v float64) Value { // Float64SliceValue creates a FLOAT64SLICE Value. func Float64SliceValue(v []float64) Value { - cp := make([]float64, len(v)) - copy(cp, v) + var float64Val float64 + cp := reflect.New(reflect.ArrayOf(len(v), reflect.TypeOf(float64Val))) + copy(cp.Elem().Slice(0, len(v)).Interface().([]float64), v) return Value{ vtype: FLOAT64SLICE, - slice: &cp, + slice: cp.Elem().Interface(), } } @@ -137,11 +140,12 @@ func StringValue(v string) Value { // StringSliceValue creates a STRINGSLICE Value. func StringSliceValue(v []string) Value { - cp := make([]string, len(v)) - copy(cp, v) + var stringVal string + cp := reflect.New(reflect.ArrayOf(len(v), reflect.TypeOf(stringVal))) + copy(cp.Elem().Slice(0, len(v)).Interface().([]string), v) return Value{ vtype: STRINGSLICE, - slice: &cp, + slice: cp.Elem().Interface(), } } @@ -159,10 +163,15 @@ func (v Value) AsBool() bool { // AsBoolSlice returns the []bool value. Make sure that the Value's type is // BOOLSLICE. func (v Value) AsBoolSlice() []bool { - if s, ok := v.slice.(*[]bool); ok { - return *s + rv := reflect.ValueOf(v.slice) + if rv.Type().Kind() != reflect.Array { + return nil } - return nil + correctLen := rv.Len() + correctType := reflect.ArrayOf(correctLen, reflect.TypeOf(false)) + cpy := reflect.New(correctType) + _ = reflect.Copy(cpy.Elem(), rv) + return cpy.Elem().Slice(0, correctLen).Interface().([]bool) } // AsInt64 returns the int64 value. Make sure that the Value's type is @@ -174,10 +183,16 @@ func (v Value) AsInt64() int64 { // AsInt64Slice returns the []int64 value. Make sure that the Value's type is // INT64SLICE. func (v Value) AsInt64Slice() []int64 { - if s, ok := v.slice.(*[]int64); ok { - return *s + rv := reflect.ValueOf(v.slice) + if rv.Type().Kind() != reflect.Array { + return nil } - return nil + correctLen := rv.Len() + var int64var int64 + correctType := reflect.ArrayOf(correctLen, reflect.TypeOf(int64var)) + cpy := reflect.New(correctType) + _ = reflect.Copy(cpy.Elem(), rv) + return cpy.Elem().Slice(0, correctLen).Interface().([]int64) } // AsFloat64 returns the float64 value. Make sure that the Value's @@ -189,10 +204,16 @@ func (v Value) AsFloat64() float64 { // AsFloat64Slice returns the []float64 value. Make sure that the Value's type is // FLOAT64SLICE. func (v Value) AsFloat64Slice() []float64 { - if s, ok := v.slice.(*[]float64); ok { - return *s + rv := reflect.ValueOf(v.slice) + if rv.Type().Kind() != reflect.Array { + return nil } - return nil + correctLen := rv.Len() + var float64var float64 + correctType := reflect.ArrayOf(correctLen, reflect.TypeOf(float64var)) + cpy := reflect.New(correctType) + _ = reflect.Copy(cpy.Elem(), rv) + return cpy.Elem().Slice(0, correctLen).Interface().([]float64) } // AsString returns the string value. Make sure that the Value's type @@ -204,10 +225,16 @@ func (v Value) AsString() string { // AsStringSlice returns the []string value. Make sure that the Value's type is // STRINGSLICE. func (v Value) AsStringSlice() []string { - if s, ok := v.slice.(*[]string); ok { - return *s + rv := reflect.ValueOf(v.slice) + if rv.Type().Kind() != reflect.Array { + return nil } - return nil + correctLen := rv.Len() + var stringStr string + correctType := reflect.ArrayOf(correctLen, reflect.TypeOf(stringStr)) + cpy := reflect.New(correctType) + _ = reflect.Copy(cpy.Elem(), rv) + return cpy.Elem().Slice(0, correctLen).Interface().([]string) } type unknownValueType struct{} diff --git a/attribute/value_test.go b/attribute/value_test.go index c9370880b54..82745e038a1 100644 --- a/attribute/value_test.go +++ b/attribute/value_test.go @@ -18,6 +18,7 @@ import ( "testing" "github.com/google/go-cmp/cmp" + "github.com/stretchr/testify/assert" "go.opentelemetry.io/otel/attribute" ) @@ -104,3 +105,83 @@ func TestValue(t *testing.T) { } } } + +func TestSetComparability(t *testing.T) { + pairs := [][2]attribute.KeyValue{ + { + attribute.Bool("Bool", true), + attribute.Bool("Bool", true), + }, + { + attribute.BoolSlice("BoolSlice", []bool{true, false, true}), + attribute.BoolSlice("BoolSlice", []bool{true, false, true}), + }, + { + attribute.Int("Int", 34), + attribute.Int("Int", 34), + }, + { + attribute.IntSlice("IntSlice", []int{312, 1, -2}), + attribute.IntSlice("IntSlice", []int{312, 1, -2}), + }, + { + attribute.Int64("Int64", 98), + attribute.Int64("Int64", 98), + }, + { + attribute.Int64Slice("Int64Slice", []int64{12, 1298, -219, 2}), + attribute.Int64Slice("Int64Slice", []int64{12, 1298, -219, 2}), + }, + { + attribute.Float64("Float64", 19.09), + attribute.Float64("Float64", 19.09), + }, + { + attribute.Float64Slice("Float64Slice", []float64{12398.1, -37.1713873737, 3}), + attribute.Float64Slice("Float64Slice", []float64{12398.1, -37.1713873737, 3}), + }, + { + attribute.String("String", "string value"), + attribute.String("String", "string value"), + }, + { + attribute.StringSlice("StringSlice", []string{"one", "two", "three"}), + attribute.StringSlice("StringSlice", []string{"one", "two", "three"}), + }, + } + + for _, p := range pairs { + s0, s1 := attribute.NewSet(p[0]), attribute.NewSet(p[1]) + m := map[attribute.Set]struct{}{s0: {}} + _, ok := m[s1] + assert.Truef(t, ok, "%s not comparable", p[0].Value.Type()) + } +} + +func TestAsSlice(t *testing.T) { + + bs1 := []bool{true, false, true} + kv := attribute.BoolSlice("BoolSlice", bs1) + bs2 := kv.Value.AsBoolSlice() + assert.Equal(t, bs1, bs2) + + i64s1 := []int64{12, 1298, -219, 2} + kv = attribute.Int64Slice("Int64Slice", i64s1) + i64s2 := kv.Value.AsInt64Slice() + assert.Equal(t, i64s1, i64s2) + + is1 := []int{12, 1298, -219, 2} + kv = attribute.IntSlice("IntSlice", is1) + i64s2 = kv.Value.AsInt64Slice() + assert.Equal(t, i64s1, i64s2) + + fs1 := []float64{12398.1, -37.1713873737, 3} + kv = attribute.Float64Slice("Float64Slice", fs1) + fs2 := kv.Value.AsFloat64Slice() + assert.Equal(t, fs1, fs2) + + ss1 := []string{"one", "two", "three"} + kv = attribute.StringSlice("StringSlice", ss1) + ss2 := kv.Value.AsStringSlice() + assert.Equal(t, ss1, ss2) +} From 84fbcb2269fb9746281cc468eeaf940d386ad561 Mon Sep 17 00:00:00 2001 From: Ziqi Zhao Date: Mon, 3 Oct 2022 14:50:51 +0800 Subject: [PATCH 02/12] Update attribute/value_test.go Co-authored-by: Tyler Yahn --- attribute/value_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/attribute/value_test.go b/attribute/value_test.go index 82745e038a1..ec79dc62a27 100644 --- a/attribute/value_test.go +++ b/attribute/value_test.go @@ -159,7 +159,6 @@ func TestSetComparability(t *testing.T) { } func TestAsSlice(t *testing.T) { - bs1 := []bool{true, false, true} kv := attribute.BoolSlice("BoolSlice", bs1) bs2 := kv.Value.AsBoolSlice() From bb492574f0d9591f43ed12e0876f327fd815a76c Mon Sep 17 00:00:00 2001 From: Ziqi Zhao Date: Sat, 8 Oct 2022 12:58:47 +0800 Subject: [PATCH 03/12] fix for comments Signed-off-by: Ziqi Zhao --- attribute/value.go | 52 ++++++++++++++++++++++------------------------ 1 file changed, 25 insertions(+), 27 deletions(-) diff --git a/attribute/value.go b/attribute/value.go index 4c5bf74c529..c9e8e2b4c83 100644 --- a/attribute/value.go +++ b/attribute/value.go @@ -57,6 +57,26 @@ const ( STRINGSLICE ) +func sliceValue[T bool | int64 | float64 | string](v []T) any { + var zero T + cp := reflect.New(reflect.ArrayOf(len(v), reflect.TypeOf(zero))) + copy(cp.Elem().Slice(0, len(v)).Interface().([]T), v) + return cp.Elem().Interface() +} + +func asSlice[T bool | int64 | float64 | string](v any) []T { + rv := reflect.ValueOf(v) + if rv.Type().Kind() != reflect.Array { + return nil + } + var zero T + correctLen := rv.Len() + correctType := reflect.ArrayOf(correctLen, reflect.TypeOf(zero)) + cpy := reflect.New(correctType) + _ = reflect.Copy(cpy.Elem(), rv) + return cpy.Elem().Slice(0, correctLen).Interface().([]T) +} + // BoolValue creates a BOOL Value. func BoolValue(v bool) Value { return Value{ @@ -67,12 +87,7 @@ func BoolValue(v bool) Value { // BoolSliceValue creates a BOOLSLICE Value. func BoolSliceValue(v []bool) Value { - cp := reflect.New(reflect.ArrayOf(len(v), reflect.TypeOf(false))) - copy(cp.Elem().Slice(0, len(v)).Interface().([]bool), v) - return Value{ - vtype: BOOLSLICE, - slice: cp.Elem().Interface(), - } + return Value{vtype: BOOLSLICE, slice: sliceValue(v)} } // IntValue creates an INT64 Value. @@ -80,6 +95,7 @@ func IntValue(v int) Value { return Int64Value(int64(v)) } +// IntSliceValue creates an INTSLICE Value. func IntSliceValue(v []int) Value { var int64Val int64 cp := reflect.New(reflect.ArrayOf(len(v), reflect.TypeOf(int64Val))) @@ -102,13 +118,7 @@ func Int64Value(v int64) Value { // Int64SliceValue creates an INT64SLICE Value. func Int64SliceValue(v []int64) Value { - var int64Val int64 - cp := reflect.New(reflect.ArrayOf(len(v), reflect.TypeOf(int64Val))) - copy(cp.Elem().Slice(0, len(v)).Interface().([]int64), v) - return Value{ - vtype: INT64SLICE, - slice: cp.Elem().Interface(), - } + return Value{vtype: INT64SLICE, slice: sliceValue(v)} } // Float64Value creates a FLOAT64 Value. @@ -121,13 +131,7 @@ func Float64Value(v float64) Value { // Float64SliceValue creates a FLOAT64SLICE Value. func Float64SliceValue(v []float64) Value { - var float64Val float64 - cp := reflect.New(reflect.ArrayOf(len(v), reflect.TypeOf(float64Val))) - copy(cp.Elem().Slice(0, len(v)).Interface().([]float64), v) - return Value{ - vtype: FLOAT64SLICE, - slice: cp.Elem().Interface(), - } + return Value{vtype: FLOAT64SLICE, slice: sliceValue(v)} } // StringValue creates a STRING Value. @@ -140,13 +144,7 @@ func StringValue(v string) Value { // StringSliceValue creates a STRINGSLICE Value. func StringSliceValue(v []string) Value { - var stringVal string - cp := reflect.New(reflect.ArrayOf(len(v), reflect.TypeOf(stringVal))) - copy(cp.Elem().Slice(0, len(v)).Interface().([]string), v) - return Value{ - vtype: STRINGSLICE, - slice: cp.Elem().Interface(), - } + return Value{vtype: STRINGSLICE, slice: sliceValue(v)} } // Type returns a type of the Value. From ff35d7fb478c3fa4646f35cbcd3db63dec703ce8 Mon Sep 17 00:00:00 2001 From: Ziqi Zhao Date: Sat, 8 Oct 2022 13:54:27 +0800 Subject: [PATCH 04/12] fix for comments Signed-off-by: Ziqi Zhao --- attribute/value.go | 72 ++++++---------------------------------------- 1 file changed, 9 insertions(+), 63 deletions(-) diff --git a/attribute/value.go b/attribute/value.go index c9e8e2b4c83..d7967512791 100644 --- a/attribute/value.go +++ b/attribute/value.go @@ -21,6 +21,7 @@ import ( "strconv" "go.opentelemetry.io/otel/internal" + "go.opentelemetry.io/otel/internal/attribute" ) //go:generate stringer -type=Type @@ -57,26 +58,6 @@ const ( STRINGSLICE ) -func sliceValue[T bool | int64 | float64 | string](v []T) any { - var zero T - cp := reflect.New(reflect.ArrayOf(len(v), reflect.TypeOf(zero))) - copy(cp.Elem().Slice(0, len(v)).Interface().([]T), v) - return cp.Elem().Interface() -} - -func asSlice[T bool | int64 | float64 | string](v any) []T { - rv := reflect.ValueOf(v) - if rv.Type().Kind() != reflect.Array { - return nil - } - var zero T - correctLen := rv.Len() - correctType := reflect.ArrayOf(correctLen, reflect.TypeOf(zero)) - cpy := reflect.New(correctType) - _ = reflect.Copy(cpy.Elem(), rv) - return cpy.Elem().Slice(0, correctLen).Interface().([]T) -} - // BoolValue creates a BOOL Value. func BoolValue(v bool) Value { return Value{ @@ -87,7 +68,7 @@ func BoolValue(v bool) Value { // BoolSliceValue creates a BOOLSLICE Value. func BoolSliceValue(v []bool) Value { - return Value{vtype: BOOLSLICE, slice: sliceValue(v)} + return Value{vtype: BOOLSLICE, slice: attribute.SliceValue(v)} } // IntValue creates an INT64 Value. @@ -118,7 +99,7 @@ func Int64Value(v int64) Value { // Int64SliceValue creates an INT64SLICE Value. func Int64SliceValue(v []int64) Value { - return Value{vtype: INT64SLICE, slice: sliceValue(v)} + return Value{vtype: INT64SLICE, slice: attribute.SliceValue(v)} } // Float64Value creates a FLOAT64 Value. @@ -131,7 +112,7 @@ func Float64Value(v float64) Value { // Float64SliceValue creates a FLOAT64SLICE Value. func Float64SliceValue(v []float64) Value { - return Value{vtype: FLOAT64SLICE, slice: sliceValue(v)} + return Value{vtype: FLOAT64SLICE, slice: attribute.SliceValue(v)} } // StringValue creates a STRING Value. @@ -144,7 +125,7 @@ func StringValue(v string) Value { // StringSliceValue creates a STRINGSLICE Value. func StringSliceValue(v []string) Value { - return Value{vtype: STRINGSLICE, slice: sliceValue(v)} + return Value{vtype: STRINGSLICE, slice: attribute.SliceValue(v)} } // Type returns a type of the Value. @@ -161,15 +142,7 @@ func (v Value) AsBool() bool { // AsBoolSlice returns the []bool value. Make sure that the Value's type is // BOOLSLICE. func (v Value) AsBoolSlice() []bool { - rv := reflect.ValueOf(v.slice) - if rv.Type().Kind() != reflect.Array { - return nil - } - correctLen := rv.Len() - correctType := reflect.ArrayOf(correctLen, reflect.TypeOf(false)) - cpy := reflect.New(correctType) - _ = reflect.Copy(cpy.Elem(), rv) - return cpy.Elem().Slice(0, correctLen).Interface().([]bool) + return attribute.AsSlice[bool](v.slice) } // AsInt64 returns the int64 value. Make sure that the Value's type is @@ -181,16 +154,7 @@ func (v Value) AsInt64() int64 { // AsInt64Slice returns the []int64 value. Make sure that the Value's type is // INT64SLICE. func (v Value) AsInt64Slice() []int64 { - rv := reflect.ValueOf(v.slice) - if rv.Type().Kind() != reflect.Array { - return nil - } - correctLen := rv.Len() - var int64var int64 - correctType := reflect.ArrayOf(correctLen, reflect.TypeOf(int64var)) - cpy := reflect.New(correctType) - _ = reflect.Copy(cpy.Elem(), rv) - return cpy.Elem().Slice(0, correctLen).Interface().([]int64) + return attribute.AsSlice[int64](v.slice) } // AsFloat64 returns the float64 value. Make sure that the Value's @@ -202,16 +166,7 @@ func (v Value) AsFloat64() float64 { // AsFloat64Slice returns the []float64 value. Make sure that the Value's type is // FLOAT64SLICE. func (v Value) AsFloat64Slice() []float64 { - rv := reflect.ValueOf(v.slice) - if rv.Type().Kind() != reflect.Array { - return nil - } - correctLen := rv.Len() - var float64var float64 - correctType := reflect.ArrayOf(correctLen, reflect.TypeOf(float64var)) - cpy := reflect.New(correctType) - _ = reflect.Copy(cpy.Elem(), rv) - return cpy.Elem().Slice(0, correctLen).Interface().([]float64) + return attribute.AsSlice[float64](v.slice) } // AsString returns the string value. Make sure that the Value's type @@ -223,16 +178,7 @@ func (v Value) AsString() string { // AsStringSlice returns the []string value. Make sure that the Value's type is // STRINGSLICE. func (v Value) AsStringSlice() []string { - rv := reflect.ValueOf(v.slice) - if rv.Type().Kind() != reflect.Array { - return nil - } - correctLen := rv.Len() - var stringStr string - correctType := reflect.ArrayOf(correctLen, reflect.TypeOf(stringStr)) - cpy := reflect.New(correctType) - _ = reflect.Copy(cpy.Elem(), rv) - return cpy.Elem().Slice(0, correctLen).Interface().([]string) + return attribute.AsSlice[string](v.slice) } type unknownValueType struct{} From efa11d3e7ce4b71c05c50198e976a17b82562823 Mon Sep 17 00:00:00 2001 From: Ziqi Zhao Date: Mon, 10 Oct 2022 09:34:08 +0800 Subject: [PATCH 05/12] fix for comments Signed-off-by: Ziqi Zhao --- internal/attribute/attribute.go | 39 +++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 internal/attribute/attribute.go diff --git a/internal/attribute/attribute.go b/internal/attribute/attribute.go new file mode 100644 index 00000000000..c9be9bb609a --- /dev/null +++ b/internal/attribute/attribute.go @@ -0,0 +1,39 @@ +// Copyright The OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package attribute + +import ( + "reflect" +) + +func SliceValue[T bool | int64 | float64 | string](v []T) any { + var zero T + cp := reflect.New(reflect.ArrayOf(len(v), reflect.TypeOf(zero))) + copy(cp.Elem().Slice(0, len(v)).Interface().([]T), v) + return cp.Elem().Interface() +} + +func AsSlice[T bool | int64 | float64 | string](v any) []T { + rv := reflect.ValueOf(v) + if rv.Type().Kind() != reflect.Array { + return nil + } + var zero T + correctLen := rv.Len() + correctType := reflect.ArrayOf(correctLen, reflect.TypeOf(zero)) + cpy := reflect.New(correctType) + _ = reflect.Copy(cpy.Elem(), rv) + return cpy.Elem().Slice(0, correctLen).Interface().([]T) +} From bec66c1c355857c9967b2eca45e75e652cd34d42 Mon Sep 17 00:00:00 2001 From: Ziqi Zhao Date: Mon, 10 Oct 2022 13:38:40 +0800 Subject: [PATCH 06/12] fix for failed test-race Signed-off-by: Ziqi Zhao --- sdk/trace/span.go | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/sdk/trace/span.go b/sdk/trace/span.go index 9760923f702..4443eb755ac 100644 --- a/sdk/trace/span.go +++ b/sdk/trace/span.go @@ -313,26 +313,13 @@ func truncateAttr(limit int, attr attribute.KeyValue) attribute.KeyValue { return attr.Key.String(safeTruncate(v, limit)) } case attribute.STRINGSLICE: - // Do no mutate the original, make a copy. - trucated := attr.Key.StringSlice(attr.Value.AsStringSlice()) - // Do not do this. - // - // v := trucated.Value.AsStringSlice() - // cp := make([]string, len(v)) - // /* Copy and truncate values to cp ... */ - // trucated.Value = attribute.StringSliceValue(cp) - // - // Copying the []string and then assigning it back as a new value with - // attribute.StringSliceValue will copy the data twice. Instead, we - // already made a copy above that only this function owns, update the - // underlying slice data of our copy. - v := trucated.Value.AsStringSlice() + v := attr.Value.AsStringSlice() for i := range v { if len(v[i]) > limit { v[i] = safeTruncate(v[i], limit) } } - return trucated + return attr.Key.StringSlice(v) } return attr } From 8141c855aadb12a05b77048ee177c02bf482b680 Mon Sep 17 00:00:00 2001 From: Ziqi Zhao Date: Mon, 10 Oct 2022 13:46:28 +0800 Subject: [PATCH 07/12] fix for failed test-race Signed-off-by: Ziqi Zhao --- attribute/value.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/attribute/value.go b/attribute/value.go index d7967512791..80a37bd6ff3 100644 --- a/attribute/value.go +++ b/attribute/value.go @@ -210,19 +210,19 @@ func (v Value) AsInterface() interface{} { func (v Value) Emit() string { switch v.Type() { case BOOLSLICE: - return fmt.Sprint(*(v.slice.(*[]bool))) + return fmt.Sprint(v.AsBoolSlice()) case BOOL: return strconv.FormatBool(v.AsBool()) case INT64SLICE: - return fmt.Sprint(*(v.slice.(*[]int64))) + return fmt.Sprint(v.AsInt64Slice()) case INT64: return strconv.FormatInt(v.AsInt64(), 10) case FLOAT64SLICE: - return fmt.Sprint(*(v.slice.(*[]float64))) + return fmt.Sprint(v.AsFloat64Slice()) case FLOAT64: return fmt.Sprint(v.AsFloat64()) case STRINGSLICE: - return fmt.Sprint(*(v.slice.(*[]string))) + return fmt.Sprint(v.AsStringSlice()) case STRING: return v.stringly default: From 06232a4e7a702e29612e65a5a4f398ce153b0d6c Mon Sep 17 00:00:00 2001 From: Ziqi Zhao Date: Mon, 10 Oct 2022 13:50:35 +0800 Subject: [PATCH 08/12] add changelog entry Signed-off-by: Ziqi Zhao --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3fb9ca0f0c5..31a49c5640c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm - Upgrade `golang.org/x/sys/unix` from `v0.0.0-20210423185535-09eb48e85fd7` to `v0.0.0-20220919091848-fb04ddd9f9c8`. This addresses [GO-2022-0493](https://pkg.go.dev/vuln/GO-2022-0493). (#3235) +### Fixed + +- Fix slice atrribute of `attribute` package to be comparable (#3108 #3252) + ## [0.32.2] Metric SDK (Alpha) - 2022-10-11 ### Added From 87d7ee59201f72ba5106469157fea0e63333fe1d Mon Sep 17 00:00:00 2001 From: Ziqi Zhao Date: Wed, 12 Oct 2022 12:37:07 +0800 Subject: [PATCH 09/12] Update internal/attribute/attribute.go Co-authored-by: Chester Cheung --- internal/attribute/attribute.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/attribute/attribute.go b/internal/attribute/attribute.go index c9be9bb609a..b43cbfc47f3 100644 --- a/internal/attribute/attribute.go +++ b/internal/attribute/attribute.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package attribute +package attribute // import "go.opentelemetry.io/otel/internal/attribute" import ( "reflect" From c01cc6dc620386e6512269e4ccd918294bdfc529 Mon Sep 17 00:00:00 2001 From: Ziqi Zhao Date: Wed, 12 Oct 2022 12:45:43 +0800 Subject: [PATCH 10/12] fix for comments Signed-off-by: Ziqi Zhao --- internal/attribute/attribute.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/internal/attribute/attribute.go b/internal/attribute/attribute.go index b43cbfc47f3..22034894473 100644 --- a/internal/attribute/attribute.go +++ b/internal/attribute/attribute.go @@ -12,12 +12,17 @@ // See the License for the specific language governing permissions and // limitations under the License. +/* +Package attribute provide several helper functions for some commonly used +logic of processing attributes. +*/ package attribute // import "go.opentelemetry.io/otel/internal/attribute" import ( "reflect" ) +// SliceValue convert a slice into an array with same elements as slice. func SliceValue[T bool | int64 | float64 | string](v []T) any { var zero T cp := reflect.New(reflect.ArrayOf(len(v), reflect.TypeOf(zero))) @@ -25,6 +30,7 @@ func SliceValue[T bool | int64 | float64 | string](v []T) any { return cp.Elem().Interface() } +// AsSlice convert an array into a slice into with same elements as array. func AsSlice[T bool | int64 | float64 | string](v any) []T { rv := reflect.ValueOf(v) if rv.Type().Kind() != reflect.Array { From aed84666eaeb98513b3a3de4cdab13eb96d0552c Mon Sep 17 00:00:00 2001 From: Ziqi Zhao Date: Thu, 13 Oct 2022 07:12:22 +0800 Subject: [PATCH 11/12] Update CHANGELOG.md Co-authored-by: Tyler Yahn --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 31a49c5640c..7f289527cf7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,7 +20,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm ### Fixed -- Fix slice atrribute of `attribute` package to be comparable (#3108 #3252) +- Slice attributes of `attribute` package are now comparable based on their value, not instance. (#3108 #3252) ## [0.32.2] Metric SDK (Alpha) - 2022-10-11 From 1ed993ed452d7f46dd40d4fcfafcff4f6a86255f Mon Sep 17 00:00:00 2001 From: Aaron Clawson <3766680+MadVikingGod@users.noreply.github.com> Date: Thu, 13 Oct 2022 09:26:03 -0500 Subject: [PATCH 12/12] Move Changelog line to Unreleased. --- CHANGELOG.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index adf80f218b1..79760b035bc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm - `sdktrace.TraceProvider.Shutdown` and `sdktrace.TraceProvider.ForceFlush` to not return error when no processor register. (#3268) +### Fixed + +- Slice attributes of `attribute` package are now comparable based on their value, not instance. (#3108 #3252) + ## [1.11.0/0.32.3] 2022-10-12 ### Added @@ -24,10 +28,6 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm - Upgrade `golang.org/x/sys/unix` from `v0.0.0-20210423185535-09eb48e85fd7` to `v0.0.0-20220919091848-fb04ddd9f9c8`. This addresses [GO-2022-0493](https://pkg.go.dev/vuln/GO-2022-0493). (#3235) -### Fixed - -- Slice attributes of `attribute` package are now comparable based on their value, not instance. (#3108 #3252) - ## [0.32.2] Metric SDK (Alpha) - 2022-10-11 ### Added