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"
|
|
|
|
"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-09-18 21:58:32 +00:00
|
|
|
startTime := time.Now()
|
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-10-02 14:26:02 +00:00
|
|
|
if err := vlstorage.CanWriteData(); 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()
|
|
|
|
}
|
|
|
|
|
2023-10-02 14:26:02 +00:00
|
|
|
vlstorage.MustAddRows(lr)
|
2023-06-21 13:31:28 +00:00
|
|
|
logstorage.PutLogRows(lr)
|
|
|
|
|
2023-09-18 21:58:32 +00:00
|
|
|
// update jsonlineRequestDuration only for successfully parsed requests.
|
|
|
|
// There is no need in updating jsonlineRequestDuration for request errors,
|
|
|
|
// since their timings are usually much smaller than the timing for successful request parsing.
|
|
|
|
jsonlineRequestDuration.UpdateDuration(startTime)
|
|
|
|
|
2023-06-21 13:31:28 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-10-02 14:26:02 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-05-20 02:08:30 +00:00
|
|
|
p := logstorage.GetJSONParser()
|
2024-05-22 19:01:20 +00:00
|
|
|
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
|
|
|
}
|
2023-07-20 23:21:47 +00:00
|
|
|
ts, err := extractTimestampFromFields(timeField, p.Fields)
|
2023-06-21 13:31:28 +00:00
|
|
|
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-07-20 23:21:47 +00:00
|
|
|
if ts == 0 {
|
|
|
|
ts = time.Now().UnixNano()
|
|
|
|
}
|
2023-06-22 02:39:22 +00:00
|
|
|
p.RenameField(msgField, "_msg")
|
2023-10-02 14:26:02 +00:00
|
|
|
processLogMessage(ts, p.Fields)
|
2024-05-20 02:08:30 +00:00
|
|
|
logstorage.PutJSONParser(p)
|
2023-09-29 09:55:38 +00:00
|
|
|
|
2023-06-21 13:31:28 +00:00
|
|
|
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
|
|
|
|
}
|
2023-07-20 23:21:47 +00:00
|
|
|
return 0, nil
|
2023-06-21 13:31:28 +00:00
|
|
|
}
|
|
|
|
|
2023-06-22 02:39:22 +00:00
|
|
|
func parseISO8601Timestamp(s string) (int64, error) {
|
2023-07-20 23:21:47 +00:00
|
|
|
if s == "0" || s == "" {
|
|
|
|
// Special case for returning the current timestamp.
|
|
|
|
// It must be automatically converted to the current timestamp by the caller.
|
|
|
|
return 0, nil
|
|
|
|
}
|
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 (
|
2023-09-18 21:58:32 +00:00
|
|
|
requestsTotal = metrics.NewCounter(`vl_http_requests_total{path="/insert/jsonline"}`)
|
|
|
|
rowsIngestedTotal = metrics.NewCounter(`vl_rows_ingested_total{type="jsonline"}`)
|
|
|
|
jsonlineRequestDuration = metrics.NewHistogram(`vl_http_request_duration_seconds{path="/insert/jsonline"}`)
|
2023-06-21 13:31:28 +00:00
|
|
|
)
|