2023-07-20 08:10:55 +00:00
|
|
|
package loki
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"math"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
2023-07-20 23:21:47 +00:00
|
|
|
"time"
|
2023-07-20 08:10:55 +00:00
|
|
|
|
2024-11-06 16:25:05 +00:00
|
|
|
"github.com/VictoriaMetrics/metrics"
|
|
|
|
"github.com/valyala/fastjson"
|
|
|
|
|
2024-06-17 20:28:15 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vlinsert/insertutils"
|
2023-07-20 23:21:47 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vlstorage"
|
2023-07-20 08:10:55 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/bytesutil"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/httpserver"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logstorage"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/protoparser/common"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/writeconcurrencylimiter"
|
|
|
|
)
|
|
|
|
|
2023-09-18 21:58:32 +00:00
|
|
|
var parserPool fastjson.ParserPool
|
2023-07-20 08:10:55 +00:00
|
|
|
|
2024-06-17 10:13:18 +00:00
|
|
|
func handleJSON(r *http.Request, w http.ResponseWriter) {
|
2023-09-18 21:58:32 +00:00
|
|
|
startTime := time.Now()
|
2024-06-17 10:13:18 +00:00
|
|
|
requestsJSONTotal.Inc()
|
2023-07-20 08:10:55 +00:00
|
|
|
reader := r.Body
|
2023-07-20 23:21:47 +00:00
|
|
|
if r.Header.Get("Content-Encoding") == "gzip" {
|
2023-07-20 08:10:55 +00:00
|
|
|
zr, err := common.GetGzipReader(reader)
|
|
|
|
if err != nil {
|
2023-07-20 23:21:47 +00:00
|
|
|
httpserver.Errorf(w, r, "cannot initialize gzip reader: %s", err)
|
2024-06-17 10:13:18 +00:00
|
|
|
return
|
2023-07-20 08:10:55 +00:00
|
|
|
}
|
|
|
|
defer common.PutGzipReader(zr)
|
|
|
|
reader = zr
|
|
|
|
}
|
|
|
|
|
2023-07-20 23:21:47 +00:00
|
|
|
wcr := writeconcurrencylimiter.GetReader(reader)
|
|
|
|
data, err := io.ReadAll(wcr)
|
|
|
|
writeconcurrencylimiter.PutReader(wcr)
|
|
|
|
if err != nil {
|
|
|
|
httpserver.Errorf(w, r, "cannot read request body: %s", err)
|
2024-06-17 10:13:18 +00:00
|
|
|
return
|
2023-07-20 23:21:47 +00:00
|
|
|
}
|
|
|
|
|
2023-07-20 08:10:55 +00:00
|
|
|
cp, err := getCommonParams(r)
|
|
|
|
if err != nil {
|
2023-07-20 23:21:47 +00:00
|
|
|
httpserver.Errorf(w, r, "cannot parse common params from request: %s", err)
|
2024-06-17 10:13:18 +00:00
|
|
|
return
|
2023-07-20 08:10:55 +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
|
|
|
}
|
2024-06-17 20:28:15 +00:00
|
|
|
lmp := cp.NewLogMessageProcessor()
|
|
|
|
n, err := parseJSONRequest(data, lmp)
|
|
|
|
lmp.MustClose()
|
2023-09-29 09:55:38 +00:00
|
|
|
if err != nil {
|
2024-11-08 21:00:56 +00:00
|
|
|
httpserver.Errorf(w, r, "cannot parse Loki json request: %s; data=%s", err, data)
|
2024-06-17 10:13:18 +00:00
|
|
|
return
|
2023-09-29 09:55:38 +00:00
|
|
|
}
|
2023-10-02 14:26:02 +00:00
|
|
|
|
2023-07-20 23:21:47 +00:00
|
|
|
rowsIngestedJSONTotal.Add(n)
|
2023-09-18 21:58:32 +00:00
|
|
|
|
2024-06-17 10:13:18 +00:00
|
|
|
// update requestJSONDuration only for successfully parsed requests
|
|
|
|
// There is no need in updating requestJSONDuration 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
|
|
|
requestJSONDuration.UpdateDuration(startTime)
|
2023-07-20 08:10:55 +00:00
|
|
|
}
|
|
|
|
|
2023-09-18 21:58:32 +00:00
|
|
|
var (
|
2024-06-17 10:13:18 +00:00
|
|
|
requestsJSONTotal = metrics.NewCounter(`vl_http_requests_total{path="/insert/loki/api/v1/push",format="json"}`)
|
|
|
|
rowsIngestedJSONTotal = metrics.NewCounter(`vl_rows_ingested_total{type="loki",format="json"}`)
|
|
|
|
requestJSONDuration = metrics.NewHistogram(`vl_http_request_duration_seconds{path="/insert/loki/api/v1/push",format="json"}`)
|
2023-09-18 21:58:32 +00:00
|
|
|
)
|
|
|
|
|
2024-06-17 20:28:15 +00:00
|
|
|
func parseJSONRequest(data []byte, lmp insertutils.LogMessageProcessor) (int, error) {
|
2023-07-20 23:21:47 +00:00
|
|
|
p := parserPool.Get()
|
|
|
|
defer parserPool.Put(p)
|
|
|
|
v, err := p.ParseBytes(data)
|
2023-07-20 08:10:55 +00:00
|
|
|
if err != nil {
|
2023-07-20 23:21:47 +00:00
|
|
|
return 0, fmt.Errorf("cannot parse JSON request body: %w", err)
|
2023-07-20 08:10:55 +00:00
|
|
|
}
|
|
|
|
|
2023-07-20 23:21:47 +00:00
|
|
|
streamsV := v.Get("streams")
|
|
|
|
if streamsV == nil {
|
2024-11-08 21:00:56 +00:00
|
|
|
return 0, fmt.Errorf("missing `streams` item in the parsed JSON")
|
2023-07-20 23:21:47 +00:00
|
|
|
}
|
|
|
|
streams, err := streamsV.Array()
|
2023-07-20 08:10:55 +00:00
|
|
|
if err != nil {
|
2023-07-20 23:21:47 +00:00
|
|
|
return 0, fmt.Errorf("`streams` item in the parsed JSON must contain an array; got %q", streamsV)
|
2023-07-20 08:10:55 +00:00
|
|
|
}
|
|
|
|
|
2023-07-20 23:21:47 +00:00
|
|
|
currentTimestamp := time.Now().UnixNano()
|
2023-07-20 08:10:55 +00:00
|
|
|
var commonFields []logstorage.Field
|
|
|
|
rowsIngested := 0
|
2023-07-20 23:21:47 +00:00
|
|
|
for _, stream := range streams {
|
|
|
|
// populate common labels from `stream` dict
|
|
|
|
commonFields = commonFields[:0]
|
|
|
|
labelsV := stream.Get("stream")
|
|
|
|
var labels *fastjson.Object
|
|
|
|
if labelsV != nil {
|
|
|
|
o, err := labelsV.Object()
|
|
|
|
if err != nil {
|
|
|
|
return rowsIngested, fmt.Errorf("`stream` item in the parsed JSON must contain an object; got %q", labelsV)
|
|
|
|
}
|
|
|
|
labels = o
|
2023-07-20 08:10:55 +00:00
|
|
|
}
|
2023-07-20 23:21:47 +00:00
|
|
|
labels.Visit(func(k []byte, v *fastjson.Value) {
|
|
|
|
vStr, errLocal := v.StringBytes()
|
|
|
|
if errLocal != nil {
|
|
|
|
err = fmt.Errorf("unexpected label value type for %q:%q; want string", k, v)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
commonFields = append(commonFields, logstorage.Field{
|
|
|
|
Name: bytesutil.ToUnsafeString(k),
|
|
|
|
Value: bytesutil.ToUnsafeString(vStr),
|
|
|
|
})
|
2023-07-20 08:10:55 +00:00
|
|
|
})
|
2023-07-20 23:21:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return rowsIngested, fmt.Errorf("error when parsing `stream` object: %w", err)
|
|
|
|
}
|
2023-07-20 08:10:55 +00:00
|
|
|
|
2023-07-20 23:21:47 +00:00
|
|
|
// populate messages from `values` array
|
|
|
|
linesV := stream.Get("values")
|
|
|
|
if linesV == nil {
|
2024-11-08 21:00:56 +00:00
|
|
|
return rowsIngested, fmt.Errorf("missing `values` item in the parsed `stream` object %q", stream)
|
2023-07-20 23:21:47 +00:00
|
|
|
}
|
|
|
|
lines, err := linesV.Array()
|
|
|
|
if err != nil {
|
|
|
|
return rowsIngested, fmt.Errorf("`values` item in the parsed JSON must contain an array; got %q", linesV)
|
|
|
|
}
|
|
|
|
|
|
|
|
fields := commonFields
|
|
|
|
for _, line := range lines {
|
|
|
|
lineA, err := line.Array()
|
|
|
|
if err != nil {
|
|
|
|
return rowsIngested, fmt.Errorf("unexpected contents of `values` item; want array; got %q", line)
|
|
|
|
}
|
2024-11-06 18:23:35 +00:00
|
|
|
if len(lineA) < 2 || len(lineA) > 3 {
|
|
|
|
return rowsIngested, fmt.Errorf("unexpected number of values in `values` item array %q; got %d want 2 or 3", line, len(lineA))
|
2023-07-20 08:10:55 +00:00
|
|
|
}
|
|
|
|
|
2023-07-20 23:21:47 +00:00
|
|
|
// parse timestamp
|
|
|
|
timestamp, err := lineA[0].StringBytes()
|
|
|
|
if err != nil {
|
|
|
|
return rowsIngested, fmt.Errorf("unexpected log timestamp type for %q; want string", lineA[0])
|
|
|
|
}
|
|
|
|
ts, err := parseLokiTimestamp(bytesutil.ToUnsafeString(timestamp))
|
2023-07-20 08:10:55 +00:00
|
|
|
if err != nil {
|
2023-07-20 23:21:47 +00:00
|
|
|
return rowsIngested, fmt.Errorf("cannot parse log timestamp %q: %w", timestamp, err)
|
|
|
|
}
|
|
|
|
if ts == 0 {
|
|
|
|
ts = currentTimestamp
|
2023-07-20 08:10:55 +00:00
|
|
|
}
|
|
|
|
|
2023-07-20 23:21:47 +00:00
|
|
|
// parse log message
|
|
|
|
msg, err := lineA[1].StringBytes()
|
|
|
|
if err != nil {
|
|
|
|
return rowsIngested, fmt.Errorf("unexpected log message type for %q; want string", lineA[1])
|
|
|
|
}
|
|
|
|
|
|
|
|
fields = append(fields[:len(commonFields)], logstorage.Field{
|
|
|
|
Name: "_msg",
|
|
|
|
Value: bytesutil.ToUnsafeString(msg),
|
|
|
|
})
|
2024-11-06 16:25:05 +00:00
|
|
|
|
2024-11-06 18:23:35 +00:00
|
|
|
// parse structured metadata - see https://grafana.com/docs/loki/latest/reference/loki-http-api/#ingest-logs
|
2024-11-06 16:25:05 +00:00
|
|
|
if len(lineA) > 2 {
|
|
|
|
structuredMetadata, err := lineA[2].Object()
|
|
|
|
if err != nil {
|
|
|
|
return rowsIngested, fmt.Errorf("unexpected structured metadata type for %q; want JSON object", lineA[2])
|
|
|
|
}
|
|
|
|
|
|
|
|
structuredMetadata.Visit(func(k []byte, v *fastjson.Value) {
|
|
|
|
vStr, errLocal := v.StringBytes()
|
|
|
|
if errLocal != nil {
|
2024-11-06 18:23:35 +00:00
|
|
|
err = fmt.Errorf("unexpected label value type for %q:%q; want string", k, v)
|
2024-11-06 16:25:05 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fields = append(fields, logstorage.Field{
|
|
|
|
Name: bytesutil.ToUnsafeString(k),
|
|
|
|
Value: bytesutil.ToUnsafeString(vStr),
|
|
|
|
})
|
|
|
|
})
|
|
|
|
if err != nil {
|
2024-11-06 18:23:35 +00:00
|
|
|
return rowsIngested, fmt.Errorf("error when parsing `structuredMetadata` object: %w", err)
|
2024-11-06 16:25:05 +00:00
|
|
|
}
|
|
|
|
}
|
2024-06-17 20:28:15 +00:00
|
|
|
lmp.AddRow(ts, fields)
|
2023-07-20 08:10:55 +00:00
|
|
|
}
|
2023-07-20 23:21:47 +00:00
|
|
|
rowsIngested += len(lines)
|
2023-07-20 08:10:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return rowsIngested, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseLokiTimestamp(s string) (int64, error) {
|
2023-07-20 23:21:47 +00:00
|
|
|
if s == "" {
|
|
|
|
// Special case - an empty timestamp must be substituted with the current time by the caller.
|
|
|
|
return 0, nil
|
|
|
|
}
|
2023-07-20 08:10:55 +00:00
|
|
|
n, err := strconv.ParseInt(s, 10, 64)
|
|
|
|
if err != nil {
|
2023-07-20 23:21:47 +00:00
|
|
|
// Fall back to parsing floating-point value
|
|
|
|
f, err := strconv.ParseFloat(s, 64)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
if f > math.MaxInt64 {
|
2023-07-21 04:00:58 +00:00
|
|
|
return 0, fmt.Errorf("too big timestamp in nanoseconds: %v; mustn't exceed %v", f, int64(math.MaxInt64))
|
2023-07-20 23:21:47 +00:00
|
|
|
}
|
|
|
|
if f < math.MinInt64 {
|
2023-07-21 04:00:58 +00:00
|
|
|
return 0, fmt.Errorf("too small timestamp in nanoseconds: %v; must be bigger or equal to %v", f, int64(math.MinInt64))
|
2023-07-20 23:21:47 +00:00
|
|
|
}
|
|
|
|
n = int64(f)
|
2023-07-20 08:10:55 +00:00
|
|
|
}
|
|
|
|
if n < 0 {
|
2023-07-20 23:21:47 +00:00
|
|
|
return 0, fmt.Errorf("too small timestamp in nanoseconds: %d; must be bigger than 0", n)
|
2023-07-20 08:10:55 +00:00
|
|
|
}
|
|
|
|
return n, nil
|
|
|
|
}
|