Skip to content

Commit

Permalink
Fix slice-valued attributes when used as map keys (#2223)
Browse files Browse the repository at this point in the history
* Fix slice-valued attributes when used as map keys

Signed-off-by: Anthony J Mirabella <a9@aneurysm9.com>

* store pointers to slice values to make them usable as map keys

Signed-off-by: Anthony J Mirabella <a9@aneurysm9.com>

* Emit slice-typed attribute values as slices, not pointers to slices

Signed-off-by: Anthony J Mirabella <a9@aneurysm9.com>

Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>
  • Loading branch information
Aneurysm9 and MrAlias authored Sep 9, 2021
1 parent df2bdbb commit eaacfaa
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 15 deletions.
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]

### Fixed

- Slice-valued attributes can correctly be used as map keys. (#2223)

## [1.0.0-RC3] - 2021-09-02

### Added
Expand Down
36 changes: 22 additions & 14 deletions attribute/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func BoolSliceValue(v []bool) Value {
copy(cp, v)
return Value{
vtype: BOOLSLICE,
slice: cp,
slice: &cp,
}
}

Expand All @@ -94,7 +94,7 @@ func IntSliceValue(v []int) Value {
}
return Value{
vtype: INT64SLICE,
slice: cp,
slice: &cp,
}
}

Expand All @@ -112,7 +112,7 @@ func Int64SliceValue(v []int64) Value {
copy(cp, v)
return Value{
vtype: INT64SLICE,
slice: cp,
slice: &cp,
}
}

Expand All @@ -130,7 +130,7 @@ func Float64SliceValue(v []float64) Value {
copy(cp, v)
return Value{
vtype: FLOAT64SLICE,
slice: cp,
slice: &cp,
}
}

Expand All @@ -148,7 +148,7 @@ func StringSliceValue(v []string) Value {
copy(cp, v)
return Value{
vtype: STRINGSLICE,
slice: cp,
slice: &cp,
}
}

Expand Down Expand Up @@ -197,8 +197,8 @@ 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
if s, ok := v.slice.(*[]bool); ok {
return *s
}
return nil
}
Expand All @@ -212,8 +212,8 @@ 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
if s, ok := v.slice.(*[]int64); ok {
return *s
}
return nil
}
Expand All @@ -227,8 +227,8 @@ func (v Value) AsFloat64() float64 {
// AsFloat64Slice returns the []float64 value. Make sure that the Value's type is
// INT64SLICE.
func (v Value) AsFloat64Slice() []float64 {
if s, ok := v.slice.([]float64); ok {
return s
if s, ok := v.slice.(*[]float64); ok {
return *s
}
return nil
}
Expand All @@ -242,8 +242,8 @@ func (v Value) AsString() string {
// AsStringSlice returns the []string value. Make sure that the Value's type is
// INT64SLICE.
func (v Value) AsStringSlice() []string {
if s, ok := v.slice.([]string); ok {
return s
if s, ok := v.slice.(*[]string); ok {
return *s
}
return nil
}
Expand Down Expand Up @@ -285,14 +285,22 @@ func (v Value) AsInterface() interface{} {
// Emit returns a string representation of Value's data.
func (v Value) Emit() string {
switch v.Type() {
case ARRAY, BOOLSLICE, INT64SLICE, FLOAT64SLICE, STRINGSLICE:
case ARRAY:
return fmt.Sprint(v.slice)
case BOOLSLICE:
return fmt.Sprint(*(v.slice.(*[]bool)))
case BOOL:
return strconv.FormatBool(v.AsBool())
case INT64SLICE:
return fmt.Sprint(*(v.slice.(*[]int64)))
case INT64:
return strconv.FormatInt(v.AsInt64(), 10)
case FLOAT64SLICE:
return fmt.Sprint(*(v.slice.(*[]float64)))
case FLOAT64:
return fmt.Sprint(v.AsFloat64())
case STRINGSLICE:
return fmt.Sprint(*(v.slice.(*[]string)))
case STRING:
return v.stringly
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ func TestSpanData(t *testing.T) {
DroppedAttributes: 1,
DroppedEvents: 2,
DroppedLinks: 3,
Resource: resource.NewSchemaless(attribute.String("rk1", "rv1"), attribute.Int64("rk2", 5)),
Resource: resource.NewSchemaless(attribute.String("rk1", "rv1"), attribute.Int64("rk2", 5), attribute.StringSlice("rk3", []string{"sv1", "sv2"})),
InstrumentationLibrary: instrumentation.Library{
Name: "go.opentelemetry.io/test/otel",
Version: "v0.0.1",
Expand Down

0 comments on commit eaacfaa

Please sign in to comment.