Skip to content

Commit

Permalink
Convert passed values into remaining types
Browse files Browse the repository at this point in the history
Signed-off-by: Daniil Rutskiy <[email protected]>
  • Loading branch information
dstdfx committed Nov 14, 2020
1 parent ffe85bd commit e4eba8b
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
25 changes: 25 additions & 0 deletions bridge/opentracing/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,14 @@ func (s *bridgeSpan) SetOperationName(operationName string) ot.Span {
return s
}

// SetTag method adds a tag to the span.
//
// Note about the following value conversions:
// - uint -> int
// - int32 -> int64
// - uint32 -> int64
// - uint64 -> int64
// - float32 -> float64
func (s *bridgeSpan) SetTag(key string, value interface{}) ot.Span {
switch key {
case string(otext.SpanKind):
Expand Down Expand Up @@ -495,17 +503,34 @@ 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:
// - uint -> int
// - int32 -> int64
// - uint32 -> int64
// - uint64 -> int64
// - float32 -> float64
func otTagToOTelLabel(k string, v interface{}) label.KeyValue {
key := otTagToOTelLabelKey(k)
switch val := v.(type) {
case bool:
return key.Bool(val)
case int64:
return key.Int64(val)
case uint64:
return key.Int64(int64(val))
case float64:
return key.Float64(val)
case int32:
return key.Int64(int64(val))
case uint32:
return key.Int64(int64(val))
case float32:
return key.Float64(float64(val))
case int:
return key.Int(val)
case uint:
return key.Int(int(val))
case string:
return key.String(val)
default:
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 int64",
value: uint(1234),
expectedValueType: label.INT64,
},
{
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 int64",
value: uint64(12345678),
expectedValueType: label.INT64,
},
{
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)
}
}
}

0 comments on commit e4eba8b

Please sign in to comment.