2019-05-22 21:16:55 +00:00
package influx
import (
2019-06-14 06:57:13 +00:00
"flag"
2020-02-25 17:09:46 +00:00
"io"
2019-05-22 21:16:55 +00:00
"net/http"
"sync"
2019-05-22 21:23:23 +00:00
"github.com/VictoriaMetrics/VictoriaMetrics/app/vminsert/netstorage"
2020-07-02 16:42:12 +00:00
"github.com/VictoriaMetrics/VictoriaMetrics/app/vminsert/relabel"
2019-05-22 21:23:23 +00:00
"github.com/VictoriaMetrics/VictoriaMetrics/lib/auth"
2019-05-22 21:16:55 +00:00
"github.com/VictoriaMetrics/VictoriaMetrics/lib/bytesutil"
2021-01-12 22:52:50 +00:00
"github.com/VictoriaMetrics/VictoriaMetrics/lib/prompbmarshal"
parserCommon "github.com/VictoriaMetrics/VictoriaMetrics/lib/protoparser/common"
2020-02-23 11:35:47 +00:00
parser "github.com/VictoriaMetrics/VictoriaMetrics/lib/protoparser/influx"
2023-02-13 17:58:48 +00:00
"github.com/VictoriaMetrics/VictoriaMetrics/lib/protoparser/influx/stream"
2019-05-22 21:16:55 +00:00
"github.com/VictoriaMetrics/VictoriaMetrics/lib/storage"
2021-03-29 21:06:56 +00:00
"github.com/VictoriaMetrics/VictoriaMetrics/lib/tenantmetrics"
2019-05-22 21:16:55 +00:00
"github.com/VictoriaMetrics/metrics"
)
2019-06-14 06:57:13 +00:00
var (
2021-09-13 14:04:28 +00:00
measurementFieldSeparator = flag . String ( "influxMeasurementFieldSeparator" , "_" , "Separator for '{measurement}{separator}{field_name}' metric name when inserted via InfluxDB line protocol" )
2023-05-10 07:50:41 +00:00
skipSingleField = flag . Bool ( "influxSkipSingleField" , false , "Uses '{measurement}' instead of '{measurement}{separator}{field_name}' for metric name if InfluxDB line contains only a single field" )
2020-07-14 11:17:22 +00:00
skipMeasurement = flag . Bool ( "influxSkipMeasurement" , false , "Uses '{field_name}' as a metric name while ignoring '{measurement}' and '-influxMeasurementFieldSeparator'" )
2022-02-16 21:25:01 +00:00
dbLabel = flag . String ( "influxDBLabel" , "db" , "Default label for the DB name sent over '?db={db_name}' query parameter" )
2019-06-14 06:57:13 +00:00
)
2019-07-27 10:20:47 +00:00
var (
2021-03-29 21:06:56 +00:00
rowsInserted = metrics . NewCounter ( ` vm_rows_inserted_total { type="influx"} ` )
rowsTenantInserted = tenantmetrics . NewCounterMap ( ` vm_tenant_inserted_rows_total { type="influx"} ` )
rowsPerInsert = metrics . NewHistogram ( ` vm_rows_per_insert { type="influx"} ` )
2019-07-27 10:20:47 +00:00
)
2019-05-22 21:16:55 +00:00
2020-02-25 17:09:46 +00:00
// InsertHandlerForReader processes remote write for influx line protocol.
//
// See https://github.com/influxdata/telegraf/tree/master/plugins/inputs/socket_listener/
func InsertHandlerForReader ( at * auth . Token , r io . Reader ) error {
2024-10-15 09:48:40 +00:00
return stream . Parse ( r , true , false , "" , "" , func ( db string , rows [ ] parser . Row ) error {
2023-01-07 02:59:39 +00:00
return insertRows ( at , db , rows , nil )
2020-02-25 17:09:46 +00:00
} )
}
// InsertHandlerForHTTP processes remote write for influx line protocol.
2019-05-22 21:16:55 +00:00
//
// See https://github.com/influxdata/influxdb/blob/4cbdc197b8117fee648d62e2e5be75c6575352f0/tsdb/README.md
2020-02-25 17:09:46 +00:00
func InsertHandlerForHTTP ( at * auth . Token , req * http . Request ) error {
2021-01-12 22:52:50 +00:00
extraLabels , err := parserCommon . GetExtraLabels ( req )
if err != nil {
return err
}
2023-01-07 02:59:39 +00:00
isGzipped := req . Header . Get ( "Content-Encoding" ) == "gzip"
2024-10-15 09:48:40 +00:00
isStreamMode := req . Header . Get ( "Stream-Mode" ) == "1"
2023-01-07 02:59:39 +00:00
q := req . URL . Query ( )
precision := q . Get ( "precision" )
// Read db tag from https://docs.influxdata.com/influxdb/v1.7/tools/api/#write-http-endpoint
db := q . Get ( "db" )
2024-10-15 09:48:40 +00:00
return stream . Parse ( req . Body , isStreamMode , isGzipped , precision , db , func ( db string , rows [ ] parser . Row ) error {
2023-01-07 02:59:39 +00:00
return insertRows ( at , db , rows , extraLabels )
2019-05-22 21:16:55 +00:00
} )
}
2022-09-30 15:04:48 +00:00
func insertRows ( at * auth . Token , db string , rows [ ] parser . Row , extraLabels [ ] prompbmarshal . Label ) error {
2019-05-22 21:16:55 +00:00
ctx := getPushCtx ( )
defer putPushCtx ( ctx )
ic := & ctx . Common
2020-02-26 19:17:35 +00:00
ic . Reset ( ) // This line is required for initializing ic internals.
2019-07-27 10:20:47 +00:00
rowsTotal := 0
2022-09-30 14:28:35 +00:00
perTenantRows := make ( map [ auth . Token ] int )
2020-07-02 16:42:12 +00:00
hasRelabeling := relabel . HasRelabeling ( )
2019-05-22 21:16:55 +00:00
for i := range rows {
r := & rows [ i ]
2020-10-09 10:29:27 +00:00
rowsTotal += len ( r . Fields )
2019-05-22 21:16:55 +00:00
ic . Labels = ic . Labels [ : 0 ]
2020-07-28 18:23:01 +00:00
hasDBKey := false
2019-05-22 21:16:55 +00:00
for j := range r . Tags {
tag := & r . Tags [ j ]
2022-02-16 21:25:01 +00:00
if tag . Key == * dbLabel {
2020-07-28 18:23:01 +00:00
hasDBKey = true
2019-08-24 10:51:51 +00:00
}
2019-05-22 21:16:55 +00:00
ic . AddLabel ( tag . Key , tag . Value )
}
2020-07-28 18:23:01 +00:00
if ! hasDBKey {
2022-02-16 21:25:01 +00:00
ic . AddLabel ( * dbLabel , db )
2020-07-28 18:23:01 +00:00
}
2021-01-12 22:52:50 +00:00
for j := range extraLabels {
label := & extraLabels [ j ]
ic . AddLabel ( label . Name , label . Value )
}
2020-07-14 11:17:22 +00:00
ctx . metricGroupBuf = ctx . metricGroupBuf [ : 0 ]
if ! * skipMeasurement {
ctx . metricGroupBuf = append ( ctx . metricGroupBuf , r . Measurement ... )
}
2021-03-22 11:53:42 +00:00
// See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/1139
skipFieldKey := len ( r . Measurement ) > 0 && len ( r . Fields ) == 1 && * skipSingleField
2019-11-30 19:54:34 +00:00
if len ( ctx . metricGroupBuf ) > 0 && ! skipFieldKey {
2019-06-14 07:51:57 +00:00
ctx . metricGroupBuf = append ( ctx . metricGroupBuf , * measurementFieldSeparator ... )
}
2019-05-22 21:16:55 +00:00
metricGroupPrefixLen := len ( ctx . metricGroupBuf )
2020-07-02 20:13:13 +00:00
if hasRelabeling {
2020-07-23 09:50:41 +00:00
ctx . originLabels = append ( ctx . originLabels [ : 0 ] , ic . Labels ... )
for j := range r . Fields {
f := & r . Fields [ j ]
if ! skipFieldKey {
ctx . metricGroupBuf = append ( ctx . metricGroupBuf [ : metricGroupPrefixLen ] , f . Key ... )
}
metricGroup := bytesutil . ToUnsafeString ( ctx . metricGroupBuf )
ic . Labels = append ( ic . Labels [ : 0 ] , ctx . originLabels ... )
ic . AddLabel ( "" , metricGroup )
ic . ApplyRelabeling ( )
if len ( ic . Labels ) == 0 {
// Skip metric without labels.
continue
}
2021-03-31 20:12:56 +00:00
ic . SortLabelsIfNeeded ( )
2022-09-30 15:04:48 +00:00
atLocal := ic . GetLocalAuthToken ( at )
2022-09-30 14:28:35 +00:00
ic . MetricNameBuf = storage . MarshalMetricNameRaw ( ic . MetricNameBuf [ : 0 ] , atLocal . AccountID , atLocal . ProjectID , nil )
2020-07-23 09:50:41 +00:00
for i := range ic . Labels {
ic . MetricNameBuf = storage . MarshalMetricLabelRaw ( ic . MetricNameBuf , & ic . Labels [ i ] )
}
2022-09-30 14:28:35 +00:00
storageNodeIdx := ic . GetStorageNodeIdx ( atLocal , ic . Labels )
if err := ic . WriteDataPointExt ( storageNodeIdx , ic . MetricNameBuf , r . Timestamp , f . Value ) ; err != nil {
2020-07-23 09:50:41 +00:00
return err
}
2022-09-30 14:28:35 +00:00
perTenantRows [ * atLocal ] ++
2020-07-02 16:42:12 +00:00
}
2020-07-23 09:50:41 +00:00
} else {
2021-03-31 20:12:56 +00:00
ic . SortLabelsIfNeeded ( )
2022-09-30 15:04:48 +00:00
atLocal := ic . GetLocalAuthToken ( at )
2022-09-30 14:28:35 +00:00
ic . MetricNameBuf = storage . MarshalMetricNameRaw ( ic . MetricNameBuf [ : 0 ] , atLocal . AccountID , atLocal . ProjectID , ic . Labels )
2020-07-23 09:50:41 +00:00
metricNameBufLen := len ( ic . MetricNameBuf )
labelsLen := len ( ic . Labels )
for j := range r . Fields {
f := & r . Fields [ j ]
if ! skipFieldKey {
ctx . metricGroupBuf = append ( ctx . metricGroupBuf [ : metricGroupPrefixLen ] , f . Key ... )
}
metricGroup := bytesutil . ToUnsafeString ( ctx . metricGroupBuf )
ic . Labels = ic . Labels [ : labelsLen ]
ic . AddLabel ( "" , metricGroup )
if len ( ic . Labels ) == 0 {
// Skip metric without labels.
continue
}
ic . MetricNameBuf = ic . MetricNameBuf [ : metricNameBufLen ]
ic . MetricNameBuf = storage . MarshalMetricLabelRaw ( ic . MetricNameBuf , & ic . Labels [ len ( ic . Labels ) - 1 ] )
2022-09-30 14:28:35 +00:00
storageNodeIdx := ic . GetStorageNodeIdx ( atLocal , ic . Labels )
if err := ic . WriteDataPointExt ( storageNodeIdx , ic . MetricNameBuf , r . Timestamp , f . Value ) ; err != nil {
2020-07-23 09:50:41 +00:00
return err
}
2022-09-30 14:28:35 +00:00
perTenantRows [ * atLocal ] ++
2019-05-22 21:23:23 +00:00
}
2019-05-22 21:16:55 +00:00
}
}
2021-03-29 08:59:04 +00:00
rowsInserted . Add ( rowsTotal )
2022-09-30 14:28:35 +00:00
rowsTenantInserted . MultiAdd ( perTenantRows )
2019-07-27 10:20:47 +00:00
rowsPerInsert . Update ( float64 ( rowsTotal ) )
2019-05-22 21:16:55 +00:00
return ic . FlushBufs ( )
}
type pushCtx struct {
2020-02-23 11:35:47 +00:00
Common netstorage . InsertCtx
2019-05-22 21:16:55 +00:00
metricGroupBuf [ ] byte
2024-11-26 18:02:06 +00:00
originLabels [ ] prompbmarshal . Label
2019-05-22 21:16:55 +00:00
}
func ( ctx * pushCtx ) reset ( ) {
2019-05-22 21:23:23 +00:00
ctx . Common . Reset ( )
2019-05-22 21:16:55 +00:00
ctx . metricGroupBuf = ctx . metricGroupBuf [ : 0 ]
2020-07-23 09:50:41 +00:00
originLabels := ctx . originLabels
for i := range originLabels {
2024-11-26 18:02:06 +00:00
originLabels [ i ] = prompbmarshal . Label { }
2020-07-23 09:50:41 +00:00
}
2024-01-14 20:33:19 +00:00
ctx . originLabels = originLabels [ : 0 ]
2019-05-22 21:16:55 +00:00
}
func getPushCtx ( ) * pushCtx {
2024-04-20 19:39:49 +00:00
if v := pushCtxPool . Get ( ) ; v != nil {
return v . ( * pushCtx )
2019-05-22 21:16:55 +00:00
}
2024-04-20 19:39:49 +00:00
return & pushCtx { }
2019-05-22 21:16:55 +00:00
}
func putPushCtx ( ctx * pushCtx ) {
ctx . reset ( )
2024-04-20 19:39:49 +00:00
pushCtxPool . Put ( ctx )
2019-05-22 21:16:55 +00:00
}
var pushCtxPool sync . Pool