Skip to content

Commit

Permalink
[chore] [exporter/logzio] remove unnecessary duplicate code with AsRaw (
Browse files Browse the repository at this point in the history
#17890)

Signed-off-by: Bogdan Drutu <bogdandrutu@gmail.com>
  • Loading branch information
bogdandrutu authored Jan 21, 2023
1 parent 2062108 commit 5967764
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 42 deletions.
2 changes: 1 addition & 1 deletion exporter/logzioexporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func (exporter *logzioExporter) pushLogData(ctx context.Context, ld plog.Logs) e
logRecords := scopeLogs.At(j).LogRecords()
for k := 0; k < logRecords.Len(); k++ {
log := logRecords.At(k)
jsonLog := convertLogRecordToJSON(log, resource, exporter.logger)
jsonLog := convertLogRecordToJSON(log, resource)
logzioLog, err := json.Marshal(jsonLog)
if err != nil {
return err
Expand Down
42 changes: 4 additions & 38 deletions exporter/logzioexporter/jsonlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,46 +17,12 @@ package logzioexporter // import "github.com/open-telemetry/opentelemetry-collec
import (
"encoding/hex"

"github.com/hashicorp/go-hclog"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/plog"
"go.uber.org/zap"
)

func convertAttributeValue(value pcommon.Value, logger hclog.Logger) 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] = convertAttributeValue(v, logger)
return true
})
return values
case pcommon.ValueTypeSlice:
arrayVal := value.Slice()
values := make([]interface{}, arrayVal.Len())
for i := 0; i < arrayVal.Len(); i++ {
values[i] = convertAttributeValue(arrayVal.At(i), logger)
}
return values
case pcommon.ValueTypeEmpty:
return nil
default:
logger.Debug("Unhandled value type", zap.String("type", value.Type().String()))
return value
}
}

// convertLogRecordToJSON Takes `plog.LogRecord` and `pcommon.Resource` input, outputs byte array that represents the log record as json string
func convertLogRecordToJSON(log plog.LogRecord, resource pcommon.Resource, logger hclog.Logger) map[string]interface{} {
func convertLogRecordToJSON(log plog.LogRecord, resource pcommon.Resource) map[string]interface{} {
jsonLog := map[string]interface{}{}
if spanID := log.SpanID(); !spanID.IsEmpty() {
jsonLog["spanID"] = hex.EncodeToString(spanID[:])
Expand All @@ -73,20 +39,20 @@ func convertLogRecordToJSON(log plog.LogRecord, resource pcommon.Resource, logge
}
// add resource attributes to each json log
resource.Attributes().Range(func(k string, v pcommon.Value) bool {
jsonLog[k] = convertAttributeValue(v, logger)
jsonLog[k] = v.AsRaw()
return true
})
// add log record attributes to each json log
log.Attributes().Range(func(k string, v pcommon.Value) bool {
jsonLog[k] = convertAttributeValue(v, logger)
jsonLog[k] = v.AsRaw()
return true
})

switch log.Body().Type() {
case pcommon.ValueTypeStr:
jsonLog["message"] = log.Body().Str()
case pcommon.ValueTypeMap:
bodyFieldsMap := convertAttributeValue(log.Body(), logger).(map[string]interface{})
bodyFieldsMap := log.Body().Map().AsRaw()
for key, value := range bodyFieldsMap {
jsonLog[key] = value
}
Expand Down
4 changes: 1 addition & 3 deletions exporter/logzioexporter/jsonlog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"strings"
"testing"

"github.com/hashicorp/go-hclog"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/config/configcompression"
Expand All @@ -46,7 +45,6 @@ func GenerateLogRecordWithMultiTypeValues() plog.LogRecord {
}

func TestConvertLogRecordToJSON(t *testing.T) {
logger := hclog.NewNullLogger()
type convertLogRecordToJSONTest struct {
log plog.LogRecord
resource pcommon.Resource
Expand Down Expand Up @@ -83,7 +81,7 @@ func TestConvertLogRecordToJSON(t *testing.T) {
},
}
for _, test := range convertLogRecordToJSONTests {
output := convertLogRecordToJSON(test.log, test.resource, logger)
output := convertLogRecordToJSON(test.log, test.resource)
require.Equal(t, output, test.expected)
}

Expand Down

0 comments on commit 5967764

Please sign in to comment.