Skip to content

Commit

Permalink
Remove extra labels types (#1314)
Browse files Browse the repository at this point in the history
* Remove extra labels types

Remove the following labels types: INT32, UINT32, UINT64
and FLOAT32.

Fix all extra labels types occurrences in the project.

Signed-off-by: Daniil Rutskiy <dstdfx@gmail.com>

* Update CHANGELOG.md

Signed-off-by: Daniil Rutskiy <dstdfx@gmail.com>

* Delete unused helpers

Signed-off-by: Daniil Rutskiy <dstdfx@gmail.com>

* Convert passed values into remaining types

Signed-off-by: Daniil Rutskiy <dstdfx@gmail.com>

* Clarify func description

* Fix uint64 convertion

Signed-off-by: Daniil Rutskiy <dstdfx@gmail.com>

* Fix uint conversion

Signed-off-by: Daniil Rutskiy <dstdfx@gmail.com>

* Update OTLP exporter label types

Co-authored-by: Tyler Yahn <codingalias@gmail.com>
  • Loading branch information
dstdfx and MrAlias authored Feb 17, 2021
1 parent 73194e4 commit 7de3b58
Show file tree
Hide file tree
Showing 25 changed files with 157 additions and 764 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- The `MockSpan` and `MockTracer` types are removed from `go.opentelemetry.io/otel/oteltest`.
`Tracer` and `Span` from the same module should be used in their place instead. (#1306)
- `WorkerCount` option is removed from `go.opentelemetry.io/otel/exporters/otlp`. (#1350)
- Remove the following labels types: INT32, UINT32, UINT64 and FLOAT32. (#1314)

### Fixed

Expand Down
27 changes: 22 additions & 5 deletions bridge/opentracing/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,15 @@ func (s *bridgeSpan) SetOperationName(operationName string) ot.Span {
return s
}

// SetTag method adds a tag to the span.
//
// Note about the following value conversions:
// - int -> int64
// - uint -> string
// - int32 -> int64
// - uint32 -> int64
// - uint64 -> string
// - float32 -> float64
func (s *bridgeSpan) SetTag(key string, value interface{}) ot.Span {
switch key {
case string(otext.SpanKind):
Expand Down Expand Up @@ -495,6 +504,14 @@ func otTagsToOTelAttributesKindAndError(tags map[string]interface{}) ([]label.Ke
return pairs, kind, err
}

// otTagToOTelLabel converts given key-value into label.KeyValue.
// Note that some conversions are not obvious:
// - int -> int64
// - uint -> string
// - int32 -> int64
// - uint32 -> int64
// - uint64 -> string
// - float32 -> float64
func otTagToOTelLabel(k string, v interface{}) label.KeyValue {
key := otTagToOTelLabelKey(k)
switch val := v.(type) {
Expand All @@ -503,19 +520,19 @@ func otTagToOTelLabel(k string, v interface{}) label.KeyValue {
case int64:
return key.Int64(val)
case uint64:
return key.Uint64(val)
return key.String(fmt.Sprintf("%d", val))
case float64:
return key.Float64(val)
case int32:
return key.Int32(val)
return key.Int64(int64(val))
case uint32:
return key.Uint32(val)
return key.Int64(int64(val))
case float32:
return key.Float32(val)
return key.Float64(float64(val))
case int:
return key.Int(val)
case uint:
return key.Uint(val)
return key.String(fmt.Sprintf("%d", val))
case string:
return key.String(val)
default:
Expand Down
2 changes: 1 addition & 1 deletion bridge/opentracing/internal/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ func (s *MockSpan) IsRecording() bool {
}

func (s *MockSpan) SetStatus(code codes.Code, msg string) {
s.SetAttributes(StatusCodeKey.Uint32(uint32(code)), StatusMessageKey.String(msg))
s.SetAttributes(StatusCodeKey.Int(int(code)), StatusMessageKey.String(msg))
}

func (s *MockSpan) SetName(name string) {
Expand Down
74 changes: 74 additions & 0 deletions bridge/opentracing/mix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -716,3 +716,77 @@ func runOTOtelOT(t *testing.T, ctx context.Context, name string, callback func(*
}(ctx2)
}(ctx)
}

func TestOtTagToOTelLabel_CheckTypeConversions(t *testing.T) {
tableTest := []struct {
key string
value interface{}
expectedValueType label.Type
}{
{
key: "bool to bool",
value: true,
expectedValueType: label.BOOL,
},
{
key: "int to int64",
value: 123,
expectedValueType: label.INT64,
},
{
key: "uint to string",
value: uint(1234),
expectedValueType: label.STRING,
},
{
key: "int32 to int64",
value: int32(12345),
expectedValueType: label.INT64,
},
{
key: "uint32 to int64",
value: uint32(123456),
expectedValueType: label.INT64,
},
{
key: "int64 to int64",
value: int64(1234567),
expectedValueType: label.INT64,
},
{
key: "uint64 to string",
value: uint64(12345678),
expectedValueType: label.STRING,
},
{
key: "float32 to float64",
value: float32(3.14),
expectedValueType: label.FLOAT64,
},
{
key: "float64 to float64",
value: float64(3.14),
expectedValueType: label.FLOAT64,
},
{
key: "string to string",
value: "string_value",
expectedValueType: label.STRING,
},
{
key: "unexpected type to string",
value: struct{}{},
expectedValueType: label.STRING,
},
}

for _, test := range tableTest {
got := otTagToOTelLabel(test.key, test.value)
if test.expectedValueType != got.Value.Type() {
t.Errorf("Expected type %s, but got %s after conversion '%v' value",
test.expectedValueType,
got.Value.Type(),
test.value)
}
}
}
2 changes: 1 addition & 1 deletion exporters/otlp/internal/otlptest/otlptest.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func RunEndToEndTest(ctx context.Context, t *testing.T, exp *otlp.Exporter, mcTr
tp2 := sdktrace.NewTracerProvider(append(pOpts,
sdktrace.WithResource(resource.NewWithAttributes(
label.String("rk1", "rv12)"),
label.Float32("rk3", 6.5),
label.Float64("rk3", 6.5),
)))...)

tr1 := tp1.Tracer("test-tracer1")
Expand Down
12 changes: 4 additions & 8 deletions exporters/otlp/internal/transform/attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,10 @@ func toAttribute(v label.KeyValue) *commonpb.KeyValue {
result.Value.Value = &commonpb.AnyValue_BoolValue{
BoolValue: v.Value.AsBool(),
}
case label.INT64, label.INT32, label.UINT32, label.UINT64:
case label.INT64:
result.Value.Value = &commonpb.AnyValue_IntValue{
IntValue: v.Value.AsInt64(),
}
case label.FLOAT32:
result.Value.Value = &commonpb.AnyValue_DoubleValue{
DoubleValue: float64(v.Value.AsFloat32()),
}
case label.FLOAT64:
result.Value.Value = &commonpb.AnyValue_DoubleValue{
DoubleValue: v.Value.AsFloat64(),
Expand Down Expand Up @@ -103,23 +99,23 @@ func arrayValues(kv label.KeyValue) []*commonpb.AnyValue {
},
}
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
case reflect.Int, reflect.Int64:
valueFunc = func(v reflect.Value) *commonpb.AnyValue {
return &commonpb.AnyValue{
Value: &commonpb.AnyValue_IntValue{
IntValue: v.Int(),
},
}
}
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
case reflect.Uintptr:
valueFunc = func(v reflect.Value) *commonpb.AnyValue {
return &commonpb.AnyValue{
Value: &commonpb.AnyValue_IntValue{
IntValue: int64(v.Uint()),
},
}
}
case reflect.Float32, reflect.Float64:
case reflect.Float64:
valueFunc = func(v reflect.Value) *commonpb.AnyValue {
return &commonpb.AnyValue{
Value: &commonpb.AnyValue_DoubleValue{
Expand Down
62 changes: 3 additions & 59 deletions exporters/otlp/internal/transform/attribute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,8 @@ func TestAttributes(t *testing.T) {
{
[]label.KeyValue{
label.Int("int to int", 123),
label.Uint("uint to int", 1234),
label.Int32("int32 to int", 12345),
label.Uint32("uint32 to int", 123456),
label.Int64("int64 to int64", 1234567),
label.Uint64("uint64 to int64", 12345678),
label.Float32("float32 to double", 3.14),
label.Float32("float64 to double", 1.61),
label.Float64("float64 to double", 1.61),
label.String("string to string", "string"),
label.Bool("bool to bool", true),
},
Expand All @@ -53,30 +48,6 @@ func TestAttributes(t *testing.T) {
},
},
},
{
Key: "uint to int",
Value: &commonpb.AnyValue{
Value: &commonpb.AnyValue_IntValue{
IntValue: 1234,
},
},
},
{
Key: "int32 to int",
Value: &commonpb.AnyValue{
Value: &commonpb.AnyValue_IntValue{
IntValue: 12345,
},
},
},
{
Key: "uint32 to int",
Value: &commonpb.AnyValue{
Value: &commonpb.AnyValue_IntValue{
IntValue: 123456,
},
},
},
{
Key: "int64 to int64",
Value: &commonpb.AnyValue{
Expand All @@ -85,22 +56,6 @@ func TestAttributes(t *testing.T) {
},
},
},
{
Key: "uint64 to int64",
Value: &commonpb.AnyValue{
Value: &commonpb.AnyValue_IntValue{
IntValue: 12345678,
},
},
},
{
Key: "float32 to double",
Value: &commonpb.AnyValue{
Value: &commonpb.AnyValue_DoubleValue{
DoubleValue: 3.14,
},
},
},
{
Key: "float64 to double",
Value: &commonpb.AnyValue{
Expand Down Expand Up @@ -151,9 +106,8 @@ func TestAttributes(t *testing.T) {

func TestArrayAttributes(t *testing.T) {
// Array KeyValue supports only arrays of primitive types:
// "bool", "int", "int32", "int64",
// "float32", "float64", "string",
// "uint", "uint32", "uint64"
// "bool", "int", "int64",
// "float64", "string",
for _, test := range []attributeTest{
{nil, nil},
{
Expand All @@ -175,24 +129,14 @@ func TestArrayAttributes(t *testing.T) {
[]label.KeyValue{
label.Array("bool array to bool array", []bool{true, false}),
label.Array("int array to int64 array", []int{1, 2, 3}),
label.Array("uint array to int64 array", []uint{1, 2, 3}),
label.Array("int32 array to int64 array", []int32{1, 2, 3}),
label.Array("uint32 array to int64 array", []uint32{1, 2, 3}),
label.Array("int64 array to int64 array", []int64{1, 2, 3}),
label.Array("uint64 array to int64 array", []uint64{1, 2, 3}),
label.Array("float32 array to double array", []float32{1.11, 2.22, 3.33}),
label.Array("float64 array to double array", []float64{1.11, 2.22, 3.33}),
label.Array("string array to string array", []string{"foo", "bar", "baz"}),
},
[]*commonpb.KeyValue{
newOTelBoolArray("bool array to bool array", []bool{true, false}),
newOTelIntArray("int array to int64 array", []int64{1, 2, 3}),
newOTelIntArray("uint array to int64 array", []int64{1, 2, 3}),
newOTelIntArray("int32 array to int64 array", []int64{1, 2, 3}),
newOTelIntArray("uint32 array to int64 array", []int64{1, 2, 3}),
newOTelIntArray("int64 array to int64 array", []int64{1, 2, 3}),
newOTelIntArray("uint64 array to int64 array", []int64{1, 2, 3}),
newOTelDoubleArray("float32 array to double array", []float64{1.11, 2.22, 3.33}),
newOTelDoubleArray("float64 array to double array", []float64{1.11, 2.22, 3.33}),
newOTelStringArray("string array to string array", []string{"foo", "bar", "baz"}),
},
Expand Down
10 changes: 5 additions & 5 deletions exporters/otlp/internal/transform/metric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ func TestStringKeyValues(t *testing.T) {
[]label.KeyValue{
label.Bool("true", true),
label.Int64("one", 1),
label.Uint64("two", 2),
label.Int64("two", 2),
label.Float64("three", 3),
label.Int32("four", 4),
label.Uint32("five", 5),
label.Float32("six", 6),
label.Int("four", 4),
label.Int("five", 5),
label.Float64("six", 6),
label.Int("seven", 7),
label.Uint("eight", 8),
label.Int("eight", 8),
label.String("the", "final word"),
},
[]*commonpb.StringKeyValue{
Expand Down
2 changes: 1 addition & 1 deletion exporters/otlp/internal/transform/span_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ func TestSpanData(t *testing.T) {
MessageEvents: []trace.Event{
{Time: startTime,
Attributes: []label.KeyValue{
label.Uint64("CompressedByteSize", 512),
label.Int64("CompressedByteSize", 512),
},
},
{Time: endTime,
Expand Down
18 changes: 0 additions & 18 deletions exporters/otlp/otlpgrpc/otlp_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,9 +280,7 @@ func TestNewExporter_withMultipleAttributeTypes(t *testing.T) {
tr := tp.Tracer("test-tracer")
testKvs := []label.KeyValue{
label.Int("Int", 1),
label.Int32("Int32", int32(2)),
label.Int64("Int64", int64(3)),
label.Float32("Float32", float32(1.11)),
label.Float64("Float64", 2.22),
label.Bool("Bool", true),
label.String("String", "test"),
Expand Down Expand Up @@ -329,14 +327,6 @@ func TestNewExporter_withMultipleAttributeTypes(t *testing.T) {
},
},
},
{
Key: "Int32",
Value: &commonpb.AnyValue{
Value: &commonpb.AnyValue_IntValue{
IntValue: 2,
},
},
},
{
Key: "Int64",
Value: &commonpb.AnyValue{
Expand All @@ -345,14 +335,6 @@ func TestNewExporter_withMultipleAttributeTypes(t *testing.T) {
},
},
},
{
Key: "Float32",
Value: &commonpb.AnyValue{
Value: &commonpb.AnyValue_DoubleValue{
DoubleValue: 1.11,
},
},
},
{
Key: "Float64",
Value: &commonpb.AnyValue{
Expand Down
Loading

0 comments on commit 7de3b58

Please sign in to comment.