2021-09-28 19:47:45 +00:00
|
|
|
package datadog
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vminsert/common"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vminsert/relabel"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/prompbmarshal"
|
|
|
|
parserCommon "github.com/VictoriaMetrics/VictoriaMetrics/lib/protoparser/common"
|
2023-02-13 17:51:35 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/protoparser/datadog/stream"
|
2021-09-28 19:47:45 +00:00
|
|
|
"github.com/VictoriaMetrics/metrics"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
rowsInserted = metrics.NewCounter(`vm_rows_inserted_total{type="datadog"}`)
|
|
|
|
rowsPerInsert = metrics.NewHistogram(`vm_rows_per_insert{type="datadog"}`)
|
|
|
|
)
|
|
|
|
|
2023-11-28 13:52:29 +00:00
|
|
|
// InsertHandlerForHTTP processes remote write for DataDog POST /api/v1/series, /api/v2/series, /api/v1/sketches, /api/beta/sketches request.
|
2021-09-28 19:47:45 +00:00
|
|
|
//
|
|
|
|
// See https://docs.datadoghq.com/api/latest/metrics/#submit-metrics
|
|
|
|
func InsertHandlerForHTTP(req *http.Request) error {
|
|
|
|
extraLabels, err := parserCommon.GetExtraLabels(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-11-28 13:52:29 +00:00
|
|
|
return stream.Parse(
|
|
|
|
req, func(series prompbmarshal.TimeSeries) error {
|
|
|
|
series.Labels = append(series.Labels, extraLabels...)
|
|
|
|
return insertRows(series)
|
|
|
|
},
|
|
|
|
)
|
2021-09-28 19:47:45 +00:00
|
|
|
}
|
|
|
|
|
2023-11-28 13:52:29 +00:00
|
|
|
func insertRows(series prompbmarshal.TimeSeries) error {
|
2021-09-28 19:47:45 +00:00
|
|
|
ctx := common.GetInsertCtx()
|
|
|
|
defer common.PutInsertCtx(ctx)
|
|
|
|
|
|
|
|
hasRelabeling := relabel.HasRelabeling()
|
2023-11-28 13:52:29 +00:00
|
|
|
rowsTotal := len(series.Samples)
|
|
|
|
|
|
|
|
ctx.Reset(rowsTotal)
|
|
|
|
ctx.Labels = ctx.Labels[:0]
|
|
|
|
for l := range series.Labels {
|
|
|
|
ctx.AddLabel(series.Labels[l].Name, series.Labels[l].Value)
|
|
|
|
}
|
|
|
|
if hasRelabeling {
|
|
|
|
ctx.ApplyRelabeling()
|
|
|
|
}
|
|
|
|
if len(ctx.Labels) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
ctx.SortLabelsIfNeeded()
|
|
|
|
for _, sample := range series.Samples {
|
2023-12-05 00:19:29 +00:00
|
|
|
if _, err := ctx.WriteDataPointExt(
|
|
|
|
[]byte{}, ctx.Labels, sample.Timestamp, sample.Value,
|
|
|
|
); err != nil {
|
2023-11-28 13:52:29 +00:00
|
|
|
return err
|
2021-09-28 19:47:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
rowsInserted.Add(rowsTotal)
|
|
|
|
rowsPerInsert.Update(float64(rowsTotal))
|
|
|
|
return ctx.FlushBufs()
|
|
|
|
}
|