2020-03-10 18:35:58 +01:00
|
|
|
package csvimport
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmagent/common"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmagent/remotewrite"
|
2021-08-05 08:44:29 +02:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/auth"
|
2020-03-10 18:35:58 +01:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/prompbmarshal"
|
2020-09-02 18:41:12 +02:00
|
|
|
parserCommon "github.com/VictoriaMetrics/VictoriaMetrics/lib/protoparser/common"
|
2020-03-10 18:35:58 +01:00
|
|
|
parser "github.com/VictoriaMetrics/VictoriaMetrics/lib/protoparser/csvimport"
|
2023-02-13 19:25:37 +01:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/protoparser/csvimport/stream"
|
2021-08-05 08:44:29 +02:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/tenantmetrics"
|
2020-03-10 18:35:58 +01:00
|
|
|
"github.com/VictoriaMetrics/metrics"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2021-08-05 08:44:29 +02:00
|
|
|
rowsInserted = metrics.NewCounter(`vmagent_rows_inserted_total{type="csvimport"}`)
|
2021-08-05 08:46:19 +02:00
|
|
|
rowsTenantInserted = tenantmetrics.NewCounterMap(`vmagent_tenant_inserted_rows_total{type="csvimport"}`)
|
2021-08-05 08:44:29 +02:00
|
|
|
rowsPerInsert = metrics.NewHistogram(`vmagent_rows_per_insert{type="csvimport"}`)
|
2020-03-10 18:35:58 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// InsertHandler processes csv data from req.
|
2021-08-05 08:46:19 +02:00
|
|
|
func InsertHandler(at *auth.Token, req *http.Request) error {
|
2020-09-02 18:41:12 +02:00
|
|
|
extraLabels, err := parserCommon.GetExtraLabels(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-02-13 19:25:37 +01:00
|
|
|
return stream.Parse(req, func(rows []parser.Row) error {
|
2023-01-07 03:59:39 +01:00
|
|
|
return insertRows(at, rows, extraLabels)
|
2020-03-10 18:35:58 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-08-05 08:46:19 +02:00
|
|
|
func insertRows(at *auth.Token, rows []parser.Row, extraLabels []prompbmarshal.Label) error {
|
2020-03-10 18:35:58 +01:00
|
|
|
ctx := common.GetPushCtx()
|
|
|
|
defer common.PutPushCtx(ctx)
|
|
|
|
|
|
|
|
tssDst := ctx.WriteRequest.Timeseries[:0]
|
|
|
|
labels := ctx.Labels[:0]
|
|
|
|
samples := ctx.Samples[:0]
|
|
|
|
for i := range rows {
|
|
|
|
r := &rows[i]
|
|
|
|
labelsLen := len(labels)
|
|
|
|
labels = append(labels, prompbmarshal.Label{
|
|
|
|
Name: "__name__",
|
|
|
|
Value: r.Metric,
|
|
|
|
})
|
|
|
|
for j := range r.Tags {
|
|
|
|
tag := &r.Tags[j]
|
|
|
|
labels = append(labels, prompbmarshal.Label{
|
|
|
|
Name: tag.Key,
|
|
|
|
Value: tag.Value,
|
|
|
|
})
|
|
|
|
}
|
2020-09-02 18:41:12 +02:00
|
|
|
labels = append(labels, extraLabels...)
|
2020-03-10 18:35:58 +01:00
|
|
|
samples = append(samples, prompbmarshal.Sample{
|
|
|
|
Value: r.Value,
|
|
|
|
Timestamp: r.Timestamp,
|
|
|
|
})
|
|
|
|
tssDst = append(tssDst, prompbmarshal.TimeSeries{
|
|
|
|
Labels: labels[labelsLen:],
|
|
|
|
Samples: samples[len(samples)-1:],
|
|
|
|
})
|
|
|
|
}
|
|
|
|
ctx.WriteRequest.Timeseries = tssDst
|
|
|
|
ctx.Labels = labels
|
|
|
|
ctx.Samples = samples
|
2023-11-25 10:31:30 +01:00
|
|
|
if !remotewrite.TryPush(at, &ctx.WriteRequest) {
|
2023-11-24 13:42:11 +01:00
|
|
|
return remotewrite.ErrQueueFullHTTPRetry
|
|
|
|
}
|
2020-03-10 18:35:58 +01:00
|
|
|
rowsInserted.Add(len(rows))
|
2021-08-05 08:46:19 +02:00
|
|
|
if at != nil {
|
|
|
|
rowsTenantInserted.Get(at).Add(len(rows))
|
2021-08-05 08:44:29 +02:00
|
|
|
}
|
2020-03-10 18:35:58 +01:00
|
|
|
rowsPerInsert.Update(float64(len(rows)))
|
|
|
|
return nil
|
|
|
|
}
|