Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[chore] remove unnecessary duplicate code with AsRaw #17889

Merged
merged 1 commit into from
Jan 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[chore] remove unnecessary duplicate code with AsRaw
Signed-off-by: Bogdan Drutu <[email protected]>
  • Loading branch information
bogdandrutu committed Jan 21, 2023
commit 6eb6fe290ff940aa7b8360dc35f35a96fc9b8c3f
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