Skip to content

Commit

Permalink
Remove deprecated Array from attribute package (#2235)
Browse files Browse the repository at this point in the history
* Remove deprecated Array from attribute pkg

* Add changes to changelog

Co-authored-by: Anthony Mirabella <a9@aneurysm9.com>
  • Loading branch information
MrAlias and Aneurysm9 committed Sep 10, 2021
1 parent 360d130 commit ef126f5
Show file tree
Hide file tree
Showing 8 changed files with 9 additions and 314 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Expand Up @@ -16,6 +16,13 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

- Removed the deprecated package `go.opentelemetry.io/otel/oteltest`. (#2234)
- Removed the deprecated package `go.opentelemetry.io/otel/bridge/opencensus/utils`. (#2233)
- Removed deprecated functions, types, and methods from `go.opentelemetry.io/otel/attribute` package.
Use the typed functions and methods added to the package instead. (#2235)
- The `Key.Array` method is removed.
- The `Array` function is removed.
- The `Any` function is removed.
- The `ArrayValue` function is removed.
- The `AsArray` function is removed.

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

Expand Down
58 changes: 0 additions & 58 deletions attribute/benchmark_test.go
Expand Up @@ -36,15 +36,6 @@ var (
outStrSlice []string
)

func benchmarkAny(k string, v interface{}) func(*testing.B) {
return func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
outKV = attribute.Any(k, v)
}
}
}

func benchmarkEmit(kv attribute.KeyValue) func(*testing.B) {
return func(b *testing.B) {
b.ReportAllocs()
Expand All @@ -54,23 +45,9 @@ func benchmarkEmit(kv attribute.KeyValue) func(*testing.B) {
}
}

func benchmarkArray(k string, v interface{}) func(*testing.B) {
a := attribute.Array(k, v)
return func(b *testing.B) {
b.Run("KeyValue", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
outKV = attribute.Array(k, v)
}
})
b.Run("Emit", benchmarkEmit(a))
}
}

func BenchmarkBool(b *testing.B) {
k, v := "bool", true
kv := attribute.Bool(k, v)
array := []bool{true, false, true}

b.Run("Value", func(b *testing.B) {
b.ReportAllocs()
Expand All @@ -90,9 +67,7 @@ func BenchmarkBool(b *testing.B) {
outBool = kv.Value.AsBool()
}
})
b.Run("Any", benchmarkAny(k, v))
b.Run("Emit", benchmarkEmit(kv))
b.Run("Array", benchmarkArray(k, array))
}

func BenchmarkBoolSlice(b *testing.B) {
Expand All @@ -117,14 +92,12 @@ func BenchmarkBoolSlice(b *testing.B) {
outBoolSlice = kv.Value.AsBoolSlice()
}
})
b.Run("Any", benchmarkAny(k, v))
b.Run("Emit", benchmarkEmit(kv))
}

func BenchmarkInt(b *testing.B) {
k, v := "int", int(42)
kv := attribute.Int(k, v)
array := []int{42, -3, 12}

b.Run("Value", func(b *testing.B) {
b.ReportAllocs()
Expand All @@ -138,9 +111,7 @@ func BenchmarkInt(b *testing.B) {
outKV = attribute.Int(k, v)
}
})
b.Run("Any", benchmarkAny(k, v))
b.Run("Emit", benchmarkEmit(kv))
b.Run("Array", benchmarkArray(k, array))
}

func BenchmarkIntSlice(b *testing.B) {
Expand All @@ -159,26 +130,12 @@ func BenchmarkIntSlice(b *testing.B) {
outKV = attribute.IntSlice(k, v)
}
})
b.Run("Any", benchmarkAny(k, v))
b.Run("Emit", benchmarkEmit(kv))
}

func BenchmarkInt8(b *testing.B) {
b.Run("Any", benchmarkAny("int8", int8(42)))
}

func BenchmarkInt16(b *testing.B) {
b.Run("Any", benchmarkAny("int16", int16(42)))
}

func BenchmarkInt32(b *testing.B) {
b.Run("Any", benchmarkAny("int32", int32(42)))
}

func BenchmarkInt64(b *testing.B) {
k, v := "int64", int64(42)
kv := attribute.Int64(k, v)
array := []int64{42, -3, 12}

b.Run("Value", func(b *testing.B) {
b.ReportAllocs()
Expand All @@ -198,9 +155,7 @@ func BenchmarkInt64(b *testing.B) {
outInt64 = kv.Value.AsInt64()
}
})
b.Run("Any", benchmarkAny(k, v))
b.Run("Emit", benchmarkEmit(kv))
b.Run("Array", benchmarkArray(k, array))
}

func BenchmarkInt64Slice(b *testing.B) {
Expand All @@ -225,14 +180,12 @@ func BenchmarkInt64Slice(b *testing.B) {
outInt64Slice = kv.Value.AsInt64Slice()
}
})
b.Run("Any", benchmarkAny(k, v))
b.Run("Emit", benchmarkEmit(kv))
}

func BenchmarkFloat64(b *testing.B) {
k, v := "float64", float64(42)
kv := attribute.Float64(k, v)
array := []float64{42, -3, 12}

b.Run("Value", func(b *testing.B) {
b.ReportAllocs()
Expand All @@ -252,9 +205,7 @@ func BenchmarkFloat64(b *testing.B) {
outFloat64 = kv.Value.AsFloat64()
}
})
b.Run("Any", benchmarkAny(k, v))
b.Run("Emit", benchmarkEmit(kv))
b.Run("Array", benchmarkArray(k, array))
}

func BenchmarkFloat64Slice(b *testing.B) {
Expand All @@ -279,14 +230,12 @@ func BenchmarkFloat64Slice(b *testing.B) {
outFloat64Slice = kv.Value.AsFloat64Slice()
}
})
b.Run("Any", benchmarkAny(k, v))
b.Run("Emit", benchmarkEmit(kv))
}

func BenchmarkString(b *testing.B) {
k, v := "string", "42"
kv := attribute.String(k, v)
array := []string{"forty-two", "negative three", "twelve"}

b.Run("Value", func(b *testing.B) {
b.ReportAllocs()
Expand All @@ -306,9 +255,7 @@ func BenchmarkString(b *testing.B) {
outStr = kv.Value.AsString()
}
})
b.Run("Any", benchmarkAny(k, v))
b.Run("Emit", benchmarkEmit(kv))
b.Run("Array", benchmarkArray(k, array))
}

func BenchmarkStringSlice(b *testing.B) {
Expand All @@ -333,10 +280,5 @@ func BenchmarkStringSlice(b *testing.B) {
outStrSlice = kv.Value.AsStringSlice()
}
})
b.Run("Any", benchmarkAny(k, v))
b.Run("Emit", benchmarkEmit(kv))
}

func BenchmarkBytes(b *testing.B) {
b.Run("Any", benchmarkAny("bytes", []byte("bytes")))
}
13 changes: 0 additions & 13 deletions attribute/key.go
Expand Up @@ -128,19 +128,6 @@ func (k Key) StringSlice(v []string) KeyValue {
}
}

// Array creates a KeyValue instance with an ARRAY Value.
//
// If creating both a key and value at the same time, use the provided
// convenience function instead -- Array(name, value).
//
// Deprecated: Use the typed *Slice methods instead.
func (k Key) Array(v interface{}) KeyValue {
return KeyValue{
Key: k,
Value: ArrayValue(v),
}
}

// Defined returns true for non-empty keys.
func (k Key) Defined() bool {
return len(k) != 0
Expand Down
59 changes: 0 additions & 59 deletions attribute/kv.go
Expand Up @@ -15,9 +15,7 @@
package attribute // import "go.opentelemetry.io/otel/attribute"

import (
"encoding/json"
"fmt"
"reflect"
)

// KeyValue holds a key and value pair.
Expand Down Expand Up @@ -86,60 +84,3 @@ func StringSlice(k string, v []string) KeyValue {
func Stringer(k string, v fmt.Stringer) KeyValue {
return Key(k).String(v.String())
}

// Array creates a new key-value pair with a passed name and a array.
// Only arrays of primitive type are supported.
//
// Deprecated: Use the typed *Slice functions instead.
func Array(k string, v interface{}) KeyValue {
return Key(k).Array(v)
}

// Any creates a new key-value pair instance with a passed name and
// automatic type inference. This is slower, and not type-safe.
//
// Deprecated: Use the typed functions instead.
func Any(k string, value interface{}) KeyValue {
if value == nil {
return String(k, "<nil>")
}

if stringer, ok := value.(fmt.Stringer); ok {
return String(k, stringer.String())
}

rv := reflect.ValueOf(value)

switch rv.Kind() {
case reflect.Array:
rv = rv.Slice(0, rv.Len())
fallthrough
case reflect.Slice:
switch reflect.TypeOf(value).Elem().Kind() {
case reflect.Bool:
return BoolSlice(k, rv.Interface().([]bool))
case reflect.Int:
return IntSlice(k, rv.Interface().([]int))
case reflect.Int64:
return Int64Slice(k, rv.Interface().([]int64))
case reflect.Float64:
return Float64Slice(k, rv.Interface().([]float64))
case reflect.String:
return StringSlice(k, rv.Interface().([]string))
default:
return KeyValue{Key: Key(k), Value: Value{vtype: INVALID}}
}
case reflect.Bool:
return Bool(k, rv.Bool())
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return Int64(k, rv.Int())
case reflect.Float64:
return Float64(k, rv.Float())
case reflect.String:
return String(k, rv.String())
}
if b, err := json.Marshal(value); b != nil && err == nil {
return String(k, string(b))
}
return String(k, fmt.Sprint(value))
}
87 changes: 0 additions & 87 deletions attribute/kv_test.go
Expand Up @@ -15,7 +15,6 @@
package attribute_test

import (
"strings"
"testing"

"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -80,87 +79,6 @@ func TestKeyValueConstructors(t *testing.T) {
}
}

func TestAny(t *testing.T) {
builder := &strings.Builder{}
builder.WriteString("foo")
jsonifyStruct := struct {
Public string
private string
Tagged string `json:"tagName"`
Empty string
OmitEmpty string `json:",omitempty"`
Omit string `json:"-"`
}{"foo", "bar", "baz", "", "", "omitted"}
invalidStruct := struct {
N complex64
}{complex(0, 0)}
for _, testcase := range []struct {
key string
value interface{}
wantType attribute.Type
wantValue interface{}
}{
{
key: "bool type inferred",
value: true,
wantType: attribute.BOOL,
wantValue: true,
},
{
key: "int64 type inferred",
value: int64(42),
wantType: attribute.INT64,
wantValue: int64(42),
},
{
key: "float64 type inferred",
value: float64(42.1),
wantType: attribute.FLOAT64,
wantValue: 42.1,
},
{
key: "string type inferred",
value: "foo",
wantType: attribute.STRING,
wantValue: "foo",
},
{
key: "stringer type inferred",
value: builder,
wantType: attribute.STRING,
wantValue: "foo",
},
{
key: "unknown value serialized as %v",
value: nil,
wantType: attribute.STRING,
wantValue: "<nil>",
},
{
key: "JSON struct serialized correctly",
value: &jsonifyStruct,
wantType: attribute.STRING,
wantValue: `{"Public":"foo","tagName":"baz","Empty":""}`,
},
{
key: "Invalid JSON struct falls back to string",
value: &invalidStruct,
wantType: attribute.STRING,
wantValue: "&{(0+0i)}",
},
} {
t.Logf("Running test case %s", testcase.key)
keyValue := attribute.Any(testcase.key, testcase.value)
if keyValue.Value.Type() != testcase.wantType {
t.Errorf("wrong value type, got %#v, expected %#v", keyValue.Value.Type(), testcase.wantType)
}
got := keyValue.Value.AsInterface()
if diff := cmp.Diff(testcase.wantValue, got); diff != "" {
t.Errorf("+got, -want: %s", diff)
}
}
}

func TestKeyValueValid(t *testing.T) {
tests := []struct {
desc string
Expand Down Expand Up @@ -206,11 +124,6 @@ func TestKeyValueValid(t *testing.T) {
valid: true,
kv: attribute.String("string", ""),
},
{
desc: "non-empty key with ARRAY type Value should be valid",
valid: true,
kv: attribute.Array("array", []int{}),
},
}

for _, test := range tests {
Expand Down

0 comments on commit ef126f5

Please sign in to comment.