2024-10-01 12:08:17 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-10-07 12:18:18 +02:00
|
|
|
"bufio"
|
2024-10-01 12:08:17 +02:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2024-10-07 10:39:07 +02:00
|
|
|
"sort"
|
2024-10-01 12:08:17 +02:00
|
|
|
"sync"
|
2024-10-07 12:18:18 +02:00
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logstorage"
|
|
|
|
)
|
|
|
|
|
|
|
|
type outputMode int
|
|
|
|
|
|
|
|
const (
|
|
|
|
outputModeJSONMultiline = outputMode(0)
|
|
|
|
outputModeJSONSingleline = outputMode(1)
|
|
|
|
outputModeLogfmt = outputMode(2)
|
2024-10-01 12:08:17 +02:00
|
|
|
)
|
|
|
|
|
2024-10-07 12:18:18 +02:00
|
|
|
func getOutputFormatter(outputMode outputMode) func(w io.Writer, fields []logstorage.Field) error {
|
|
|
|
switch outputMode {
|
|
|
|
case outputModeJSONMultiline:
|
|
|
|
return func(w io.Writer, fields []logstorage.Field) error {
|
|
|
|
return writeJSONObject(w, fields, true)
|
|
|
|
}
|
|
|
|
case outputModeJSONSingleline:
|
|
|
|
return func(w io.Writer, fields []logstorage.Field) error {
|
|
|
|
return writeJSONObject(w, fields, false)
|
|
|
|
}
|
|
|
|
case outputModeLogfmt:
|
|
|
|
return writeLogfmtObject
|
|
|
|
default:
|
|
|
|
panic(fmt.Errorf("BUG: unexpected outputMode=%d", outputMode))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-01 12:08:17 +02:00
|
|
|
type jsonPrettifier struct {
|
2024-10-07 12:18:18 +02:00
|
|
|
r io.ReadCloser
|
|
|
|
formatter func(w io.Writer, fields []logstorage.Field) error
|
2024-10-01 12:08:17 +02:00
|
|
|
|
|
|
|
d *json.Decoder
|
|
|
|
|
|
|
|
pr *io.PipeReader
|
|
|
|
pw *io.PipeWriter
|
2024-10-07 12:18:18 +02:00
|
|
|
bw *bufio.Writer
|
2024-10-01 12:08:17 +02:00
|
|
|
|
|
|
|
wg sync.WaitGroup
|
|
|
|
}
|
|
|
|
|
2024-10-07 12:18:18 +02:00
|
|
|
func newJSONPrettifier(r io.ReadCloser, outputMode outputMode) *jsonPrettifier {
|
2024-10-01 12:08:17 +02:00
|
|
|
d := json.NewDecoder(r)
|
|
|
|
pr, pw := io.Pipe()
|
2024-10-07 12:18:18 +02:00
|
|
|
bw := bufio.NewWriter(pw)
|
|
|
|
|
|
|
|
formatter := getOutputFormatter(outputMode)
|
2024-10-01 12:08:17 +02:00
|
|
|
|
|
|
|
jp := &jsonPrettifier{
|
2024-10-07 12:18:18 +02:00
|
|
|
r: r,
|
|
|
|
formatter: formatter,
|
|
|
|
|
|
|
|
d: d,
|
|
|
|
|
|
|
|
pr: pr,
|
|
|
|
pw: pw,
|
|
|
|
bw: bw,
|
2024-10-01 12:08:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
jp.wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer jp.wg.Done()
|
|
|
|
err := jp.prettifyJSONLines()
|
|
|
|
jp.closePipesWithError(err)
|
|
|
|
}()
|
|
|
|
|
|
|
|
return jp
|
|
|
|
}
|
|
|
|
|
|
|
|
func (jp *jsonPrettifier) closePipesWithError(err error) {
|
|
|
|
_ = jp.pr.CloseWithError(err)
|
|
|
|
_ = jp.pw.CloseWithError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (jp *jsonPrettifier) prettifyJSONLines() error {
|
|
|
|
for jp.d.More() {
|
2024-10-07 12:18:18 +02:00
|
|
|
fields, err := readNextJSONObject(jp.d)
|
2024-10-01 12:08:17 +02:00
|
|
|
if err != nil {
|
2024-10-05 21:25:43 +02:00
|
|
|
return err
|
2024-10-01 12:08:17 +02:00
|
|
|
}
|
2024-10-07 12:18:18 +02:00
|
|
|
sort.Slice(fields, func(i, j int) bool {
|
|
|
|
return fields[i].Name < fields[j].Name
|
|
|
|
})
|
|
|
|
if err := jp.formatter(jp.bw, fields); err != nil {
|
2024-10-01 12:08:17 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2024-10-07 12:18:18 +02:00
|
|
|
return jp.bw.Flush()
|
2024-10-01 12:08:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (jp *jsonPrettifier) Close() error {
|
|
|
|
jp.closePipesWithError(io.ErrUnexpectedEOF)
|
2024-10-07 12:18:18 +02:00
|
|
|
err := jp.r.Close()
|
2024-10-01 12:08:17 +02:00
|
|
|
jp.wg.Wait()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (jp *jsonPrettifier) Read(p []byte) (int, error) {
|
|
|
|
return jp.pr.Read(p)
|
|
|
|
}
|
2024-10-05 21:25:43 +02:00
|
|
|
|
2024-10-07 12:18:18 +02:00
|
|
|
func readNextJSONObject(d *json.Decoder) ([]logstorage.Field, error) {
|
2024-10-05 21:25:43 +02:00
|
|
|
t, err := d.Token()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot read '{': %w", err)
|
|
|
|
}
|
|
|
|
delim, ok := t.(json.Delim)
|
|
|
|
if !ok || delim.String() != "{" {
|
|
|
|
return nil, fmt.Errorf("unexpected token read; got %q; want '{'", delim)
|
|
|
|
}
|
|
|
|
|
2024-10-07 12:18:18 +02:00
|
|
|
var fields []logstorage.Field
|
2024-10-05 21:25:43 +02:00
|
|
|
for {
|
|
|
|
// Read object key
|
|
|
|
t, err := d.Token()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot read JSON object key or closing brace: %w", err)
|
|
|
|
}
|
|
|
|
delim, ok := t.(json.Delim)
|
|
|
|
if ok {
|
|
|
|
if delim.String() == "}" {
|
2024-10-07 12:18:18 +02:00
|
|
|
return fields, nil
|
2024-10-05 21:25:43 +02:00
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("unexpected delimiter read; got %q; want '}'", delim)
|
|
|
|
}
|
|
|
|
key, ok := t.(string)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("unexpected token read for object key: %v; want string or '}'", t)
|
|
|
|
}
|
|
|
|
|
|
|
|
// read object value
|
|
|
|
t, err = d.Token()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot read JSON object value: %w", err)
|
|
|
|
}
|
|
|
|
value, ok := t.(string)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("unexpected token read for oject value: %v; want string", t)
|
|
|
|
}
|
|
|
|
|
2024-10-07 12:18:18 +02:00
|
|
|
fields = append(fields, logstorage.Field{
|
|
|
|
Name: key,
|
|
|
|
Value: value,
|
2024-10-05 21:25:43 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-07 12:18:18 +02:00
|
|
|
func writeLogfmtObject(w io.Writer, fields []logstorage.Field) error {
|
|
|
|
data := logstorage.MarshalFieldsToLogfmt(nil, fields)
|
|
|
|
_, err := fmt.Fprintf(w, "%s\n", data)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeJSONObject(w io.Writer, fields []logstorage.Field, isMultiline bool) error {
|
|
|
|
if len(fields) == 0 {
|
2024-10-05 21:25:43 +02:00
|
|
|
fmt.Fprintf(w, "{}\n")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-10-07 12:18:18 +02:00
|
|
|
fmt.Fprintf(w, "{")
|
|
|
|
writeNewlineIfNeeded(w, isMultiline)
|
|
|
|
if err := writeJSONObjectKeyValue(w, fields[0], isMultiline); err != nil {
|
2024-10-05 21:25:43 +02:00
|
|
|
return err
|
|
|
|
}
|
2024-10-07 12:18:18 +02:00
|
|
|
for _, f := range fields[1:] {
|
|
|
|
fmt.Fprintf(w, ",")
|
|
|
|
writeNewlineIfNeeded(w, isMultiline)
|
|
|
|
if err := writeJSONObjectKeyValue(w, f, isMultiline); err != nil {
|
2024-10-05 21:25:43 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2024-10-07 12:18:18 +02:00
|
|
|
writeNewlineIfNeeded(w, isMultiline)
|
|
|
|
fmt.Fprintf(w, "}\n")
|
2024-10-05 21:25:43 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-10-07 12:18:18 +02:00
|
|
|
func writeNewlineIfNeeded(w io.Writer, isMultiline bool) {
|
|
|
|
if isMultiline {
|
|
|
|
fmt.Fprintf(w, "\n")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeJSONObjectKeyValue(w io.Writer, f logstorage.Field, isMultiline bool) error {
|
|
|
|
key := getJSONString(f.Name)
|
|
|
|
value := getJSONString(f.Value)
|
|
|
|
if isMultiline {
|
|
|
|
_, err := fmt.Fprintf(w, " %s: %s", key, value)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err := fmt.Fprintf(w, "%s:%s", key, value)
|
2024-10-05 21:25:43 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func getJSONString(s string) string {
|
|
|
|
data, err := json.Marshal(s)
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Errorf("unexpected error when marshaling string to JSON: %w", err))
|
|
|
|
}
|
|
|
|
return string(data)
|
|
|
|
}
|