mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2026-06-30 14:06:39 +03:00
The prompb and prompbmarshal share exactly the same models and provide
marshal and unmarshale capabilities for them. This creates duplication
(changes in one model has to be made in another, case with metadata) and
confusion where for example you compare same looking models but golang
says they are not the same (because of the type).
This commit merge prompbmarshal logic into prompb so the rest of the
code is aligned on prompb models.
Moves samplesPool and labelsPool to WriteRequestUnmarshaller.
Make WriteRequest struct clean from unmarshal logic.
The benchmark shows no significant changes:
$benchstat prompbmarshal.bench prompb2.bench
goos: darwin
goarch: arm64
pkg: github.com/VictoriaMetrics/VictoriaMetrics/lib/prompb
cpu: Apple M1 Pro
│ prompbmarshal.bench │ prompb2.bench │
│ sec/op │ sec/op vs base │
WriteRequestUnmarshalProtobuf-10 189.2µ ± 5% 190.8µ ± 8% ~ (p=0.579 n=10)
WriteRequestMarshalProtobuf-10 145.3µ ± 7% 143.6µ ± 2% ~ (p=0.143 n=10)
geomean 165.8µ 165.5µ -0.14%
│ prompbmarshal.bench │ prompb2.bench │
│ B/s │ B/s vs base │
WriteRequestUnmarshalProtobuf-10 50.42Mi ± 5% 49.99Mi ± 8% ~ (p=0.593 n=10)
WriteRequestMarshalProtobuf-10 65.64Mi ± 7% 66.39Mi ± 2% ~ (p=0.143 n=10)
geomean 57.53Mi 57.61Mi +0.14%
│ prompbmarshal.bench │ prompb2.bench │
│ B/op │ B/op vs base │
WriteRequestUnmarshalProtobuf-10 27.70Ki ± 4% 26.90Ki ± 7% ~ (p=0.190 n=10)
WriteRequestMarshalProtobuf-10 3.267Ki ± 12% 3.273Ki ± 12% ~ (p=0.971 n=10)
geomean 9.514Ki 9.383Ki -1.38%
│ prompbmarshal.bench │ prompb2.bench │
│ allocs/op │ allocs/op vs base │
WriteRequestUnmarshalProtobuf-10 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹
WriteRequestMarshalProtobuf-10 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹
geomean ² +0.00% ²
¹ all samples are equal
² summaries must be >0 to compute geomean
84 lines
2.3 KiB
Go
84 lines
2.3 KiB
Go
package datadogv1
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vminsert/common"
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vminsert/relabel"
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/prompb"
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/protoparser/datadogutil"
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/protoparser/datadogv1"
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/protoparser/datadogv1/stream"
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/protoparser/protoparserutil"
|
|
"github.com/VictoriaMetrics/metrics"
|
|
)
|
|
|
|
var (
|
|
rowsInserted = metrics.NewCounter(`vm_rows_inserted_total{type="datadogv1"}`)
|
|
rowsPerInsert = metrics.NewHistogram(`vm_rows_per_insert{type="datadogv1"}`)
|
|
)
|
|
|
|
// InsertHandlerForHTTP processes remote write for DataDog POST /api/v1/series request.
|
|
func InsertHandlerForHTTP(req *http.Request) error {
|
|
extraLabels, err := protoparserutil.GetExtraLabels(req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
ce := req.Header.Get("Content-Encoding")
|
|
return stream.Parse(req.Body, ce, func(series []datadogv1.Series) error {
|
|
return insertRows(series, extraLabels)
|
|
})
|
|
}
|
|
|
|
func insertRows(series []datadogv1.Series, extraLabels []prompb.Label) error {
|
|
ctx := common.GetInsertCtx()
|
|
defer common.PutInsertCtx(ctx)
|
|
|
|
rowsLen := 0
|
|
for i := range series {
|
|
rowsLen += len(series[i].Points)
|
|
}
|
|
ctx.Reset(rowsLen)
|
|
rowsTotal := 0
|
|
hasRelabeling := relabel.HasRelabeling()
|
|
for i := range series {
|
|
ss := &series[i]
|
|
rowsTotal += len(ss.Points)
|
|
ctx.Labels = ctx.Labels[:0]
|
|
ctx.AddLabel("", ss.Metric)
|
|
if ss.Host != "" {
|
|
ctx.AddLabel("host", ss.Host)
|
|
}
|
|
if ss.Device != "" {
|
|
ctx.AddLabel("device", ss.Device)
|
|
}
|
|
for _, tag := range ss.Tags {
|
|
name, value := datadogutil.SplitTag(tag)
|
|
if name == "host" {
|
|
name = "exported_host"
|
|
}
|
|
ctx.AddLabel(name, value)
|
|
}
|
|
for j := range extraLabels {
|
|
label := &extraLabels[j]
|
|
ctx.AddLabel(label.Name, label.Value)
|
|
}
|
|
if !ctx.TryPrepareLabels(hasRelabeling) {
|
|
continue
|
|
}
|
|
var metricNameRaw []byte
|
|
var err error
|
|
for _, pt := range ss.Points {
|
|
timestamp := pt.Timestamp()
|
|
value := pt.Value()
|
|
metricNameRaw, err = ctx.WriteDataPointExt(metricNameRaw, ctx.Labels, timestamp, value)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
rowsInserted.Add(rowsTotal)
|
|
rowsPerInsert.Update(float64(rowsTotal))
|
|
return ctx.FlushBufs()
|
|
}
|