Skip to content

Commit

Permalink
[chore] [exporter/awscloudwatchlogs] remove unnecessary duplicate cod…
Browse files Browse the repository at this point in the history
…e with AsRaw (#17889)

Signed-off-by: Bogdan Drutu <[email protected]>
  • Loading branch information
bogdandrutu authored Jan 24, 2023
1 parent 7fe0dc4 commit 88cada9
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 107 deletions.
35 changes: 2 additions & 33 deletions exporter/awscloudwatchlogsexporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func logToCWLog(resourceAttrs map[string]interface{}, log plog.LogRecord) (*clou
// TODO(jbd): Benchmark and improve the allocations.
// Evaluate go.elastic.co/fastjson as a replacement for encoding/json.
body := cwLogBody{
Body: attrValue(log.Body()),
Body: log.Body().AsRaw(),
SeverityNumber: int32(log.SeverityNumber()),
SeverityText: log.SeverityText(),
DroppedAttributesCount: log.DroppedAttributesCount(),
Expand Down Expand Up @@ -218,39 +218,8 @@ func attrsValue(attrs pcommon.Map) map[string]interface{} {
}
out := make(map[string]interface{}, attrs.Len())
attrs.Range(func(k string, v pcommon.Value) bool {
out[k] = attrValue(v)
out[k] = v.AsRaw()
return true
})
return out
}

func attrValue(value pcommon.Value) interface{} {
switch value.Type() {
case pcommon.ValueTypeInt:
return value.Int()
case pcommon.ValueTypeBool:
return value.Bool()
case pcommon.ValueTypeDouble:
return value.Double()
case pcommon.ValueTypeStr:
return value.Str()
case pcommon.ValueTypeMap:
values := map[string]interface{}{}
value.Map().Range(func(k string, v pcommon.Value) bool {
values[k] = attrValue(v)
return true
})
return values
case pcommon.ValueTypeSlice:
arrayVal := value.Slice()
values := make([]interface{}, arrayVal.Len())
for i := 0; i < arrayVal.Len(); i++ {
values[i] = attrValue(arrayVal.At(i))
}
return values
case pcommon.ValueTypeEmpty:
return nil
default:
return nil
}
}
74 changes: 0 additions & 74 deletions exporter/awscloudwatchlogsexporter/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,80 +151,6 @@ func testLogRecordWithoutTrace() plog.LogRecord {
return record
}

func TestAttrValue(t *testing.T) {
tests := []struct {
name string
value pcommon.Value
want interface{}
}{
{
name: "null",
value: pcommon.NewValueEmpty(),
want: nil,
},
{
name: "bool",
value: pcommon.NewValueBool(true),
want: true,
},
{
name: "int",
value: pcommon.NewValueInt(5),
want: int64(5),
},
{
name: "double",
value: pcommon.NewValueDouble(6.7),
want: float64(6.7),
},
{
name: "map",
value: func() pcommon.Value {
mAttr := pcommon.NewValueMap()
m := mAttr.Map()
m.PutStr("key1", "value1")
m.PutEmpty("key2")
m.PutBool("key3", true)
m.PutInt("key4", 4)
m.PutDouble("key5", 5.6)
return mAttr
}(),
want: map[string]interface{}{
"key1": "value1",
"key2": nil,
"key3": true,
"key4": int64(4),
"key5": float64(5.6),
},
},
{
name: "array",
value: func() pcommon.Value {
arrAttr := pcommon.NewValueSlice()
arr := arrAttr.Slice()
for _, av := range []pcommon.Value{
pcommon.NewValueDouble(1.2),
pcommon.NewValueDouble(1.6),
pcommon.NewValueBool(true),
pcommon.NewValueStr("hello"),
pcommon.NewValueEmpty(),
} {
tgt := arr.AppendEmpty()
av.CopyTo(tgt)
}
return arrAttr
}(),
want: []interface{}{1.2, 1.6, true, "hello", nil},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := attrValue(tt.value)
assert.Equal(t, tt.want, got)
})
}
}

func TestConsumeLogs(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand Down

0 comments on commit 88cada9

Please sign in to comment.