2020-02-23 11:35:47 +00:00
|
|
|
package promremotewrite
|
|
|
|
|
|
|
|
import (
|
2021-09-20 11:49:28 +00:00
|
|
|
"io"
|
2020-02-23 11:35:47 +00:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmagent/common"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmagent/remotewrite"
|
2021-08-05 06:44:29 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/auth"
|
2020-02-23 11:35:47 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/bytesutil"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/prompb"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/prompbmarshal"
|
2021-01-12 22:52:50 +00:00
|
|
|
parserCommon "github.com/VictoriaMetrics/VictoriaMetrics/lib/protoparser/common"
|
2020-02-23 11:35:47 +00:00
|
|
|
parser "github.com/VictoriaMetrics/VictoriaMetrics/lib/protoparser/promremotewrite"
|
2021-08-05 06:44:29 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/tenantmetrics"
|
2020-02-23 11:35:47 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/writeconcurrencylimiter"
|
|
|
|
"github.com/VictoriaMetrics/metrics"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2021-08-05 06:46:19 +00:00
|
|
|
rowsInserted = metrics.NewCounter(`vmagent_rows_inserted_total{type="promremotewrite"}`)
|
|
|
|
rowsTenantInserted = tenantmetrics.NewCounterMap(`vmagent_tenant_inserted_rows_total{type="promremotewrite"}`)
|
|
|
|
rowsPerInsert = metrics.NewHistogram(`vmagent_rows_per_insert{type="promremotewrite"}`)
|
2020-02-23 11:35:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// InsertHandler processes remote write for prometheus.
|
2021-08-05 06:46:19 +00:00
|
|
|
func InsertHandler(at *auth.Token, req *http.Request) error {
|
2021-01-12 22:52:50 +00:00
|
|
|
extraLabels, err := parserCommon.GetExtraLabels(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-02-23 11:35:47 +00:00
|
|
|
return writeconcurrencylimiter.Do(func() error {
|
2021-09-20 11:49:28 +00:00
|
|
|
return parser.ParseStream(req.Body, func(tss []prompb.TimeSeries) error {
|
2021-08-05 06:46:19 +00:00
|
|
|
return insertRows(at, tss, extraLabels)
|
2021-01-12 22:52:50 +00:00
|
|
|
})
|
2020-02-23 11:35:47 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-09-20 11:49:28 +00:00
|
|
|
// InsertHandlerForReader processes metrics from given reader
|
2021-09-28 21:52:07 +00:00
|
|
|
func InsertHandlerForReader(at *auth.Token, r io.Reader) error {
|
2021-09-20 11:49:28 +00:00
|
|
|
return writeconcurrencylimiter.Do(func() error {
|
|
|
|
return parser.ParseStream(r, func(tss []prompb.TimeSeries) error {
|
2021-09-28 21:52:07 +00:00
|
|
|
return insertRows(at, tss, nil)
|
2021-09-20 11:49:28 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-08-05 06:46:19 +00:00
|
|
|
func insertRows(at *auth.Token, timeseries []prompb.TimeSeries, extraLabels []prompbmarshal.Label) error {
|
2020-02-23 11:35:47 +00:00
|
|
|
ctx := common.GetPushCtx()
|
|
|
|
defer common.PutPushCtx(ctx)
|
|
|
|
|
|
|
|
rowsTotal := 0
|
|
|
|
tssDst := ctx.WriteRequest.Timeseries[:0]
|
|
|
|
labels := ctx.Labels[:0]
|
|
|
|
samples := ctx.Samples[:0]
|
|
|
|
for i := range timeseries {
|
|
|
|
ts := ×eries[i]
|
2020-10-09 10:29:27 +00:00
|
|
|
rowsTotal += len(ts.Samples)
|
2020-02-23 11:35:47 +00:00
|
|
|
labelsLen := len(labels)
|
|
|
|
for i := range ts.Labels {
|
|
|
|
label := &ts.Labels[i]
|
|
|
|
labels = append(labels, prompbmarshal.Label{
|
|
|
|
Name: bytesutil.ToUnsafeString(label.Name),
|
|
|
|
Value: bytesutil.ToUnsafeString(label.Value),
|
|
|
|
})
|
|
|
|
}
|
2021-01-12 22:52:50 +00:00
|
|
|
labels = append(labels, extraLabels...)
|
2020-02-23 11:35:47 +00:00
|
|
|
samplesLen := len(samples)
|
|
|
|
for i := range ts.Samples {
|
|
|
|
sample := &ts.Samples[i]
|
|
|
|
samples = append(samples, prompbmarshal.Sample{
|
|
|
|
Value: sample.Value,
|
|
|
|
Timestamp: sample.Timestamp,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
tssDst = append(tssDst, prompbmarshal.TimeSeries{
|
|
|
|
Labels: labels[labelsLen:],
|
|
|
|
Samples: samples[samplesLen:],
|
|
|
|
})
|
|
|
|
}
|
|
|
|
ctx.WriteRequest.Timeseries = tssDst
|
|
|
|
ctx.Labels = labels
|
|
|
|
ctx.Samples = samples
|
2021-08-05 06:46:19 +00:00
|
|
|
remotewrite.PushWithAuthToken(at, &ctx.WriteRequest)
|
2020-02-23 11:35:47 +00:00
|
|
|
rowsInserted.Add(rowsTotal)
|
2021-08-05 06:46:19 +00:00
|
|
|
if at != nil {
|
|
|
|
rowsTenantInserted.Get(at).Add(rowsTotal)
|
2021-08-05 06:44:29 +00:00
|
|
|
}
|
2020-02-23 11:35:47 +00:00
|
|
|
rowsPerInsert.Update(float64(rowsTotal))
|
|
|
|
return nil
|
|
|
|
}
|