Skip to content

Commit cc5a0fb

Browse files
authored
fix: sort the incoming keys from the log output (#116)
Signed-off-by: Gergely Brautigam <182850+Skarlso@users.noreply.github.com> On-behalf-of: @SAP gergely.brautigam@sap.com
1 parent 8d95892 commit cc5a0fb

2 files changed

Lines changed: 25 additions & 2 deletions

File tree

bindings/go/plugin/manager/registries/plugins/log_streamer.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"encoding/json"
77
"fmt"
88
"log/slog"
9+
"sort"
910

1011
"ocm.software/open-component-model/bindings/go/plugin/manager/types"
1112
)
@@ -118,8 +119,17 @@ func parseLine(line string) (record, error) {
118119
delete(parsed, "msg")
119120
delete(parsed, "level")
120121

121-
for k, v := range parsed {
122-
result.args = append(result.args, k, v)
122+
var keys []string
123+
124+
for k := range parsed {
125+
keys = append(keys, k)
126+
}
127+
128+
// make the output determinable
129+
sort.Strings(keys)
130+
131+
for _, k := range keys {
132+
result.args = append(result.args, k, parsed[k])
123133
}
124134

125135
return result, nil

bindings/go/plugin/manager/registries/plugins/log_streamer_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,19 @@ func TestParseLine(t *testing.T) {
2828
},
2929
wantErr: false,
3030
},
31+
{
32+
name: "make sure args are sorted",
33+
// notice that order is key2, key1 but the output should be key1, key2
34+
// this is unlikely as slog also orders, but the result is parsed into a map
35+
// so that would fail from time to time on the other end
36+
input: `{"level":"info","msg":"test message","key2":42,"key1":"value1"}`,
37+
want: record{
38+
level: "info",
39+
msg: "test message",
40+
args: []any{"key1", "value1", "key2", float64(42)},
41+
},
42+
wantErr: false,
43+
},
3144
{
3245
name: "invalid JSON",
3346
input: `{"level":"info","msg":"test message"`,

0 commit comments

Comments
 (0)