2020-07-10 11:00:35 +02:00
|
|
|
package prometheusimport
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vminsert/common"
|
2020-07-23 12:33:10 +02:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vminsert/relabel"
|
2023-01-24 07:14:34 +01:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/httpserver"
|
2020-09-02 18:41:12 +02:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/prompbmarshal"
|
|
|
|
parserCommon "github.com/VictoriaMetrics/VictoriaMetrics/lib/protoparser/common"
|
2020-07-10 11:00:35 +02:00
|
|
|
parser "github.com/VictoriaMetrics/VictoriaMetrics/lib/protoparser/prometheus"
|
2023-02-13 18:26:07 +01:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/protoparser/prometheus/stream"
|
2020-07-10 11:00:35 +02:00
|
|
|
"github.com/VictoriaMetrics/metrics"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
rowsInserted = metrics.NewCounter(`vm_rows_inserted_total{type="prometheus"}`)
|
|
|
|
rowsPerInsert = metrics.NewHistogram(`vm_rows_per_insert{type="prometheus"}`)
|
|
|
|
)
|
|
|
|
|
|
|
|
// InsertHandler processes `/api/v1/import/prometheus` request.
|
|
|
|
func InsertHandler(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-07 03:59:39 +01:00
|
|
|
isGzipped := req.Header.Get("Content-Encoding") == "gzip"
|
2023-10-02 21:32:11 +02:00
|
|
|
return stream.Parse(req.Body, defaultTimestamp, isGzipped, true, func(rows []parser.Row) error {
|
2023-01-07 03:59:39 +01:00
|
|
|
return insertRows(rows, extraLabels)
|
2023-01-24 07:14:34 +01:00
|
|
|
}, func(s string) {
|
2023-01-24 07:22:05 +01:00
|
|
|
httpserver.LogError(req, s)
|
2023-01-24 07:14:34 +01:00
|
|
|
})
|
2020-07-10 11:00:35 +02:00
|
|
|
}
|
|
|
|
|
2020-09-02 18:41:12 +02:00
|
|
|
func insertRows(rows []parser.Row, extraLabels []prompbmarshal.Label) error {
|
2020-07-10 11:00:35 +02:00
|
|
|
ctx := common.GetInsertCtx()
|
|
|
|
defer common.PutInsertCtx(ctx)
|
|
|
|
|
|
|
|
ctx.Reset(len(rows))
|
2020-07-23 12:33:10 +02:00
|
|
|
hasRelabeling := relabel.HasRelabeling()
|
2020-07-10 11:00:35 +02:00
|
|
|
for i := range rows {
|
|
|
|
r := &rows[i]
|
|
|
|
ctx.Labels = ctx.Labels[:0]
|
|
|
|
ctx.AddLabel("", r.Metric)
|
|
|
|
for j := range r.Tags {
|
|
|
|
tag := &r.Tags[j]
|
|
|
|
ctx.AddLabel(tag.Key, tag.Value)
|
|
|
|
}
|
2020-09-02 18:41:12 +02:00
|
|
|
for j := range extraLabels {
|
|
|
|
label := &extraLabels[j]
|
|
|
|
ctx.AddLabel(label.Name, label.Value)
|
|
|
|
}
|
2020-07-23 12:33:10 +02:00
|
|
|
if hasRelabeling {
|
|
|
|
ctx.ApplyRelabeling()
|
|
|
|
}
|
2020-07-10 11:00:35 +02:00
|
|
|
if len(ctx.Labels) == 0 {
|
|
|
|
// Skip metric without labels.
|
|
|
|
continue
|
|
|
|
}
|
2021-03-31 22:12:56 +02:00
|
|
|
ctx.SortLabelsIfNeeded()
|
2020-07-24 22:19:49 +02:00
|
|
|
if err := ctx.WriteDataPoint(nil, ctx.Labels, r.Timestamp, r.Value); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-07-10 11:00:35 +02:00
|
|
|
}
|
|
|
|
rowsInserted.Add(len(rows))
|
|
|
|
rowsPerInsert.Update(float64(len(rows)))
|
|
|
|
return ctx.FlushBufs()
|
|
|
|
}
|