2021-09-28 21:47:45 +02:00
|
|
|
package datadog
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vminsert/netstorage"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vminsert/relabel"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/auth"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/prompbmarshal"
|
|
|
|
parserCommon "github.com/VictoriaMetrics/VictoriaMetrics/lib/protoparser/common"
|
2023-02-13 18:51:35 +01:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/protoparser/datadog/stream"
|
2021-09-28 21:47:45 +02:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/storage"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/tenantmetrics"
|
|
|
|
"github.com/VictoriaMetrics/metrics"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
rowsInserted = metrics.NewCounter(`vm_rows_inserted_total{type="datadog"}`)
|
|
|
|
rowsTenantInserted = tenantmetrics.NewCounterMap(`vm_tenant_inserted_rows_total{type="datadog"}`)
|
|
|
|
rowsPerInsert = metrics.NewHistogram(`vm_rows_per_insert{type="datadog"}`)
|
|
|
|
)
|
|
|
|
|
2023-11-28 14:52:29 +01:00
|
|
|
// InsertHandlerForHTTP processes remote write for DataDog POST /api/v1/series, /api/v2/series, /api/v1/sketches, /api/beta/sketches request.
|
2021-09-28 21:47:45 +02:00
|
|
|
//
|
|
|
|
// See https://docs.datadoghq.com/api/latest/metrics/#submit-metrics
|
|
|
|
func InsertHandlerForHTTP(at *auth.Token, req *http.Request) error {
|
|
|
|
extraLabels, err := parserCommon.GetExtraLabels(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-11-28 14:52:29 +01:00
|
|
|
return stream.Parse(
|
|
|
|
req, func(series prompbmarshal.TimeSeries) error {
|
|
|
|
series.Labels = append(series.Labels, extraLabels...)
|
|
|
|
return insertRows(at, series)
|
|
|
|
},
|
|
|
|
)
|
2021-09-28 21:47:45 +02:00
|
|
|
}
|
|
|
|
|
2023-11-28 14:52:29 +01:00
|
|
|
func insertRows(at *auth.Token, series prompbmarshal.TimeSeries) error {
|
2021-09-28 21:47:45 +02:00
|
|
|
ctx := netstorage.GetInsertCtx()
|
|
|
|
defer netstorage.PutInsertCtx(ctx)
|
|
|
|
|
|
|
|
ctx.Reset()
|
2022-09-30 16:28:35 +02:00
|
|
|
perTenantRows := make(map[auth.Token]int)
|
2021-09-28 21:47:45 +02:00
|
|
|
hasRelabeling := relabel.HasRelabeling()
|
2023-11-28 14:52:29 +01:00
|
|
|
rowsTotal := len(series.Samples)
|
|
|
|
|
|
|
|
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()
|
|
|
|
atLocal := ctx.GetLocalAuthToken(at)
|
|
|
|
ctx.MetricNameBuf = storage.MarshalMetricNameRaw(ctx.MetricNameBuf[:0], atLocal.AccountID, atLocal.ProjectID, ctx.Labels)
|
|
|
|
storageNodeIdx := ctx.GetStorageNodeIdx(atLocal, ctx.Labels)
|
|
|
|
for _, sample := range series.Samples {
|
|
|
|
err := ctx.WriteDataPointExt(storageNodeIdx, ctx.MetricNameBuf, sample.Timestamp, sample.Value)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-09-28 21:47:45 +02:00
|
|
|
}
|
|
|
|
}
|
2023-11-28 14:52:29 +01:00
|
|
|
perTenantRows[*atLocal] = rowsTotal
|
2021-09-28 21:47:45 +02:00
|
|
|
rowsInserted.Add(rowsTotal)
|
2022-09-30 16:28:35 +02:00
|
|
|
rowsTenantInserted.MultiAdd(perTenantRows)
|
2021-09-28 21:47:45 +02:00
|
|
|
rowsPerInsert.Update(float64(rowsTotal))
|
|
|
|
return ctx.FlushBufs()
|
|
|
|
}
|