2023-06-21 13:31:28 +00:00
|
|
|
package jsonline
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2023-06-22 02:39:22 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vlinsert/insertutils"
|
2023-06-21 13:31:28 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vlstorage"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/bytesutil"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/httpserver"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
|
2023-06-22 02:39:22 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logjson"
|
2023-06-21 13:31:28 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logstorage"
|
2023-06-22 02:39:22 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/protoparser/common"
|
2023-06-21 13:31:28 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/writeconcurrencylimiter"
|
|
|
|
"github.com/VictoriaMetrics/metrics"
|
|
|
|
)
|
|
|
|
|
|
|
|
// RequestHandler processes jsonline insert requests
|
2023-06-22 02:39:22 +00:00
|
|
|
func RequestHandler(w http.ResponseWriter, r *http.Request) bool {
|
2023-06-21 13:31:28 +00:00
|
|
|
w.Header().Add("Content-Type", "application/json")
|
|
|
|
|
2023-06-22 02:39:22 +00:00
|
|
|
if r.Method != "POST" {
|
2023-06-21 13:31:28 +00:00
|
|
|
w.WriteHeader(http.StatusMethodNotAllowed)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
requestsTotal.Inc()
|
|
|
|
|
2023-06-22 02:39:22 +00:00
|
|
|
cp, err := insertutils.GetCommonParams(r)
|
2023-06-21 13:31:28 +00:00
|
|
|
if err != nil {
|
|
|
|
httpserver.Errorf(w, r, "%s", err)
|
|
|
|
return true
|
|
|
|
}
|
2023-06-22 02:39:22 +00:00
|
|
|
lr := logstorage.GetLogRows(cp.StreamFields, cp.IgnoreFields)
|
|
|
|
processLogMessage := cp.GetProcessLogMessageFunc(lr)
|
2023-06-21 13:31:28 +00:00
|
|
|
|
|
|
|
reader := r.Body
|
|
|
|
if r.Header.Get("Content-Encoding") == "gzip" {
|
2023-06-22 02:39:22 +00:00
|
|
|
zr, err := common.GetGzipReader(reader)
|
2023-06-21 13:31:28 +00:00
|
|
|
if err != nil {
|
2023-06-22 02:39:22 +00:00
|
|
|
logger.Errorf("cannot read gzipped _bulk request: %s", err)
|
2023-06-21 13:31:28 +00:00
|
|
|
return true
|
|
|
|
}
|
2023-06-22 02:39:22 +00:00
|
|
|
defer common.PutGzipReader(zr)
|
2023-06-21 13:31:28 +00:00
|
|
|
reader = zr
|
|
|
|
}
|
|
|
|
|
|
|
|
wcr := writeconcurrencylimiter.GetReader(reader)
|
|
|
|
defer writeconcurrencylimiter.PutReader(wcr)
|
|
|
|
|
|
|
|
lb := lineBufferPool.Get()
|
|
|
|
defer lineBufferPool.Put(lb)
|
|
|
|
|
2023-06-22 02:39:22 +00:00
|
|
|
lb.B = bytesutil.ResizeNoCopyNoOverallocate(lb.B, insertutils.MaxLineSizeBytes.IntN())
|
2023-06-21 13:31:28 +00:00
|
|
|
sc := bufio.NewScanner(wcr)
|
|
|
|
sc.Buffer(lb.B, len(lb.B))
|
|
|
|
|
|
|
|
n := 0
|
|
|
|
for {
|
2023-06-22 02:39:22 +00:00
|
|
|
ok, err := readLine(sc, cp.TimeField, cp.MsgField, processLogMessage)
|
2023-06-21 13:31:28 +00:00
|
|
|
wcr.DecConcurrency()
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("cannot read line #%d in /jsonline request: %s", n, err)
|
2023-06-22 02:39:22 +00:00
|
|
|
break
|
2023-06-21 13:31:28 +00:00
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
n++
|
|
|
|
rowsIngestedTotal.Inc()
|
|
|
|
}
|
|
|
|
|
|
|
|
vlstorage.MustAddRows(lr)
|
|
|
|
logstorage.PutLogRows(lr)
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func readLine(sc *bufio.Scanner, timeField, msgField string, processLogMessage func(timestamp int64, fields []logstorage.Field)) (bool, error) {
|
2023-06-22 02:39:22 +00:00
|
|
|
var line []byte
|
|
|
|
for len(line) == 0 {
|
|
|
|
if !sc.Scan() {
|
|
|
|
if err := sc.Err(); err != nil {
|
|
|
|
if errors.Is(err, bufio.ErrTooLong) {
|
|
|
|
return false, fmt.Errorf(`cannot read json line, since its size exceeds -insert.maxLineSizeBytes=%d`, insertutils.MaxLineSizeBytes.IntN())
|
|
|
|
}
|
|
|
|
return false, err
|
2023-06-21 13:31:28 +00:00
|
|
|
}
|
2023-06-22 02:39:22 +00:00
|
|
|
return false, nil
|
2023-06-21 13:31:28 +00:00
|
|
|
}
|
2023-06-22 02:39:22 +00:00
|
|
|
line = sc.Bytes()
|
2023-06-21 13:31:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
p := logjson.GetParser()
|
|
|
|
if err := p.ParseLogMessage(line); err != nil {
|
2023-06-22 02:39:22 +00:00
|
|
|
return false, fmt.Errorf("cannot parse json-encoded log entry: %w", err)
|
2023-06-21 13:31:28 +00:00
|
|
|
}
|
|
|
|
timestamp, err := extractTimestampFromFields(timeField, p.Fields)
|
|
|
|
if err != nil {
|
2023-06-22 02:39:22 +00:00
|
|
|
return false, fmt.Errorf("cannot parse timestamp: %w", err)
|
2023-06-21 13:31:28 +00:00
|
|
|
}
|
2023-06-22 02:39:22 +00:00
|
|
|
p.RenameField(msgField, "_msg")
|
2023-06-21 13:31:28 +00:00
|
|
|
processLogMessage(timestamp, p.Fields)
|
|
|
|
logjson.PutParser(p)
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func extractTimestampFromFields(timeField string, fields []logstorage.Field) (int64, error) {
|
|
|
|
for i := range fields {
|
|
|
|
f := &fields[i]
|
|
|
|
if f.Name != timeField {
|
|
|
|
continue
|
|
|
|
}
|
2023-06-22 02:39:22 +00:00
|
|
|
timestamp, err := parseISO8601Timestamp(f.Value)
|
2023-06-21 13:31:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
f.Value = ""
|
|
|
|
return timestamp, nil
|
|
|
|
}
|
|
|
|
return time.Now().UnixNano(), nil
|
|
|
|
}
|
|
|
|
|
2023-06-22 02:39:22 +00:00
|
|
|
func parseISO8601Timestamp(s string) (int64, error) {
|
2023-06-21 13:31:28 +00:00
|
|
|
t, err := time.Parse(time.RFC3339, s)
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("cannot parse timestamp %q: %w", s, err)
|
|
|
|
}
|
|
|
|
return t.UnixNano(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var lineBufferPool bytesutil.ByteBufferPool
|
|
|
|
|
|
|
|
var (
|
|
|
|
requestsTotal = metrics.NewCounter(`vl_http_requests_total{path="/insert/jsonline"}`)
|
|
|
|
rowsIngestedTotal = metrics.NewCounter(`vl_rows_ingested_total{type="jsonline"}`)
|
|
|
|
)
|