2023-07-20 08:10:55 +00:00
|
|
|
package loki
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
2023-07-20 23:21:47 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2023-07-20 08:10:55 +00:00
|
|
|
"sync"
|
2023-07-20 23:21:47 +00:00
|
|
|
"time"
|
2023-07-20 08:10:55 +00:00
|
|
|
|
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/writeconcurrencylimiter"
|
2023-07-20 23:21:47 +00:00
|
|
|
"github.com/VictoriaMetrics/metrics"
|
|
|
|
"github.com/golang/snappy"
|
2023-07-20 08:10:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2023-09-18 21:58:32 +00:00
|
|
|
bytesBufPool bytesutil.ByteBufferPool
|
|
|
|
pushReqsPool sync.Pool
|
2023-07-20 08:10:55 +00:00
|
|
|
)
|
|
|
|
|
2024-06-17 10:13:18 +00:00
|
|
|
func handleProtobuf(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
|
|
|
requestsProtobufTotal.Inc()
|
2023-07-20 08:10:55 +00:00
|
|
|
wcr := writeconcurrencylimiter.GetReader(r.Body)
|
2023-07-20 23:21:47 +00:00
|
|
|
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 := parseProtobufRequest(data, lmp)
|
|
|
|
lmp.MustClose()
|
2023-07-20 08:10:55 +00:00
|
|
|
if err != nil {
|
2023-10-02 14:26:02 +00:00
|
|
|
httpserver.Errorf(w, r, "cannot parse Loki protobuf request: %s", err)
|
2024-06-17 10:13:18 +00:00
|
|
|
return
|
2023-07-20 08:10:55 +00:00
|
|
|
}
|
2023-09-18 21:58:32 +00:00
|
|
|
|
2023-07-20 23:21:47 +00:00
|
|
|
rowsIngestedProtobufTotal.Add(n)
|
2023-09-18 21:58:32 +00:00
|
|
|
|
2024-06-17 10:13:18 +00:00
|
|
|
// update requestProtobufDuration only for successfully parsed requests
|
|
|
|
// There is no need in updating requestProtobufDuration 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
|
|
|
requestProtobufDuration.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
|
|
|
requestsProtobufTotal = metrics.NewCounter(`vl_http_requests_total{path="/insert/loki/api/v1/push",format="protobuf"}`)
|
|
|
|
rowsIngestedProtobufTotal = metrics.NewCounter(`vl_rows_ingested_total{type="loki",format="protobuf"}`)
|
|
|
|
requestProtobufDuration = metrics.NewHistogram(`vl_http_request_duration_seconds{path="/insert/loki/api/v1/push",format="protobuf"}`)
|
2023-09-18 21:58:32 +00:00
|
|
|
)
|
|
|
|
|
2024-06-17 20:28:15 +00:00
|
|
|
func parseProtobufRequest(data []byte, lmp insertutils.LogMessageProcessor) (int, error) {
|
2023-07-20 08:10:55 +00:00
|
|
|
bb := bytesBufPool.Get()
|
|
|
|
defer bytesBufPool.Put(bb)
|
2023-07-20 23:21:47 +00:00
|
|
|
|
|
|
|
buf, err := snappy.Decode(bb.B[:cap(bb.B)], 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 decode snappy-encoded request body: %w", err)
|
2023-07-20 08:10:55 +00:00
|
|
|
}
|
2023-07-20 23:21:47 +00:00
|
|
|
bb.B = buf
|
|
|
|
|
|
|
|
req := getPushRequest()
|
|
|
|
defer putPushRequest(req)
|
2023-07-20 08:10:55 +00:00
|
|
|
|
2024-07-10 00:42:41 +00:00
|
|
|
err = req.UnmarshalProtobuf(bb.B)
|
2023-07-20 08:10:55 +00:00
|
|
|
if err != nil {
|
2023-10-25 19:24:01 +00:00
|
|
|
return 0, fmt.Errorf("cannot parse request body: %w", err)
|
2023-07-20 08:10:55 +00:00
|
|
|
}
|
|
|
|
|
2024-07-10 00:42:41 +00:00
|
|
|
fields := getFields()
|
|
|
|
defer putFields(fields)
|
|
|
|
|
2023-07-20 08:10:55 +00:00
|
|
|
rowsIngested := 0
|
2023-07-20 23:21:47 +00:00
|
|
|
streams := req.Streams
|
|
|
|
currentTimestamp := time.Now().UnixNano()
|
|
|
|
for i := range streams {
|
|
|
|
stream := &streams[i]
|
2023-07-20 08:10:55 +00:00
|
|
|
// st.Labels contains labels for the stream.
|
|
|
|
// Labels are same for all entries in the stream.
|
2024-07-10 00:42:41 +00:00
|
|
|
fields.fields, err = parsePromLabels(fields.fields[:0], stream.Labels)
|
2023-07-20 08:10:55 +00:00
|
|
|
if err != nil {
|
2023-10-25 19:24:01 +00:00
|
|
|
return rowsIngested, fmt.Errorf("cannot parse stream labels %q: %w", stream.Labels, err)
|
2023-07-20 08:10:55 +00:00
|
|
|
}
|
2024-07-10 00:42:41 +00:00
|
|
|
commonFieldsLen := len(fields.fields)
|
2023-07-20 23:21:47 +00:00
|
|
|
|
|
|
|
entries := stream.Entries
|
|
|
|
for j := range entries {
|
2024-07-10 00:42:41 +00:00
|
|
|
e := &entries[j]
|
|
|
|
fields.fields = fields.fields[:commonFieldsLen]
|
|
|
|
|
|
|
|
for _, lp := range e.StructuredMetadata {
|
|
|
|
fields.fields = append(fields.fields, logstorage.Field{
|
|
|
|
Name: lp.Name,
|
|
|
|
Value: lp.Value,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fields.fields = append(fields.fields, logstorage.Field{
|
2023-07-20 23:21:47 +00:00
|
|
|
Name: "_msg",
|
2024-07-10 00:42:41 +00:00
|
|
|
Value: e.Line,
|
2023-07-20 23:21:47 +00:00
|
|
|
})
|
2024-07-10 00:42:41 +00:00
|
|
|
|
|
|
|
ts := e.Timestamp.UnixNano()
|
2023-07-20 23:21:47 +00:00
|
|
|
if ts == 0 {
|
|
|
|
ts = currentTimestamp
|
|
|
|
}
|
2024-07-10 00:42:41 +00:00
|
|
|
|
|
|
|
lmp.AddRow(ts, fields.fields)
|
2023-07-20 08:10:55 +00:00
|
|
|
}
|
2023-07-20 23:21:47 +00:00
|
|
|
rowsIngested += len(stream.Entries)
|
2023-07-20 08:10:55 +00:00
|
|
|
}
|
|
|
|
return rowsIngested, nil
|
|
|
|
}
|
|
|
|
|
2024-07-10 00:42:41 +00:00
|
|
|
func getFields() *fields {
|
|
|
|
v := fieldsPool.Get()
|
|
|
|
if v == nil {
|
|
|
|
return &fields{}
|
|
|
|
}
|
|
|
|
return v.(*fields)
|
|
|
|
}
|
|
|
|
|
|
|
|
func putFields(f *fields) {
|
|
|
|
f.fields = f.fields[:0]
|
|
|
|
fieldsPool.Put(f)
|
|
|
|
}
|
|
|
|
|
|
|
|
var fieldsPool sync.Pool
|
|
|
|
|
|
|
|
type fields struct {
|
|
|
|
fields []logstorage.Field
|
|
|
|
}
|
|
|
|
|
2023-07-20 23:21:47 +00:00
|
|
|
// parsePromLabels parses log fields in Prometheus text exposition format from s, appends them to dst and returns the result.
|
2023-07-20 08:10:55 +00:00
|
|
|
//
|
|
|
|
// See test data of promtail for examples: https://github.com/grafana/loki/blob/a24ef7b206e0ca63ee74ca6ecb0a09b745cd2258/pkg/push/types_test.go
|
2023-07-20 23:21:47 +00:00
|
|
|
func parsePromLabels(dst []logstorage.Field, s string) ([]logstorage.Field, error) {
|
|
|
|
// Make sure s is wrapped into `{...}`
|
|
|
|
s = strings.TrimSpace(s)
|
|
|
|
if len(s) < 2 {
|
|
|
|
return nil, fmt.Errorf("too short string to parse: %q", s)
|
2023-07-20 08:10:55 +00:00
|
|
|
}
|
2023-07-20 23:21:47 +00:00
|
|
|
if s[0] != '{' {
|
|
|
|
return nil, fmt.Errorf("missing `{` at the beginning of %q", s)
|
2023-07-20 08:10:55 +00:00
|
|
|
}
|
2023-07-20 23:21:47 +00:00
|
|
|
if s[len(s)-1] != '}' {
|
|
|
|
return nil, fmt.Errorf("missing `}` at the end of %q", s)
|
2023-07-20 11:54:22 +00:00
|
|
|
}
|
2023-07-20 23:21:47 +00:00
|
|
|
s = s[1 : len(s)-1]
|
2023-07-20 11:54:22 +00:00
|
|
|
|
2023-07-20 23:21:47 +00:00
|
|
|
for len(s) > 0 {
|
|
|
|
// Parse label name
|
|
|
|
n := strings.IndexByte(s, '=')
|
|
|
|
if n < 0 {
|
|
|
|
return nil, fmt.Errorf("cannot find `=` char for label value at %s", s)
|
|
|
|
}
|
|
|
|
name := s[:n]
|
|
|
|
s = s[n+1:]
|
|
|
|
|
|
|
|
// Parse label value
|
|
|
|
qs, err := strconv.QuotedPrefix(s)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot parse value for label %q at %s: %w", name, s, err)
|
|
|
|
}
|
|
|
|
s = s[len(qs):]
|
|
|
|
value, err := strconv.Unquote(qs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot unquote value %q for label %q: %w", qs, name, err)
|
|
|
|
}
|
2023-07-20 08:10:55 +00:00
|
|
|
|
2023-07-20 23:21:47 +00:00
|
|
|
// Append the found field to dst.
|
|
|
|
dst = append(dst, logstorage.Field{
|
|
|
|
Name: name,
|
|
|
|
Value: value,
|
|
|
|
})
|
|
|
|
|
|
|
|
// Check whether there are other labels remaining
|
|
|
|
if len(s) == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if !strings.HasPrefix(s, ",") {
|
|
|
|
return nil, fmt.Errorf("missing `,` char at %s", s)
|
|
|
|
}
|
|
|
|
s = s[1:]
|
|
|
|
s = strings.TrimPrefix(s, " ")
|
|
|
|
}
|
2023-07-20 08:10:55 +00:00
|
|
|
return dst, nil
|
|
|
|
}
|
|
|
|
|
2023-07-20 23:21:47 +00:00
|
|
|
func getPushRequest() *PushRequest {
|
2023-07-20 08:10:55 +00:00
|
|
|
v := pushReqsPool.Get()
|
|
|
|
if v == nil {
|
|
|
|
return &PushRequest{}
|
|
|
|
}
|
|
|
|
return v.(*PushRequest)
|
|
|
|
}
|
|
|
|
|
2023-07-20 23:21:47 +00:00
|
|
|
func putPushRequest(req *PushRequest) {
|
2024-07-10 00:42:41 +00:00
|
|
|
req.reset()
|
2023-07-20 23:21:47 +00:00
|
|
|
pushReqsPool.Put(req)
|
2023-07-20 08:10:55 +00:00
|
|
|
}
|