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"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/httpserver"
|
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"
|
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/VictoriaMetrics/lib/writeconcurrencylimiter"
|
|
|
|
"github.com/VictoriaMetrics/metrics"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2021-08-05 08:44:29 +02:00
|
|
|
rowsInserted = metrics.NewCounter(`vmagent_rows_inserted_total{type="csvimport"}`)
|
|
|
|
rowsTenantInserted = tenantmetrics.NewCounterMap(`vm_tenant_inserted_rows_total{type="csvimport"}`)
|
|
|
|
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:44:29 +02:00
|
|
|
func InsertHandler(p *httpserver.Path, req *http.Request) error {
|
2020-09-02 18:41:12 +02:00
|
|
|
extraLabels, err := parserCommon.GetExtraLabels(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-03-10 18:35:58 +01:00
|
|
|
return writeconcurrencylimiter.Do(func() error {
|
2020-09-02 18:41:12 +02:00
|
|
|
return parser.ParseStream(req, func(rows []parser.Row) error {
|
2021-08-05 08:44:29 +02:00
|
|
|
return insertRows(p, rows, extraLabels)
|
2020-09-02 18:41:12 +02:00
|
|
|
})
|
2020-03-10 18:35:58 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-08-05 08:44:29 +02:00
|
|
|
func insertRows(p *httpserver.Path, 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
|
2021-08-05 08:44:29 +02:00
|
|
|
remotewrite.Push(p, &ctx.WriteRequest)
|
2020-03-10 18:35:58 +01:00
|
|
|
rowsInserted.Add(len(rows))
|
2021-08-05 08:44:29 +02:00
|
|
|
if p != nil {
|
|
|
|
at, err := auth.NewToken(p.AuthToken)
|
|
|
|
if err == nil {
|
|
|
|
rowsTenantInserted.Get(at).Add(len(rows))
|
|
|
|
}
|
|
|
|
}
|
2020-03-10 18:35:58 +01:00
|
|
|
rowsPerInsert.Update(float64(len(rows)))
|
|
|
|
return nil
|
|
|
|
}
|