From 88cada9fabf1612bf13c6438631d900daf1a9827 Mon Sep 17 00:00:00 2001 From: Bogdan Drutu Date: Mon, 23 Jan 2023 22:54:06 -0800 Subject: [PATCH] [chore] [exporter/awscloudwatchlogs] remove unnecessary duplicate code with AsRaw (#17889) Signed-off-by: Bogdan Drutu --- .../awscloudwatchlogsexporter/exporter.go | 35 +-------- .../exporter_test.go | 74 ------------------- 2 files changed, 2 insertions(+), 107 deletions(-) diff --git a/exporter/awscloudwatchlogsexporter/exporter.go b/exporter/awscloudwatchlogsexporter/exporter.go index b569b6108c52..83cf41f948e7 100644 --- a/exporter/awscloudwatchlogsexporter/exporter.go +++ b/exporter/awscloudwatchlogsexporter/exporter.go @@ -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(), @@ -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 - } -} diff --git a/exporter/awscloudwatchlogsexporter/exporter_test.go b/exporter/awscloudwatchlogsexporter/exporter_test.go index 4f0607b26d68..a2ee98a0f900 100644 --- a/exporter/awscloudwatchlogsexporter/exporter_test.go +++ b/exporter/awscloudwatchlogsexporter/exporter_test.go @@ -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()