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
|
2024-06-17 10:13:18 +00:00
|
|
|
func RequestHandler(w http.ResponseWriter, r *http.Request) {
|
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)
|
2024-06-17 10:13:18 +00:00
|
|
|
return
|
2023-06-21 13:31:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2024-06-17 10:13:18 +00:00
|
|
|
return
|
2023-06-21 13:31:28 +00:00
|
|
|
}
|
2023-10-02 14:26:02 +00:00
|
|
|
if err := vlstorage.CanWriteData(); err != nil {
|
|
|
|
httpserver.Errorf(w, r, "%s", err)
|
2024-06-17 10:13:18 +00:00
|
|
|
return
|
2023-10-02 14:26:02 +00:00
|
|
|
}
|
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 {
|
2024-06-17 10:13:18 +00:00
|
|
|
logger.Errorf("cannot read gzipped jsonline request: %s", err)
|
|
|
|
return
|
2023-06-21 13:31:28 +00:00
|
|
|
}
|
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 {
|
2024-06-17 10:13:18 +00:00
|
|
|
errorsTotal.Inc()
|
2023-06-21 13:31:28 +00:00
|
|
|
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)
|
|
|
|
|
2024-06-17 10:13:18 +00:00
|
|
|
// update requestDuration only for successfully parsed requests.
|
|
|
|
// There is no need in updating requestDuration for request errors,
|
2023-09-18 21:58:32 +00:00
|
|
|
// since their timings are usually much smaller than the timing for successful request parsing.
|
2024-06-17 10:13:18 +00:00
|
|
|
requestDuration.UpdateDuration(startTime)
|
2023-06-21 13:31:28 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2024-06-17 10:13:18 +00:00
|
|
|
ts, err := insertutils.ExtractTimestampISO8601FromFields(timeField, p.Fields)
|
2023-06-21 13:31:28 +00:00
|
|
|
if err != nil {
|
2024-06-17 10:13:18 +00:00
|
|
|
return false, fmt.Errorf("cannot get timestamp: %w", err)
|
2023-07-20 23:21:47 +00:00
|
|
|
}
|
2024-06-17 10:13:18 +00:00
|
|
|
logstorage.RenameField(p.Fields, 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
|
|
|
|
}
|
|
|
|
|
|
|
|
var lineBufferPool bytesutil.ByteBufferPool
|
|
|
|
|
|
|
|
var (
|
2024-06-17 10:13:18 +00: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 13:31:28 +00:00
|
|
|
)
|