2020-07-10 09:00:35 +00:00
|
|
|
package prometheusimport
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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"
|
2023-01-24 06:14:34 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/httpserver"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
|
2020-07-10 09:00:35 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/prompbmarshal"
|
2020-09-02 16:41:12 +00:00
|
|
|
parserCommon "github.com/VictoriaMetrics/VictoriaMetrics/lib/protoparser/common"
|
2020-07-10 09:00:35 +00:00
|
|
|
parser "github.com/VictoriaMetrics/VictoriaMetrics/lib/protoparser/prometheus"
|
2021-08-05 06:44:29 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/tenantmetrics"
|
2020-07-10 09:00:35 +00:00
|
|
|
"github.com/VictoriaMetrics/metrics"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2021-08-05 06:44:29 +00:00
|
|
|
rowsInserted = metrics.NewCounter(`vmagent_rows_inserted_total{type="prometheus"}`)
|
2021-08-05 06:46:19 +00:00
|
|
|
rowsTenantInserted = tenantmetrics.NewCounterMap(`vmagent_tenant_inserted_rows_total{type="prometheus"}`)
|
2021-08-05 06:44:29 +00:00
|
|
|
rowsPerInsert = metrics.NewHistogram(`vmagent_rows_per_insert{type="prometheus"}`)
|
2020-07-10 09:00:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// InsertHandler processes `/api/v1/import/prometheus` request.
|
2021-08-05 06:46:19 +00:00
|
|
|
func InsertHandler(at *auth.Token, req *http.Request) error {
|
2020-09-02 16:41:12 +00:00
|
|
|
extraLabels, err := parserCommon.GetExtraLabels(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-09-11 10:26:41 +00:00
|
|
|
defaultTimestamp, err := parserCommon.GetTimestamp(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-24 06:14:34 +00:00
|
|
|
remoteAddr := httpserver.GetQuotedRemoteAddr(req)
|
|
|
|
uri := httpserver.GetRequestURI(req)
|
2023-01-07 02:59:39 +00:00
|
|
|
isGzipped := req.Header.Get("Content-Encoding") == "gzip"
|
|
|
|
return parser.ParseStream(req.Body, defaultTimestamp, isGzipped, func(rows []parser.Row) error {
|
|
|
|
return insertRows(at, rows, extraLabels)
|
2023-01-24 06:14:34 +00:00
|
|
|
}, func(s string) {
|
|
|
|
logger.Errorf("error parsing prometheus text protocol, uri - %s, remote address - %q: %s", uri, remoteAddr, s)
|
|
|
|
})
|
2021-09-20 11:49:28 +00:00
|
|
|
}
|
|
|
|
|
2021-08-05 06:46:19 +00:00
|
|
|
func insertRows(at *auth.Token, rows []parser.Row, extraLabels []prompbmarshal.Label) error {
|
2020-07-10 09:00:35 +00: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 16:41:12 +00:00
|
|
|
labels = append(labels, extraLabels...)
|
2020-07-10 09:00:35 +00: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
|
2022-08-08 11:46:24 +00:00
|
|
|
remotewrite.Push(at, &ctx.WriteRequest)
|
2020-07-10 09:00:35 +00:00
|
|
|
rowsInserted.Add(len(rows))
|
2021-08-05 06:46:19 +00:00
|
|
|
if at != nil {
|
|
|
|
rowsTenantInserted.Get(at).Add(len(rows))
|
2021-08-05 06:44:29 +00:00
|
|
|
}
|
2020-07-10 09:00:35 +00:00
|
|
|
rowsPerInsert.Update(float64(len(rows)))
|
|
|
|
return nil
|
|
|
|
}
|