2023-06-20 07:55:12 +02:00
|
|
|
package logsql
|
|
|
|
|
|
|
|
import (
|
2024-05-12 16:33:29 +02:00
|
|
|
"context"
|
2024-05-15 04:55:44 +02:00
|
|
|
"fmt"
|
|
|
|
"math"
|
2023-06-20 07:55:12 +02:00
|
|
|
"net/http"
|
2024-05-15 04:55:44 +02:00
|
|
|
"time"
|
2023-06-20 07:55:12 +02:00
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vlstorage"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/bytesutil"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/httpserver"
|
2024-02-18 21:58:47 +01:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/httputils"
|
2023-06-20 07:55:12 +02:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logstorage"
|
2024-05-15 04:55:44 +02:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promutils"
|
2023-06-20 07:55:12 +02:00
|
|
|
)
|
|
|
|
|
2024-05-12 16:33:29 +02:00
|
|
|
// ProcessQueryRequest handles /select/logsql/query request.
|
|
|
|
func ProcessQueryRequest(ctx context.Context, w http.ResponseWriter, r *http.Request) {
|
2023-06-20 07:55:12 +02:00
|
|
|
// Extract tenantID
|
|
|
|
tenantID, err := logstorage.GetTenantIDFromRequest(r)
|
|
|
|
if err != nil {
|
|
|
|
httpserver.Errorf(w, r, "%s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-05-15 04:55:44 +02:00
|
|
|
// Parse query
|
2023-06-20 07:55:12 +02:00
|
|
|
qStr := r.FormValue("query")
|
|
|
|
q, err := logstorage.ParseQuery(qStr)
|
|
|
|
if err != nil {
|
|
|
|
httpserver.Errorf(w, r, "cannot parse query [%s]: %s", qStr, err)
|
|
|
|
return
|
|
|
|
}
|
2024-02-18 22:01:34 +01:00
|
|
|
|
2024-05-15 04:55:44 +02:00
|
|
|
// Parse optional start and end args
|
|
|
|
start, okStart, err := getTimeNsec(r, "start")
|
|
|
|
if err != nil {
|
|
|
|
httpserver.Errorf(w, r, "%s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
end, okEnd, err := getTimeNsec(r, "end")
|
|
|
|
if err != nil {
|
|
|
|
httpserver.Errorf(w, r, "%s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if okStart || okEnd {
|
|
|
|
if !okStart {
|
|
|
|
start = math.MinInt64
|
|
|
|
}
|
|
|
|
if !okEnd {
|
|
|
|
end = math.MaxInt64
|
|
|
|
}
|
|
|
|
q.AddTimeFilter(start, end)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse limit query arg
|
|
|
|
limit, err := httputils.GetInt(r, "limit")
|
|
|
|
if err != nil {
|
|
|
|
httpserver.Errorf(w, r, "%s", err)
|
|
|
|
return
|
|
|
|
}
|
2024-05-14 03:05:03 +02:00
|
|
|
if limit > 0 {
|
|
|
|
q.AddPipeLimit(uint64(limit))
|
|
|
|
}
|
2024-05-15 04:55:44 +02:00
|
|
|
q.Optimize()
|
2024-05-14 03:05:03 +02:00
|
|
|
|
2023-06-20 07:55:12 +02:00
|
|
|
tenantIDs := []logstorage.TenantID{tenantID}
|
2024-05-12 16:33:29 +02:00
|
|
|
|
2024-05-14 03:05:03 +02:00
|
|
|
bw := getBufferedWriter(w)
|
2024-05-12 16:33:29 +02:00
|
|
|
|
|
|
|
writeBlock := func(_ uint, timestamps []int64, columns []logstorage.BlockColumn) {
|
2023-06-20 07:55:12 +02:00
|
|
|
if len(columns) == 0 {
|
2024-02-18 22:01:34 +01:00
|
|
|
return
|
2023-06-20 07:55:12 +02:00
|
|
|
}
|
2024-02-18 22:01:34 +01:00
|
|
|
|
2023-06-20 07:55:12 +02:00
|
|
|
bb := blockResultPool.Get()
|
2024-05-12 16:33:29 +02:00
|
|
|
for i := range timestamps {
|
|
|
|
WriteJSONRow(bb, columns, i)
|
2023-06-20 07:55:12 +02:00
|
|
|
}
|
2024-05-14 03:05:03 +02:00
|
|
|
bw.WriteIgnoreErrors(bb.B)
|
2024-02-18 22:01:34 +01:00
|
|
|
blockResultPool.Put(bb)
|
2024-05-12 16:33:29 +02:00
|
|
|
}
|
|
|
|
|
2024-05-15 04:55:44 +02:00
|
|
|
w.Header().Set("Content-Type", "application/stream+json; charset=utf-8")
|
2024-05-14 03:05:03 +02:00
|
|
|
err = vlstorage.RunQuery(ctx, tenantIDs, q, writeBlock)
|
2024-05-12 16:33:29 +02:00
|
|
|
|
2024-05-14 03:05:03 +02:00
|
|
|
bw.FlushIgnoreErrors()
|
|
|
|
putBufferedWriter(bw)
|
2024-05-12 16:33:29 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
httpserver.Errorf(w, r, "cannot execute query [%s]: %s", qStr, err)
|
|
|
|
}
|
|
|
|
|
2023-06-20 07:55:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var blockResultPool bytesutil.ByteBufferPool
|
2024-05-15 04:55:44 +02:00
|
|
|
|
|
|
|
func getTimeNsec(r *http.Request, argName string) (int64, bool, error) {
|
|
|
|
s := r.FormValue(argName)
|
|
|
|
if s == "" {
|
|
|
|
return 0, false, nil
|
|
|
|
}
|
|
|
|
currentTimestamp := float64(time.Now().UnixNano()) / 1e9
|
|
|
|
secs, err := promutils.ParseTimeAt(s, currentTimestamp)
|
|
|
|
if err != nil {
|
|
|
|
return 0, false, fmt.Errorf("cannot parse %s=%s: %w", argName, s, err)
|
|
|
|
}
|
|
|
|
return int64(secs * 1e9), true, nil
|
|
|
|
}
|