Skip to content

Commit

Permalink
Remove unused code and unused arguments from translator package (#2968)
Browse files Browse the repository at this point in the history
Signed-off-by: Bogdan Drutu <[email protected]>
  • Loading branch information
bogdandrutu authored Apr 20, 2021
1 parent 65420f4 commit 4431693
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 182 deletions.
110 changes: 4 additions & 106 deletions translator/trace/protospan_translation.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package tracetranslator
import (
"encoding/json"
"fmt"
"math"
"regexp"
"strconv"

Expand Down Expand Up @@ -75,7 +74,6 @@ type attrValDescript struct {
}

var attrValDescriptions = getAttrValDescripts()
var complexAttrValDescriptions = getComplexAttrValDescripts()

func getAttrValDescripts() []*attrValDescript {
descriptions := make([]*attrValDescript, 0, 5)
Expand All @@ -88,11 +86,6 @@ func getAttrValDescripts() []*attrValDescript {
return descriptions
}

func getComplexAttrValDescripts() []*attrValDescript {
descriptions := getAttrValDescripts()
return descriptions[4:]
}

func constructAttrValDescript(regex string, attrType pdata.AttributeValueType) *attrValDescript {
regexc := regexp.MustCompile(regex)
return &attrValDescript{
Expand Down Expand Up @@ -185,111 +178,16 @@ func AttributeArrayToSlice(attrArray pdata.AnyValueArray) []interface{} {
return rawSlice
}

// UpsertStringToAttributeMap upserts a string value to the specified key as it's native OTLP type
func UpsertStringToAttributeMap(key string, val string, dest pdata.AttributeMap, omitSimpleTypes bool) {
switch DetermineValueType(val, omitSimpleTypes) {
case pdata.AttributeValueINT:
iVal, _ := strconv.ParseInt(val, 10, 64)
dest.UpsertInt(key, iVal)
case pdata.AttributeValueDOUBLE:
fVal, _ := strconv.ParseFloat(val, 64)
dest.UpsertDouble(key, fVal)
case pdata.AttributeValueBOOL:
bVal, _ := strconv.ParseBool(val)
dest.UpsertBool(key, bVal)
case pdata.AttributeValueMAP:
var attrs map[string]interface{}
err := json.Unmarshal([]byte(val), &attrs)
if err == nil {
attrMap := pdata.NewAttributeValueMap()
jsonMapToAttributeMap(attrs, attrMap.MapVal())
dest.Upsert(key, attrMap)
} else {
dest.UpsertString(key, "")
}
case pdata.AttributeValueARRAY:
var jArray []interface{}
err := json.Unmarshal([]byte(val), &jArray)
if err == nil {
attrArr := pdata.NewAttributeValueArray()
jsonArrayToAttributeArray(jArray, attrArr.ArrayVal())
dest.Upsert(key, attrArr)
} else {
dest.UpsertString(key, "")
}
default:
dest.UpsertString(key, val)
}
}

// DetermineValueType returns the native OTLP attribute type the string translates to.
func DetermineValueType(value string, omitSimpleTypes bool) pdata.AttributeValueType {
if omitSimpleTypes {
for _, desc := range complexAttrValDescriptions {
if desc.regex.MatchString(value) {
return desc.attrType
}
}
} else {
for _, desc := range attrValDescriptions {
if desc.regex.MatchString(value) {
return desc.attrType
}
func DetermineValueType(value string) pdata.AttributeValueType {
for _, desc := range attrValDescriptions {
if desc.regex.MatchString(value) {
return desc.attrType
}
}
return pdata.AttributeValueSTRING
}

func jsonMapToAttributeMap(attrs map[string]interface{}, dest pdata.AttributeMap) {
for key, val := range attrs {
if val == nil {
dest.Upsert(key, pdata.NewAttributeValueNull())
continue
}
if s, ok := val.(string); ok {
dest.UpsertString(key, s)
} else if d, ok := val.(float64); ok {
if math.Mod(d, 1.0) == 0.0 {
dest.UpsertInt(key, int64(d))
} else {
dest.UpsertDouble(key, d)
}
} else if b, ok := val.(bool); ok {
dest.UpsertBool(key, b)
} else if m, ok := val.(map[string]interface{}); ok {
value := pdata.NewAttributeValueMap()
jsonMapToAttributeMap(m, value.MapVal())
dest.Upsert(key, value)
} else if a, ok := val.([]interface{}); ok {
value := pdata.NewAttributeValueArray()
jsonArrayToAttributeArray(a, value.ArrayVal())
dest.Upsert(key, value)
}
}
}

func jsonArrayToAttributeArray(jArray []interface{}, dest pdata.AnyValueArray) {
for _, val := range jArray {
if val == nil {
dest.Append(pdata.NewAttributeValueNull())
continue
}
if s, ok := val.(string); ok {
dest.Append(pdata.NewAttributeValueString(s))
} else if d, ok := val.(float64); ok {
if math.Mod(d, 1.0) == 0.0 {
dest.Append(pdata.NewAttributeValueInt(int64(d)))
} else {
dest.Append(pdata.NewAttributeValueDouble(d))
}
} else if b, ok := val.(bool); ok {
dest.Append(pdata.NewAttributeValueBool(b))
} else {
dest.Append(pdata.NewAttributeValueString("<Invalid array value>"))
}
}
}

// StatusCodeFromHTTP takes an HTTP status code and return the appropriate OpenTelemetry status code
// See: https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/http.md#status
func StatusCodeFromHTTP(httpStatusCode int) pdata.StatusCode {
Expand Down
102 changes: 28 additions & 74 deletions translator/trace/protospan_translation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,118 +60,72 @@ func TestAttributeValueToString(t *testing.T) {
expected: "true",
},
{
name: "null",
input: pdata.NewAttributeValueNull(),
name: "empty_map",
input: pdata.NewAttributeValueMap(),
jsonLike: false,
expected: "",
},
{
name: "null",
input: pdata.NewAttributeValueNull(),
jsonLike: true,
expected: "null",
expected: "{}",
},
{
name: "map",
input: pdata.NewAttributeValueMap(),
name: "simple_map",
input: simpleAttributeValueMap(),
jsonLike: false,
expected: "{}",
expected: "{\"arrKey\":[\"strOne\",\"strTwo\"],\"boolKey\":false,\"floatKey\":18.6,\"intKey\":7,\"mapKey\":{\"keyOne\":\"valOne\",\"keyTwo\":\"valTwo\"},\"nullKey\":null,\"strKey\":\"strVal\"}",
},
{
name: "array",
name: "empty_array",
input: pdata.NewAttributeValueArray(),
jsonLike: false,
expected: "[]",
},
{
name: "array",
name: "simple_array",
input: simpleAttributeValueArray(),
jsonLike: false,
expected: "[\"strVal\",7,18.6,false,null]",
},
{
name: "null",
input: pdata.NewAttributeValueNull(),
jsonLike: false,
expected: "",
},
{
name: "null_json",
input: pdata.NewAttributeValueNull(),
jsonLike: true,
expected: "null",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
actual := AttributeValueToString(test.input, test.jsonLike)
assert.Equal(t, test.expected, actual)
dest := pdata.NewAttributeMap()
key := "keyOne"
UpsertStringToAttributeMap(key, actual, dest, false)
val, ok := dest.Get(key)
assert.True(t, ok)
if !test.jsonLike {
switch test.input.Type() {
case pdata.AttributeValueINT, pdata.AttributeValueDOUBLE, pdata.AttributeValueBOOL:
assert.EqualValues(t, test.input, val)
case pdata.AttributeValueARRAY:
assert.NotNil(t, val)
default:
assert.Equal(t, test.expected, val.StringVal())
}
}
})
}
}

func TestAttributeMapToStringAndBack(t *testing.T) {
expected := pdata.NewAttributeValueMap()
attrMap := expected.MapVal()
func simpleAttributeValueMap() pdata.AttributeValue {
ret := pdata.NewAttributeValueMap()
attrMap := ret.MapVal()
attrMap.UpsertString("strKey", "strVal")
attrMap.UpsertInt("intKey", 7)
attrMap.UpsertDouble("floatKey", 18.6)
attrMap.UpsertBool("boolKey", false)
attrMap.Upsert("nullKey", pdata.NewAttributeValueNull())
attrMap.Upsert("mapKey", constructTestAttributeSubmap())
attrMap.Upsert("arrKey", constructTestAttributeSubarray())
strVal := AttributeValueToString(expected, false)
dest := pdata.NewAttributeMap()
UpsertStringToAttributeMap("parent", strVal, dest, false)
actual, ok := dest.Get("parent")
assert.True(t, ok)
compareMaps(t, attrMap, actual.MapVal())
return ret
}

func TestAttributeArrayToStringAndBack(t *testing.T) {
expected := pdata.NewAttributeValueArray()
attrArr := expected.ArrayVal()
func simpleAttributeValueArray() pdata.AttributeValue {
ret := pdata.NewAttributeValueArray()
attrArr := ret.ArrayVal()
attrArr.Append(pdata.NewAttributeValueString("strVal"))
attrArr.Append(pdata.NewAttributeValueInt(7))
attrArr.Append(pdata.NewAttributeValueDouble(18.6))
attrArr.Append(pdata.NewAttributeValueBool(false))
attrArr.Append(pdata.NewAttributeValueNull())
strVal := AttributeValueToString(expected, false)
dest := pdata.NewAttributeMap()
UpsertStringToAttributeMap("parent", strVal, dest, false)
actual, ok := dest.Get("parent")
assert.True(t, ok)
compareArrays(t, attrArr, actual.ArrayVal())
}

func compareMaps(t *testing.T, expected pdata.AttributeMap, actual pdata.AttributeMap) {
expected.Range(func(k string, e pdata.AttributeValue) bool {
a, ok := actual.Get(k)
assert.True(t, ok)
if ok {
if e.Type() == pdata.AttributeValueMAP {
compareMaps(t, e.MapVal(), a.MapVal())
} else {
assert.Equal(t, e, a)
}
}
return true
})
}

func compareArrays(t *testing.T, expected pdata.AnyValueArray, actual pdata.AnyValueArray) {
for i := 0; i < expected.Len(); i++ {
e := expected.At(i)
a := actual.At(i)
if e.Type() == pdata.AttributeValueMAP {
compareMaps(t, e.MapVal(), a.MapVal())
} else {
assert.Equal(t, e, a)
}
}
return ret
}

func constructTestAttributeSubmap() pdata.AttributeValue {
Expand Down
2 changes: 1 addition & 1 deletion translator/trace/zipkin/zipkinv1_to_protospan.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ func parseAnnotationValue(value string, parseStringTags bool) *tracepb.Attribute
pbAttrib := &tracepb.AttributeValue{}

if parseStringTags {
switch tracetranslator.DetermineValueType(value, false) {
switch tracetranslator.DetermineValueType(value) {
case pdata.AttributeValueINT:
iValue, _ := strconv.ParseInt(value, 10, 64)
pbAttrib.Value = &tracepb.AttributeValue_IntValue{IntValue: iValue}
Expand Down
2 changes: 1 addition & 1 deletion translator/trace/zipkin/zipkinv2_to_traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ func tagsToAttributeMap(tags map[string]string, dest pdata.AttributeMap, parseSt
}

if parseStringTags {
switch tracetranslator.DetermineValueType(val, false) {
switch tracetranslator.DetermineValueType(val) {
case pdata.AttributeValueINT:
iValue, _ := strconv.ParseInt(val, 10, 64)
dest.UpsertInt(key, iValue)
Expand Down

0 comments on commit 4431693

Please sign in to comment.