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