-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson.go
More file actions
240 lines (197 loc) · 6.28 KB
/
json.go
File metadata and controls
240 lines (197 loc) · 6.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package ecslog
import (
"encoding/json"
"fmt"
"log/slog"
"strconv"
"strings"
"time"
"github.com/oidq/ecslog/internal/sjson"
)
// preformattedKinds is lookup of which kinds should be pre-formatted when creating new Handler with attributes.
//
// It is fairly cheap to format for example Kind.Uint64 to given buffer for every log entry,
// but it makes sense to pre-format these kinds to newly allocated buffers.
var preformattedKinds = [16]bool{
slog.KindAny: true,
slog.KindGroup: true,
slog.KindLogValuer: true,
}
type preformattedValue struct {
// value contains preformatted value with necessary quotations
value []byte
}
func resolveRecord(output []byte, t0 time.Time, msg string, sortedAttrs []slog.Attr) []byte {
output = append(output, '{')
// @timestamp and message has special treatment to prevent unnecessary operations regarding
// slog.Time and slog.String
hasValue := false
if !t0.IsZero() {
output = appendKey(output, false, "@timestamp")
output = appendTime(output, t0)
hasValue = true
}
if msg != "" {
output = appendKey(output, hasValue, "message")
output = appendJsonString(output, msg)
hasValue = true
}
output = resolveGroupContent(output, hasValue, 0, sortedAttrs)
output = append(output, '}')
return output
}
func resolveGroup(output []byte, prefixLen int, attributes []slog.Attr) []byte {
output = append(output, '{')
output = resolveGroupContent(output, false, prefixLen, attributes)
output = append(output, '}')
return output
}
func resolveGroupPtr(output []byte, prefixLen int, attributes []slog.Attr) []byte {
output = append(output, '{')
output = resolveGroupContent(output, false, prefixLen, attributes)
output = append(output, '}')
return output
}
func resolveGroupContent(output []byte, hasValue bool, prefixLen int, attributes []slog.Attr) []byte {
// slice of fields in the current group,
// it is created by slicing the attributes parameter
var currentGroupFields []slog.Attr
currentGroup := ""
var key string
for i := range attributes {
attr := attributes[i]
key = attr.Key[prefixLen:]
groupSeparatorIndex := strings.IndexRune(key, groupSeparator)
if groupSeparatorIndex != -1 {
// continuing of an established group
if currentGroup == "" || currentGroup == key[:groupSeparatorIndex] {
currentGroup = key[:groupSeparatorIndex]
currentGroupFields = attributes[i-len(currentGroupFields) : i+1]
continue
}
// we encountered a new group -> flush the current one
output = appendKey(output, hasValue, currentGroup)
output = resolveGroupPtr(output, prefixLen+len(currentGroup)+1, currentGroupFields)
hasValue = true
// create new group
currentGroup = key[:groupSeparatorIndex]
currentGroupFields = attributes[i : i+1]
continue
}
// top level attribute with the same name as the group (we necessarily have a group open)
if currentGroup == key {
// "Group" exists, but top-level atomic attribute is present
// which overrides the value and we use it.
currentGroup = ""
currentGroupFields = currentGroupFields[:0]
output = appendJsonKV(output, hasValue, key, attr.Value)
hasValue = true
continue
}
// top level attribute with the same name as the previous one -> skip
if i < len(attributes)-1 && attributes[i+1].Key == attributes[i].Key {
continue
}
attr.Key = key
output = appendJsonKV(output, hasValue, key, attr.Value)
hasValue = true
}
// deal with any unfinished group
if currentGroup != "" {
output = appendKey(output, hasValue, currentGroup)
output = resolveGroupPtr(output, prefixLen+len(currentGroup)+1, currentGroupFields)
hasValue = true
}
return output
}
func appendKey(output []byte, hasValue bool, key string) []byte {
if hasValue {
output = append(output, ',')
}
output = appendJsonString(output, key)
output = append(output, ':')
return output
}
func shouldPreformat(kind slog.Kind) bool {
if int(kind) > len(preformattedKinds) {
return false
}
return preformattedKinds[kind]
}
func preformatValue(value slog.Value) slog.Value {
var formattedValue []byte
formatted := appendJsonValue(formattedValue, value)
return slog.AnyValue(preformattedValue{value: formatted})
}
func appendJsonKV(output []byte, hasValue bool, key string, value slog.Value) []byte {
output = appendKey(output, hasValue, key)
output = appendJsonValue(output, value)
return output
}
func appendJsonValue(output []byte, value slog.Value) []byte {
if value.Kind() == slog.KindLogValuer {
value = value.Resolve()
}
switch value.Kind() {
case slog.KindGroup:
output = resolveGroup(output, 0, value.Group())
case slog.KindBool:
if value.Bool() {
output = append(output, "true"...)
} else {
output = append(output, "false"...)
}
case slog.KindTime:
output = appendTime(output, value.Time())
case slog.KindUint64:
output = strconv.AppendUint(output, value.Uint64(), 10)
case slog.KindInt64:
output = strconv.AppendInt(output, value.Int64(), 10)
case slog.KindString:
output = appendJsonString(output, value.String())
case slog.KindDuration:
output = strconv.AppendInt(output, value.Duration().Nanoseconds(), 10)
case slog.KindFloat64:
// adhere to json.Marshal with floats
output = appendMarshal(output, value.Float64())
case slog.KindAny:
val := value.Any()
if pref, ok := val.(preformattedValue); ok {
return append(output, pref.value...)
}
output = appendMarshal(output, val)
default:
output = appendJsonString(output, fmt.Sprintf("ERR! invalid value: %#v", value.Any()))
}
return output
}
func appendTime(output []byte, t0 time.Time) []byte {
output = append(output, '"')
output = t0.AppendFormat(output, time.RFC3339Nano)
output = append(output, '"')
return output
}
func appendJsonString(output []byte, value string) []byte {
output = append(output, '"')
output = sjson.AppendStringContent(output, value)
output = append(output, '"')
return output
}
type simpleWriter struct {
buffer []byte
}
func (w *simpleWriter) Write(p []byte) (n int, err error) {
w.buffer = append(w.buffer, p...)
return len(p), nil
}
func appendMarshal(output []byte, v any) []byte {
writer := simpleWriter{buffer: output}
enc := json.NewEncoder(&writer)
enc.SetEscapeHTML(false)
err := enc.Encode(v)
if err != nil {
output = appendJsonString(output, "ERR!"+err.Error())
}
// remove newline
return writer.buffer[:len(writer.buffer)-1]
}