Skip to content

Commit

Permalink
sort by keys
Browse files Browse the repository at this point in the history
  • Loading branch information
kuiperda committed Sep 10, 2024
1 parent ae0136e commit 5b008ba
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 14 deletions.
34 changes: 23 additions & 11 deletions processor/marshalprocessor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,40 +77,52 @@ func (mp *marshalProcessor) processLogs(_ context.Context, ld plog.Logs) (plog.L
return ld, nil
}

// convertMapToKV recursively converts a map to a key-value string
// convertMapToKV recursively converts a map to a key value string
func (mp *marshalProcessor) convertMapToKV(logBody pcommon.Map, inNestedValue bool) string {
var kvStrings []string
var keyValues []struct {
key string
val pcommon.Value
}

// Sort by keys
logBody.Range(func(k string, v pcommon.Value) bool {
k = mp.escapeAndQuoteKV(k, inNestedValue)
keyValues = append(keyValues, struct {
key string
val pcommon.Value
}{key: k, val: v})
return true
})
sort.Slice(keyValues, func(i, j int) bool {
return keyValues[i].key < keyValues[j].key
})

// Convert KV pairs
for _, kv := range keyValues {
k := mp.escapeAndQuoteKV(kv.key, inNestedValue)

var vStr string
switch v.Type() {
switch kv.val.Type() {
case pcommon.ValueTypeMap:
vStr = mp.convertMapToKV(v.Map(), true)
vStr = mp.convertMapToKV(kv.val.Map(), true)
vStr = `[` + vStr + `]`
if !inNestedValue && (mp.kvSeparator == mp.mapKVSeparator || mp.kvPairSeparator == mp.mapKVPairSeparator) {
vStr = `"` + vStr + `"`
}
default:
vStr = mp.escapeAndQuoteKV(v.AsString(), inNestedValue)
vStr = mp.escapeAndQuoteKV(kv.val.AsString(), inNestedValue)
}

if !inNestedValue {
kvStrings = append(kvStrings, fmt.Sprintf("%s%s%v", k, mp.kvSeparator, vStr))
} else {
kvStrings = append(kvStrings, fmt.Sprintf("%s%s%v", k, mp.mapKVSeparator, vStr))
}

return true
})

sort.Strings(kvStrings)
}

if !inNestedValue {
return strings.Join(kvStrings, mp.kvPairSeparator)
}

return strings.Join(kvStrings, mp.mapKVPairSeparator)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"timeUnixNano": "1724942726840056000",
"severityNumber": 5,
"body": {
"stringValue": "\"nam,e\":\"test:\",\"nested-n1:\":1,bindplane-otel-attributes-baba:you,bindplane-otel-attributes-host:\"myh,ost\",nested-n2:2,severity:155"
"stringValue": "bindplane-otel-attributes-baba:you,bindplane-otel-attributes-host:\"myh,ost\",\"nam,e\":\"test:\",\"nested-n1:\":1,nested-n2:2,severity:155"
},
"attributes": [
{ "key": "baba", "value": { "stringValue": "you" } },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"timeUnixNano": "1724942726840056000",
"severityNumber": 5,
"body": {
"stringValue": "\"nam e\"=\"test=\" \"nested-n1=\"=1 bindplane-otel-attributes-baba=you bindplane-otel-attributes-host=\"myh \\\"ost\" nested-n2=2 s\\\"everity=155"
"stringValue": "bindplane-otel-attributes-baba=you bindplane-otel-attributes-host=\"myh \\\"ost\" \"nam e\"=\"test=\" \"nested-n1=\"=1 nested-n2=2 s\\\"everity=155"
},
"attributes": [
{ "key": "baba", "value": { "stringValue": "you" } },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"timeUnixNano": "1724942726840056000",
"severityNumber": 5,
"body": {
"stringValue": "\"nam e\"=\"test=\" \"nested-n1=\"=1 bindplane-otel-attributes-baba=you bindplane-otel-attributes-host=\"myh ost\" nested-n2=2 severity=155"
"stringValue": "bindplane-otel-attributes-baba=you bindplane-otel-attributes-host=\"myh ost\" \"nam e\"=\"test=\" \"nested-n1=\"=1 nested-n2=2 severity=155"
},
"attributes": [
{ "key": "baba", "value": { "stringValue": "you" } },
Expand Down

0 comments on commit 5b008ba

Please sign in to comment.