Skip to content

Commit

Permalink
change key to a string alias and KeyValue constructors (#217)
Browse files Browse the repository at this point in the history
* change key to a string alias and KeyValue constructors

* fix int and uint tests
  • Loading branch information
paivagustavo authored and rghetia committed Oct 17, 2019
1 parent 75562dd commit 5e409de
Show file tree
Hide file tree
Showing 15 changed files with 288 additions and 102 deletions.
6 changes: 2 additions & 4 deletions api/core/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import (
"unsafe"
)

type Key struct {
Name string
}
type Key string

type KeyValue struct {
Key Key
Expand Down Expand Up @@ -147,7 +145,7 @@ func (k Key) Uint(v uint) KeyValue {
}

func (k Key) Defined() bool {
return len(k.Name) != 0
return len(k) != 0
}

// TODO make this a lazy one-time conversion.
Expand Down
28 changes: 13 additions & 15 deletions api/core/key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestBool(t *testing.T) {
} {
t.Run(testcase.name, func(t *testing.T) {
//proto: func (k core.Key) core.Bool(v bool) KeyValue {}
have := core.Key{}.Bool(testcase.v)
have := core.Key("").Bool(testcase.v)
if diff := cmp.Diff(testcase.want, have.Value); diff != "" {
t.Fatal(diff)
}
Expand All @@ -51,7 +51,7 @@ func TestInt64(t *testing.T) {
} {
t.Run(testcase.name, func(t *testing.T) {
//proto: func (k core.Key) Int64(v int64) KeyValue {
have := core.Key{}.Int64(testcase.v)
have := core.Key("").Int64(testcase.v)
if diff := cmp.Diff(testcase.want, have.Value); diff != "" {
t.Fatal(diff)
}
Expand All @@ -76,7 +76,7 @@ func TestUint64(t *testing.T) {
} {
t.Run(testcase.name, func(t *testing.T) {
//proto: func (k core.Key) Uint64(v uint64) KeyValue {
have := core.Key{}.Uint64(testcase.v)
have := core.Key("").Uint64(testcase.v)
if diff := cmp.Diff(testcase.want, have.Value); diff != "" {
t.Fatal(diff)
}
Expand All @@ -101,7 +101,7 @@ func TestFloat64(t *testing.T) {
} {
t.Run(testcase.name, func(t *testing.T) {
//proto: func (k core.Key) Float64(v float64) KeyValue {
have := core.Key{}.Float64(testcase.v)
have := core.Key("").Float64(testcase.v)
if diff := cmp.Diff(testcase.want, have.Value); diff != "" {
t.Fatal(diff)
}
Expand All @@ -126,7 +126,7 @@ func TestInt32(t *testing.T) {
} {
t.Run(testcase.name, func(t *testing.T) {
//proto: func (k core.Key) Int32(v int32) KeyValue {
have := core.Key{}.Int32(testcase.v)
have := core.Key("").Int32(testcase.v)
if diff := cmp.Diff(testcase.want, have.Value); diff != "" {
t.Fatal(diff)
}
Expand All @@ -151,7 +151,7 @@ func TestUint32(t *testing.T) {
} {
t.Run(testcase.name, func(t *testing.T) {
//proto: func (k core.Key) Uint32(v uint32) KeyValue {
have := core.Key{}.Uint32(testcase.v)
have := core.Key("").Uint32(testcase.v)
if diff := cmp.Diff(testcase.want, have.Value); diff != "" {
t.Fatal(diff)
}
Expand All @@ -176,7 +176,7 @@ func TestFloat32(t *testing.T) {
} {
t.Run(testcase.name, func(t *testing.T) {
//proto: func (k core.Key) Float32(v float32) KeyValue {
have := core.Key{}.Float32(testcase.v)
have := core.Key("").Float32(testcase.v)
if diff := cmp.Diff(testcase.want, have.Value); diff != "" {
t.Fatal(diff)
}
Expand All @@ -201,7 +201,7 @@ func TestString(t *testing.T) {
} {
t.Run(testcase.name, func(t *testing.T) {
//proto: func (k core.Key) String(v string) KeyValue {
have := core.Key{}.String(testcase.v)
have := core.Key("").String(testcase.v)
if diff := cmp.Diff(testcase.want, have.Value); diff != "" {
t.Fatal(diff)
}
Expand All @@ -226,7 +226,7 @@ func TestBytes(t *testing.T) {
} {
t.Run(testcase.name, func(t *testing.T) {
//proto: func (k core.Key) Bytes(v []byte) KeyValue {
have := core.Key{}.Bytes(testcase.v)
have := core.Key("").Bytes(testcase.v)
if diff := cmp.Diff(testcase.want, have.Value); diff != "" {
t.Fatal(diff)
}
Expand Down Expand Up @@ -257,7 +257,7 @@ func TestInt(t *testing.T) {
} {
t.Run(testcase.name, func(t *testing.T) {
//proto: func (k core.Key) Int(v int) KeyValue {
have := core.Key{}.Int(testcase.v)
have := core.Key("").Int(testcase.v)
if diff := cmp.Diff(testcase.want, have.Value); diff != "" {
t.Fatal(diff)
}
Expand Down Expand Up @@ -288,7 +288,7 @@ func TestUint(t *testing.T) {
} {
t.Run(testcase.name, func(t *testing.T) {
//proto: func (k core.Key) Uint(v uint) KeyValue {
have := core.Key{}.Uint(testcase.v)
have := core.Key("").Uint(testcase.v)
if diff := cmp.Diff(testcase.want, have.Value); diff != "" {
t.Fatal(diff)
}
Expand All @@ -304,14 +304,12 @@ func TestDefined(t *testing.T) {
}{
{
name: "Key.Defined() returns true when len(v.Name) != 0",
k: core.Key{
Name: "foo",
},
k: core.Key("foo"),
want: true,
},
{
name: "Key.Defined() returns false when len(v.Name) == 0",
k: core.Key{},
k: core.Key(""),
want: false,
},
} {
Expand Down
2 changes: 1 addition & 1 deletion api/distributedcontext/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func Do(ctx context.Context, f func(ctx context.Context)) {
m := FromContext(ctx)
keyvals := make([]string, 0, 2*len(m.m))
for k, v := range m.m {
keyvals = append(keyvals, k.Name, v.value.Emit())
keyvals = append(keyvals, string(k), v.value.Emit())
}
pprof.Do(ctx, pprof.Labels(keyvals...), f)
}
48 changes: 45 additions & 3 deletions api/key/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,49 @@ import (
)

func New(name string) core.Key {
return core.Key{
Name: name,
}
return core.Key(name)
}

func Bool(k string, v bool) core.KeyValue {
return New(k).Bool(v)
}

func Int64(k string, v int64) core.KeyValue {
return New(k).Int64(v)
}

func Uint64(k string, v uint64) core.KeyValue {
return New(k).Uint64(v)
}

func Float64(k string, v float64) core.KeyValue {
return New(k).Float64(v)
}

func Int32(k string, v int32) core.KeyValue {
return New(k).Int32(v)
}

func Uint32(k string, v uint32) core.KeyValue {
return New(k).Uint32(v)
}

func Float32(k string, v float32) core.KeyValue {
return New(k).Float32(v)
}

func String(k, v string) core.KeyValue {
return New(k).String(v)
}

func Bytes(k string, v []byte) core.KeyValue {
return New(k).Bytes(v)
}

func Int(k string, v int) core.KeyValue {
return New(k).Int(v)
}

func Uint(k string, v uint) core.KeyValue {
return New(k).Uint(v)
}
155 changes: 155 additions & 0 deletions api/key/key_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
package key_test

import (
"testing"
"unsafe"

"github.com/google/go-cmp/cmp"

"go.opentelemetry.io/api/core"
"go.opentelemetry.io/api/key"
)

func TestKeyValueConstructors(t *testing.T) {

tt := []struct {
name string
actual core.KeyValue
expected core.KeyValue
}{
{
name: "Bool",
actual: key.Bool("k1", true),
expected: core.KeyValue{
Key: "k1",
Value: core.Value{
Type: core.BOOL,
Bool: true,
},
},
},
{
name: "Int64",
actual: key.Int64("k1", 123),
expected: core.KeyValue{
Key: "k1",
Value: core.Value{
Type: core.INT64,
Int64: 123,
},
},
},
{
name: "Uint64",
actual: key.Uint64("k1", 1),
expected: core.KeyValue{
Key: "k1",
Value: core.Value{
Type: core.UINT64,
Uint64: 1,
},
},
},
{
name: "Float64",
actual: key.Float64("k1", 123.5),
expected: core.KeyValue{
Key: "k1",
Value: core.Value{
Type: core.FLOAT64,
Float64: 123.5,
},
},
},
{
name: "Int32",
actual: key.Int32("k1", 123),
expected: core.KeyValue{
Key: "k1",
Value: core.Value{
Type: core.INT32,
Int64: 123,
},
},
},
{
name: "Uint32",
actual: key.Uint32("k1", 123),
expected: core.KeyValue{
Key: "k1",
Value: core.Value{
Type: core.UINT32,
Uint64: 123,
},
},
},
{
name: "Float32",
actual: key.Float32("k1", 123.5),
expected: core.KeyValue{
Key: "k1",
Value: core.Value{
Type: core.FLOAT32,
Float64: 123.5,
},
},
},
{
name: "Bytes",
actual: key.Bytes("k1", []byte("v1")),
expected: core.KeyValue{
Key: "k1",
Value: core.Value{
Type: core.BYTES,
Bytes: []byte("v1"),
},
},
},
{
name: "Int",
actual: key.Int("k1", 123),
expected: core.KeyValue{
Key: "k1",
Value: core.Value{
Int64: 123,
Type: IntType(123),
},
},
},
{
name: "Uint",
actual: key.Uint("k1", 123),
expected: core.KeyValue{
Key: "k1",
Value: core.Value{
Uint64: 123,
Type: UintType(123),
},
},
},
}

for _, test := range tt {
t.Run(test.name, func(t *testing.T) {
if diff := cmp.Diff(test.actual, test.expected); diff != "" {
t.Fatal(diff)
}
})
}
}

// IntType returns the core.ValueType depending on system int byte-size
func IntType(v int) core.ValueType {
if unsafe.Sizeof(v) == 4 {
return core.INT32
}
return core.INT64
}

// UintType returns the core.ValueType depending on system uint byte-size
func UintType(v uint) core.ValueType {
if unsafe.Sizeof(v) == 4 {
return core.UINT32
}
return core.UINT64
}
Loading

0 comments on commit 5e409de

Please sign in to comment.