2023-06-21 15:31:28 +02:00
|
|
|
package jsonline
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2024-06-17 22:28:15 +02:00
|
|
|
"io"
|
2023-06-21 15:31:28 +02:00
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2023-06-22 04:39:22 +02:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vlinsert/insertutils"
|
2023-06-21 15:31:28 +02: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 04:39:22 +02:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/protoparser/common"
|
2023-06-21 15:31:28 +02:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/writeconcurrencylimiter"
|
|
|
|
"github.com/VictoriaMetrics/metrics"
|
|
|
|
)
|
|
|
|
|
|
|
|
// RequestHandler processes jsonline insert requests
|
2024-06-17 12:13:18 +02:00
|
|
|
func RequestHandler(w http.ResponseWriter, r *http.Request) {
|
2023-09-18 23:58:32 +02:00
|
|
|
startTime := time.Now()
|
2023-06-21 15:31:28 +02:00
|
|
|
w.Header().Add("Content-Type", "application/json")
|
|
|
|
|
2023-06-22 04:39:22 +02:00
|
|
|
if r.Method != "POST" {
|
2023-06-21 15:31:28 +02:00
|
|
|
w.WriteHeader(http.StatusMethodNotAllowed)
|
2024-06-17 12:13:18 +02:00
|
|
|
return
|
2023-06-21 15:31:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
requestsTotal.Inc()
|
|
|
|
|
2023-06-22 04:39:22 +02:00
|
|
|
cp, err := insertutils.GetCommonParams(r)
|
2023-06-21 15:31:28 +02:00
|
|
|
if err != nil {
|
|
|
|
httpserver.Errorf(w, r, "%s", err)
|
2024-06-17 12:13:18 +02:00
|
|
|
return
|
2023-06-21 15:31:28 +02:00
|
|
|
}
|
2023-10-02 16:26:02 +02:00
|
|
|
if err := vlstorage.CanWriteData(); err != nil {
|
|
|
|
httpserver.Errorf(w, r, "%s", err)
|
2024-06-17 12:13:18 +02:00
|
|
|
return
|
2023-10-02 16:26:02 +02:00
|
|
|
}
|
2023-06-21 15:31:28 +02:00
|
|
|
|
|
|
|
reader := r.Body
|
|
|
|
if r.Header.Get("Content-Encoding") == "gzip" {
|
2023-06-22 04:39:22 +02:00
|
|
|
zr, err := common.GetGzipReader(reader)
|
2023-06-21 15:31:28 +02:00
|
|
|
if err != nil {
|
2024-06-17 12:13:18 +02:00
|
|
|
logger.Errorf("cannot read gzipped jsonline request: %s", err)
|
|
|
|
return
|
2023-06-21 15:31:28 +02:00
|
|
|
}
|
2023-06-22 04:39:22 +02:00
|
|
|
defer common.PutGzipReader(zr)
|
2023-06-21 15:31:28 +02:00
|
|
|
reader = zr
|
|
|
|
}
|
|
|
|
|
2024-06-17 22:28:15 +02:00
|
|
|
lmp := cp.NewLogMessageProcessor()
|
2024-10-30 14:13:56 +01:00
|
|
|
err = processStreamInternal(reader, cp.TimeField, cp.MsgFields, lmp)
|
2024-06-17 22:28:15 +02:00
|
|
|
lmp.MustClose()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("jsonline: %s", err)
|
|
|
|
} else {
|
|
|
|
// update requestDuration only for successfully parsed requests.
|
|
|
|
// There is no need in updating requestDuration for request errors,
|
|
|
|
// since their timings are usually much smaller than the timing for successful request parsing.
|
|
|
|
requestDuration.UpdateDuration(startTime)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-30 14:13:56 +01:00
|
|
|
func processStreamInternal(r io.Reader, timeField string, msgFields []string, lmp insertutils.LogMessageProcessor) error {
|
2024-06-17 22:28:15 +02:00
|
|
|
wcr := writeconcurrencylimiter.GetReader(r)
|
2023-06-21 15:31:28 +02:00
|
|
|
defer writeconcurrencylimiter.PutReader(wcr)
|
|
|
|
|
|
|
|
lb := lineBufferPool.Get()
|
|
|
|
defer lineBufferPool.Put(lb)
|
|
|
|
|
2023-06-22 04:39:22 +02:00
|
|
|
lb.B = bytesutil.ResizeNoCopyNoOverallocate(lb.B, insertutils.MaxLineSizeBytes.IntN())
|
2023-06-21 15:31:28 +02:00
|
|
|
sc := bufio.NewScanner(wcr)
|
|
|
|
sc.Buffer(lb.B, len(lb.B))
|
|
|
|
|
|
|
|
n := 0
|
|
|
|
for {
|
2024-10-30 14:13:56 +01:00
|
|
|
ok, err := readLine(sc, timeField, msgFields, lmp)
|
2023-06-21 15:31:28 +02:00
|
|
|
wcr.DecConcurrency()
|
|
|
|
if err != nil {
|
2024-06-17 12:13:18 +02:00
|
|
|
errorsTotal.Inc()
|
2024-06-17 22:28:15 +02:00
|
|
|
return fmt.Errorf("cannot read line #%d in /jsonline request: %s", n, err)
|
2023-06-21 15:31:28 +02:00
|
|
|
}
|
|
|
|
if !ok {
|
2024-06-17 22:28:15 +02:00
|
|
|
return nil
|
2023-06-21 15:31:28 +02:00
|
|
|
}
|
|
|
|
n++
|
|
|
|
rowsIngestedTotal.Inc()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-30 14:13:56 +01:00
|
|
|
func readLine(sc *bufio.Scanner, timeField string, msgFields []string, lmp insertutils.LogMessageProcessor) (bool, error) {
|
2023-06-22 04:39:22 +02: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 15:31:28 +02:00
|
|
|
}
|
2023-06-22 04:39:22 +02:00
|
|
|
return false, nil
|
2023-06-21 15:31:28 +02:00
|
|
|
}
|
2023-06-22 04:39:22 +02:00
|
|
|
line = sc.Bytes()
|
2023-06-21 15:31:28 +02:00
|
|
|
}
|
|
|
|
|
2024-05-20 04:08:30 +02:00
|
|
|
p := logstorage.GetJSONParser()
|
2024-05-22 21:01:20 +02:00
|
|
|
if err := p.ParseLogMessage(line); err != nil {
|
2023-06-22 04:39:22 +02:00
|
|
|
return false, fmt.Errorf("cannot parse json-encoded log entry: %w", err)
|
2023-06-21 15:31:28 +02:00
|
|
|
}
|
2024-06-18 00:23:17 +02:00
|
|
|
ts, err := insertutils.ExtractTimestampRFC3339NanoFromFields(timeField, p.Fields)
|
2023-06-21 15:31:28 +02:00
|
|
|
if err != nil {
|
2024-06-17 12:13:18 +02:00
|
|
|
return false, fmt.Errorf("cannot get timestamp: %w", err)
|
2023-07-21 01:21:47 +02:00
|
|
|
}
|
2024-10-30 14:13:56 +01:00
|
|
|
logstorage.RenameField(p.Fields, msgFields, "_msg")
|
2024-06-17 22:28:15 +02:00
|
|
|
lmp.AddRow(ts, p.Fields)
|
2024-05-20 04:08:30 +02:00
|
|
|
logstorage.PutJSONParser(p)
|
2023-09-29 11:55:38 +02:00
|
|
|
|
2023-06-21 15:31:28 +02:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var lineBufferPool bytesutil.ByteBufferPool
|
|
|
|
|
|
|
|
var (
|
2024-06-17 12:13:18 +02:00
|
|
|
rowsIngestedTotal = metrics.NewCounter(`vl_rows_ingested_total{type="jsonline"}`)
|
|
|
|
|
|
|
|
requestsTotal = metrics.NewCounter(`vl_http_requests_total{path="/insert/jsonline"}`)
|
|
|
|
errorsTotal = metrics.NewCounter(`vl_http_errors_total{path="/insert/jsonline"}`)
|
|
|
|
|
|
|
|
requestDuration = metrics.NewHistogram(`vl_http_request_duration_seconds{path="/insert/jsonline"}`)
|
2023-06-21 15:31:28 +02:00
|
|
|
)
|