1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-05-31 22:49:54 +02:00

Remove BYTES values, as they are are not thread safe (#249)

* Remove BYTES values, as they are mutable

* Remove more BYTES
This commit is contained in:
Joshua MacDonald 2019-10-30 10:20:53 -07:00 committed by GitHub
parent a2f3dcaf9a
commit 320c62a780
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 11 additions and 86 deletions

View File

@ -21,10 +21,14 @@ type Value struct {
Uint64 uint64 Uint64 uint64
Float64 float64 Float64 float64
String string String string
Bytes []byte
// TODO See how segmentio/stats handles this type, it's much smaller. // Note: this type could be made smaller by using a
// TODO Lazy value type? // core.Number to represent four of these fields, e.g.,
// struct {
// Type ValueType
// String string
// Number Number
// }
} }
const ( const (
@ -37,7 +41,6 @@ const (
FLOAT32 FLOAT32
FLOAT64 FLOAT64
STRING STRING
BYTES
) )
func (k Key) Bool(v bool) KeyValue { func (k Key) Bool(v bool) KeyValue {
@ -120,16 +123,6 @@ func (k Key) String(v string) KeyValue {
} }
} }
func (k Key) Bytes(v []byte) KeyValue {
return KeyValue{
Key: k,
Value: Value{
Type: BYTES,
Bytes: v,
},
}
}
func (k Key) Int(v int) KeyValue { func (k Key) Int(v int) KeyValue {
if unsafe.Sizeof(v) == 4 { if unsafe.Sizeof(v) == 4 {
return k.Int32(int32(v)) return k.Int32(int32(v))
@ -148,7 +141,6 @@ func (k Key) Defined() bool {
return len(k) != 0 return len(k) != 0
} }
// TODO make this a lazy one-time conversion.
func (v Value) Emit() string { func (v Value) Emit() string {
switch v.Type { switch v.Type {
case BOOL: case BOOL:
@ -161,8 +153,6 @@ func (v Value) Emit() string {
return fmt.Sprint(v.Float64) return fmt.Sprint(v.Float64)
case STRING: case STRING:
return v.String return v.String
case BYTES:
return string(v.Bytes)
} }
return "unknown" return "unknown"
} }

View File

@ -209,31 +209,6 @@ func TestString(t *testing.T) {
} }
} }
func TestBytes(t *testing.T) {
for _, testcase := range []struct {
name string
v []byte
want core.Value
}{
{
name: "Key.Bytes() correctly returns keys's internal []byte value",
v: []byte{'f', 'o', 'o'},
want: core.Value{
Type: core.BYTES,
Bytes: []byte{'f', 'o', 'o'},
},
},
} {
t.Run(testcase.name, func(t *testing.T) {
//proto: func (k core.Key) Bytes(v []byte) KeyValue {
have := core.Key("").Bytes(testcase.v)
if diff := cmp.Diff(testcase.want, have.Value); diff != "" {
t.Fatal(diff)
}
})
}
}
func TestInt(t *testing.T) { func TestInt(t *testing.T) {
WTYPE := core.INT64 WTYPE := core.INT64
if unsafe.Sizeof(int(42)) == 4 { if unsafe.Sizeof(int(42)) == 4 {
@ -393,14 +368,6 @@ func TestEmit(t *testing.T) {
}, },
want: "foo", want: "foo",
}, },
{
name: `test Key.Emit() can emit a string representing self.BYTES`,
v: core.Value{
Type: core.BYTES,
Bytes: []byte{'f', 'o', 'o'},
},
want: "foo",
},
} { } {
t.Run(testcase.name, func(t *testing.T) { t.Run(testcase.name, func(t *testing.T) {
//proto: func (v core.Value) Emit() string { //proto: func (v core.Value) Emit() string {

View File

@ -40,10 +40,6 @@ func String(k, v string) core.KeyValue {
return New(k).String(v) 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 { func Int(k string, v int) core.KeyValue {
return New(k).Int(v) return New(k).Int(v)
} }

View File

@ -94,17 +94,6 @@ func TestKeyValueConstructors(t *testing.T) {
}, },
}, },
}, },
{
name: "Bytes",
actual: key.Bytes("k1", []byte("v1")),
expected: core.KeyValue{
Key: "k1",
Value: core.Value{
Type: core.BYTES,
Bytes: []byte("v1"),
},
},
},
{ {
name: "Int", name: "Int",
actual: key.Int("k1", 123), actual: key.Int("k1", 123),

View File

@ -422,8 +422,6 @@ func otTagToOtelCoreKeyValue(k string, v interface{}) otelcore.KeyValue {
return key.Uint(val) return key.Uint(val)
case string: case string:
return key.String(val) return key.String(val)
case []byte:
return key.Bytes(val)
default: default:
return key.String(fmt.Sprint(v)) return key.String(fmt.Sprint(v))
} }

View File

@ -38,7 +38,6 @@ func initTracer() func() {
Tags: []core.KeyValue{ Tags: []core.KeyValue{
key.String("exporter", "jaeger"), key.String("exporter", "jaeger"),
key.Float64("float", 312.23), key.Float64("float", 312.23),
key.Bytes("bytes", []byte("byte array")),
}, },
}), }),
) )

View File

@ -248,12 +248,6 @@ func keyValueToTag(kv core.KeyValue) *gen.Tag {
VDouble: &kv.Value.Float64, VDouble: &kv.Value.Float64,
VType: gen.TagType_DOUBLE, VType: gen.TagType_DOUBLE,
} }
case core.BYTES:
tag = &gen.Tag{
Key: string(kv.Key),
VBinary: kv.Value.Bytes,
VType: gen.TagType_BINARY,
}
} }
return tag return tag
} }

View File

@ -45,7 +45,6 @@ func Test_spanDataToThrift(t *testing.T) {
keyValue := "value" keyValue := "value"
statusCodeValue := int64(2) statusCodeValue := int64(2)
doubleValue := float64(123.456) doubleValue := float64(123.456)
bytesValue := []byte("byte array")
boolTrue := true boolTrue := true
statusMessage := "Unknown" statusMessage := "Unknown"
@ -75,7 +74,6 @@ func Test_spanDataToThrift(t *testing.T) {
Attributes: []core.KeyValue{ Attributes: []core.KeyValue{
key.String("key", keyValue), key.String("key", keyValue),
key.Float64("double", doubleValue), key.Float64("double", doubleValue),
key.Bytes("bytes", bytesValue),
// Jaeger doesn't handle Uint tags, this should be ignored. // Jaeger doesn't handle Uint tags, this should be ignored.
key.Uint64("ignored", 123), key.Uint64("ignored", 123),
}, },
@ -92,7 +90,6 @@ func Test_spanDataToThrift(t *testing.T) {
Tags: []*gen.Tag{ Tags: []*gen.Tag{
{Key: "double", VType: gen.TagType_DOUBLE, VDouble: &doubleValue}, {Key: "double", VType: gen.TagType_DOUBLE, VDouble: &doubleValue},
{Key: "key", VType: gen.TagType_STRING, VStr: &keyValue}, {Key: "key", VType: gen.TagType_STRING, VStr: &keyValue},
{Key: "bytes", VType: gen.TagType_BINARY, VBinary: bytesValue},
{Key: "error", VType: gen.TagType_BOOL, VBool: &boolTrue}, {Key: "error", VType: gen.TagType_BOOL, VBool: &boolTrue},
{Key: "status.code", VType: gen.TagType_LONG, VLong: &statusCodeValue}, {Key: "status.code", VType: gen.TagType_LONG, VLong: &statusCodeValue},
{Key: "status.message", VType: gen.TagType_STRING, VStr: &statusMessage}, {Key: "status.message", VType: gen.TagType_STRING, VStr: &statusMessage},

View File

@ -82,11 +82,11 @@ func TestExporter_ExportSpan(t *testing.T) {
`"Attributes":[` + `"Attributes":[` +
`{` + `{` +
`"Key":"key",` + `"Key":"key",` +
`"Value":{"Type":8,"Bool":false,"Int64":0,"Uint64":0,"Float64":0,"String":"value","Bytes":null}` + `"Value":{"Type":8,"Bool":false,"Int64":0,"Uint64":0,"Float64":0,"String":"value"}` +
`},` + `},` +
`{` + `{` +
`"Key":"double",` + `"Key":"double",` +
`"Value":{"Type":7,"Bool":false,"Int64":0,"Uint64":0,"Float64":123.456,"String":"","Bytes":null}` + `"Value":{"Type":7,"Bool":false,"Int64":0,"Uint64":0,"Float64":123.456,"String":""}` +
`}` + `}` +
`],` + `],` +
`"MessageEvents":null,` + `"MessageEvents":null,` +

View File

@ -436,7 +436,6 @@ func TestInjectCorrelationContextToHTTPReq(t *testing.T) {
key.New("key7").Uint64(123), key.New("key7").Uint64(123),
key.New("key8").Float64(123.567), key.New("key8").Float64(123.567),
key.New("key9").Float32(123.567), key.New("key9").Float32(123.567),
key.New("key10").Bytes([]byte{0x68, 0x69}),
}, },
wantInHeader: []string{ wantInHeader: []string{
"key1=true", "key1=true",
@ -448,7 +447,6 @@ func TestInjectCorrelationContextToHTTPReq(t *testing.T) {
"key7=123", "key7=123",
"key8=123.567", "key8=123.567",
"key9=123.56700134277344", "key9=123.56700134277344",
"key10=hi",
}, },
}, },
} }

View File

@ -99,9 +99,8 @@ func BenchmarkSpanWithAttributes_all(b *testing.B) {
key.New("key6").Uint32(123), key.New("key6").Uint32(123),
key.New("key7").Float64(123.456), key.New("key7").Float64(123.456),
key.New("key8").Float32(123.456), key.New("key8").Float32(123.456),
key.New("key9").Bytes([]byte{1, 2, 3, 4}), key.New("key9").Int(123),
key.New("key10").Int(123), key.New("key10").Uint(123),
key.New("key11").Uint(123),
) )
span.End() span.End()
} }
@ -125,7 +124,6 @@ func BenchmarkSpanWithAttributes_all_2x(b *testing.B) {
key.New("key6").Uint32(123), key.New("key6").Uint32(123),
key.New("key7").Float64(123.456), key.New("key7").Float64(123.456),
key.New("key8").Float32(123.456), key.New("key8").Float32(123.456),
key.New("key9").Bytes([]byte{1, 2, 3, 4}),
key.New("key10").Int(123), key.New("key10").Int(123),
key.New("key11").Uint(123), key.New("key11").Uint(123),
key.New("key21").Bool(false), key.New("key21").Bool(false),
@ -136,7 +134,6 @@ func BenchmarkSpanWithAttributes_all_2x(b *testing.B) {
key.New("key26").Uint32(123), key.New("key26").Uint32(123),
key.New("key27").Float64(123.456), key.New("key27").Float64(123.456),
key.New("key28").Float32(123.456), key.New("key28").Float32(123.456),
key.New("key29").Bytes([]byte{1, 2, 3, 4}),
key.New("key210").Int(123), key.New("key210").Int(123),
key.New("key211").Uint(123), key.New("key211").Uint(123),
) )